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" |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 18 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExternalASTSource.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Builtins.h" |
Chris Lattner | a9376d4 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 29 | #include "RecordLayoutBuilder.h" |
| 30 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | |
| 33 | enum FloatingRank { |
| 34 | FloatRank, DoubleRank, LongDoubleRank |
| 35 | }; |
| 36 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 37 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
| 38 | TargetInfo &t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 39 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 40 | Builtin::Context &builtins, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | bool FreeMem, unsigned size_reserve) : |
| 42 | GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0), |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 43 | ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 44 | sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
| 45 | SourceMgr(SM), LangOpts(LOpts), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | LoadedExternalComments(false), FreeMemory(FreeMem), Target(t), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 47 | Idents(idents), Selectors(sels), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) { |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 49 | ObjCIdRedefinitionType = QualType(); |
| 50 | ObjCClassRedefinitionType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 52 | TUDecl = TranslationUnitDecl::Create(*this); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 53 | InitBuiltinTypes(); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | ASTContext::~ASTContext() { |
| 57 | // Deallocate all the types. |
| 58 | while (!Types.empty()) { |
Ted Kremenek | 4b05b1d | 2008-05-21 16:38:54 +0000 | [diff] [blame] | 59 | Types.back()->Destroy(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | Types.pop_back(); |
| 61 | } |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 62 | |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 63 | { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 64 | llvm::FoldingSet<ExtQuals>::iterator |
| 65 | I = ExtQualNodes.begin(), E = ExtQualNodes.end(); |
| 66 | while (I != E) |
| 67 | Deallocate(&*I++); |
| 68 | } |
| 69 | |
| 70 | { |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 71 | llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 72 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); |
| 73 | while (I != E) { |
| 74 | ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); |
| 75 | delete R; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 80 | llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator |
| 81 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 82 | while (I != E) { |
| 83 | ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); |
| 84 | delete R; |
| 85 | } |
| 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(); |
| 92 | NNS != NNSEnd; |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 93 | /* Increment in loop */) |
| 94 | (*NNS++).Destroy(*this); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 95 | |
| 96 | if (GlobalNestedNameSpecifier) |
| 97 | GlobalNestedNameSpecifier->Destroy(*this); |
| 98 | |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 99 | TUDecl->Destroy(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 103 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 104 | ExternalSource.reset(Source.take()); |
| 105 | } |
| 106 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | void ASTContext::PrintStats() const { |
| 108 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 109 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 110 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 111 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 113 | #define ABSTRACT_TYPE(Name, Parent) |
| 114 | #include "clang/AST/TypeNodes.def" |
| 115 | 0 // Extra |
| 116 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 117 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 118 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 119 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 120 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 123 | unsigned Idx = 0; |
| 124 | unsigned TotalBytes = 0; |
| 125 | #define TYPE(Name, Parent) \ |
| 126 | if (counts[Idx]) \ |
| 127 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 128 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 129 | ++Idx; |
| 130 | #define ABSTRACT_TYPE(Name, Parent) |
| 131 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 133 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 134 | |
| 135 | if (ExternalSource.get()) { |
| 136 | fprintf(stderr, "\n"); |
| 137 | ExternalSource->PrintStats(); |
| 138 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 142 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 143 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 144 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 145 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | void ASTContext::InitBuiltinTypes() { |
| 149 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | // C99 6.2.5p19. |
| 152 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 154 | // C99 6.2.5p2. |
| 155 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 156 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 157 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 158 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 159 | else |
| 160 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 161 | // C99 6.2.5p4. |
| 162 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 163 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 164 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 165 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 166 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | // C99 6.2.5p6. |
| 169 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 170 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 171 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 172 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 173 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | // C99 6.2.5p10. |
| 176 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 177 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 178 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 180 | // GNU extension, 128-bit integers. |
| 181 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 182 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 183 | |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 184 | if (LangOpts.CPlusPlus) // C++ 3.9.1p5 |
| 185 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 186 | else // C99 |
| 187 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 188 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 189 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 190 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 191 | else // C99 |
| 192 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 193 | |
| 194 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 195 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 196 | else // C99 |
| 197 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 198 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 199 | // Placeholder type for functions. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 200 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 201 | |
| 202 | // Placeholder type for type-dependent expressions whose type is |
| 203 | // completely unknown. No code should ever check a type against |
| 204 | // DependentTy and users should never see it; however, it is here to |
| 205 | // help diagnose failures to properly check for type-dependent |
| 206 | // expressions. |
| 207 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 208 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | // Placeholder type for C++0x auto declarations whose real type has |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 210 | // not yet been deduced. |
| 211 | InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 213 | // C99 6.2.5p11. |
| 214 | FloatComplexTy = getComplexType(FloatTy); |
| 215 | DoubleComplexTy = getComplexType(DoubleTy); |
| 216 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 217 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 218 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 220 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 221 | ObjCIdTypedefType = QualType(); |
| 222 | ObjCClassTypedefType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 223 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 224 | // Builtin types for 'id' and 'Class'. |
| 225 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 226 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 227 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 228 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 229 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 230 | // void * type |
| 231 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 232 | |
| 233 | // nullptr type (C++0x 2.14.7) |
| 234 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 237 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 238 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 239 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 240 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 241 | = InstantiatedFromStaticDataMember.find(Var); |
| 242 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 243 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 245 | return Pos->second; |
| 246 | } |
| 247 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 249 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
| 250 | TemplateSpecializationKind TSK) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 251 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 252 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 253 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 254 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 255 | InstantiatedFromStaticDataMember[Inst] |
| 256 | = new (*this) MemberSpecializationInfo(Tmpl, TSK); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 259 | UnresolvedUsingDecl * |
| 260 | ASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 262 | = InstantiatedFromUnresolvedUsingDecl.find(UUD); |
| 263 | if (Pos == InstantiatedFromUnresolvedUsingDecl.end()) |
| 264 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 266 | return Pos->second; |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | ASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD, |
| 271 | UnresolvedUsingDecl *UUD) { |
| 272 | assert(!InstantiatedFromUnresolvedUsingDecl[UD] && |
| 273 | "Already noted what using decl what instantiated from"); |
| 274 | InstantiatedFromUnresolvedUsingDecl[UD] = UUD; |
| 275 | } |
| 276 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 277 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 278 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 279 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 280 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 281 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 283 | return Pos->second; |
| 284 | } |
| 285 | |
| 286 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 287 | FieldDecl *Tmpl) { |
| 288 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 289 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 290 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 291 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 293 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 294 | } |
| 295 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 296 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | class BeforeInTranslationUnit |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 298 | : std::binary_function<SourceRange, SourceRange, bool> { |
| 299 | SourceManager *SourceMgr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 301 | public: |
| 302 | explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 304 | bool operator()(SourceRange X, SourceRange Y) { |
| 305 | return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin()); |
| 306 | } |
| 307 | }; |
| 308 | } |
| 309 | |
| 310 | /// \brief Determine whether the given comment is a Doxygen-style comment. |
| 311 | /// |
| 312 | /// \param Start the start of the comment text. |
| 313 | /// |
| 314 | /// \param End the end of the comment text. |
| 315 | /// |
| 316 | /// \param Member whether we want to check whether this is a member comment |
| 317 | /// (which requires a < after the Doxygen-comment delimiter). Otherwise, |
| 318 | /// we only return true when we find a non-member comment. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | static bool |
| 320 | isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 321 | bool Member = false) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | const char *BufferStart |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 323 | = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first; |
| 324 | const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin()); |
| 325 | const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 327 | if (End - Start < 4) |
| 328 | return false; |
| 329 | |
| 330 | assert(Start[0] == '/' && "Not a comment?"); |
| 331 | if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*')) |
| 332 | return false; |
| 333 | if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/')) |
| 334 | return false; |
| 335 | |
| 336 | return (Start[3] == '<') == Member; |
| 337 | } |
| 338 | |
| 339 | /// \brief Retrieve the comment associated with the given declaration, if |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | /// it has one. |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 341 | const char *ASTContext::getCommentForDecl(const Decl *D) { |
| 342 | if (!D) |
| 343 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 345 | // Check whether we have cached a comment string for this declaration |
| 346 | // already. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | llvm::DenseMap<const Decl *, std::string>::iterator Pos |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 348 | = DeclComments.find(D); |
| 349 | if (Pos != DeclComments.end()) |
| 350 | return Pos->second.c_str(); |
| 351 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | // 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] | 353 | // that source, do so now. |
| 354 | if (ExternalSource && !LoadedExternalComments) { |
| 355 | std::vector<SourceRange> LoadedComments; |
| 356 | ExternalSource->ReadComments(LoadedComments); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 358 | if (!LoadedComments.empty()) |
| 359 | Comments.insert(Comments.begin(), LoadedComments.begin(), |
| 360 | LoadedComments.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 362 | LoadedExternalComments = true; |
| 363 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 364 | |
| 365 | // If there are no comments anywhere, we won't find anything. |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 366 | if (Comments.empty()) |
| 367 | return 0; |
| 368 | |
| 369 | // If the declaration doesn't map directly to a location in a file, we |
| 370 | // can't find the comment. |
| 371 | SourceLocation DeclStartLoc = D->getLocStart(); |
| 372 | if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID()) |
| 373 | return 0; |
| 374 | |
| 375 | // Find the comment that occurs just before this declaration. |
| 376 | std::vector<SourceRange>::iterator LastComment |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | = std::lower_bound(Comments.begin(), Comments.end(), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 378 | SourceRange(DeclStartLoc), |
| 379 | BeforeInTranslationUnit(&SourceMgr)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 381 | // Decompose the location for the start of the declaration and find the |
| 382 | // beginning of the file buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | std::pair<FileID, unsigned> DeclStartDecomp |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 384 | = SourceMgr.getDecomposedLoc(DeclStartLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | const char *FileBufferStart |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 386 | = SourceMgr.getBufferData(DeclStartDecomp.first).first; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 388 | // First check whether we have a comment for a member. |
| 389 | if (LastComment != Comments.end() && |
| 390 | !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) && |
| 391 | isDoxygenComment(SourceMgr, *LastComment, true)) { |
| 392 | std::pair<FileID, unsigned> LastCommentEndDecomp |
| 393 | = SourceMgr.getDecomposedLoc(LastComment->getEnd()); |
| 394 | if (DeclStartDecomp.first == LastCommentEndDecomp.first && |
| 395 | SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | == SourceMgr.getLineNumber(LastCommentEndDecomp.first, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 397 | LastCommentEndDecomp.second)) { |
| 398 | // The Doxygen member comment comes after the declaration starts and |
| 399 | // is on the same line and in the same file as the declaration. This |
| 400 | // is the comment we want. |
| 401 | std::string &Result = DeclComments[D]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | Result.append(FileBufferStart + |
| 403 | SourceMgr.getFileOffset(LastComment->getBegin()), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 404 | FileBufferStart + LastCommentEndDecomp.second + 1); |
| 405 | return Result.c_str(); |
| 406 | } |
| 407 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 409 | if (LastComment == Comments.begin()) |
| 410 | return 0; |
| 411 | --LastComment; |
| 412 | |
| 413 | // Decompose the end of the comment. |
| 414 | std::pair<FileID, unsigned> LastCommentEndDecomp |
| 415 | = SourceMgr.getDecomposedLoc(LastComment->getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 417 | // If the comment and the declaration aren't in the same file, then they |
| 418 | // aren't related. |
| 419 | if (DeclStartDecomp.first != LastCommentEndDecomp.first) |
| 420 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 422 | // Check that we actually have a Doxygen comment. |
| 423 | if (!isDoxygenComment(SourceMgr, *LastComment)) |
| 424 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 426 | // Compute the starting line for the declaration and for the end of the |
| 427 | // comment (this is expensive). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | unsigned DeclStartLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 429 | = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second); |
| 430 | unsigned CommentEndLine |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | = SourceMgr.getLineNumber(LastCommentEndDecomp.first, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 432 | LastCommentEndDecomp.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 434 | // If the comment does not end on the line prior to the declaration, then |
| 435 | // the comment is not associated with the declaration at all. |
| 436 | if (CommentEndLine + 1 != DeclStartLine) |
| 437 | return 0; |
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 | // We have a comment, but there may be more comments on the previous lines. |
| 440 | // Keep looking so long as the comments are still Doxygen comments and are |
| 441 | // still adjacent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | unsigned ExpectedLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 443 | = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1; |
| 444 | std::vector<SourceRange>::iterator FirstComment = LastComment; |
| 445 | while (FirstComment != Comments.begin()) { |
| 446 | // Look at the previous comment |
| 447 | --FirstComment; |
| 448 | std::pair<FileID, unsigned> Decomp |
| 449 | = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 451 | // If this previous comment is in a different file, we're done. |
| 452 | if (Decomp.first != DeclStartDecomp.first) { |
| 453 | ++FirstComment; |
| 454 | break; |
| 455 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 457 | // If this comment is not a Doxygen comment, we're done. |
| 458 | if (!isDoxygenComment(SourceMgr, *FirstComment)) { |
| 459 | ++FirstComment; |
| 460 | break; |
| 461 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 463 | // If the line number is not what we expected, we're done. |
| 464 | unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second); |
| 465 | if (Line != ExpectedLine) { |
| 466 | ++FirstComment; |
| 467 | break; |
| 468 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 470 | // Set the next expected line number. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | ExpectedLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 472 | = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1; |
| 473 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 475 | // The iterator range [FirstComment, LastComment] contains all of the |
| 476 | // BCPL comments that, together, are associated with this declaration. |
| 477 | // Form a single comment block string for this declaration that concatenates |
| 478 | // all of these comments. |
| 479 | std::string &Result = DeclComments[D]; |
| 480 | while (FirstComment != LastComment) { |
| 481 | std::pair<FileID, unsigned> DecompStart |
| 482 | = SourceMgr.getDecomposedLoc(FirstComment->getBegin()); |
| 483 | std::pair<FileID, unsigned> DecompEnd |
| 484 | = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); |
| 485 | Result.append(FileBufferStart + DecompStart.second, |
| 486 | FileBufferStart + DecompEnd.second + 1); |
| 487 | ++FirstComment; |
| 488 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 490 | // Append the last comment line. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | Result.append(FileBufferStart + |
| 492 | SourceMgr.getFileOffset(LastComment->getBegin()), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 493 | FileBufferStart + LastCommentEndDecomp.second + 1); |
| 494 | return Result.c_str(); |
| 495 | } |
| 496 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 497 | //===----------------------------------------------------------------------===// |
| 498 | // Type Sizing and Analysis |
| 499 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 500 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 501 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 502 | /// scalar floating point type. |
| 503 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 504 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 505 | assert(BT && "Not a floating point type!"); |
| 506 | switch (BT->getKind()) { |
| 507 | default: assert(0 && "Not a floating point type!"); |
| 508 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 509 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 510 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 511 | } |
| 512 | } |
| 513 | |
Mike Stump | 196efbf | 2009-09-22 02:43:44 +0000 | [diff] [blame] | 514 | /// getDeclAlignInBytes - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 515 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 516 | /// this method will assert on them. |
Daniel Dunbar | b7d0844 | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 517 | unsigned ASTContext::getDeclAlignInBytes(const Decl *D) { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 518 | unsigned Align = Target.getCharWidth(); |
| 519 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 520 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 521 | Align = std::max(Align, AA->getAlignment()); |
| 522 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 523 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 524 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 525 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 526 | unsigned AS = RT->getPointeeType().getAddressSpace(); |
Anders Carlsson | f093023 | 2009-04-10 04:52:36 +0000 | [diff] [blame] | 527 | Align = Target.getPointerAlign(AS); |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 528 | } else if (!T->isIncompleteType() && !T->isFunctionType()) { |
| 529 | // Incomplete or function types default to 1. |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 530 | while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) |
| 531 | T = cast<ArrayType>(T)->getElementType(); |
| 532 | |
| 533 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 534 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 535 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 536 | |
| 537 | return Align / Target.getCharWidth(); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 538 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 539 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 540 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 541 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 542 | /// |
| 543 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 544 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 545 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 546 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 547 | ASTContext::getTypeInfo(const Type *T) { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 548 | uint64_t Width=0; |
| 549 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 550 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 551 | #define TYPE(Class, Base) |
| 552 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 553 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 554 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 555 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 556 | assert(false && "Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 557 | break; |
| 558 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 559 | case Type::FunctionNoProto: |
| 560 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 561 | // GCC extension: alignof(function) = 32 bits |
| 562 | Width = 0; |
| 563 | Align = 32; |
| 564 | break; |
| 565 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 566 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 567 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 568 | Width = 0; |
| 569 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 570 | break; |
| 571 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 572 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 573 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 575 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 576 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 577 | Align = EltInfo.second; |
| 578 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 579 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 580 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 581 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 582 | const VectorType *VT = cast<VectorType>(T); |
| 583 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 584 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 585 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 586 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 587 | // This happens for non-power-of-2 length vectors. |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 588 | if (VT->getNumElements() & (VT->getNumElements()-1)) { |
| 589 | Align = llvm::NextPowerOf2(Align); |
| 590 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 591 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 592 | break; |
| 593 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 594 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 595 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 596 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 597 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 598 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 599 | // GCC extension: alignof(void) = 8 bits. |
| 600 | Width = 0; |
| 601 | Align = 8; |
| 602 | break; |
| 603 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 604 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 605 | Width = Target.getBoolWidth(); |
| 606 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 607 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 608 | case BuiltinType::Char_S: |
| 609 | case BuiltinType::Char_U: |
| 610 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 611 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 612 | Width = Target.getCharWidth(); |
| 613 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 614 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 615 | case BuiltinType::WChar: |
| 616 | Width = Target.getWCharWidth(); |
| 617 | Align = Target.getWCharAlign(); |
| 618 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 619 | case BuiltinType::Char16: |
| 620 | Width = Target.getChar16Width(); |
| 621 | Align = Target.getChar16Align(); |
| 622 | break; |
| 623 | case BuiltinType::Char32: |
| 624 | Width = Target.getChar32Width(); |
| 625 | Align = Target.getChar32Align(); |
| 626 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 627 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 628 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 629 | Width = Target.getShortWidth(); |
| 630 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 631 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 632 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 633 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 634 | Width = Target.getIntWidth(); |
| 635 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 636 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 637 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 638 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 639 | Width = Target.getLongWidth(); |
| 640 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 641 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 642 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 643 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 644 | Width = Target.getLongLongWidth(); |
| 645 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 646 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 647 | case BuiltinType::Int128: |
| 648 | case BuiltinType::UInt128: |
| 649 | Width = 128; |
| 650 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 651 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 652 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 653 | Width = Target.getFloatWidth(); |
| 654 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 655 | break; |
| 656 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 657 | Width = Target.getDoubleWidth(); |
| 658 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 659 | break; |
| 660 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 661 | Width = Target.getLongDoubleWidth(); |
| 662 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 663 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 664 | case BuiltinType::NullPtr: |
| 665 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 666 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 667 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 668 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 669 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 670 | case Type::FixedWidthInt: |
| 671 | // FIXME: This isn't precisely correct; the width/alignment should depend |
| 672 | // on the available types for the target |
| 673 | Width = cast<FixedWidthIntType>(T)->getWidth(); |
Chris Lattner | 736166b | 2009-02-15 21:20:13 +0000 | [diff] [blame] | 674 | Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 675 | Align = Width; |
| 676 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 677 | case Type::ObjCObjectPointer: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 678 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 679 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 680 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 681 | case Type::BlockPointer: { |
| 682 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 683 | Width = Target.getPointerWidth(AS); |
| 684 | Align = Target.getPointerAlign(AS); |
| 685 | break; |
| 686 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 687 | case Type::Pointer: { |
| 688 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 689 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 690 | Align = Target.getPointerAlign(AS); |
| 691 | break; |
| 692 | } |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 693 | case Type::LValueReference: |
| 694 | case Type::RValueReference: |
Chris Lattner | 7ab2ed8 | 2007-07-13 22:16:13 +0000 | [diff] [blame] | 695 | // "When applied to a reference or a reference type, the result is the size |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 696 | // of the referenced type." C++98 5.3.3p2: expr.sizeof. |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 697 | // FIXME: This is wrong for struct layout: a reference in a struct has |
| 698 | // pointer size. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 699 | return getTypeInfo(cast<ReferenceType>(T)->getPointeeType()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 700 | case Type::MemberPointer: { |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 701 | // FIXME: This is ABI dependent. We use the Itanium C++ ABI. |
| 702 | // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers |
| 703 | // If we ever want to support other ABIs this needs to be abstracted. |
| 704 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 705 | QualType Pointee = cast<MemberPointerType>(T)->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 706 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 707 | getTypeInfo(getPointerDiffType()); |
| 708 | Width = PtrDiffInfo.first; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 709 | if (Pointee->isFunctionType()) |
| 710 | Width *= 2; |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 711 | Align = PtrDiffInfo.second; |
| 712 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 713 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 714 | case Type::Complex: { |
| 715 | // Complex types have the same alignment as their elements, but twice the |
| 716 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 718 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 719 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 720 | Align = EltInfo.second; |
| 721 | break; |
| 722 | } |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 723 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 724 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 725 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 726 | Width = Layout.getSize(); |
| 727 | Align = Layout.getAlignment(); |
| 728 | break; |
| 729 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 730 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 731 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 732 | const TagType *TT = cast<TagType>(T); |
| 733 | |
| 734 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 735 | Width = 1; |
| 736 | Align = 1; |
| 737 | break; |
| 738 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 740 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 741 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 742 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 743 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 744 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 745 | Width = Layout.getSize(); |
| 746 | Align = Layout.getAlignment(); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 747 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 748 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 749 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 750 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 751 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 752 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 753 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 754 | case Type::Elaborated: |
| 755 | return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType() |
| 756 | .getTypePtr()); |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 757 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 758 | case Type::Typedef: { |
| 759 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 760 | if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) { |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 761 | Align = Aligned->getAlignment(); |
| 762 | Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr()); |
| 763 | } else |
| 764 | return getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 765 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 766 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 767 | |
| 768 | case Type::TypeOfExpr: |
| 769 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 770 | .getTypePtr()); |
| 771 | |
| 772 | case Type::TypeOf: |
| 773 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 774 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 775 | case Type::Decltype: |
| 776 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 777 | .getTypePtr()); |
| 778 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 779 | case Type::QualifiedName: |
| 780 | return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 782 | case Type::TemplateSpecialization: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 783 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 784 | "Cannot request the size of a dependent type"); |
| 785 | // FIXME: this is likely to be wrong once we support template |
| 786 | // aliases, since a template alias could refer to a typedef that |
| 787 | // has an __aligned__ attribute on it. |
| 788 | return getTypeInfo(getCanonicalType(T)); |
| 789 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 790 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 791 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 792 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 795 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 796 | /// type for the current target in bits. This can be different than the ABI |
| 797 | /// alignment in cases where it is beneficial for performance to overalign |
| 798 | /// a data type. |
| 799 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) { |
| 800 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 801 | |
| 802 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 803 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 804 | T = CT->getElementType().getTypePtr(); |
| 805 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 806 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 807 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 808 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 809 | return ABIAlign; |
| 810 | } |
| 811 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 812 | static void CollectLocalObjCIvars(ASTContext *Ctx, |
| 813 | const ObjCInterfaceDecl *OI, |
| 814 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 815 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 816 | E = OI->ivar_end(); I != E; ++I) { |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 817 | ObjCIvarDecl *IVDecl = *I; |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 818 | if (!IVDecl->isInvalidDecl()) |
| 819 | Fields.push_back(cast<FieldDecl>(IVDecl)); |
| 820 | } |
| 821 | } |
| 822 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 823 | void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 824 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
| 825 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 826 | CollectObjCIvars(SuperClass, Fields); |
| 827 | CollectLocalObjCIvars(this, OI, Fields); |
| 828 | } |
| 829 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 830 | /// ShallowCollectObjCIvars - |
| 831 | /// Collect all ivars, including those synthesized, in the current class. |
| 832 | /// |
| 833 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 834 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars, |
| 835 | bool CollectSynthesized) { |
| 836 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 837 | E = OI->ivar_end(); I != E; ++I) { |
| 838 | Ivars.push_back(*I); |
| 839 | } |
| 840 | if (CollectSynthesized) |
| 841 | CollectSynthesizedIvars(OI, Ivars); |
| 842 | } |
| 843 | |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 844 | void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD, |
| 845 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 846 | for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), |
| 847 | E = PD->prop_end(); I != E; ++I) |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 848 | if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) |
| 849 | Ivars.push_back(Ivar); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 851 | // Also look into nested protocols. |
| 852 | for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), |
| 853 | E = PD->protocol_end(); P != E; ++P) |
| 854 | CollectProtocolSynthesizedIvars(*P, Ivars); |
| 855 | } |
| 856 | |
| 857 | /// CollectSynthesizedIvars - |
| 858 | /// This routine collect synthesized ivars for the designated class. |
| 859 | /// |
| 860 | void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI, |
| 861 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 862 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), |
| 863 | E = OI->prop_end(); I != E; ++I) { |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 864 | if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) |
| 865 | Ivars.push_back(Ivar); |
| 866 | } |
| 867 | // Also look into interface's protocol list for properties declared |
| 868 | // in the protocol and whose ivars are synthesized. |
| 869 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 870 | PE = OI->protocol_end(); P != PE; ++P) { |
| 871 | ObjCProtocolDecl *PD = (*P); |
| 872 | CollectProtocolSynthesizedIvars(PD, Ivars); |
| 873 | } |
| 874 | } |
| 875 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 876 | unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) { |
| 877 | unsigned count = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 878 | for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), |
| 879 | E = PD->prop_end(); I != E; ++I) |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 880 | if ((*I)->getPropertyIvarDecl()) |
| 881 | ++count; |
| 882 | |
| 883 | // Also look into nested protocols. |
| 884 | for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), |
| 885 | E = PD->protocol_end(); P != E; ++P) |
| 886 | count += CountProtocolSynthesizedIvars(*P); |
| 887 | return count; |
| 888 | } |
| 889 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 891 | unsigned count = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 892 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), |
| 893 | E = OI->prop_end(); I != E; ++I) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 894 | if ((*I)->getPropertyIvarDecl()) |
| 895 | ++count; |
| 896 | } |
| 897 | // Also look into interface's protocol list for properties declared |
| 898 | // in the protocol and whose ivars are synthesized. |
| 899 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 900 | PE = OI->protocol_end(); P != PE; ++P) { |
| 901 | ObjCProtocolDecl *PD = (*P); |
| 902 | count += CountProtocolSynthesizedIvars(PD); |
| 903 | } |
| 904 | return count; |
| 905 | } |
| 906 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 907 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 908 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 909 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 910 | I = ObjCImpls.find(D); |
| 911 | if (I != ObjCImpls.end()) |
| 912 | return cast<ObjCImplementationDecl>(I->second); |
| 913 | return 0; |
| 914 | } |
| 915 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 916 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 917 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 918 | I = ObjCImpls.find(D); |
| 919 | if (I != ObjCImpls.end()) |
| 920 | return cast<ObjCCategoryImplDecl>(I->second); |
| 921 | return 0; |
| 922 | } |
| 923 | |
| 924 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 925 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 926 | ObjCImplementationDecl *ImplD) { |
| 927 | assert(IFaceD && ImplD && "Passed null params"); |
| 928 | ObjCImpls[IFaceD] = ImplD; |
| 929 | } |
| 930 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 931 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 932 | ObjCCategoryImplDecl *ImplD) { |
| 933 | assert(CatD && ImplD && "Passed null params"); |
| 934 | ObjCImpls[CatD] = ImplD; |
| 935 | } |
| 936 | |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 937 | /// \brief Allocate an uninitialized DeclaratorInfo. |
| 938 | /// |
| 939 | /// The caller should initialize the memory held by DeclaratorInfo using |
| 940 | /// the TypeLoc wrappers. |
| 941 | /// |
| 942 | /// \param T the type that will be the basis for type source info. This type |
| 943 | /// should refer to how the declarator was written in source code, not to |
| 944 | /// what type semantic analysis resolved the declarator to. |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 945 | DeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T, |
| 946 | unsigned DataSize) { |
| 947 | if (!DataSize) |
| 948 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 949 | else |
| 950 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
| 951 | "incorrect data size provided to CreateDeclaratorInfo!"); |
| 952 | |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 953 | DeclaratorInfo *DInfo = |
| 954 | (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8); |
| 955 | new (DInfo) DeclaratorInfo(T); |
| 956 | return DInfo; |
| 957 | } |
| 958 | |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 959 | DeclaratorInfo *ASTContext::getTrivialDeclaratorInfo(QualType T, |
| 960 | SourceLocation L) { |
| 961 | DeclaratorInfo *DI = CreateDeclaratorInfo(T); |
| 962 | DI->getTypeLoc().initialize(L); |
| 963 | return DI; |
| 964 | } |
| 965 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 966 | /// getInterfaceLayoutImpl - Get or compute information about the |
| 967 | /// layout of the given interface. |
| 968 | /// |
| 969 | /// \param Impl - If given, also include the layout of the interface's |
| 970 | /// implementation. This may differ by including synthesized ivars. |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 971 | const ASTRecordLayout & |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 972 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
| 973 | const ObjCImplementationDecl *Impl) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 974 | assert(!D->isForwardDecl() && "Invalid interface decl!"); |
| 975 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 976 | // Look up this layout, if already laid out, return what we have. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | ObjCContainerDecl *Key = |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 978 | Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; |
| 979 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 980 | return *Entry; |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 981 | |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 982 | // Add in synthesized ivar count if laying out an implementation. |
| 983 | if (Impl) { |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 984 | unsigned FieldCount = D->ivar_size(); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 985 | unsigned SynthCount = CountSynthesizedIvars(D); |
| 986 | FieldCount += SynthCount; |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 987 | // If there aren't any sythesized ivars then reuse the interface |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 988 | // entry. Note we can't cache this because we simply free all |
| 989 | // entries later; however we shouldn't look up implementations |
| 990 | // frequently. |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 991 | if (SynthCount == 0) |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 992 | return getObjCLayout(D, 0); |
| 993 | } |
| 994 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 995 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 996 | ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl); |
| 997 | ObjCLayouts[Key] = NewEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 998 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 999 | return *NewEntry; |
| 1000 | } |
| 1001 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1002 | const ASTRecordLayout & |
| 1003 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 1004 | return getObjCLayout(D, 0); |
| 1005 | } |
| 1006 | |
| 1007 | const ASTRecordLayout & |
| 1008 | ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { |
| 1009 | return getObjCLayout(D->getClassInterface(), D); |
| 1010 | } |
| 1011 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 1012 | /// getASTRecordLayout - Get or compute information about the layout of the |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1013 | /// specified record (struct/union/class), which indicates its size and field |
| 1014 | /// position information. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1015 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1016 | D = D->getDefinition(*this); |
| 1017 | assert(D && "Cannot get layout of forward declarations!"); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1018 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1019 | // Look up this layout, if already laid out, return what we have. |
Eli Friedman | ab22c43 | 2009-07-22 20:29:16 +0000 | [diff] [blame] | 1020 | // Note that we can't save a reference to the entry because this function |
| 1021 | // is recursive. |
| 1022 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1023 | if (Entry) return *Entry; |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1024 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 1026 | ASTRecordLayoutBuilder::ComputeLayout(*this, D); |
Eli Friedman | ab22c43 | 2009-07-22 20:29:16 +0000 | [diff] [blame] | 1027 | ASTRecordLayouts[D] = NewEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1029 | return *NewEntry; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1032 | //===----------------------------------------------------------------------===// |
| 1033 | // Type creation/memoization methods |
| 1034 | //===----------------------------------------------------------------------===// |
| 1035 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1036 | QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) { |
| 1037 | unsigned Fast = Quals.getFastQualifiers(); |
| 1038 | Quals.removeFastQualifiers(); |
| 1039 | |
| 1040 | // Check if we've already instantiated this type. |
| 1041 | llvm::FoldingSetNodeID ID; |
| 1042 | ExtQuals::Profile(ID, TypeNode, Quals); |
| 1043 | void *InsertPos = 0; |
| 1044 | if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 1045 | assert(EQ->getQualifiers() == Quals); |
| 1046 | QualType T = QualType(EQ, Fast); |
| 1047 | return T; |
| 1048 | } |
| 1049 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1050 | ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1051 | ExtQualNodes.InsertNode(New, InsertPos); |
| 1052 | QualType T = QualType(New, Fast); |
| 1053 | return T; |
| 1054 | } |
| 1055 | |
| 1056 | QualType ASTContext::getVolatileType(QualType T) { |
| 1057 | QualType CanT = getCanonicalType(T); |
| 1058 | if (CanT.isVolatileQualified()) return T; |
| 1059 | |
| 1060 | QualifierCollector Quals; |
| 1061 | const Type *TypeNode = Quals.strip(T); |
| 1062 | Quals.addVolatile(); |
| 1063 | |
| 1064 | return getExtQualType(TypeNode, Quals); |
| 1065 | } |
| 1066 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1067 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1068 | QualType CanT = getCanonicalType(T); |
| 1069 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1070 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1071 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1072 | // If we are composing extended qualifiers together, merge together |
| 1073 | // into one ExtQuals node. |
| 1074 | QualifierCollector Quals; |
| 1075 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1076 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1077 | // If this type already has an address space specified, it cannot get |
| 1078 | // another one. |
| 1079 | assert(!Quals.hasAddressSpace() && |
| 1080 | "Type cannot be in multiple addr spaces!"); |
| 1081 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1082 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1083 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1086 | QualType ASTContext::getObjCGCQualType(QualType T, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1087 | Qualifiers::GC GCAttr) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1088 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1089 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1090 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1091 | |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1092 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1093 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1094 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1095 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1096 | return getPointerType(ResultType); |
| 1097 | } |
| 1098 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1100 | // If we are composing extended qualifiers together, merge together |
| 1101 | // into one ExtQuals node. |
| 1102 | QualifierCollector Quals; |
| 1103 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1105 | // If this type already has an ObjCGC specified, it cannot get |
| 1106 | // another one. |
| 1107 | assert(!Quals.hasObjCGCAttr() && |
| 1108 | "Type cannot have multiple ObjCGCs!"); |
| 1109 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1110 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1111 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1112 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1113 | |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1114 | QualType ASTContext::getNoReturnType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1115 | QualType ResultType; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1116 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1117 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1118 | ResultType = getNoReturnType(Pointee); |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 1119 | ResultType = getPointerType(ResultType); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1120 | } else if (T->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1121 | QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1122 | ResultType = getNoReturnType(Pointee); |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 1123 | ResultType = getBlockPointerType(ResultType); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1124 | } else { |
| 1125 | assert (T->isFunctionType() |
| 1126 | && "can't noreturn qualify non-pointer to function or block type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | |
Benjamin Kramer | bdbeeb5 | 2009-09-25 11:47:22 +0000 | [diff] [blame] | 1128 | if (const FunctionNoProtoType *FNPT = T->getAs<FunctionNoProtoType>()) { |
| 1129 | ResultType = getFunctionNoProtoType(FNPT->getResultType(), true); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1130 | } else { |
| 1131 | const FunctionProtoType *F = T->getAs<FunctionProtoType>(); |
| 1132 | ResultType |
| 1133 | = getFunctionType(F->getResultType(), F->arg_type_begin(), |
| 1134 | F->getNumArgs(), F->isVariadic(), F->getTypeQuals(), |
| 1135 | F->hasExceptionSpec(), F->hasAnyExceptionSpec(), |
| 1136 | F->getNumExceptions(), F->exception_begin(), true); |
| 1137 | } |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1138 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1139 | |
| 1140 | return getQualifiedType(ResultType, T.getQualifiers()); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1143 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1144 | /// number with the specified element type. |
| 1145 | QualType ASTContext::getComplexType(QualType T) { |
| 1146 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1147 | // structure. |
| 1148 | llvm::FoldingSetNodeID ID; |
| 1149 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1151 | void *InsertPos = 0; |
| 1152 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1153 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1154 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1155 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1156 | // so fill in the canonical type field. |
| 1157 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1158 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1159 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1161 | // Get the new insert position for the node we care about. |
| 1162 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1163 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1164 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1165 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1166 | Types.push_back(New); |
| 1167 | ComplexTypes.InsertNode(New, InsertPos); |
| 1168 | return QualType(New, 0); |
| 1169 | } |
| 1170 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1171 | QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) { |
| 1172 | llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ? |
| 1173 | SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes; |
| 1174 | FixedWidthIntType *&Entry = Map[Width]; |
| 1175 | if (!Entry) |
| 1176 | Entry = new FixedWidthIntType(Width, Signed); |
| 1177 | return QualType(Entry, 0); |
| 1178 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1179 | |
| 1180 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1181 | /// the specified type. |
| 1182 | QualType ASTContext::getPointerType(QualType T) { |
| 1183 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1184 | // structure. |
| 1185 | llvm::FoldingSetNodeID ID; |
| 1186 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1187 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1188 | void *InsertPos = 0; |
| 1189 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1190 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1191 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1192 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1193 | // so fill in the canonical type field. |
| 1194 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1195 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1196 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1198 | // Get the new insert position for the node we care about. |
| 1199 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1200 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1201 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1202 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1203 | Types.push_back(New); |
| 1204 | PointerTypes.InsertNode(New, InsertPos); |
| 1205 | return QualType(New, 0); |
| 1206 | } |
| 1207 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1208 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1209 | /// a pointer to the specified block. |
| 1210 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1211 | assert(T->isFunctionType() && "block of function types only"); |
| 1212 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1213 | // structure. |
| 1214 | llvm::FoldingSetNodeID ID; |
| 1215 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1217 | void *InsertPos = 0; |
| 1218 | if (BlockPointerType *PT = |
| 1219 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1220 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1221 | |
| 1222 | // 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] | 1223 | // type either so fill in the canonical type field. |
| 1224 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1225 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1226 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1228 | // Get the new insert position for the node we care about. |
| 1229 | BlockPointerType *NewIP = |
| 1230 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1231 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1232 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1233 | BlockPointerType *New |
| 1234 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1235 | Types.push_back(New); |
| 1236 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1237 | return QualType(New, 0); |
| 1238 | } |
| 1239 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1240 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1241 | /// lvalue reference to the specified type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1242 | QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1243 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1244 | // structure. |
| 1245 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1246 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1247 | |
| 1248 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1249 | if (LValueReferenceType *RT = |
| 1250 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1251 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1252 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1253 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1254 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1255 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1256 | // either, so fill in the canonical type field. |
| 1257 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1258 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1259 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1260 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1261 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1262 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1263 | LValueReferenceType *NewIP = |
| 1264 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1265 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1268 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1269 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1270 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1271 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1272 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1273 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1274 | return QualType(New, 0); |
| 1275 | } |
| 1276 | |
| 1277 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1278 | /// rvalue reference to the specified type. |
| 1279 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1280 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1281 | // structure. |
| 1282 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1283 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1284 | |
| 1285 | void *InsertPos = 0; |
| 1286 | if (RValueReferenceType *RT = |
| 1287 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1288 | return QualType(RT, 0); |
| 1289 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1290 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1291 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1292 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1293 | // either, so fill in the canonical type field. |
| 1294 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1295 | if (InnerRef || !T.isCanonical()) { |
| 1296 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1297 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1298 | |
| 1299 | // Get the new insert position for the node we care about. |
| 1300 | RValueReferenceType *NewIP = |
| 1301 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1302 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1303 | } |
| 1304 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1305 | RValueReferenceType *New |
| 1306 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1307 | Types.push_back(New); |
| 1308 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1309 | return QualType(New, 0); |
| 1310 | } |
| 1311 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1312 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1313 | /// member pointer to the specified type, in the specified class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1314 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1315 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1316 | // structure. |
| 1317 | llvm::FoldingSetNodeID ID; |
| 1318 | MemberPointerType::Profile(ID, T, Cls); |
| 1319 | |
| 1320 | void *InsertPos = 0; |
| 1321 | if (MemberPointerType *PT = |
| 1322 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1323 | return QualType(PT, 0); |
| 1324 | |
| 1325 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1326 | // type either, so fill in the canonical type field. |
| 1327 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1328 | if (!T.isCanonical()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1329 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1330 | |
| 1331 | // Get the new insert position for the node we care about. |
| 1332 | MemberPointerType *NewIP = |
| 1333 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1334 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1335 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1336 | MemberPointerType *New |
| 1337 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1338 | Types.push_back(New); |
| 1339 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1340 | return QualType(New, 0); |
| 1341 | } |
| 1342 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1344 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1345 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1346 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1347 | ArrayType::ArraySizeModifier ASM, |
| 1348 | unsigned EltTypeQuals) { |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1349 | assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) && |
| 1350 | "Constant array of VLAs is illegal!"); |
| 1351 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1352 | // Convert the array size into a canonical width matching the pointer size for |
| 1353 | // the target. |
| 1354 | llvm::APInt ArySize(ArySizeIn); |
| 1355 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1356 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1357 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1358 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1360 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1362 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1363 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1364 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1365 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1366 | // so fill in the canonical type field. |
| 1367 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1368 | if (!EltTy.isCanonical()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1370 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1371 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1373 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1374 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1375 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1376 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1377 | ConstantArrayType *New = new(*this,TypeAlignment) |
| 1378 | ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1379 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1380 | Types.push_back(New); |
| 1381 | return QualType(New, 0); |
| 1382 | } |
| 1383 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1384 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1385 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1386 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1387 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1388 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1389 | unsigned EltTypeQuals, |
| 1390 | SourceRange Brackets) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1391 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1392 | // that have an expression provided for their size. |
| 1393 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1394 | VariableArrayType *New = new(*this, TypeAlignment) |
| 1395 | VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1396 | |
| 1397 | VariableArrayTypes.push_back(New); |
| 1398 | Types.push_back(New); |
| 1399 | return QualType(New, 0); |
| 1400 | } |
| 1401 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1402 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1403 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1404 | /// type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1405 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1406 | Expr *NumElts, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1407 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1408 | unsigned EltTypeQuals, |
| 1409 | SourceRange Brackets) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1411 | "Size must be type- or value-dependent!"); |
| 1412 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1413 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM, |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1415 | EltTypeQuals, NumElts); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1416 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1417 | void *InsertPos = 0; |
| 1418 | DependentSizedArrayType *Canon |
| 1419 | = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1420 | DependentSizedArrayType *New; |
| 1421 | if (Canon) { |
| 1422 | // We already have a canonical version of this array type; use it as |
| 1423 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1424 | New = new (*this, TypeAlignment) |
| 1425 | DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), |
| 1426 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1427 | } else { |
| 1428 | QualType CanonEltTy = getCanonicalType(EltTy); |
| 1429 | if (CanonEltTy == EltTy) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1430 | New = new (*this, TypeAlignment) |
| 1431 | DependentSizedArrayType(*this, EltTy, QualType(), |
| 1432 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1433 | DependentSizedArrayTypes.InsertNode(New, InsertPos); |
| 1434 | } else { |
| 1435 | QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts, |
| 1436 | ASM, EltTypeQuals, |
| 1437 | SourceRange()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1438 | New = new (*this, TypeAlignment) |
| 1439 | DependentSizedArrayType(*this, EltTy, Canon, |
| 1440 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1441 | } |
| 1442 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1444 | Types.push_back(New); |
| 1445 | return QualType(New, 0); |
| 1446 | } |
| 1447 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1448 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1449 | ArrayType::ArraySizeModifier ASM, |
| 1450 | unsigned EltTypeQuals) { |
| 1451 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1452 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1453 | |
| 1454 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1455 | if (IncompleteArrayType *ATP = |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1456 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1457 | return QualType(ATP, 0); |
| 1458 | |
| 1459 | // If the element type isn't canonical, this won't be a canonical type |
| 1460 | // either, so fill in the canonical type field. |
| 1461 | QualType Canonical; |
| 1462 | |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1463 | if (!EltTy.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1464 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1465 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1466 | |
| 1467 | // Get the new insert position for the node we care about. |
| 1468 | IncompleteArrayType *NewIP = |
| 1469 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1470 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1471 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1472 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1473 | IncompleteArrayType *New = new (*this, TypeAlignment) |
| 1474 | IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1475 | |
| 1476 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1477 | Types.push_back(New); |
| 1478 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1481 | /// getVectorType - Return the unique reference to a vector type of |
| 1482 | /// the specified element type and size. VectorType must be a built-in type. |
| 1483 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1484 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1486 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1487 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1489 | // Check if we've already instantiated a vector of this type. |
| 1490 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1491 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1492 | void *InsertPos = 0; |
| 1493 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1494 | return QualType(VTP, 0); |
| 1495 | |
| 1496 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1497 | // so fill in the canonical type field. |
| 1498 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1499 | if (!vecType.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1500 | Canonical = getVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1502 | // Get the new insert position for the node we care about. |
| 1503 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1504 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1505 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1506 | VectorType *New = new (*this, TypeAlignment) |
| 1507 | VectorType(vecType, NumElts, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1508 | VectorTypes.InsertNode(New, InsertPos); |
| 1509 | Types.push_back(New); |
| 1510 | return QualType(New, 0); |
| 1511 | } |
| 1512 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1513 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1514 | /// 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] | 1515 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1516 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1517 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1518 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1519 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1521 | // Check if we've already instantiated a vector of this type. |
| 1522 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1523 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1524 | void *InsertPos = 0; |
| 1525 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1526 | return QualType(VTP, 0); |
| 1527 | |
| 1528 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1529 | // so fill in the canonical type field. |
| 1530 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1531 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1532 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1534 | // Get the new insert position for the node we care about. |
| 1535 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1536 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1537 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1538 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1539 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1540 | VectorTypes.InsertNode(New, InsertPos); |
| 1541 | Types.push_back(New); |
| 1542 | return QualType(New, 0); |
| 1543 | } |
| 1544 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1545 | QualType ASTContext::getDependentSizedExtVectorType(QualType vecType, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1546 | Expr *SizeExpr, |
| 1547 | SourceLocation AttrLoc) { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1548 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1550 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1551 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1552 | void *InsertPos = 0; |
| 1553 | DependentSizedExtVectorType *Canon |
| 1554 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1555 | DependentSizedExtVectorType *New; |
| 1556 | if (Canon) { |
| 1557 | // We already have a canonical version of this array type; use it as |
| 1558 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1559 | New = new (*this, TypeAlignment) |
| 1560 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1561 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1562 | } else { |
| 1563 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1564 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1565 | New = new (*this, TypeAlignment) |
| 1566 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1567 | AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1568 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1569 | } else { |
| 1570 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1571 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1572 | New = new (*this, TypeAlignment) |
| 1573 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1574 | } |
| 1575 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1577 | Types.push_back(New); |
| 1578 | return QualType(New, 0); |
| 1579 | } |
| 1580 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1581 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1582 | /// |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1583 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1584 | // Unique functions, to guarantee there is only one function of a particular |
| 1585 | // structure. |
| 1586 | llvm::FoldingSetNodeID ID; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1587 | FunctionNoProtoType::Profile(ID, ResultTy, NoReturn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1589 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1590 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1591 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1592 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1593 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1594 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1595 | if (!ResultTy.isCanonical()) { |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1596 | Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1597 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1598 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1599 | FunctionNoProtoType *NewIP = |
| 1600 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1601 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1602 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1603 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1604 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
| 1605 | FunctionNoProtoType(ResultTy, Canonical, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1606 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1607 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1608 | return QualType(New, 0); |
| 1609 | } |
| 1610 | |
| 1611 | /// getFunctionType - Return a normal function type with a typed argument |
| 1612 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1613 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1614 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1615 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1616 | bool hasAnyExceptionSpec, unsigned NumExs, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1617 | const QualType *ExArray, bool NoReturn) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1618 | // Unique functions, to guarantee there is only one function of a particular |
| 1619 | // structure. |
| 1620 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1621 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1622 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1623 | NumExs, ExArray, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1624 | |
| 1625 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1627 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1628 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1629 | |
| 1630 | // Determine whether the type being created is already canonical or not. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1631 | bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1632 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1633 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1634 | isCanonical = false; |
| 1635 | |
| 1636 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1637 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1638 | QualType Canonical; |
| 1639 | if (!isCanonical) { |
| 1640 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1641 | CanonicalArgs.reserve(NumArgs); |
| 1642 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1643 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1644 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1645 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1646 | CanonicalArgs.data(), NumArgs, |
Douglas Gregor | 47259d9 | 2009-08-05 19:03:35 +0000 | [diff] [blame] | 1647 | isVariadic, TypeQuals, false, |
| 1648 | false, 0, 0, NoReturn); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1649 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1650 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1651 | FunctionProtoType *NewIP = |
| 1652 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1653 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1654 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1655 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1656 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1657 | // for two variable size arrays (for parameter and exception types) at the |
| 1658 | // end of them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | FunctionProtoType *FTP = |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1660 | (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + |
| 1661 | NumArgs*sizeof(QualType) + |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1662 | NumExs*sizeof(QualType), TypeAlignment); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1663 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1664 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1665 | ExArray, NumExs, Canonical, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1666 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1667 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1668 | return QualType(FTP, 0); |
| 1669 | } |
| 1670 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1671 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1672 | /// specified type declaration. |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1673 | QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1674 | assert(Decl && "Passed null for Decl param"); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1675 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1677 | if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1678 | return getTypedefType(Typedef); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1679 | else if (isa<TemplateTypeParmDecl>(Decl)) { |
| 1680 | assert(false && "Template type parameter types are always available."); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1681 | } else if (ObjCInterfaceDecl *ObjCInterface |
| 1682 | = dyn_cast<ObjCInterfaceDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1683 | return getObjCInterfaceType(ObjCInterface); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1684 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1685 | if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1686 | if (PrevDecl) |
| 1687 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1688 | else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1689 | Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1690 | } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1691 | if (PrevDecl) |
| 1692 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1693 | else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1694 | Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1695 | } else |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1696 | assert(false && "TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1697 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1698 | if (!PrevDecl) Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1699 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1702 | /// getTypedefType - Return the unique reference to the type for the |
| 1703 | /// specified typename decl. |
| 1704 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 1705 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1707 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1708 | Decl->TypeForDecl = new(*this, TypeAlignment) |
| 1709 | TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1710 | Types.push_back(Decl->TypeForDecl); |
| 1711 | return QualType(Decl->TypeForDecl, 0); |
| 1712 | } |
| 1713 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1714 | /// \brief Retrieve a substitution-result type. |
| 1715 | QualType |
| 1716 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
| 1717 | QualType Replacement) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1718 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1719 | && "replacement types must always be canonical"); |
| 1720 | |
| 1721 | llvm::FoldingSetNodeID ID; |
| 1722 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 1723 | void *InsertPos = 0; |
| 1724 | SubstTemplateTypeParmType *SubstParm |
| 1725 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1726 | |
| 1727 | if (!SubstParm) { |
| 1728 | SubstParm = new (*this, TypeAlignment) |
| 1729 | SubstTemplateTypeParmType(Parm, Replacement); |
| 1730 | Types.push_back(SubstParm); |
| 1731 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 1732 | } |
| 1733 | |
| 1734 | return QualType(SubstParm, 0); |
| 1735 | } |
| 1736 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1737 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1738 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1739 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1741 | bool ParameterPack, |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1742 | IdentifierInfo *Name) { |
| 1743 | llvm::FoldingSetNodeID ID; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1744 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1745 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1746 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1747 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1748 | |
| 1749 | if (TypeParm) |
| 1750 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1752 | if (Name) { |
| 1753 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1754 | TypeParm = new (*this, TypeAlignment) |
| 1755 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1756 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1757 | TypeParm = new (*this, TypeAlignment) |
| 1758 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1759 | |
| 1760 | Types.push_back(TypeParm); |
| 1761 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1762 | |
| 1763 | return QualType(TypeParm, 0); |
| 1764 | } |
| 1765 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1766 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1767 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
| 1768 | const TemplateArgument *Args, |
| 1769 | unsigned NumArgs, |
| 1770 | QualType Canon) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1771 | if (!Canon.isNull()) |
| 1772 | Canon = getCanonicalType(Canon); |
| 1773 | else { |
| 1774 | // Build the canonical template specialization type. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1775 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 1776 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 1777 | CanonArgs.reserve(NumArgs); |
| 1778 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1779 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 1780 | |
| 1781 | // Determine whether this canonical template specialization type already |
| 1782 | // exists. |
| 1783 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1785 | CanonArgs.data(), NumArgs, *this); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1786 | |
| 1787 | void *InsertPos = 0; |
| 1788 | TemplateSpecializationType *Spec |
| 1789 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1790 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1791 | if (!Spec) { |
| 1792 | // Allocate a new canonical template specialization type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1794 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1795 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1796 | Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1797 | CanonArgs.data(), NumArgs, |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1798 | Canon); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1799 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1800 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1801 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1802 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1803 | if (Canon.isNull()) |
| 1804 | Canon = QualType(Spec, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 | assert(Canon->isDependentType() && |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1806 | "Non-dependent template-id type must have a canonical type"); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1807 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 1808 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1809 | // Allocate the (non-canonical) template specialization type, but don't |
| 1810 | // try to unique it: these types typically have location information that |
| 1811 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1812 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1813 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1814 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1815 | TemplateSpecializationType *Spec |
| 1816 | = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1817 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1819 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | QualType |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1824 | ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS, |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1825 | QualType NamedType) { |
| 1826 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1827 | QualifiedNameType::Profile(ID, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1828 | |
| 1829 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1830 | QualifiedNameType *T |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1831 | = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1832 | if (T) |
| 1833 | return QualType(T, 0); |
| 1834 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1835 | T = new (*this) QualifiedNameType(NNS, NamedType, |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1836 | getCanonicalType(NamedType)); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1837 | Types.push_back(T); |
| 1838 | QualifiedNameTypes.InsertNode(T, InsertPos); |
| 1839 | return QualType(T, 0); |
| 1840 | } |
| 1841 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1842 | QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1843 | const IdentifierInfo *Name, |
| 1844 | QualType Canon) { |
| 1845 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1846 | |
| 1847 | if (Canon.isNull()) { |
| 1848 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 1849 | if (CanonNNS != NNS) |
| 1850 | Canon = getTypenameType(CanonNNS, Name); |
| 1851 | } |
| 1852 | |
| 1853 | llvm::FoldingSetNodeID ID; |
| 1854 | TypenameType::Profile(ID, NNS, Name); |
| 1855 | |
| 1856 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | TypenameType *T |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1858 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1859 | if (T) |
| 1860 | return QualType(T, 0); |
| 1861 | |
| 1862 | T = new (*this) TypenameType(NNS, Name, Canon); |
| 1863 | Types.push_back(T); |
| 1864 | TypenameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1865 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1868 | QualType |
| 1869 | ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1870 | const TemplateSpecializationType *TemplateId, |
| 1871 | QualType Canon) { |
| 1872 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1873 | |
| 1874 | if (Canon.isNull()) { |
| 1875 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 1876 | QualType CanonType = getCanonicalType(QualType(TemplateId, 0)); |
| 1877 | if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) { |
| 1878 | const TemplateSpecializationType *CanonTemplateId |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1879 | = CanonType->getAs<TemplateSpecializationType>(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1880 | assert(CanonTemplateId && |
| 1881 | "Canonical type must also be a template specialization type"); |
| 1882 | Canon = getTypenameType(CanonNNS, CanonTemplateId); |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | llvm::FoldingSetNodeID ID; |
| 1887 | TypenameType::Profile(ID, NNS, TemplateId); |
| 1888 | |
| 1889 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1890 | TypenameType *T |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1891 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1892 | if (T) |
| 1893 | return QualType(T, 0); |
| 1894 | |
| 1895 | T = new (*this) TypenameType(NNS, TemplateId, Canon); |
| 1896 | Types.push_back(T); |
| 1897 | TypenameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1898 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 1901 | QualType |
| 1902 | ASTContext::getElaboratedType(QualType UnderlyingType, |
| 1903 | ElaboratedType::TagKind Tag) { |
| 1904 | llvm::FoldingSetNodeID ID; |
| 1905 | ElaboratedType::Profile(ID, UnderlyingType, Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1906 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 1907 | void *InsertPos = 0; |
| 1908 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1909 | if (T) |
| 1910 | return QualType(T, 0); |
| 1911 | |
| 1912 | QualType Canon = getCanonicalType(UnderlyingType); |
| 1913 | |
| 1914 | T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon); |
| 1915 | Types.push_back(T); |
| 1916 | ElaboratedTypes.InsertNode(T, InsertPos); |
| 1917 | return QualType(T, 0); |
| 1918 | } |
| 1919 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1920 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 1921 | /// alphabetically. |
| 1922 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 1923 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1924 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1927 | static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols, |
| 1928 | unsigned NumProtocols) { |
| 1929 | if (NumProtocols == 0) return true; |
| 1930 | |
| 1931 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 1932 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 1933 | return false; |
| 1934 | return true; |
| 1935 | } |
| 1936 | |
| 1937 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1938 | unsigned &NumProtocols) { |
| 1939 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1940 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1941 | // Sort protocols, keyed by name. |
| 1942 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 1943 | |
| 1944 | // Remove duplicates. |
| 1945 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 1946 | NumProtocols = ProtocolsEnd-Protocols; |
| 1947 | } |
| 1948 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1949 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 1950 | /// the given interface decl and the conforming protocol list. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1951 | QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | ObjCProtocolDecl **Protocols, |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1953 | unsigned NumProtocols) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1954 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1955 | ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1956 | |
| 1957 | void *InsertPos = 0; |
| 1958 | if (ObjCObjectPointerType *QT = |
| 1959 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1960 | return QualType(QT, 0); |
| 1961 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1962 | // Sort the protocol list alphabetically to canonicalize it. |
| 1963 | QualType Canonical; |
| 1964 | if (!InterfaceT.isCanonical() || |
| 1965 | !areSortedAndUniqued(Protocols, NumProtocols)) { |
| 1966 | if (!areSortedAndUniqued(Protocols, NumProtocols)) { |
| 1967 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); |
| 1968 | unsigned UniqueCount = NumProtocols; |
| 1969 | |
| 1970 | std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); |
| 1971 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
| 1972 | |
| 1973 | Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), |
| 1974 | &Sorted[0], UniqueCount); |
| 1975 | } else { |
| 1976 | Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), |
| 1977 | Protocols, NumProtocols); |
| 1978 | } |
| 1979 | |
| 1980 | // Regenerate InsertPos. |
| 1981 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1982 | } |
| 1983 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1984 | // No Match; |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1985 | ObjCObjectPointerType *QType = new (*this, TypeAlignment) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1986 | ObjCObjectPointerType(Canonical, InterfaceT, Protocols, NumProtocols); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1987 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1988 | Types.push_back(QType); |
| 1989 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
| 1990 | return QualType(QType, 0); |
| 1991 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1992 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1993 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 1994 | /// specified ObjC interface decl. The list of protocols is optional. |
| 1995 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1996 | ObjCProtocolDecl **Protocols, unsigned NumProtocols) { |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1997 | llvm::FoldingSetNodeID ID; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1998 | ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1999 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2000 | void *InsertPos = 0; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2001 | if (ObjCInterfaceType *QT = |
| 2002 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2003 | return QualType(QT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2004 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2005 | // Sort the protocol list alphabetically to canonicalize it. |
| 2006 | QualType Canonical; |
| 2007 | if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2008 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); |
| 2009 | std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); |
| 2010 | |
| 2011 | unsigned UniqueCount = NumProtocols; |
| 2012 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
| 2013 | |
| 2014 | Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount); |
| 2015 | |
| 2016 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2017 | } |
| 2018 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2019 | ObjCInterfaceType *QType = new (*this, TypeAlignment) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2020 | ObjCInterfaceType(Canonical, const_cast<ObjCInterfaceDecl*>(Decl), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2021 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2022 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2023 | Types.push_back(QType); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2024 | ObjCInterfaceTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2025 | return QualType(QType, 0); |
| 2026 | } |
| 2027 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2028 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2029 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2030 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2031 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2032 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2033 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2034 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2035 | if (tofExpr->isTypeDependent()) { |
| 2036 | llvm::FoldingSetNodeID ID; |
| 2037 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2038 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2039 | void *InsertPos = 0; |
| 2040 | DependentTypeOfExprType *Canon |
| 2041 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2042 | if (Canon) { |
| 2043 | // We already have a "canonical" version of an identical, dependent |
| 2044 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2045 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2046 | QualType((TypeOfExprType*)Canon, 0)); |
| 2047 | } |
| 2048 | else { |
| 2049 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2050 | Canon |
| 2051 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2052 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2053 | toe = Canon; |
| 2054 | } |
| 2055 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2056 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2057 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2058 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2059 | Types.push_back(toe); |
| 2060 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2063 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2064 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2065 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2066 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2067 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2068 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2069 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2070 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2071 | Types.push_back(tot); |
| 2072 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2075 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2076 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
| 2077 | static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2078 | if (e->isTypeDependent()) |
| 2079 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2080 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2081 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2082 | // as the type of the entity named by e. |
| 2083 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2084 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2085 | return VD->getType(); |
| 2086 | } |
| 2087 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2088 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2089 | return FD->getType(); |
| 2090 | } |
| 2091 | // If e is a function call or an invocation of an overloaded operator, |
| 2092 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2093 | // return type of that function. |
| 2094 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2095 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2096 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2097 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2098 | |
| 2099 | // 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] | 2100 | // defined as T&, otherwise decltype(e) is defined as T. |
| 2101 | if (e->isLvalue(Context) == Expr::LV_Valid) |
| 2102 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2104 | return T; |
| 2105 | } |
| 2106 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2107 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2108 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2109 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2110 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2111 | /// on canonical type's (which are always unique). |
| 2112 | QualType ASTContext::getDecltypeType(Expr *e) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2113 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2114 | if (e->isTypeDependent()) { |
| 2115 | llvm::FoldingSetNodeID ID; |
| 2116 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2118 | void *InsertPos = 0; |
| 2119 | DependentDecltypeType *Canon |
| 2120 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2121 | if (Canon) { |
| 2122 | // We already have a "canonical" version of an equivalent, dependent |
| 2123 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2124 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2125 | QualType((DecltypeType*)Canon, 0)); |
| 2126 | } |
| 2127 | else { |
| 2128 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2129 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2130 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2131 | dt = Canon; |
| 2132 | } |
| 2133 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2134 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2135 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2136 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2137 | Types.push_back(dt); |
| 2138 | return QualType(dt, 0); |
| 2139 | } |
| 2140 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2141 | /// getTagDeclType - Return the unique reference to the type for the |
| 2142 | /// specified TagDecl (struct/union/class/enum) decl. |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2143 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2144 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2145 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2146 | // away const? mutable? |
| 2147 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2150 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2151 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2152 | /// needs to agree with the definition in <stddef.h>. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2153 | QualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2154 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2157 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2158 | /// Used when in C++, as a GCC extension. |
| 2159 | QualType ASTContext::getSignedWCharType() const { |
| 2160 | // FIXME: derive from "Target" ? |
| 2161 | return WCharTy; |
| 2162 | } |
| 2163 | |
| 2164 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2165 | /// Used when in C++, as a GCC extension. |
| 2166 | QualType ASTContext::getUnsignedWCharType() const { |
| 2167 | // FIXME: derive from "Target" ? |
| 2168 | return UnsignedIntTy; |
| 2169 | } |
| 2170 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2171 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2172 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2173 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2174 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2177 | //===----------------------------------------------------------------------===// |
| 2178 | // Type Operators |
| 2179 | //===----------------------------------------------------------------------===// |
| 2180 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2181 | CanQualType ASTContext::getCanonicalParamType(QualType T) { |
| 2182 | // Push qualifiers into arrays, and then discard any remaining |
| 2183 | // qualifiers. |
| 2184 | T = getCanonicalType(T); |
| 2185 | const Type *Ty = T.getTypePtr(); |
| 2186 | |
| 2187 | QualType Result; |
| 2188 | if (isa<ArrayType>(Ty)) { |
| 2189 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2190 | } else if (isa<FunctionType>(Ty)) { |
| 2191 | Result = getPointerType(QualType(Ty, 0)); |
| 2192 | } else { |
| 2193 | Result = QualType(Ty, 0); |
| 2194 | } |
| 2195 | |
| 2196 | return CanQualType::CreateUnsafe(Result); |
| 2197 | } |
| 2198 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2199 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 2200 | /// the specified potentially non-canonical type. The non-canonical version |
| 2201 | /// of a type may have many "decorated" versions of types. Decorators can |
| 2202 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 2203 | /// to be free of any of these, allowing two canonical types to be compared |
| 2204 | /// for exact equality with a simple pointer comparison. |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2205 | CanQualType ASTContext::getCanonicalType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2206 | QualifierCollector Quals; |
| 2207 | const Type *Ptr = Quals.strip(T); |
| 2208 | QualType CanType = Ptr->getCanonicalTypeInternal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2209 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2210 | // The canonical internal type will be the canonical type *except* |
| 2211 | // that we push type qualifiers down through array types. |
| 2212 | |
| 2213 | // If there are no new qualifiers to push down, stop here. |
| 2214 | if (!Quals.hasQualifiers()) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2215 | return CanQualType::CreateUnsafe(CanType); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2216 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2217 | // If the type qualifiers are on an array type, get the canonical |
| 2218 | // type of the array with the qualifiers applied to the element |
| 2219 | // type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2220 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 2221 | if (!AT) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2222 | return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2223 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2224 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2225 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2226 | QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2227 | NewEltTy = getCanonicalType(NewEltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2228 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2229 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2230 | return CanQualType::CreateUnsafe( |
| 2231 | getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2232 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2233 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2234 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2235 | return CanQualType::CreateUnsafe( |
| 2236 | getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2237 | IAT->getIndexTypeCVRQualifiers())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2238 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2239 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2240 | return CanQualType::CreateUnsafe( |
| 2241 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2242 | DSAT->getSizeExpr() ? |
| 2243 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2244 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2245 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2246 | DSAT->getBracketsRange())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2247 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2248 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2249 | return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2250 | VAT->getSizeExpr() ? |
| 2251 | VAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2252 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2253 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2254 | VAT->getBracketsRange())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2257 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
| 2258 | // If this template name refers to a template, the canonical |
| 2259 | // template name merely stores the template itself. |
| 2260 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2261 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2262 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2263 | // If this template name refers to a set of overloaded function templates, |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 2264 | /// the canonical template name merely stores the set of function templates. |
Douglas Gregor | 6ebd15e | 2009-07-31 05:24:01 +0000 | [diff] [blame] | 2265 | if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) { |
| 2266 | OverloadedFunctionDecl *CanonOvl = 0; |
| 2267 | for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), |
| 2268 | FEnd = Ovl->function_end(); |
| 2269 | F != FEnd; ++F) { |
| 2270 | Decl *Canon = F->get()->getCanonicalDecl(); |
| 2271 | if (CanonOvl || Canon != F->get()) { |
| 2272 | if (!CanonOvl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2273 | CanonOvl = OverloadedFunctionDecl::Create(*this, |
| 2274 | Ovl->getDeclContext(), |
Douglas Gregor | 6ebd15e | 2009-07-31 05:24:01 +0000 | [diff] [blame] | 2275 | Ovl->getDeclName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2276 | |
Douglas Gregor | 6ebd15e | 2009-07-31 05:24:01 +0000 | [diff] [blame] | 2277 | CanonOvl->addOverload( |
| 2278 | AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon))); |
| 2279 | } |
| 2280 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2281 | |
Douglas Gregor | 6ebd15e | 2009-07-31 05:24:01 +0000 | [diff] [blame] | 2282 | return TemplateName(CanonOvl? CanonOvl : Ovl); |
| 2283 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2284 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2285 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2286 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2287 | return DTN->CanonicalTemplateName; |
| 2288 | } |
| 2289 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2290 | TemplateArgument |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2291 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { |
| 2292 | switch (Arg.getKind()) { |
| 2293 | case TemplateArgument::Null: |
| 2294 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2295 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2296 | case TemplateArgument::Expression: |
| 2297 | // FIXME: Build canonical expression? |
| 2298 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2299 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2300 | case TemplateArgument::Declaration: |
| 2301 | return TemplateArgument(SourceLocation(), |
| 2302 | Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2303 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2304 | case TemplateArgument::Integral: |
| 2305 | return TemplateArgument(SourceLocation(), |
| 2306 | *Arg.getAsIntegral(), |
| 2307 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2308 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2309 | case TemplateArgument::Type: |
| 2310 | return TemplateArgument(SourceLocation(), |
| 2311 | getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2312 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2313 | case TemplateArgument::Pack: { |
| 2314 | // FIXME: Allocate in ASTContext |
| 2315 | TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()]; |
| 2316 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2317 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2318 | AEnd = Arg.pack_end(); |
| 2319 | A != AEnd; (void)++A, ++Idx) |
| 2320 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2321 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2322 | TemplateArgument Result; |
| 2323 | Result.setArgumentPack(CanonArgs, Arg.pack_size(), false); |
| 2324 | return Result; |
| 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | // Silence GCC warning |
| 2329 | assert(false && "Unhandled template argument kind"); |
| 2330 | return TemplateArgument(); |
| 2331 | } |
| 2332 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2333 | NestedNameSpecifier * |
| 2334 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2336 | return 0; |
| 2337 | |
| 2338 | switch (NNS->getKind()) { |
| 2339 | case NestedNameSpecifier::Identifier: |
| 2340 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2341 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2342 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2343 | NNS->getAsIdentifier()); |
| 2344 | |
| 2345 | case NestedNameSpecifier::Namespace: |
| 2346 | // A namespace is canonical; build a nested-name-specifier with |
| 2347 | // this namespace and no prefix. |
| 2348 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2349 | |
| 2350 | case NestedNameSpecifier::TypeSpec: |
| 2351 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2352 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | return NestedNameSpecifier::Create(*this, 0, |
| 2354 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2355 | T.getTypePtr()); |
| 2356 | } |
| 2357 | |
| 2358 | case NestedNameSpecifier::Global: |
| 2359 | // The global specifier is canonical and unique. |
| 2360 | return NNS; |
| 2361 | } |
| 2362 | |
| 2363 | // Required to silence a GCC warning |
| 2364 | return 0; |
| 2365 | } |
| 2366 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2367 | |
| 2368 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 2369 | // Handle the non-qualified case efficiently. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2370 | if (!T.hasQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2371 | // Handle the common positive case fast. |
| 2372 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2373 | return AT; |
| 2374 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2375 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2376 | // Handle the common negative case fast. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2377 | QualType CType = T->getCanonicalTypeInternal(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2378 | if (!isa<ArrayType>(CType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2379 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2380 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2381 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2382 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2383 | // 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] | 2384 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2385 | // If we get here, we either have type qualifiers on the type, or we have |
| 2386 | // 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] | 2387 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2388 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2389 | QualifierCollector Qs; |
| 2390 | const Type *Ty = Qs.strip(T.getDesugaredType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2391 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2392 | // If we have a simple case, just return now. |
| 2393 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2394 | if (ATy == 0 || Qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2395 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2396 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2397 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 2398 | // qualifiers into the array element type and return a new array type. |
| 2399 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2400 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2401 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2403 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 2404 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2405 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2406 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2407 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 2408 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 2409 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2410 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2411 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2412 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2413 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 2414 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2415 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2416 | DSAT->getSizeExpr() ? |
| 2417 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2418 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2419 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2420 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2421 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2422 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2423 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2424 | VAT->getSizeExpr() ? |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2425 | VAT->getSizeExpr()->Retain() : 0, |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2426 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2427 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2428 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2432 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 2433 | /// specified array type to a pointer. This operation is non-trivial when |
| 2434 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 2435 | /// this returns a pointer to a properly qualified element of the array. |
| 2436 | /// |
| 2437 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 2438 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2439 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 2440 | // typedefs in the element type of the array. This also handles propagation |
| 2441 | // of type qualifiers from the array type into the element type if present |
| 2442 | // (C99 6.7.3p8). |
| 2443 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 2444 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2445 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2446 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2447 | |
| 2448 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2449 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2452 | QualType ASTContext::getBaseElementType(QualType QT) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2453 | QualifierCollector Qs; |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2454 | while (true) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2455 | const Type *UT = Qs.strip(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2456 | if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) { |
| 2457 | QT = AT->getElementType(); |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 2458 | } else { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2459 | return Qs.apply(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2460 | } |
| 2461 | } |
| 2462 | } |
| 2463 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2464 | QualType ASTContext::getBaseElementType(const ArrayType *AT) { |
| 2465 | QualType ElemTy = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2466 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2467 | if (const ArrayType *AT = getAsArrayType(ElemTy)) |
| 2468 | return getBaseElementType(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2469 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 2470 | return ElemTy; |
| 2471 | } |
| 2472 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2473 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2474 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2475 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 2476 | uint64_t ElementCount = 1; |
| 2477 | do { |
| 2478 | ElementCount *= CA->getSize().getZExtValue(); |
| 2479 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 2480 | } while (CA); |
| 2481 | return ElementCount; |
| 2482 | } |
| 2483 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2484 | /// getFloatingRank - Return a relative rank for floating point types. |
| 2485 | /// 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] | 2486 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2487 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2488 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2489 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2490 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 2491 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2492 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2493 | case BuiltinType::Float: return FloatRank; |
| 2494 | case BuiltinType::Double: return DoubleRank; |
| 2495 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 2496 | } |
| 2497 | } |
| 2498 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2499 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 2500 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2501 | /// 'typeDomain' is a real floating point or complex type. |
| 2502 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2503 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 2504 | QualType Domain) const { |
| 2505 | FloatingRank EltRank = getFloatingRank(Size); |
| 2506 | if (Domain->isComplexType()) { |
| 2507 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2508 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2509 | case FloatRank: return FloatComplexTy; |
| 2510 | case DoubleRank: return DoubleComplexTy; |
| 2511 | case LongDoubleRank: return LongDoubleComplexTy; |
| 2512 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2513 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2514 | |
| 2515 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 2516 | switch (EltRank) { |
| 2517 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 2518 | case FloatRank: return FloatTy; |
| 2519 | case DoubleRank: return DoubleTy; |
| 2520 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2521 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2524 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 2525 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 2526 | /// '_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] | 2527 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2528 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 2529 | FloatingRank LHSR = getFloatingRank(LHS); |
| 2530 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2531 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2532 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2533 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2534 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2535 | return 1; |
| 2536 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2539 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 2540 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 2541 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2542 | unsigned ASTContext::getIntegerRank(Type *T) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2543 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2544 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
| 2545 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
| 2546 | |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 2547 | if (T->isSpecificBuiltinType(BuiltinType::WChar)) |
| 2548 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 2549 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2550 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 2551 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 2552 | |
| 2553 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 2554 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 2555 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2556 | // There are two things which impact the integer rank: the width, and |
| 2557 | // the ordering of builtins. The builtin ordering is encoded in the |
| 2558 | // bottom three bits; the width is encoded in the bits above that. |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 2559 | if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2560 | return FWIT->getWidth() << 3; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2561 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2562 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2563 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 2564 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2565 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2566 | case BuiltinType::Char_S: |
| 2567 | case BuiltinType::Char_U: |
| 2568 | case BuiltinType::SChar: |
| 2569 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2570 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2571 | case BuiltinType::Short: |
| 2572 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2573 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2574 | case BuiltinType::Int: |
| 2575 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2576 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2577 | case BuiltinType::Long: |
| 2578 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2579 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2580 | case BuiltinType::LongLong: |
| 2581 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2582 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2583 | case BuiltinType::Int128: |
| 2584 | case BuiltinType::UInt128: |
| 2585 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2586 | } |
| 2587 | } |
| 2588 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2589 | /// \brief Whether this is a promotable bitfield reference according |
| 2590 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 2591 | /// |
| 2592 | /// \returns the type this bit-field will promote to, or NULL if no |
| 2593 | /// promotion occurs. |
| 2594 | QualType ASTContext::isPromotableBitField(Expr *E) { |
| 2595 | FieldDecl *Field = E->getBitField(); |
| 2596 | if (!Field) |
| 2597 | return QualType(); |
| 2598 | |
| 2599 | QualType FT = Field->getType(); |
| 2600 | |
| 2601 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 2602 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 2603 | uint64_t IntSize = getTypeSize(IntTy); |
| 2604 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 2605 | // to the size of int, it gets promoted no matter what its type is. |
| 2606 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 2607 | if (BitWidth < IntSize) |
| 2608 | return IntTy; |
| 2609 | |
| 2610 | if (BitWidth == IntSize) |
| 2611 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 2612 | |
| 2613 | // Types bigger than int are not subject to promotions, and therefore act |
| 2614 | // like the base type. |
| 2615 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 2616 | // is ridiculous. |
| 2617 | return QualType(); |
| 2618 | } |
| 2619 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2620 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 2621 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 2622 | /// integer type. |
| 2623 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) { |
| 2624 | assert(!Promotable.isNull()); |
| 2625 | assert(Promotable->isPromotableIntegerType()); |
| 2626 | if (Promotable->isSignedIntegerType()) |
| 2627 | return IntTy; |
| 2628 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 2629 | uint64_t IntSize = getTypeSize(IntTy); |
| 2630 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 2631 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 2632 | } |
| 2633 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2634 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2635 | /// 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] | 2636 | /// LHS < RHS, return -1. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2637 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2638 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 2639 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2640 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2641 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2642 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 2643 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2644 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2645 | unsigned LHSRank = getIntegerRank(LHSC); |
| 2646 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2647 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2648 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 2649 | if (LHSRank == RHSRank) return 0; |
| 2650 | return LHSRank > RHSRank ? 1 : -1; |
| 2651 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2652 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2653 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 2654 | if (LHSUnsigned) { |
| 2655 | // If the unsigned [LHS] type is larger, return it. |
| 2656 | if (LHSRank >= RHSRank) |
| 2657 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2658 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2659 | // If the signed type can represent all values of the unsigned type, it |
| 2660 | // 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] | 2661 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2662 | return -1; |
| 2663 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2664 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2665 | // If the unsigned [RHS] type is larger, return it. |
| 2666 | if (RHSRank >= LHSRank) |
| 2667 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2668 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2669 | // If the signed type can represent all values of the unsigned type, it |
| 2670 | // 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] | 2671 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2672 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2673 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2674 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2675 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2676 | QualType ASTContext::getCFConstantStringType() { |
| 2677 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2678 | CFConstantStringTypeDecl = |
| 2679 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2680 | &Idents.get("NSConstantString")); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2681 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2682 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2683 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2684 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2685 | // int flags; |
| 2686 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2687 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2688 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2689 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2690 | FieldTypes[3] = LongTy; |
| 2691 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2692 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2693 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2694 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2695 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2696 | FieldTypes[i], /*DInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2697 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2698 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2699 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | CFConstantStringTypeDecl->completeDefinition(*this); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2703 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2704 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2705 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 2706 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2707 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2708 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2709 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2710 | assert(Rec && "Invalid CFConstantStringType"); |
| 2711 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 2712 | } |
| 2713 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | QualType ASTContext::getObjCFastEnumerationStateType() { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2715 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2716 | ObjCFastEnumerationStateTypeDecl = |
| 2717 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2718 | &Idents.get("__objcFastEnumerationState")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2719 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2720 | QualType FieldTypes[] = { |
| 2721 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2722 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2723 | getPointerType(UnsignedLongTy), |
| 2724 | getConstantArrayType(UnsignedLongTy, |
| 2725 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 2726 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2727 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2728 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2729 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2730 | ObjCFastEnumerationStateTypeDecl, |
| 2731 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2732 | FieldTypes[i], /*DInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2733 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2734 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2735 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2736 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2737 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2738 | ObjCFastEnumerationStateTypeDecl->completeDefinition(*this); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2739 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2740 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2741 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 2742 | } |
| 2743 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2744 | QualType ASTContext::getBlockDescriptorType() { |
| 2745 | if (BlockDescriptorType) |
| 2746 | return getTagDeclType(BlockDescriptorType); |
| 2747 | |
| 2748 | RecordDecl *T; |
| 2749 | // FIXME: Needs the FlagAppleBlock bit. |
| 2750 | T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2751 | &Idents.get("__block_descriptor")); |
| 2752 | |
| 2753 | QualType FieldTypes[] = { |
| 2754 | UnsignedLongTy, |
| 2755 | UnsignedLongTy, |
| 2756 | }; |
| 2757 | |
| 2758 | const char *FieldNames[] = { |
| 2759 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2760 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2761 | }; |
| 2762 | |
| 2763 | for (size_t i = 0; i < 2; ++i) { |
| 2764 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2765 | T, |
| 2766 | SourceLocation(), |
| 2767 | &Idents.get(FieldNames[i]), |
| 2768 | FieldTypes[i], /*DInfo=*/0, |
| 2769 | /*BitWidth=*/0, |
| 2770 | /*Mutable=*/false); |
| 2771 | T->addDecl(Field); |
| 2772 | } |
| 2773 | |
| 2774 | T->completeDefinition(*this); |
| 2775 | |
| 2776 | BlockDescriptorType = T; |
| 2777 | |
| 2778 | return getTagDeclType(BlockDescriptorType); |
| 2779 | } |
| 2780 | |
| 2781 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 2782 | const RecordType *Rec = T->getAs<RecordType>(); |
| 2783 | assert(Rec && "Invalid BlockDescriptorType"); |
| 2784 | BlockDescriptorType = Rec->getDecl(); |
| 2785 | } |
| 2786 | |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2787 | QualType ASTContext::getBlockDescriptorExtendedType() { |
| 2788 | if (BlockDescriptorExtendedType) |
| 2789 | return getTagDeclType(BlockDescriptorExtendedType); |
| 2790 | |
| 2791 | RecordDecl *T; |
| 2792 | // FIXME: Needs the FlagAppleBlock bit. |
| 2793 | T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2794 | &Idents.get("__block_descriptor_withcopydispose")); |
| 2795 | |
| 2796 | QualType FieldTypes[] = { |
| 2797 | UnsignedLongTy, |
| 2798 | UnsignedLongTy, |
| 2799 | getPointerType(VoidPtrTy), |
| 2800 | getPointerType(VoidPtrTy) |
| 2801 | }; |
| 2802 | |
| 2803 | const char *FieldNames[] = { |
| 2804 | "reserved", |
| 2805 | "Size", |
| 2806 | "CopyFuncPtr", |
| 2807 | "DestroyFuncPtr" |
| 2808 | }; |
| 2809 | |
| 2810 | for (size_t i = 0; i < 4; ++i) { |
| 2811 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2812 | T, |
| 2813 | SourceLocation(), |
| 2814 | &Idents.get(FieldNames[i]), |
| 2815 | FieldTypes[i], /*DInfo=*/0, |
| 2816 | /*BitWidth=*/0, |
| 2817 | /*Mutable=*/false); |
| 2818 | T->addDecl(Field); |
| 2819 | } |
| 2820 | |
| 2821 | T->completeDefinition(*this); |
| 2822 | |
| 2823 | BlockDescriptorExtendedType = T; |
| 2824 | |
| 2825 | return getTagDeclType(BlockDescriptorExtendedType); |
| 2826 | } |
| 2827 | |
| 2828 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 2829 | const RecordType *Rec = T->getAs<RecordType>(); |
| 2830 | assert(Rec && "Invalid BlockDescriptorType"); |
| 2831 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 2832 | } |
| 2833 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2834 | bool ASTContext::BlockRequiresCopying(QualType Ty) { |
| 2835 | if (Ty->isBlockPointerType()) |
| 2836 | return true; |
| 2837 | if (isObjCNSObjectType(Ty)) |
| 2838 | return true; |
| 2839 | if (Ty->isObjCObjectPointerType()) |
| 2840 | return true; |
| 2841 | return false; |
| 2842 | } |
| 2843 | |
| 2844 | QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) { |
| 2845 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2846 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2847 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2848 | // unsigned int __flags; |
| 2849 | // unsigned int __size; |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 2850 | // void *__copy_helper; // as needed |
| 2851 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2852 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2853 | // } * |
| 2854 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2855 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 2856 | |
| 2857 | // FIXME: Move up |
Fariborz Jahanian | 4d0d85c | 2009-10-24 00:16:42 +0000 | [diff] [blame] | 2858 | static unsigned int UniqueBlockByRefTypeID = 0; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 2859 | llvm::SmallString<36> Name; |
| 2860 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 2861 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2862 | RecordDecl *T; |
| 2863 | T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 2864 | &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2865 | T->startDefinition(); |
| 2866 | QualType Int32Ty = IntTy; |
| 2867 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 2868 | QualType FieldTypes[] = { |
| 2869 | getPointerType(VoidPtrTy), |
| 2870 | getPointerType(getTagDeclType(T)), |
| 2871 | Int32Ty, |
| 2872 | Int32Ty, |
| 2873 | getPointerType(VoidPtrTy), |
| 2874 | getPointerType(VoidPtrTy), |
| 2875 | Ty |
| 2876 | }; |
| 2877 | |
| 2878 | const char *FieldNames[] = { |
| 2879 | "__isa", |
| 2880 | "__forwarding", |
| 2881 | "__flags", |
| 2882 | "__size", |
| 2883 | "__copy_helper", |
| 2884 | "__destroy_helper", |
| 2885 | DeclName, |
| 2886 | }; |
| 2887 | |
| 2888 | for (size_t i = 0; i < 7; ++i) { |
| 2889 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 2890 | continue; |
| 2891 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 2892 | &Idents.get(FieldNames[i]), |
| 2893 | FieldTypes[i], /*DInfo=*/0, |
| 2894 | /*BitWidth=*/0, /*Mutable=*/false); |
| 2895 | T->addDecl(Field); |
| 2896 | } |
| 2897 | |
| 2898 | T->completeDefinition(*this); |
| 2899 | |
| 2900 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2901 | } |
| 2902 | |
| 2903 | |
| 2904 | QualType ASTContext::getBlockParmType( |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2905 | bool BlockHasCopyDispose, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2906 | llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2907 | // FIXME: Move up |
Fariborz Jahanian | 4d0d85c | 2009-10-24 00:16:42 +0000 | [diff] [blame] | 2908 | static unsigned int UniqueBlockParmTypeID = 0; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 2909 | llvm::SmallString<36> Name; |
| 2910 | llvm::raw_svector_ostream(Name) << "__block_literal_" |
| 2911 | << ++UniqueBlockParmTypeID; |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2912 | RecordDecl *T; |
| 2913 | T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 2914 | &Idents.get(Name.str())); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2915 | QualType FieldTypes[] = { |
| 2916 | getPointerType(VoidPtrTy), |
| 2917 | IntTy, |
| 2918 | IntTy, |
| 2919 | getPointerType(VoidPtrTy), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2920 | (BlockHasCopyDispose ? |
| 2921 | getPointerType(getBlockDescriptorExtendedType()) : |
| 2922 | getPointerType(getBlockDescriptorType())) |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2923 | }; |
| 2924 | |
| 2925 | const char *FieldNames[] = { |
| 2926 | "__isa", |
| 2927 | "__flags", |
| 2928 | "__reserved", |
| 2929 | "__FuncPtr", |
| 2930 | "__descriptor" |
| 2931 | }; |
| 2932 | |
| 2933 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2934 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2935 | &Idents.get(FieldNames[i]), |
| 2936 | FieldTypes[i], /*DInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2937 | /*BitWidth=*/0, /*Mutable=*/false); |
| 2938 | T->addDecl(Field); |
| 2939 | } |
| 2940 | |
| 2941 | for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) { |
| 2942 | const Expr *E = BlockDeclRefDecls[i]; |
| 2943 | const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 2944 | clang::IdentifierInfo *Name = 0; |
| 2945 | if (BDRE) { |
| 2946 | const ValueDecl *D = BDRE->getDecl(); |
| 2947 | Name = &Idents.get(D->getName()); |
| 2948 | } |
| 2949 | QualType FieldType = E->getType(); |
| 2950 | |
| 2951 | if (BDRE && BDRE->isByRef()) |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2952 | FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(), |
| 2953 | FieldType); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2954 | |
| 2955 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 2956 | Name, FieldType, /*DInfo=*/0, |
| 2957 | /*BitWidth=*/0, /*Mutable=*/false); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2958 | T->addDecl(Field); |
| 2959 | } |
| 2960 | |
| 2961 | T->completeDefinition(*this); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2962 | |
| 2963 | return getPointerType(getTagDeclType(T)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2966 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2967 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2968 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 2969 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 2970 | } |
| 2971 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 2972 | // This returns true if a type has been typedefed to BOOL: |
| 2973 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 2974 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 2975 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 2976 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 2977 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2978 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2979 | return false; |
| 2980 | } |
| 2981 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2982 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2983 | /// purpose. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2984 | int ASTContext::getObjCEncodingTypeSize(QualType type) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2985 | uint64_t sz = getTypeSize(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2986 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2987 | // Make all integer and enum types at least as large as an int |
| 2988 | if (sz > 0 && type->isIntegralType()) |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2989 | sz = std::max(sz, getTypeSize(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2990 | // Treat arrays as pointers, since that's how they're passed in. |
| 2991 | else if (type->isArrayType()) |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2992 | sz = getTypeSize(VoidPtrTy); |
| 2993 | return sz / getTypeSize(CharTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2996 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2997 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2998 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 2999 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3000 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3001 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3002 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3003 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3004 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3005 | // Compute size of all parameters. |
| 3006 | // Start with computing size of a pointer in number of bytes. |
| 3007 | // FIXME: There might(should) be a better way of doing this computation! |
| 3008 | SourceLocation Loc; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3009 | int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3010 | // The first two arguments (self and _cmd) are pointers; account for |
| 3011 | // their size. |
| 3012 | int ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3013 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3014 | E = Decl->param_end(); PI != E; ++PI) { |
| 3015 | QualType PType = (*PI)->getType(); |
| 3016 | int sz = getObjCEncodingTypeSize(PType); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3017 | assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3018 | ParmOffset += sz; |
| 3019 | } |
| 3020 | S += llvm::utostr(ParmOffset); |
| 3021 | S += "@0:"; |
| 3022 | S += llvm::utostr(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3024 | // Argument types. |
| 3025 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3026 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3027 | E = Decl->param_end(); PI != E; ++PI) { |
| 3028 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3029 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3030 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3031 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3032 | // Use array's original type only if it has known number of |
| 3033 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3034 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3035 | PType = PVDecl->getType(); |
| 3036 | } else if (PType->isFunctionType()) |
| 3037 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3038 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3039 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3040 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3041 | getObjCEncodingForType(PType, S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3042 | S += llvm::utostr(ParmOffset); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3043 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3044 | } |
| 3045 | } |
| 3046 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3047 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3048 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3049 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3050 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3051 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3052 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3053 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3054 | /// encoded as single characters, followed by an identifier. Property types |
| 3055 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3056 | /// these attributes are defined by the following enumeration: |
| 3057 | /// @code |
| 3058 | /// enum PropertyAttributes { |
| 3059 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3060 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3061 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3062 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3063 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3064 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3065 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3066 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3067 | /// kPropertyWeak = 'W' // 'weak' property |
| 3068 | /// kPropertyStrong = 'P' // property GC'able |
| 3069 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3070 | /// }; |
| 3071 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3072 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3073 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3074 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3075 | // Collect information from the property implementation decl(s). |
| 3076 | bool Dynamic = false; |
| 3077 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3078 | |
| 3079 | // FIXME: Duplicated code due to poor abstraction. |
| 3080 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3081 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3082 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3083 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3084 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3085 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3086 | ObjCPropertyImplDecl *PID = *i; |
| 3087 | if (PID->getPropertyDecl() == PD) { |
| 3088 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3089 | Dynamic = true; |
| 3090 | } else { |
| 3091 | SynthesizePID = PID; |
| 3092 | } |
| 3093 | } |
| 3094 | } |
| 3095 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3096 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3097 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3098 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3099 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3100 | ObjCPropertyImplDecl *PID = *i; |
| 3101 | if (PID->getPropertyDecl() == PD) { |
| 3102 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3103 | Dynamic = true; |
| 3104 | } else { |
| 3105 | SynthesizePID = PID; |
| 3106 | } |
| 3107 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3108 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3109 | } |
| 3110 | } |
| 3111 | |
| 3112 | // FIXME: This is not very efficient. |
| 3113 | S = "T"; |
| 3114 | |
| 3115 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3116 | // GCC has some special rules regarding encoding of properties which |
| 3117 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3118 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3119 | true /* outermost type */, |
| 3120 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3121 | |
| 3122 | if (PD->isReadOnly()) { |
| 3123 | S += ",R"; |
| 3124 | } else { |
| 3125 | switch (PD->getSetterKind()) { |
| 3126 | case ObjCPropertyDecl::Assign: break; |
| 3127 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3128 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3129 | } |
| 3130 | } |
| 3131 | |
| 3132 | // It really isn't clear at all what this means, since properties |
| 3133 | // are "dynamic by default". |
| 3134 | if (Dynamic) |
| 3135 | S += ",D"; |
| 3136 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3137 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3138 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3139 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3140 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3141 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3142 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3146 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3147 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | if (SynthesizePID) { |
| 3151 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3152 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3153 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | // FIXME: OBJCGC: weak & strong |
| 3157 | } |
| 3158 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3159 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3160 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3161 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3162 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3163 | /// |
| 3164 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3165 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3166 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3167 | if (BT->getKind() == BuiltinType::ULong && |
| 3168 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3169 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3170 | else |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3171 | if (BT->getKind() == BuiltinType::Long && |
| 3172 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3173 | PointeeTy = IntTy; |
| 3174 | } |
| 3175 | } |
| 3176 | } |
| 3177 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3178 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3179 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3180 | // We follow the behavior of gcc, expanding structures which are |
| 3181 | // directly pointed to, and expanding embedded structures. Note that |
| 3182 | // these rules are sufficient to prevent recursive encoding of the |
| 3183 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3184 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3185 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3188 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3189 | const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3190 | const Expr *E = FD->getBitWidth(); |
| 3191 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 3192 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3193 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3194 | S += 'b'; |
| 3195 | S += llvm::utostr(N); |
| 3196 | } |
| 3197 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3198 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3199 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 3200 | bool ExpandPointedToStructures, |
| 3201 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3202 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3203 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3204 | bool EncodingProperty) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3205 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3206 | if (FD && FD->isBitField()) |
| 3207 | return EncodeBitField(this, S, FD); |
| 3208 | char encoding; |
| 3209 | switch (BT->getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3210 | default: assert(0 && "Unhandled builtin type kind"); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3211 | case BuiltinType::Void: encoding = 'v'; break; |
| 3212 | case BuiltinType::Bool: encoding = 'B'; break; |
| 3213 | case BuiltinType::Char_U: |
| 3214 | case BuiltinType::UChar: encoding = 'C'; break; |
| 3215 | case BuiltinType::UShort: encoding = 'S'; break; |
| 3216 | case BuiltinType::UInt: encoding = 'I'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3217 | case BuiltinType::ULong: |
| 3218 | encoding = |
| 3219 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
Fariborz Jahanian | 72696e1 | 2009-02-11 22:31:45 +0000 | [diff] [blame] | 3220 | break; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3221 | case BuiltinType::UInt128: encoding = 'T'; break; |
| 3222 | case BuiltinType::ULongLong: encoding = 'Q'; break; |
| 3223 | case BuiltinType::Char_S: |
| 3224 | case BuiltinType::SChar: encoding = 'c'; break; |
| 3225 | case BuiltinType::Short: encoding = 's'; break; |
| 3226 | case BuiltinType::Int: encoding = 'i'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3227 | case BuiltinType::Long: |
| 3228 | encoding = |
| 3229 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3230 | break; |
| 3231 | case BuiltinType::LongLong: encoding = 'q'; break; |
| 3232 | case BuiltinType::Int128: encoding = 't'; break; |
| 3233 | case BuiltinType::Float: encoding = 'f'; break; |
| 3234 | case BuiltinType::Double: encoding = 'd'; break; |
| 3235 | case BuiltinType::LongDouble: encoding = 'd'; break; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3236 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3237 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3238 | S += encoding; |
| 3239 | return; |
| 3240 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3241 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3242 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3243 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3244 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3245 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3246 | return; |
| 3247 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3248 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3249 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3250 | QualType PointeeTy = PT->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3251 | bool isReadOnly = false; |
| 3252 | // For historical/compatibility reasons, the read-only qualifier of the |
| 3253 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 3254 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3255 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3256 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3257 | if (OutermostType && T.isConstQualified()) { |
| 3258 | isReadOnly = true; |
| 3259 | S += 'r'; |
| 3260 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3261 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3262 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3263 | while (P->getAs<PointerType>()) |
| 3264 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3265 | if (P.isConstQualified()) { |
| 3266 | isReadOnly = true; |
| 3267 | S += 'r'; |
| 3268 | } |
| 3269 | } |
| 3270 | if (isReadOnly) { |
| 3271 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 3272 | // combinations need to be rearranged. |
| 3273 | // Rewrite "in const" from "nr" to "rn" |
| 3274 | const char * s = S.c_str(); |
| 3275 | int len = S.length(); |
| 3276 | if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') { |
| 3277 | std::string replace = "rn"; |
| 3278 | S.replace(S.end()-2, S.end(), replace); |
| 3279 | } |
| 3280 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3281 | if (isObjCSelType(PointeeTy)) { |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 3282 | S += ':'; |
| 3283 | return; |
Fariborz Jahanian | c2939bc | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 3284 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3286 | if (PointeeTy->isCharType()) { |
| 3287 | // char pointer types should be encoded as '*' unless it is a |
| 3288 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3289 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3290 | S += '*'; |
| 3291 | return; |
| 3292 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3293 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 3294 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 3295 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 3296 | S += '#'; |
| 3297 | return; |
| 3298 | } |
| 3299 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 3300 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 3301 | S += '@'; |
| 3302 | return; |
| 3303 | } |
| 3304 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3305 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3306 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3307 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 3308 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3309 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3310 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3311 | return; |
| 3312 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3313 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3314 | if (const ArrayType *AT = |
| 3315 | // Ignore type qualifiers etc. |
| 3316 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3317 | if (isa<IncompleteArrayType>(AT)) { |
| 3318 | // Incomplete arrays are encoded as a pointer to the array element. |
| 3319 | S += '^'; |
| 3320 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3322 | false, ExpandStructures, FD); |
| 3323 | } else { |
| 3324 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3325 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3326 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 3327 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 3328 | else { |
| 3329 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 3330 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 3331 | S += '0'; |
| 3332 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3333 | |
| 3334 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3335 | false, ExpandStructures, FD); |
| 3336 | S += ']'; |
| 3337 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3338 | return; |
| 3339 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3340 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3341 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 3342 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3343 | return; |
| 3344 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3345 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3346 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3347 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3348 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3349 | // Anonymous structures print as '?' |
| 3350 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 3351 | S += II->getName(); |
| 3352 | } else { |
| 3353 | S += '?'; |
| 3354 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3355 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3356 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3357 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 3358 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3359 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3360 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3361 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3362 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3363 | S += '"'; |
| 3364 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3365 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3366 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3367 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3368 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3369 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3370 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3371 | QualType qt = Field->getType(); |
| 3372 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3373 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3374 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3375 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3376 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 3377 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3378 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3379 | return; |
| 3380 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3382 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3383 | if (FD && FD->isBitField()) |
| 3384 | EncodeBitField(this, S, FD); |
| 3385 | else |
| 3386 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3387 | return; |
| 3388 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3389 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3390 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 3391 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3392 | return; |
| 3393 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3394 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3395 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3396 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3397 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3398 | S += '{'; |
| 3399 | const IdentifierInfo *II = OI->getIdentifier(); |
| 3400 | S += II->getName(); |
| 3401 | S += '='; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3402 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3403 | CollectObjCIvars(OI, RecFields); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3404 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3405 | if (RecFields[i]->isBitField()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3406 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3407 | RecFields[i]); |
| 3408 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3409 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3410 | FD); |
| 3411 | } |
| 3412 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3413 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3414 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3415 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3416 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3417 | if (OPT->isObjCIdType()) { |
| 3418 | S += '@'; |
| 3419 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3420 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3421 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame^] | 3422 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 3423 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 3424 | // Since this is a binary compatibility issue, need to consult with runtime |
| 3425 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3426 | S += '#'; |
| 3427 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3428 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3429 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3430 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3432 | ExpandPointedToStructures, |
| 3433 | ExpandStructures, FD); |
| 3434 | if (FD || EncodingProperty) { |
| 3435 | // Note that we do extended encoding of protocol qualifer list |
| 3436 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3437 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3438 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3439 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3440 | S += '<'; |
| 3441 | S += (*I)->getNameAsString(); |
| 3442 | S += '>'; |
| 3443 | } |
| 3444 | S += '"'; |
| 3445 | } |
| 3446 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3447 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3448 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3449 | QualType PointeeTy = OPT->getPointeeType(); |
| 3450 | if (!EncodingProperty && |
| 3451 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 3452 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3453 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3454 | // {...}; |
| 3455 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3456 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 3457 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3458 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3459 | return; |
| 3460 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3461 | |
| 3462 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame^] | 3463 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3464 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame^] | 3465 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3466 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3467 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3468 | S += '<'; |
| 3469 | S += (*I)->getNameAsString(); |
| 3470 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3471 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3472 | S += '"'; |
| 3473 | } |
| 3474 | return; |
| 3475 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3476 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3477 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3480 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3481 | std::string& S) const { |
| 3482 | if (QT & Decl::OBJC_TQ_In) |
| 3483 | S += 'n'; |
| 3484 | if (QT & Decl::OBJC_TQ_Inout) |
| 3485 | S += 'N'; |
| 3486 | if (QT & Decl::OBJC_TQ_Out) |
| 3487 | S += 'o'; |
| 3488 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 3489 | S += 'O'; |
| 3490 | if (QT & Decl::OBJC_TQ_Byref) |
| 3491 | S += 'R'; |
| 3492 | if (QT & Decl::OBJC_TQ_Oneway) |
| 3493 | S += 'V'; |
| 3494 | } |
| 3495 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3496 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3497 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3498 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3499 | BuiltinVaListType = T; |
| 3500 | } |
| 3501 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3502 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3503 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 3504 | } |
| 3505 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3506 | void ASTContext::setObjCSelType(QualType T) { |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3507 | ObjCSelType = T; |
| 3508 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3509 | const TypedefType *TT = T->getAs<TypedefType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3510 | if (!TT) |
| 3511 | return; |
| 3512 | TypedefDecl *TD = TT->getDecl(); |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3513 | |
| 3514 | // typedef struct objc_selector *SEL; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3515 | const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>(); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 3516 | if (!ptr) |
| 3517 | return; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3518 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 3519 | if (!rec) |
| 3520 | return; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3521 | SelStructType = rec; |
| 3522 | } |
| 3523 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3524 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3525 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3528 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3529 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3532 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3533 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3534 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3535 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3536 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3537 | } |
| 3538 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3539 | /// \brief Retrieve the template name that represents a qualified |
| 3540 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3541 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3542 | bool TemplateKeyword, |
| 3543 | TemplateDecl *Template) { |
| 3544 | llvm::FoldingSetNodeID ID; |
| 3545 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 3546 | |
| 3547 | void *InsertPos = 0; |
| 3548 | QualifiedTemplateName *QTN = |
| 3549 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3550 | if (!QTN) { |
| 3551 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 3552 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 3553 | } |
| 3554 | |
| 3555 | return TemplateName(QTN); |
| 3556 | } |
| 3557 | |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 3558 | /// \brief Retrieve the template name that represents a qualified |
| 3559 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3560 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 3561 | bool TemplateKeyword, |
| 3562 | OverloadedFunctionDecl *Template) { |
| 3563 | llvm::FoldingSetNodeID ID; |
| 3564 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3565 | |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 3566 | void *InsertPos = 0; |
| 3567 | QualifiedTemplateName *QTN = |
| 3568 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3569 | if (!QTN) { |
| 3570 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 3571 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 3572 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3573 | |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 3574 | return TemplateName(QTN); |
| 3575 | } |
| 3576 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3577 | /// \brief Retrieve the template name that represents a dependent |
| 3578 | /// template name such as \c MetaFun::template apply. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3579 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3580 | const IdentifierInfo *Name) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3581 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 3582 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3583 | |
| 3584 | llvm::FoldingSetNodeID ID; |
| 3585 | DependentTemplateName::Profile(ID, NNS, Name); |
| 3586 | |
| 3587 | void *InsertPos = 0; |
| 3588 | DependentTemplateName *QTN = |
| 3589 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3590 | |
| 3591 | if (QTN) |
| 3592 | return TemplateName(QTN); |
| 3593 | |
| 3594 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 3595 | if (CanonNNS == NNS) { |
| 3596 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 3597 | } else { |
| 3598 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 3599 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
| 3600 | } |
| 3601 | |
| 3602 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3603 | return TemplateName(QTN); |
| 3604 | } |
| 3605 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3606 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 3607 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 3608 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3609 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3610 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3611 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3612 | case TargetInfo::SignedShort: return ShortTy; |
| 3613 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 3614 | case TargetInfo::SignedInt: return IntTy; |
| 3615 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 3616 | case TargetInfo::SignedLong: return LongTy; |
| 3617 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 3618 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 3619 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 3620 | } |
| 3621 | |
| 3622 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3623 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3624 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 3625 | |
| 3626 | //===----------------------------------------------------------------------===// |
| 3627 | // Type Predicates. |
| 3628 | //===----------------------------------------------------------------------===// |
| 3629 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3630 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 3631 | /// NSObject attribute on a c-style pointer type. |
| 3632 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3633 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3634 | /// |
| 3635 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 3636 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 3637 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3638 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3639 | return true; |
| 3640 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3641 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3642 | } |
| 3643 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3644 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 3645 | /// garbage collection attribute. |
| 3646 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3647 | Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
| 3648 | Qualifiers::GC GCAttrs = Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3649 | if (getLangOptions().ObjC1 && |
| 3650 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3651 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3652 | // Default behavious under objective-c's gc is for objective-c pointers |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3653 | // (or pointers to them) be treated as though they were declared |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3654 | // as __strong. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3655 | if (GCAttrs == Qualifiers::GCNone) { |
Fariborz Jahanian | 75212ee | 2009-09-10 23:38:45 +0000 | [diff] [blame] | 3656 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3657 | GCAttrs = Qualifiers::Strong; |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3658 | else if (Ty->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3659 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3660 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 3661 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 3662 | // set on them. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3663 | else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3664 | return Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3665 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3666 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3667 | } |
| 3668 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3669 | //===----------------------------------------------------------------------===// |
| 3670 | // Type Compatibility Testing |
| 3671 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 3672 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3673 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3674 | /// compatible. |
| 3675 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 3676 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3677 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3678 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3679 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3682 | //===----------------------------------------------------------------------===// |
| 3683 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 3684 | //===----------------------------------------------------------------------===// |
| 3685 | |
| 3686 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 3687 | /// inheritance hierarchy of 'rProto'. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3688 | bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 3689 | ObjCProtocolDecl *rProto) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3690 | if (lProto == rProto) |
| 3691 | return true; |
| 3692 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 3693 | E = rProto->protocol_end(); PI != E; ++PI) |
| 3694 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 3695 | return true; |
| 3696 | return false; |
| 3697 | } |
| 3698 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3699 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 3700 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 3701 | /// otherwise. |
| 3702 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 3703 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 3704 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 3705 | return false; |
| 3706 | } |
| 3707 | |
| 3708 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 3709 | /// ObjCQualifiedIDType. |
| 3710 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 3711 | bool compare) { |
| 3712 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3713 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3714 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 3715 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3716 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3717 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 3718 | return true; |
| 3719 | |
| 3720 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3721 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3722 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3723 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3724 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3725 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3726 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3727 | // make sure we check the class hierarchy. |
| 3728 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 3729 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3730 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3731 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3732 | // see if static class implements all of id's protocols, directly or |
| 3733 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3734 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3735 | return false; |
| 3736 | } |
| 3737 | } |
| 3738 | // If there are no qualifiers and no interface, we have an 'id'. |
| 3739 | return true; |
| 3740 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3741 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3742 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3743 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3744 | ObjCProtocolDecl *lhsProto = *I; |
| 3745 | bool match = false; |
| 3746 | |
| 3747 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3748 | // see if static class implements all of id's protocols, directly or |
| 3749 | // through its super class and categories. |
| 3750 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 3751 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 3752 | ObjCProtocolDecl *rhsProto = *J; |
| 3753 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 3754 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 3755 | match = true; |
| 3756 | break; |
| 3757 | } |
| 3758 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3759 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3760 | // make sure we check the class hierarchy. |
| 3761 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 3762 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3763 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3764 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3765 | // see if static class implements all of id's protocols, directly or |
| 3766 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3767 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3768 | match = true; |
| 3769 | break; |
| 3770 | } |
| 3771 | } |
| 3772 | } |
| 3773 | if (!match) |
| 3774 | return false; |
| 3775 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3776 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3777 | return true; |
| 3778 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3779 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3780 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 3781 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 3782 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3783 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3784 | lhs->getAsObjCInterfacePointerType()) { |
| 3785 | if (lhsOPT->qual_empty()) { |
| 3786 | bool match = false; |
| 3787 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 3788 | for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), |
| 3789 | E = rhsQID->qual_end(); I != E; ++I) { |
| 3790 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3791 | // see if static class implements all of id's protocols, directly or |
| 3792 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3793 | if (lhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3794 | match = true; |
| 3795 | break; |
| 3796 | } |
| 3797 | } |
| 3798 | if (!match) |
| 3799 | return false; |
| 3800 | } |
| 3801 | return true; |
| 3802 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3803 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3804 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 3805 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 3806 | ObjCProtocolDecl *lhsProto = *I; |
| 3807 | bool match = false; |
| 3808 | |
| 3809 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3810 | // see if static class implements all of id's protocols, directly or |
| 3811 | // through its super class and categories. |
| 3812 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 3813 | E = rhsQID->qual_end(); J != E; ++J) { |
| 3814 | ObjCProtocolDecl *rhsProto = *J; |
| 3815 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 3816 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 3817 | match = true; |
| 3818 | break; |
| 3819 | } |
| 3820 | } |
| 3821 | if (!match) |
| 3822 | return false; |
| 3823 | } |
| 3824 | return true; |
| 3825 | } |
| 3826 | return false; |
| 3827 | } |
| 3828 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3829 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3830 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 3831 | /// protocol qualifiers on the LHS or RHS. |
| 3832 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3833 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 3834 | const ObjCObjectPointerType *RHSOPT) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3835 | // If either type represents the built-in 'id' or 'Class' types, return true. |
| 3836 | if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3837 | return true; |
| 3838 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3839 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3840 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 3841 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3842 | false); |
| 3843 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3844 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 3845 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3846 | if (LHS && RHS) // We have 2 user-defined types. |
| 3847 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3848 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3849 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 3852 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 3853 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 3854 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 3855 | /// warning is issued. So, its invokation is extremely rare. |
| 3856 | QualType ASTContext::areCommonBaseCompatible( |
| 3857 | const ObjCObjectPointerType *LHSOPT, |
| 3858 | const ObjCObjectPointerType *RHSOPT) { |
| 3859 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 3860 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 3861 | if (!LHS || !RHS) |
| 3862 | return QualType(); |
| 3863 | |
| 3864 | while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) { |
| 3865 | QualType LHSTy = getObjCInterfaceType(LHSIDecl); |
| 3866 | LHS = LHSTy->getAs<ObjCInterfaceType>(); |
| 3867 | if (canAssignObjCInterfaces(LHS, RHS)) |
| 3868 | return getObjCObjectPointerType(LHSTy); |
| 3869 | } |
| 3870 | |
| 3871 | return QualType(); |
| 3872 | } |
| 3873 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3874 | bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, |
| 3875 | const ObjCInterfaceType *RHS) { |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3876 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 3877 | // the LHS. |
| 3878 | if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 3879 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3880 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3881 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 3882 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 3883 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3884 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3885 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3886 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 3887 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 3888 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3889 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3890 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 3891 | for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(), |
| 3892 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 3893 | LHSPI != LHSPE; LHSPI++) { |
| 3894 | bool RHSImplementsProtocol = false; |
| 3895 | |
| 3896 | // If the RHS doesn't implement the protocol on the left, the types |
| 3897 | // are incompatible. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 3898 | for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3899 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 3900 | RHSPI != RHSPE; RHSPI++) { |
| 3901 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 3902 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 3903 | break; |
| 3904 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 3905 | } |
| 3906 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 3907 | if (!RHSImplementsProtocol) |
| 3908 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3909 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 3910 | // The RHS implements all protocols listed on the LHS. |
| 3911 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3912 | } |
| 3913 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3914 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 3915 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3916 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 3917 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3918 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3919 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3920 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3921 | |
| 3922 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 3923 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3926 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 3927 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3928 | /// 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] | 3929 | /// same. See 6.7.[2,3,5] for additional rules. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3930 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { |
| 3931 | return !mergeTypes(LHS, RHS).isNull(); |
| 3932 | } |
| 3933 | |
| 3934 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3935 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 3936 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3937 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 3938 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3939 | bool allLTypes = true; |
| 3940 | bool allRTypes = true; |
| 3941 | |
| 3942 | // Check return type |
| 3943 | QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); |
| 3944 | if (retType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3945 | if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) |
| 3946 | allLTypes = false; |
| 3947 | if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) |
| 3948 | allRTypes = false; |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 3949 | // FIXME: double check this |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 3950 | bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr(); |
| 3951 | if (NoReturn != lbase->getNoReturnAttr()) |
| 3952 | allLTypes = false; |
| 3953 | if (NoReturn != rbase->getNoReturnAttr()) |
| 3954 | allRTypes = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3955 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3956 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3957 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 3958 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3959 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 3960 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 3961 | |
| 3962 | // Compatible functions must have the same number of arguments |
| 3963 | if (lproto_nargs != rproto_nargs) |
| 3964 | return QualType(); |
| 3965 | |
| 3966 | // Variadic and non-variadic functions aren't compatible |
| 3967 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 3968 | return QualType(); |
| 3969 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 3970 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 3971 | return QualType(); |
| 3972 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3973 | // Check argument compatibility |
| 3974 | llvm::SmallVector<QualType, 10> types; |
| 3975 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 3976 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 3977 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
| 3978 | QualType argtype = mergeTypes(largtype, rargtype); |
| 3979 | if (argtype.isNull()) return QualType(); |
| 3980 | types.push_back(argtype); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3981 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 3982 | allLTypes = false; |
| 3983 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 3984 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3985 | } |
| 3986 | if (allLTypes) return lhs; |
| 3987 | if (allRTypes) return rhs; |
| 3988 | return getFunctionType(retType, types.begin(), types.size(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 3989 | lproto->isVariadic(), lproto->getTypeQuals(), |
| 3990 | NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
| 3993 | if (lproto) allRTypes = false; |
| 3994 | if (rproto) allLTypes = false; |
| 3995 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3996 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3997 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3998 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3999 | if (proto->isVariadic()) return QualType(); |
| 4000 | // Check that the types are compatible with the types that |
| 4001 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 4002 | // The only types actually affected are promotable integer |
| 4003 | // types and floats, which would be passed as a different |
| 4004 | // type depending on whether the prototype is visible. |
| 4005 | unsigned proto_nargs = proto->getNumArgs(); |
| 4006 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 4007 | QualType argTy = proto->getArgType(i); |
| 4008 | if (argTy->isPromotableIntegerType() || |
| 4009 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 4010 | return QualType(); |
| 4011 | } |
| 4012 | |
| 4013 | if (allLTypes) return lhs; |
| 4014 | if (allRTypes) return rhs; |
| 4015 | return getFunctionType(retType, proto->arg_type_begin(), |
Mike Stump | 2d3c191 | 2009-07-27 00:44:23 +0000 | [diff] [blame] | 4016 | proto->getNumArgs(), proto->isVariadic(), |
| 4017 | proto->getTypeQuals(), NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | if (allLTypes) return lhs; |
| 4021 | if (allRTypes) return rhs; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4022 | return getFunctionNoProtoType(retType, NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
| 4025 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 4026 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 4027 | // type is adjusted to "T" prior to any further analysis, the expression |
| 4028 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4029 | // expression is an lvalue unless the reference is an rvalue reference and |
| 4030 | // the expression is a function call (possibly inside parentheses). |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4031 | // FIXME: C++ shouldn't be going through here! The rules are different |
| 4032 | // enough that they should be handled separately. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4033 | // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really* |
| 4034 | // shouldn't be going through here! |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4035 | if (const ReferenceType *RT = LHS->getAs<ReferenceType>()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 4036 | LHS = RT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4037 | if (const ReferenceType *RT = RHS->getAs<ReferenceType>()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 4038 | RHS = RT->getPointeeType(); |
Chris Lattner | f3692dc | 2008-04-07 05:37:56 +0000 | [diff] [blame] | 4039 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4040 | QualType LHSCan = getCanonicalType(LHS), |
| 4041 | RHSCan = getCanonicalType(RHS); |
| 4042 | |
| 4043 | // If two types are identical, they are compatible. |
| 4044 | if (LHSCan == RHSCan) |
| 4045 | return LHS; |
| 4046 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4047 | // If the qualifiers are different, the types aren't compatible... mostly. |
| 4048 | Qualifiers LQuals = LHSCan.getQualifiers(); |
| 4049 | Qualifiers RQuals = RHSCan.getQualifiers(); |
| 4050 | if (LQuals != RQuals) { |
| 4051 | // If any of these qualifiers are different, we have a type |
| 4052 | // mismatch. |
| 4053 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 4054 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 4055 | return QualType(); |
| 4056 | |
| 4057 | // Exactly one GC qualifier difference is allowed: __strong is |
| 4058 | // okay if the other type has no GC qualifier but is an Objective |
| 4059 | // C object pointer (i.e. implicitly strong by default). We fix |
| 4060 | // this by pretending that the unqualified type was actually |
| 4061 | // qualified __strong. |
| 4062 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 4063 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 4064 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 4065 | |
| 4066 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 4067 | return QualType(); |
| 4068 | |
| 4069 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 4070 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 4071 | } |
| 4072 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 4073 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 4074 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4075 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
| 4078 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4079 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 4080 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 4081 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4082 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4083 | // We want to consider the two function types to be the same for these |
| 4084 | // comparisons, just force one to the other. |
| 4085 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 4086 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 4087 | |
| 4088 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4089 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 4090 | LHSClass = Type::ConstantArray; |
| 4091 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 4092 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4093 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 4094 | // Canonicalize ExtVector -> Vector. |
| 4095 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 4096 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4097 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4098 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4099 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4100 | // 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] | 4101 | // a signed integer type, or an unsigned integer type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4102 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4103 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 4104 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4105 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4106 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4107 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 4108 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4109 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4110 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4111 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4112 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4113 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 4114 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4115 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4116 | #define TYPE(Class, Base) |
| 4117 | #define ABSTRACT_TYPE(Class, Base) |
| 4118 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4119 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4120 | #include "clang/AST/TypeNodes.def" |
| 4121 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 4122 | return QualType(); |
| 4123 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4124 | case Type::LValueReference: |
| 4125 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4126 | case Type::MemberPointer: |
| 4127 | assert(false && "C++ should never be in mergeTypes"); |
| 4128 | return QualType(); |
| 4129 | |
| 4130 | case Type::IncompleteArray: |
| 4131 | case Type::VariableArray: |
| 4132 | case Type::FunctionProto: |
| 4133 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4134 | assert(false && "Types are eliminated above"); |
| 4135 | return QualType(); |
| 4136 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4137 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4138 | { |
| 4139 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4140 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 4141 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4142 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 4143 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4144 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4145 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4146 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4147 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4148 | return getPointerType(ResultType); |
| 4149 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4150 | case Type::BlockPointer: |
| 4151 | { |
| 4152 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4153 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 4154 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4155 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 4156 | if (ResultType.isNull()) return QualType(); |
| 4157 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 4158 | return LHS; |
| 4159 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 4160 | return RHS; |
| 4161 | return getBlockPointerType(ResultType); |
| 4162 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4163 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4164 | { |
| 4165 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 4166 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 4167 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 4168 | return QualType(); |
| 4169 | |
| 4170 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 4171 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
| 4172 | QualType ResultType = mergeTypes(LHSElem, RHSElem); |
| 4173 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4174 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4175 | return LHS; |
| 4176 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4177 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4178 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 4179 | ArrayType::ArraySizeModifier(), 0); |
| 4180 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 4181 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4182 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 4183 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4184 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4185 | return LHS; |
| 4186 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4187 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4188 | if (LVAT) { |
| 4189 | // FIXME: This isn't correct! But tricky to implement because |
| 4190 | // the array's size has to be the size of LHS, but the type |
| 4191 | // has to be different. |
| 4192 | return LHS; |
| 4193 | } |
| 4194 | if (RVAT) { |
| 4195 | // FIXME: This isn't correct! But tricky to implement because |
| 4196 | // the array's size has to be the size of RHS, but the type |
| 4197 | // has to be different. |
| 4198 | return RHS; |
| 4199 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4200 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 4201 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4202 | return getIncompleteArrayType(ResultType, |
| 4203 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4204 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4205 | case Type::FunctionNoProto: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4206 | return mergeFunctionTypes(LHS, RHS); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4207 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4208 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4209 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4210 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4211 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4212 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 4213 | case Type::Complex: |
| 4214 | // Distinct complex types are incompatible. |
| 4215 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4216 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4217 | // FIXME: The merged type should be an ExtVector! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4218 | if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4219 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4220 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4221 | case Type::ObjCInterface: { |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4222 | // Check if the interfaces are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4223 | // FIXME: This should be type compatibility, e.g. whether |
| 4224 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4225 | const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>(); |
| 4226 | const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>(); |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4227 | if (LHSIface && RHSIface && |
| 4228 | canAssignObjCInterfaces(LHSIface, RHSIface)) |
| 4229 | return LHS; |
| 4230 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4231 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4232 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4233 | case Type::ObjCObjectPointer: { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4234 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 4235 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4236 | return LHS; |
| 4237 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 4238 | return QualType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4239 | } |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4240 | case Type::FixedWidthInt: |
| 4241 | // Distinct fixed-width integers are not compatible. |
| 4242 | return QualType(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4243 | case Type::TemplateSpecialization: |
| 4244 | assert(false && "Dependent types have no size"); |
| 4245 | break; |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4246 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4247 | |
| 4248 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4249 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 4250 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 4251 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4252 | // Integer Predicates |
| 4253 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 4254 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4255 | unsigned ASTContext::getIntWidth(QualType T) { |
| 4256 | if (T == BoolTy) |
| 4257 | return 1; |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4258 | if (FixedWidthIntType *FWIT = dyn_cast<FixedWidthIntType>(T)) { |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4259 | return FWIT->getWidth(); |
| 4260 | } |
| 4261 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4262 | return (unsigned)getTypeSize(T); |
| 4263 | } |
| 4264 | |
| 4265 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
| 4266 | assert(T->isSignedIntegerType() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4267 | |
| 4268 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 4269 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 4270 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
| 4271 | VTy->getNumElements()); |
| 4272 | |
| 4273 | // For enums, we return the unsigned version of the base type. |
| 4274 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4275 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4276 | |
| 4277 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 4278 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4279 | switch (BTy->getKind()) { |
| 4280 | case BuiltinType::Char_S: |
| 4281 | case BuiltinType::SChar: |
| 4282 | return UnsignedCharTy; |
| 4283 | case BuiltinType::Short: |
| 4284 | return UnsignedShortTy; |
| 4285 | case BuiltinType::Int: |
| 4286 | return UnsignedIntTy; |
| 4287 | case BuiltinType::Long: |
| 4288 | return UnsignedLongTy; |
| 4289 | case BuiltinType::LongLong: |
| 4290 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4291 | case BuiltinType::Int128: |
| 4292 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4293 | default: |
| 4294 | assert(0 && "Unexpected signed integer type"); |
| 4295 | return QualType(); |
| 4296 | } |
| 4297 | } |
| 4298 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4299 | ExternalASTSource::~ExternalASTSource() { } |
| 4300 | |
| 4301 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4302 | |
| 4303 | |
| 4304 | //===----------------------------------------------------------------------===// |
| 4305 | // Builtin Type Computation |
| 4306 | //===----------------------------------------------------------------------===// |
| 4307 | |
| 4308 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
| 4309 | /// pointer over the consumed characters. This returns the resultant type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4310 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4311 | ASTContext::GetBuiltinTypeError &Error, |
| 4312 | bool AllowTypeModifiers = true) { |
| 4313 | // Modifiers. |
| 4314 | int HowLong = 0; |
| 4315 | bool Signed = false, Unsigned = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4316 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4317 | // Read the modifiers first. |
| 4318 | bool Done = false; |
| 4319 | while (!Done) { |
| 4320 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4321 | default: Done = true; --Str; break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4322 | case 'S': |
| 4323 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 4324 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 4325 | Signed = true; |
| 4326 | break; |
| 4327 | case 'U': |
| 4328 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 4329 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 4330 | Unsigned = true; |
| 4331 | break; |
| 4332 | case 'L': |
| 4333 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 4334 | ++HowLong; |
| 4335 | break; |
| 4336 | } |
| 4337 | } |
| 4338 | |
| 4339 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4340 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4341 | // Read the base type. |
| 4342 | switch (*Str++) { |
| 4343 | default: assert(0 && "Unknown builtin type letter!"); |
| 4344 | case 'v': |
| 4345 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4346 | "Bad modifiers used with 'v'!"); |
| 4347 | Type = Context.VoidTy; |
| 4348 | break; |
| 4349 | case 'f': |
| 4350 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4351 | "Bad modifiers used with 'f'!"); |
| 4352 | Type = Context.FloatTy; |
| 4353 | break; |
| 4354 | case 'd': |
| 4355 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 4356 | "Bad modifiers used with 'd'!"); |
| 4357 | if (HowLong) |
| 4358 | Type = Context.LongDoubleTy; |
| 4359 | else |
| 4360 | Type = Context.DoubleTy; |
| 4361 | break; |
| 4362 | case 's': |
| 4363 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 4364 | if (Unsigned) |
| 4365 | Type = Context.UnsignedShortTy; |
| 4366 | else |
| 4367 | Type = Context.ShortTy; |
| 4368 | break; |
| 4369 | case 'i': |
| 4370 | if (HowLong == 3) |
| 4371 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 4372 | else if (HowLong == 2) |
| 4373 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 4374 | else if (HowLong == 1) |
| 4375 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 4376 | else |
| 4377 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 4378 | break; |
| 4379 | case 'c': |
| 4380 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 4381 | if (Signed) |
| 4382 | Type = Context.SignedCharTy; |
| 4383 | else if (Unsigned) |
| 4384 | Type = Context.UnsignedCharTy; |
| 4385 | else |
| 4386 | Type = Context.CharTy; |
| 4387 | break; |
| 4388 | case 'b': // boolean |
| 4389 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 4390 | Type = Context.BoolTy; |
| 4391 | break; |
| 4392 | case 'z': // size_t. |
| 4393 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 4394 | Type = Context.getSizeType(); |
| 4395 | break; |
| 4396 | case 'F': |
| 4397 | Type = Context.getCFConstantStringType(); |
| 4398 | break; |
| 4399 | case 'a': |
| 4400 | Type = Context.getBuiltinVaListType(); |
| 4401 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4402 | break; |
| 4403 | case 'A': |
| 4404 | // This is a "reference" to a va_list; however, what exactly |
| 4405 | // this means depends on how va_list is defined. There are two |
| 4406 | // different kinds of va_list: ones passed by value, and ones |
| 4407 | // passed by reference. An example of a by-value va_list is |
| 4408 | // x86, where va_list is a char*. An example of by-ref va_list |
| 4409 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 4410 | // we want this argument to be a char*&; for x86-64, we want |
| 4411 | // it to be a __va_list_tag*. |
| 4412 | Type = Context.getBuiltinVaListType(); |
| 4413 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4414 | if (Type->isArrayType()) { |
| 4415 | Type = Context.getArrayDecayedType(Type); |
| 4416 | } else { |
| 4417 | Type = Context.getLValueReferenceType(Type); |
| 4418 | } |
| 4419 | break; |
| 4420 | case 'V': { |
| 4421 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4422 | unsigned NumElements = strtoul(Str, &End, 10); |
| 4423 | assert(End != Str && "Missing vector size"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4424 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4425 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4426 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4427 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 4428 | Type = Context.getVectorType(ElementType, NumElements); |
| 4429 | break; |
| 4430 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 4431 | case 'X': { |
| 4432 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 4433 | Type = Context.getComplexType(ElementType); |
| 4434 | break; |
| 4435 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4436 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4437 | Type = Context.getFILEType(); |
| 4438 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4439 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4440 | return QualType(); |
| 4441 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4442 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4443 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4444 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4445 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4446 | else |
| 4447 | Type = Context.getjmp_bufType(); |
| 4448 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4449 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4450 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4451 | return QualType(); |
| 4452 | } |
| 4453 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4454 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4455 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4456 | if (!AllowTypeModifiers) |
| 4457 | return Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4458 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4459 | Done = false; |
| 4460 | while (!Done) { |
| 4461 | switch (*Str++) { |
| 4462 | default: Done = true; --Str; break; |
| 4463 | case '*': |
| 4464 | Type = Context.getPointerType(Type); |
| 4465 | break; |
| 4466 | case '&': |
| 4467 | Type = Context.getLValueReferenceType(Type); |
| 4468 | break; |
| 4469 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 4470 | case 'C': |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4471 | Type = Type.withConst(); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4472 | break; |
| 4473 | } |
| 4474 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4475 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4476 | return Type; |
| 4477 | } |
| 4478 | |
| 4479 | /// GetBuiltinType - Return the type for the specified builtin. |
| 4480 | QualType ASTContext::GetBuiltinType(unsigned id, |
| 4481 | GetBuiltinTypeError &Error) { |
| 4482 | const char *TypeStr = BuiltinInfo.GetTypeString(id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4483 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4484 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4485 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4486 | Error = GE_None; |
| 4487 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4488 | if (Error != GE_None) |
| 4489 | return QualType(); |
| 4490 | while (TypeStr[0] && TypeStr[0] != '.') { |
| 4491 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4492 | if (Error != GE_None) |
| 4493 | return QualType(); |
| 4494 | |
| 4495 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 4496 | if (Ty->isArrayType()) |
| 4497 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4498 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4499 | ArgTypes.push_back(Ty); |
| 4500 | } |
| 4501 | |
| 4502 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 4503 | "'.' should only occur at end of builtin type list!"); |
| 4504 | |
| 4505 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 4506 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 4507 | return getFunctionNoProtoType(ResType); |
| 4508 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), |
| 4509 | TypeStr[0] == '.', 0); |
| 4510 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4511 | |
| 4512 | QualType |
| 4513 | ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 4514 | // Perform the usual unary conversions. We do this early so that |
| 4515 | // integral promotions to "int" can allow us to exit early, in the |
| 4516 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 4517 | // qualifiers. For example, "const float" and "float" are |
| 4518 | // equivalent. |
| 4519 | if (lhs->isPromotableIntegerType()) |
| 4520 | lhs = getPromotedIntegerType(lhs); |
| 4521 | else |
| 4522 | lhs = lhs.getUnqualifiedType(); |
| 4523 | if (rhs->isPromotableIntegerType()) |
| 4524 | rhs = getPromotedIntegerType(rhs); |
| 4525 | else |
| 4526 | rhs = rhs.getUnqualifiedType(); |
| 4527 | |
| 4528 | // If both types are identical, no conversion is needed. |
| 4529 | if (lhs == rhs) |
| 4530 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4531 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4532 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 4533 | // The caller can deal with this (e.g. pointer + int). |
| 4534 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 4535 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4536 | |
| 4537 | // At this point, we have two different arithmetic types. |
| 4538 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4539 | // Handle complex types first (C99 6.3.1.8p1). |
| 4540 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 4541 | // if we have an integer operand, the result is the complex type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4542 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4543 | // convert the rhs to the lhs complex type. |
| 4544 | return lhs; |
| 4545 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4546 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4547 | // convert the lhs to the rhs complex type. |
| 4548 | return rhs; |
| 4549 | } |
| 4550 | // This handles complex/complex, complex/float, or float/complex. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4551 | // When both operands are complex, the shorter operand is converted to the |
| 4552 | // type of the longer, and that is the type of the result. This corresponds |
| 4553 | // to what is done when combining two real floating-point operands. |
| 4554 | // The fun begins when size promotion occur across type domains. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4555 | // 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] | 4556 | // floating-point type, the less precise type is converted, within it's |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4557 | // 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] | 4558 | // when combining a "long double" with a "double _Complex", the |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4559 | // "double _Complex" is promoted to "long double _Complex". |
| 4560 | int result = getFloatingTypeOrder(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4561 | |
| 4562 | if (result > 0) { // The left side is bigger, convert rhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4563 | rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4564 | } else if (result < 0) { // The right side is bigger, convert lhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4565 | lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4566 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4567 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 4568 | // domains match. This is a requirement for our implementation, C99 |
| 4569 | // does not require this promotion. |
| 4570 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 4571 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
| 4572 | return rhs; |
| 4573 | } else { // handle "_Complex double, double". |
| 4574 | return lhs; |
| 4575 | } |
| 4576 | } |
| 4577 | return lhs; // The domain/size match exactly. |
| 4578 | } |
| 4579 | // Now handle "real" floating types (i.e. float, double, long double). |
| 4580 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 4581 | // if we have an integer operand, the result is the real floating type. |
| 4582 | if (rhs->isIntegerType()) { |
| 4583 | // convert rhs to the lhs floating point type. |
| 4584 | return lhs; |
| 4585 | } |
| 4586 | if (rhs->isComplexIntegerType()) { |
| 4587 | // convert rhs to the complex floating point type. |
| 4588 | return getComplexType(lhs); |
| 4589 | } |
| 4590 | if (lhs->isIntegerType()) { |
| 4591 | // convert lhs to the rhs floating point type. |
| 4592 | return rhs; |
| 4593 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4594 | if (lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4595 | // convert lhs to the complex floating point type. |
| 4596 | return getComplexType(rhs); |
| 4597 | } |
| 4598 | // We have two real floating types, float/complex combos were handled above. |
| 4599 | // Convert the smaller operand to the bigger result. |
| 4600 | int result = getFloatingTypeOrder(lhs, rhs); |
| 4601 | if (result > 0) // convert the rhs |
| 4602 | return lhs; |
| 4603 | assert(result < 0 && "illegal float comparison"); |
| 4604 | return rhs; // convert the lhs |
| 4605 | } |
| 4606 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 4607 | // Handle GCC complex int extension. |
| 4608 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 4609 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 4610 | |
| 4611 | if (lhsComplexInt && rhsComplexInt) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4612 | if (getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4613 | rhsComplexInt->getElementType()) >= 0) |
| 4614 | return lhs; // convert the rhs |
| 4615 | return rhs; |
| 4616 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 4617 | // convert the rhs to the lhs complex type. |
| 4618 | return lhs; |
| 4619 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 4620 | // convert the lhs to the rhs complex type. |
| 4621 | return rhs; |
| 4622 | } |
| 4623 | } |
| 4624 | // Finally, we have two differing integer types. |
| 4625 | // The rules for this case are in C99 6.3.1.8 |
| 4626 | int compare = getIntegerTypeOrder(lhs, rhs); |
| 4627 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 4628 | rhsSigned = rhs->isSignedIntegerType(); |
| 4629 | QualType destType; |
| 4630 | if (lhsSigned == rhsSigned) { |
| 4631 | // Same signedness; use the higher-ranked type |
| 4632 | destType = compare >= 0 ? lhs : rhs; |
| 4633 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 4634 | // The unsigned type has greater than or equal rank to the |
| 4635 | // signed type, so use the unsigned type |
| 4636 | destType = lhsSigned ? rhs : lhs; |
| 4637 | } else if (getIntWidth(lhs) != getIntWidth(rhs)) { |
| 4638 | // The two types are different widths; if we are here, that |
| 4639 | // means the signed type is larger than the unsigned type, so |
| 4640 | // use the signed type. |
| 4641 | destType = lhsSigned ? lhs : rhs; |
| 4642 | } else { |
| 4643 | // The signed type is higher-ranked than the unsigned type, |
| 4644 | // but isn't actually any bigger (like unsigned int and long |
| 4645 | // on most 32-bit systems). Use the unsigned type corresponding |
| 4646 | // to the signed type. |
| 4647 | destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 4648 | } |
| 4649 | return destType; |
| 4650 | } |