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) { |
Jeffrey Yasskin | 3958b50 | 2009-11-10 01:17:45 +0000 | [diff] [blame] | 261 | llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::const_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 | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 876 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 877 | /// those inherited by it. |
| 878 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
| 879 | llvm::SmallVectorImpl<ObjCProtocolDecl*> &Protocols) { |
| 880 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 881 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 882 | PE = OI->protocol_end(); P != PE; ++P) { |
| 883 | ObjCProtocolDecl *Proto = (*P); |
| 884 | Protocols.push_back(Proto); |
| 885 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 886 | PE = Proto->protocol_end(); P != PE; ++P) |
| 887 | CollectInheritedProtocols(*P, Protocols); |
| 888 | } |
| 889 | |
| 890 | // Categories of this Interface. |
| 891 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 892 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 893 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 894 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 895 | while (SD) { |
| 896 | CollectInheritedProtocols(SD, Protocols); |
| 897 | SD = SD->getSuperClass(); |
| 898 | } |
| 899 | return; |
| 900 | } |
| 901 | if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 902 | for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(), |
| 903 | PE = OC->protocol_end(); P != PE; ++P) { |
| 904 | ObjCProtocolDecl *Proto = (*P); |
| 905 | Protocols.push_back(Proto); |
| 906 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 907 | PE = Proto->protocol_end(); P != PE; ++P) |
| 908 | CollectInheritedProtocols(*P, Protocols); |
| 909 | } |
| 910 | return; |
| 911 | } |
| 912 | if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 913 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 914 | PE = OP->protocol_end(); P != PE; ++P) { |
| 915 | ObjCProtocolDecl *Proto = (*P); |
| 916 | Protocols.push_back(Proto); |
| 917 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 918 | PE = Proto->protocol_end(); P != PE; ++P) |
| 919 | CollectInheritedProtocols(*P, Protocols); |
| 920 | } |
| 921 | return; |
| 922 | } |
| 923 | } |
| 924 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 925 | unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) { |
| 926 | unsigned count = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 927 | for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), |
| 928 | E = PD->prop_end(); I != E; ++I) |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 929 | if ((*I)->getPropertyIvarDecl()) |
| 930 | ++count; |
| 931 | |
| 932 | // Also look into nested protocols. |
| 933 | for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), |
| 934 | E = PD->protocol_end(); P != E; ++P) |
| 935 | count += CountProtocolSynthesizedIvars(*P); |
| 936 | return count; |
| 937 | } |
| 938 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 940 | unsigned count = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 941 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), |
| 942 | E = OI->prop_end(); I != E; ++I) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 943 | if ((*I)->getPropertyIvarDecl()) |
| 944 | ++count; |
| 945 | } |
| 946 | // Also look into interface's protocol list for properties declared |
| 947 | // in the protocol and whose ivars are synthesized. |
| 948 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 949 | PE = OI->protocol_end(); P != PE; ++P) { |
| 950 | ObjCProtocolDecl *PD = (*P); |
| 951 | count += CountProtocolSynthesizedIvars(PD); |
| 952 | } |
| 953 | return count; |
| 954 | } |
| 955 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 956 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 957 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 958 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 959 | I = ObjCImpls.find(D); |
| 960 | if (I != ObjCImpls.end()) |
| 961 | return cast<ObjCImplementationDecl>(I->second); |
| 962 | return 0; |
| 963 | } |
| 964 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 965 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 966 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 967 | I = ObjCImpls.find(D); |
| 968 | if (I != ObjCImpls.end()) |
| 969 | return cast<ObjCCategoryImplDecl>(I->second); |
| 970 | return 0; |
| 971 | } |
| 972 | |
| 973 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 974 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 975 | ObjCImplementationDecl *ImplD) { |
| 976 | assert(IFaceD && ImplD && "Passed null params"); |
| 977 | ObjCImpls[IFaceD] = ImplD; |
| 978 | } |
| 979 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 980 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 981 | ObjCCategoryImplDecl *ImplD) { |
| 982 | assert(CatD && ImplD && "Passed null params"); |
| 983 | ObjCImpls[CatD] = ImplD; |
| 984 | } |
| 985 | |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 986 | /// \brief Allocate an uninitialized DeclaratorInfo. |
| 987 | /// |
| 988 | /// The caller should initialize the memory held by DeclaratorInfo using |
| 989 | /// the TypeLoc wrappers. |
| 990 | /// |
| 991 | /// \param T the type that will be the basis for type source info. This type |
| 992 | /// should refer to how the declarator was written in source code, not to |
| 993 | /// what type semantic analysis resolved the declarator to. |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 994 | DeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T, |
| 995 | unsigned DataSize) { |
| 996 | if (!DataSize) |
| 997 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 998 | else |
| 999 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
| 1000 | "incorrect data size provided to CreateDeclaratorInfo!"); |
| 1001 | |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1002 | DeclaratorInfo *DInfo = |
| 1003 | (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8); |
| 1004 | new (DInfo) DeclaratorInfo(T); |
| 1005 | return DInfo; |
| 1006 | } |
| 1007 | |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1008 | DeclaratorInfo *ASTContext::getTrivialDeclaratorInfo(QualType T, |
| 1009 | SourceLocation L) { |
| 1010 | DeclaratorInfo *DI = CreateDeclaratorInfo(T); |
| 1011 | DI->getTypeLoc().initialize(L); |
| 1012 | return DI; |
| 1013 | } |
| 1014 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1015 | /// getInterfaceLayoutImpl - Get or compute information about the |
| 1016 | /// layout of the given interface. |
| 1017 | /// |
| 1018 | /// \param Impl - If given, also include the layout of the interface's |
| 1019 | /// implementation. This may differ by including synthesized ivars. |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1020 | const ASTRecordLayout & |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1021 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
| 1022 | const ObjCImplementationDecl *Impl) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 1023 | assert(!D->isForwardDecl() && "Invalid interface decl!"); |
| 1024 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1025 | // Look up this layout, if already laid out, return what we have. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1026 | ObjCContainerDecl *Key = |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 1027 | Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; |
| 1028 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 1029 | return *Entry; |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1030 | |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 1031 | // Add in synthesized ivar count if laying out an implementation. |
| 1032 | if (Impl) { |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 1033 | unsigned FieldCount = D->ivar_size(); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1034 | unsigned SynthCount = CountSynthesizedIvars(D); |
| 1035 | FieldCount += SynthCount; |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 1036 | // If there aren't any sythesized ivars then reuse the interface |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 1037 | // entry. Note we can't cache this because we simply free all |
| 1038 | // entries later; however we shouldn't look up implementations |
| 1039 | // frequently. |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1040 | if (SynthCount == 0) |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 1041 | return getObjCLayout(D, 0); |
| 1042 | } |
| 1043 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 1045 | ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl); |
| 1046 | ObjCLayouts[Key] = NewEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1048 | return *NewEntry; |
| 1049 | } |
| 1050 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1051 | const ASTRecordLayout & |
| 1052 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 1053 | return getObjCLayout(D, 0); |
| 1054 | } |
| 1055 | |
| 1056 | const ASTRecordLayout & |
| 1057 | ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { |
| 1058 | return getObjCLayout(D->getClassInterface(), D); |
| 1059 | } |
| 1060 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 1061 | /// getASTRecordLayout - Get or compute information about the layout of the |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1062 | /// specified record (struct/union/class), which indicates its size and field |
| 1063 | /// position information. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1064 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1065 | D = D->getDefinition(*this); |
| 1066 | assert(D && "Cannot get layout of forward declarations!"); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1067 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1068 | // Look up this layout, if already laid out, return what we have. |
Eli Friedman | ab22c43 | 2009-07-22 20:29:16 +0000 | [diff] [blame] | 1069 | // Note that we can't save a reference to the entry because this function |
| 1070 | // is recursive. |
| 1071 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1072 | if (Entry) return *Entry; |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1073 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1074 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 1075 | ASTRecordLayoutBuilder::ComputeLayout(*this, D); |
Eli Friedman | ab22c43 | 2009-07-22 20:29:16 +0000 | [diff] [blame] | 1076 | ASTRecordLayouts[D] = NewEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1077 | |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1078 | return *NewEntry; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1081 | //===----------------------------------------------------------------------===// |
| 1082 | // Type creation/memoization methods |
| 1083 | //===----------------------------------------------------------------------===// |
| 1084 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1085 | QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) { |
| 1086 | unsigned Fast = Quals.getFastQualifiers(); |
| 1087 | Quals.removeFastQualifiers(); |
| 1088 | |
| 1089 | // Check if we've already instantiated this type. |
| 1090 | llvm::FoldingSetNodeID ID; |
| 1091 | ExtQuals::Profile(ID, TypeNode, Quals); |
| 1092 | void *InsertPos = 0; |
| 1093 | if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 1094 | assert(EQ->getQualifiers() == Quals); |
| 1095 | QualType T = QualType(EQ, Fast); |
| 1096 | return T; |
| 1097 | } |
| 1098 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1099 | ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1100 | ExtQualNodes.InsertNode(New, InsertPos); |
| 1101 | QualType T = QualType(New, Fast); |
| 1102 | return T; |
| 1103 | } |
| 1104 | |
| 1105 | QualType ASTContext::getVolatileType(QualType T) { |
| 1106 | QualType CanT = getCanonicalType(T); |
| 1107 | if (CanT.isVolatileQualified()) return T; |
| 1108 | |
| 1109 | QualifierCollector Quals; |
| 1110 | const Type *TypeNode = Quals.strip(T); |
| 1111 | Quals.addVolatile(); |
| 1112 | |
| 1113 | return getExtQualType(TypeNode, Quals); |
| 1114 | } |
| 1115 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1116 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1117 | QualType CanT = getCanonicalType(T); |
| 1118 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1119 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1120 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1121 | // If we are composing extended qualifiers together, merge together |
| 1122 | // into one ExtQuals node. |
| 1123 | QualifierCollector Quals; |
| 1124 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1126 | // If this type already has an address space specified, it cannot get |
| 1127 | // another one. |
| 1128 | assert(!Quals.hasAddressSpace() && |
| 1129 | "Type cannot be in multiple addr spaces!"); |
| 1130 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1131 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1132 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1135 | QualType ASTContext::getObjCGCQualType(QualType T, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1136 | Qualifiers::GC GCAttr) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1137 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1138 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1139 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1140 | |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1141 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1142 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1143 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1144 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1145 | return getPointerType(ResultType); |
| 1146 | } |
| 1147 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1149 | // If we are composing extended qualifiers together, merge together |
| 1150 | // into one ExtQuals node. |
| 1151 | QualifierCollector Quals; |
| 1152 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1154 | // If this type already has an ObjCGC specified, it cannot get |
| 1155 | // another one. |
| 1156 | assert(!Quals.hasObjCGCAttr() && |
| 1157 | "Type cannot have multiple ObjCGCs!"); |
| 1158 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1160 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1161 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1162 | |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1163 | QualType ASTContext::getNoReturnType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1164 | QualType ResultType; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1165 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1166 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1167 | ResultType = getNoReturnType(Pointee); |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 1168 | ResultType = getPointerType(ResultType); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1169 | } else if (T->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1170 | QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1171 | ResultType = getNoReturnType(Pointee); |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 1172 | ResultType = getBlockPointerType(ResultType); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1173 | } else { |
| 1174 | assert (T->isFunctionType() |
| 1175 | && "can't noreturn qualify non-pointer to function or block type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1176 | |
Benjamin Kramer | bdbeeb5 | 2009-09-25 11:47:22 +0000 | [diff] [blame] | 1177 | if (const FunctionNoProtoType *FNPT = T->getAs<FunctionNoProtoType>()) { |
| 1178 | ResultType = getFunctionNoProtoType(FNPT->getResultType(), true); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1179 | } else { |
| 1180 | const FunctionProtoType *F = T->getAs<FunctionProtoType>(); |
| 1181 | ResultType |
| 1182 | = getFunctionType(F->getResultType(), F->arg_type_begin(), |
| 1183 | F->getNumArgs(), F->isVariadic(), F->getTypeQuals(), |
| 1184 | F->hasExceptionSpec(), F->hasAnyExceptionSpec(), |
| 1185 | F->getNumExceptions(), F->exception_begin(), true); |
| 1186 | } |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1187 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1188 | |
| 1189 | return getQualifiedType(ResultType, T.getQualifiers()); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1192 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1193 | /// number with the specified element type. |
| 1194 | QualType ASTContext::getComplexType(QualType T) { |
| 1195 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1196 | // structure. |
| 1197 | llvm::FoldingSetNodeID ID; |
| 1198 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1200 | void *InsertPos = 0; |
| 1201 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1202 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1203 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1204 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1205 | // so fill in the canonical type field. |
| 1206 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1207 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1208 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1210 | // Get the new insert position for the node we care about. |
| 1211 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1212 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1213 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1214 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1215 | Types.push_back(New); |
| 1216 | ComplexTypes.InsertNode(New, InsertPos); |
| 1217 | return QualType(New, 0); |
| 1218 | } |
| 1219 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1220 | QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) { |
| 1221 | llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ? |
| 1222 | SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes; |
| 1223 | FixedWidthIntType *&Entry = Map[Width]; |
| 1224 | if (!Entry) |
| 1225 | Entry = new FixedWidthIntType(Width, Signed); |
| 1226 | return QualType(Entry, 0); |
| 1227 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1228 | |
| 1229 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1230 | /// the specified type. |
| 1231 | QualType ASTContext::getPointerType(QualType T) { |
| 1232 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1233 | // structure. |
| 1234 | llvm::FoldingSetNodeID ID; |
| 1235 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1236 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1237 | void *InsertPos = 0; |
| 1238 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1239 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1241 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1242 | // so fill in the canonical type field. |
| 1243 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1244 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1245 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1247 | // Get the new insert position for the node we care about. |
| 1248 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1249 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1250 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1251 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1252 | Types.push_back(New); |
| 1253 | PointerTypes.InsertNode(New, InsertPos); |
| 1254 | return QualType(New, 0); |
| 1255 | } |
| 1256 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1258 | /// a pointer to the specified block. |
| 1259 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1260 | assert(T->isFunctionType() && "block of function types only"); |
| 1261 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1262 | // structure. |
| 1263 | llvm::FoldingSetNodeID ID; |
| 1264 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1266 | void *InsertPos = 0; |
| 1267 | if (BlockPointerType *PT = |
| 1268 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1269 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | |
| 1271 | // 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] | 1272 | // type either so fill in the canonical type field. |
| 1273 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1274 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1275 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1276 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1277 | // Get the new insert position for the node we care about. |
| 1278 | BlockPointerType *NewIP = |
| 1279 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1280 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1281 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1282 | BlockPointerType *New |
| 1283 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1284 | Types.push_back(New); |
| 1285 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1286 | return QualType(New, 0); |
| 1287 | } |
| 1288 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1289 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1290 | /// lvalue reference to the specified type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1291 | QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1292 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1293 | // structure. |
| 1294 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1295 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1296 | |
| 1297 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1298 | if (LValueReferenceType *RT = |
| 1299 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1300 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1301 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1302 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1303 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1304 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1305 | // either, so fill in the canonical type field. |
| 1306 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1307 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1308 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1309 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1310 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1311 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1312 | LValueReferenceType *NewIP = |
| 1313 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1314 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1317 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1318 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1319 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1320 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1321 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1322 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1323 | return QualType(New, 0); |
| 1324 | } |
| 1325 | |
| 1326 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1327 | /// rvalue reference to the specified type. |
| 1328 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1329 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1330 | // structure. |
| 1331 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1332 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1333 | |
| 1334 | void *InsertPos = 0; |
| 1335 | if (RValueReferenceType *RT = |
| 1336 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1337 | return QualType(RT, 0); |
| 1338 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1339 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1340 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1341 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1342 | // either, so fill in the canonical type field. |
| 1343 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1344 | if (InnerRef || !T.isCanonical()) { |
| 1345 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1346 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1347 | |
| 1348 | // Get the new insert position for the node we care about. |
| 1349 | RValueReferenceType *NewIP = |
| 1350 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1351 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1352 | } |
| 1353 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1354 | RValueReferenceType *New |
| 1355 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1356 | Types.push_back(New); |
| 1357 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1358 | return QualType(New, 0); |
| 1359 | } |
| 1360 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1361 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1362 | /// member pointer to the specified type, in the specified class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1364 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1365 | // structure. |
| 1366 | llvm::FoldingSetNodeID ID; |
| 1367 | MemberPointerType::Profile(ID, T, Cls); |
| 1368 | |
| 1369 | void *InsertPos = 0; |
| 1370 | if (MemberPointerType *PT = |
| 1371 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1372 | return QualType(PT, 0); |
| 1373 | |
| 1374 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1375 | // type either, so fill in the canonical type field. |
| 1376 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1377 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1378 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1379 | |
| 1380 | // Get the new insert position for the node we care about. |
| 1381 | MemberPointerType *NewIP = |
| 1382 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1383 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1384 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1385 | MemberPointerType *New |
| 1386 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1387 | Types.push_back(New); |
| 1388 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1389 | return QualType(New, 0); |
| 1390 | } |
| 1391 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1393 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1394 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1395 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1396 | ArrayType::ArraySizeModifier ASM, |
| 1397 | unsigned EltTypeQuals) { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1398 | assert((EltTy->isDependentType() || |
| 1399 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1400 | "Constant array of VLAs is illegal!"); |
| 1401 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1402 | // Convert the array size into a canonical width matching the pointer size for |
| 1403 | // the target. |
| 1404 | llvm::APInt ArySize(ArySizeIn); |
| 1405 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1406 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1407 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1408 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1409 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1410 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1411 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1412 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1413 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1415 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1416 | // so fill in the canonical type field. |
| 1417 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1418 | if (!EltTy.isCanonical()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1420 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1421 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1423 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1424 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1425 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1426 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1427 | ConstantArrayType *New = new(*this,TypeAlignment) |
| 1428 | ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1429 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1430 | Types.push_back(New); |
| 1431 | return QualType(New, 0); |
| 1432 | } |
| 1433 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1434 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1435 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1436 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1437 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1438 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1439 | unsigned EltTypeQuals, |
| 1440 | SourceRange Brackets) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1441 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1442 | // that have an expression provided for their size. |
| 1443 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1444 | VariableArrayType *New = new(*this, TypeAlignment) |
| 1445 | VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1446 | |
| 1447 | VariableArrayTypes.push_back(New); |
| 1448 | Types.push_back(New); |
| 1449 | return QualType(New, 0); |
| 1450 | } |
| 1451 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1452 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1453 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1454 | /// type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1455 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1456 | Expr *NumElts, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1457 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1458 | unsigned EltTypeQuals, |
| 1459 | SourceRange Brackets) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 | assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1461 | "Size must be type- or value-dependent!"); |
| 1462 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1463 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM, |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1465 | EltTypeQuals, NumElts); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1466 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1467 | void *InsertPos = 0; |
| 1468 | DependentSizedArrayType *Canon |
| 1469 | = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1470 | DependentSizedArrayType *New; |
| 1471 | if (Canon) { |
| 1472 | // We already have a canonical version of this array type; use it as |
| 1473 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1474 | New = new (*this, TypeAlignment) |
| 1475 | DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), |
| 1476 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1477 | } else { |
| 1478 | QualType CanonEltTy = getCanonicalType(EltTy); |
| 1479 | if (CanonEltTy == EltTy) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1480 | New = new (*this, TypeAlignment) |
| 1481 | DependentSizedArrayType(*this, EltTy, QualType(), |
| 1482 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1483 | DependentSizedArrayTypes.InsertNode(New, InsertPos); |
| 1484 | } else { |
| 1485 | QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts, |
| 1486 | ASM, EltTypeQuals, |
| 1487 | SourceRange()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1488 | New = new (*this, TypeAlignment) |
| 1489 | DependentSizedArrayType(*this, EltTy, Canon, |
| 1490 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1491 | } |
| 1492 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1494 | Types.push_back(New); |
| 1495 | return QualType(New, 0); |
| 1496 | } |
| 1497 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1498 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1499 | ArrayType::ArraySizeModifier ASM, |
| 1500 | unsigned EltTypeQuals) { |
| 1501 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1502 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1503 | |
| 1504 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1505 | if (IncompleteArrayType *ATP = |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1506 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1507 | return QualType(ATP, 0); |
| 1508 | |
| 1509 | // If the element type isn't canonical, this won't be a canonical type |
| 1510 | // either, so fill in the canonical type field. |
| 1511 | QualType Canonical; |
| 1512 | |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1513 | if (!EltTy.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1514 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1515 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1516 | |
| 1517 | // Get the new insert position for the node we care about. |
| 1518 | IncompleteArrayType *NewIP = |
| 1519 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1520 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1521 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1522 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1523 | IncompleteArrayType *New = new (*this, TypeAlignment) |
| 1524 | IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1525 | |
| 1526 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1527 | Types.push_back(New); |
| 1528 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1531 | /// getVectorType - Return the unique reference to a vector type of |
| 1532 | /// the specified element type and size. VectorType must be a built-in type. |
| 1533 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1534 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1536 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1537 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1538 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1539 | // Check if we've already instantiated a vector of this type. |
| 1540 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1542 | void *InsertPos = 0; |
| 1543 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1544 | return QualType(VTP, 0); |
| 1545 | |
| 1546 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1547 | // so fill in the canonical type field. |
| 1548 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1549 | if (!vecType.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1550 | Canonical = getVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1551 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1552 | // Get the new insert position for the node we care about. |
| 1553 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1554 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1555 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1556 | VectorType *New = new (*this, TypeAlignment) |
| 1557 | VectorType(vecType, NumElts, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1558 | VectorTypes.InsertNode(New, InsertPos); |
| 1559 | Types.push_back(New); |
| 1560 | return QualType(New, 0); |
| 1561 | } |
| 1562 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1563 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1564 | /// 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] | 1565 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1566 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1568 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1569 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1571 | // Check if we've already instantiated a vector of this type. |
| 1572 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1574 | void *InsertPos = 0; |
| 1575 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1576 | return QualType(VTP, 0); |
| 1577 | |
| 1578 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1579 | // so fill in the canonical type field. |
| 1580 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1581 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1582 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1583 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1584 | // Get the new insert position for the node we care about. |
| 1585 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1586 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1587 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1588 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1589 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1590 | VectorTypes.InsertNode(New, InsertPos); |
| 1591 | Types.push_back(New); |
| 1592 | return QualType(New, 0); |
| 1593 | } |
| 1594 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | QualType ASTContext::getDependentSizedExtVectorType(QualType vecType, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1596 | Expr *SizeExpr, |
| 1597 | SourceLocation AttrLoc) { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1598 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1599 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1600 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1602 | void *InsertPos = 0; |
| 1603 | DependentSizedExtVectorType *Canon |
| 1604 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1605 | DependentSizedExtVectorType *New; |
| 1606 | if (Canon) { |
| 1607 | // We already have a canonical version of this array type; use it as |
| 1608 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1609 | New = new (*this, TypeAlignment) |
| 1610 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1611 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1612 | } else { |
| 1613 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1614 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1615 | New = new (*this, TypeAlignment) |
| 1616 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1617 | AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1618 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1619 | } else { |
| 1620 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1621 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1622 | New = new (*this, TypeAlignment) |
| 1623 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1624 | } |
| 1625 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1627 | Types.push_back(New); |
| 1628 | return QualType(New, 0); |
| 1629 | } |
| 1630 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1631 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1632 | /// |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1633 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1634 | // Unique functions, to guarantee there is only one function of a particular |
| 1635 | // structure. |
| 1636 | llvm::FoldingSetNodeID ID; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1637 | FunctionNoProtoType::Profile(ID, ResultTy, NoReturn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1638 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1639 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1640 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1641 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1642 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1644 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1645 | if (!ResultTy.isCanonical()) { |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1646 | Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1647 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1648 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1649 | FunctionNoProtoType *NewIP = |
| 1650 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1651 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1652 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1654 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
| 1655 | FunctionNoProtoType(ResultTy, Canonical, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1656 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1657 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1658 | return QualType(New, 0); |
| 1659 | } |
| 1660 | |
| 1661 | /// getFunctionType - Return a normal function type with a typed argument |
| 1662 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1663 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1664 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1665 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1666 | bool hasAnyExceptionSpec, unsigned NumExs, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1667 | const QualType *ExArray, bool NoReturn) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1668 | // Unique functions, to guarantee there is only one function of a particular |
| 1669 | // structure. |
| 1670 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1671 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1672 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1673 | NumExs, ExArray, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1674 | |
| 1675 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1677 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1678 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1679 | |
| 1680 | // Determine whether the type being created is already canonical or not. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1681 | bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1682 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1683 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1684 | isCanonical = false; |
| 1685 | |
| 1686 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1687 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1688 | QualType Canonical; |
| 1689 | if (!isCanonical) { |
| 1690 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1691 | CanonicalArgs.reserve(NumArgs); |
| 1692 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1693 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1694 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1695 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1696 | CanonicalArgs.data(), NumArgs, |
Douglas Gregor | 47259d9 | 2009-08-05 19:03:35 +0000 | [diff] [blame] | 1697 | isVariadic, TypeQuals, false, |
| 1698 | false, 0, 0, NoReturn); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1699 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1700 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1701 | FunctionProtoType *NewIP = |
| 1702 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1703 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1704 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1705 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1706 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1707 | // for two variable size arrays (for parameter and exception types) at the |
| 1708 | // end of them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | FunctionProtoType *FTP = |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1710 | (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + |
| 1711 | NumArgs*sizeof(QualType) + |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1712 | NumExs*sizeof(QualType), TypeAlignment); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1713 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1714 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1715 | ExArray, NumExs, Canonical, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1716 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1717 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1718 | return QualType(FTP, 0); |
| 1719 | } |
| 1720 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1721 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1722 | /// specified type declaration. |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1723 | QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1724 | assert(Decl && "Passed null for Decl param"); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1725 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1727 | if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1728 | return getTypedefType(Typedef); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1729 | else if (isa<TemplateTypeParmDecl>(Decl)) { |
| 1730 | assert(false && "Template type parameter types are always available."); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1731 | } else if (ObjCInterfaceDecl *ObjCInterface |
| 1732 | = dyn_cast<ObjCInterfaceDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1733 | return getObjCInterfaceType(ObjCInterface); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1734 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1735 | if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1736 | if (PrevDecl) |
| 1737 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1738 | else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1739 | Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1740 | } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1741 | if (PrevDecl) |
| 1742 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1743 | else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1744 | Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1745 | } else |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1746 | assert(false && "TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1747 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1748 | if (!PrevDecl) Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1749 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1752 | /// getTypedefType - Return the unique reference to the type for the |
| 1753 | /// specified typename decl. |
| 1754 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 1755 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1756 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1757 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1758 | Decl->TypeForDecl = new(*this, TypeAlignment) |
| 1759 | TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1760 | Types.push_back(Decl->TypeForDecl); |
| 1761 | return QualType(Decl->TypeForDecl, 0); |
| 1762 | } |
| 1763 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1764 | /// \brief Retrieve a substitution-result type. |
| 1765 | QualType |
| 1766 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
| 1767 | QualType Replacement) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1768 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1769 | && "replacement types must always be canonical"); |
| 1770 | |
| 1771 | llvm::FoldingSetNodeID ID; |
| 1772 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 1773 | void *InsertPos = 0; |
| 1774 | SubstTemplateTypeParmType *SubstParm |
| 1775 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1776 | |
| 1777 | if (!SubstParm) { |
| 1778 | SubstParm = new (*this, TypeAlignment) |
| 1779 | SubstTemplateTypeParmType(Parm, Replacement); |
| 1780 | Types.push_back(SubstParm); |
| 1781 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 1782 | } |
| 1783 | |
| 1784 | return QualType(SubstParm, 0); |
| 1785 | } |
| 1786 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1787 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1789 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1790 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1791 | bool ParameterPack, |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1792 | IdentifierInfo *Name) { |
| 1793 | llvm::FoldingSetNodeID ID; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1794 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1795 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1796 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1797 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1798 | |
| 1799 | if (TypeParm) |
| 1800 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1801 | |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1802 | if (Name) { |
| 1803 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1804 | TypeParm = new (*this, TypeAlignment) |
| 1805 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1806 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1807 | TypeParm = new (*this, TypeAlignment) |
| 1808 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1809 | |
| 1810 | Types.push_back(TypeParm); |
| 1811 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1812 | |
| 1813 | return QualType(TypeParm, 0); |
| 1814 | } |
| 1815 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1817 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1818 | const TemplateArgumentLoc *Args, |
| 1819 | unsigned NumArgs, |
| 1820 | QualType Canon) { |
| 1821 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 1822 | ArgVec.reserve(NumArgs); |
| 1823 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1824 | ArgVec.push_back(Args[i].getArgument()); |
| 1825 | |
| 1826 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, Canon); |
| 1827 | } |
| 1828 | |
| 1829 | QualType |
| 1830 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1831 | const TemplateArgument *Args, |
| 1832 | unsigned NumArgs, |
| 1833 | QualType Canon) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1834 | if (!Canon.isNull()) |
| 1835 | Canon = getCanonicalType(Canon); |
| 1836 | else { |
| 1837 | // Build the canonical template specialization type. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1838 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 1839 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 1840 | CanonArgs.reserve(NumArgs); |
| 1841 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1842 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 1843 | |
| 1844 | // Determine whether this canonical template specialization type already |
| 1845 | // exists. |
| 1846 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1848 | CanonArgs.data(), NumArgs, *this); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1849 | |
| 1850 | void *InsertPos = 0; |
| 1851 | TemplateSpecializationType *Spec |
| 1852 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1854 | if (!Spec) { |
| 1855 | // Allocate a new canonical template specialization type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1857 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1858 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1860 | CanonArgs.data(), NumArgs, |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1861 | Canon); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1862 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1863 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1864 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1865 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1866 | if (Canon.isNull()) |
| 1867 | Canon = QualType(Spec, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1868 | assert(Canon->isDependentType() && |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1869 | "Non-dependent template-id type must have a canonical type"); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1870 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 1871 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1872 | // Allocate the (non-canonical) template specialization type, but don't |
| 1873 | // try to unique it: these types typically have location information that |
| 1874 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1876 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1877 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | TemplateSpecializationType *Spec |
| 1879 | = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1880 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1882 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1883 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1886 | QualType |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1887 | ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS, |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1888 | QualType NamedType) { |
| 1889 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1890 | QualifiedNameType::Profile(ID, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1891 | |
| 1892 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1893 | QualifiedNameType *T |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1894 | = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1895 | if (T) |
| 1896 | return QualType(T, 0); |
| 1897 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1898 | T = new (*this) QualifiedNameType(NNS, NamedType, |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1899 | getCanonicalType(NamedType)); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1900 | Types.push_back(T); |
| 1901 | QualifiedNameTypes.InsertNode(T, InsertPos); |
| 1902 | return QualType(T, 0); |
| 1903 | } |
| 1904 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1906 | const IdentifierInfo *Name, |
| 1907 | QualType Canon) { |
| 1908 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1909 | |
| 1910 | if (Canon.isNull()) { |
| 1911 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 1912 | if (CanonNNS != NNS) |
| 1913 | Canon = getTypenameType(CanonNNS, Name); |
| 1914 | } |
| 1915 | |
| 1916 | llvm::FoldingSetNodeID ID; |
| 1917 | TypenameType::Profile(ID, NNS, Name); |
| 1918 | |
| 1919 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1920 | TypenameType *T |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1921 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1922 | if (T) |
| 1923 | return QualType(T, 0); |
| 1924 | |
| 1925 | T = new (*this) TypenameType(NNS, Name, Canon); |
| 1926 | Types.push_back(T); |
| 1927 | TypenameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1928 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1931 | QualType |
| 1932 | ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1933 | const TemplateSpecializationType *TemplateId, |
| 1934 | QualType Canon) { |
| 1935 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1936 | |
| 1937 | if (Canon.isNull()) { |
| 1938 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 1939 | QualType CanonType = getCanonicalType(QualType(TemplateId, 0)); |
| 1940 | if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) { |
| 1941 | const TemplateSpecializationType *CanonTemplateId |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1942 | = CanonType->getAs<TemplateSpecializationType>(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1943 | assert(CanonTemplateId && |
| 1944 | "Canonical type must also be a template specialization type"); |
| 1945 | Canon = getTypenameType(CanonNNS, CanonTemplateId); |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | llvm::FoldingSetNodeID ID; |
| 1950 | TypenameType::Profile(ID, NNS, TemplateId); |
| 1951 | |
| 1952 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1953 | TypenameType *T |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1954 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1955 | if (T) |
| 1956 | return QualType(T, 0); |
| 1957 | |
| 1958 | T = new (*this) TypenameType(NNS, TemplateId, Canon); |
| 1959 | Types.push_back(T); |
| 1960 | TypenameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1961 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 1964 | QualType |
| 1965 | ASTContext::getElaboratedType(QualType UnderlyingType, |
| 1966 | ElaboratedType::TagKind Tag) { |
| 1967 | llvm::FoldingSetNodeID ID; |
| 1968 | ElaboratedType::Profile(ID, UnderlyingType, Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1969 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 1970 | void *InsertPos = 0; |
| 1971 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1972 | if (T) |
| 1973 | return QualType(T, 0); |
| 1974 | |
| 1975 | QualType Canon = getCanonicalType(UnderlyingType); |
| 1976 | |
| 1977 | T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon); |
| 1978 | Types.push_back(T); |
| 1979 | ElaboratedTypes.InsertNode(T, InsertPos); |
| 1980 | return QualType(T, 0); |
| 1981 | } |
| 1982 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1983 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 1984 | /// alphabetically. |
| 1985 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 1986 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1987 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1990 | static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols, |
| 1991 | unsigned NumProtocols) { |
| 1992 | if (NumProtocols == 0) return true; |
| 1993 | |
| 1994 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 1995 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 1996 | return false; |
| 1997 | return true; |
| 1998 | } |
| 1999 | |
| 2000 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2001 | unsigned &NumProtocols) { |
| 2002 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2004 | // Sort protocols, keyed by name. |
| 2005 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2006 | |
| 2007 | // Remove duplicates. |
| 2008 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2009 | NumProtocols = ProtocolsEnd-Protocols; |
| 2010 | } |
| 2011 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2012 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2013 | /// the given interface decl and the conforming protocol list. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2014 | QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2015 | ObjCProtocolDecl **Protocols, |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2016 | unsigned NumProtocols) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2017 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2018 | ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2019 | |
| 2020 | void *InsertPos = 0; |
| 2021 | if (ObjCObjectPointerType *QT = |
| 2022 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2023 | return QualType(QT, 0); |
| 2024 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2025 | // Sort the protocol list alphabetically to canonicalize it. |
| 2026 | QualType Canonical; |
| 2027 | if (!InterfaceT.isCanonical() || |
| 2028 | !areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2029 | if (!areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2030 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); |
| 2031 | unsigned UniqueCount = NumProtocols; |
| 2032 | |
| 2033 | std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); |
| 2034 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
| 2035 | |
| 2036 | Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), |
| 2037 | &Sorted[0], UniqueCount); |
| 2038 | } else { |
| 2039 | Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), |
| 2040 | Protocols, NumProtocols); |
| 2041 | } |
| 2042 | |
| 2043 | // Regenerate InsertPos. |
| 2044 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2045 | } |
| 2046 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2047 | // No Match; |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2048 | ObjCObjectPointerType *QType = new (*this, TypeAlignment) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2049 | ObjCObjectPointerType(Canonical, InterfaceT, Protocols, NumProtocols); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2050 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2051 | Types.push_back(QType); |
| 2052 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
| 2053 | return QualType(QType, 0); |
| 2054 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2055 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2056 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2057 | /// specified ObjC interface decl. The list of protocols is optional. |
| 2058 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2059 | ObjCProtocolDecl **Protocols, unsigned NumProtocols) { |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2060 | llvm::FoldingSetNodeID ID; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2061 | ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2062 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2063 | void *InsertPos = 0; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2064 | if (ObjCInterfaceType *QT = |
| 2065 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2066 | return QualType(QT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2068 | // Sort the protocol list alphabetically to canonicalize it. |
| 2069 | QualType Canonical; |
| 2070 | if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2071 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); |
| 2072 | std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); |
| 2073 | |
| 2074 | unsigned UniqueCount = NumProtocols; |
| 2075 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
| 2076 | |
| 2077 | Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount); |
| 2078 | |
| 2079 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2080 | } |
| 2081 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2082 | ObjCInterfaceType *QType = new (*this, TypeAlignment) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2083 | ObjCInterfaceType(Canonical, const_cast<ObjCInterfaceDecl*>(Decl), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2084 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2085 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2086 | Types.push_back(QType); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2087 | ObjCInterfaceTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2088 | return QualType(QType, 0); |
| 2089 | } |
| 2090 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2091 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2092 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2093 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2095 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2096 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2097 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2098 | if (tofExpr->isTypeDependent()) { |
| 2099 | llvm::FoldingSetNodeID ID; |
| 2100 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2102 | void *InsertPos = 0; |
| 2103 | DependentTypeOfExprType *Canon |
| 2104 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2105 | if (Canon) { |
| 2106 | // We already have a "canonical" version of an identical, dependent |
| 2107 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2108 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2109 | QualType((TypeOfExprType*)Canon, 0)); |
| 2110 | } |
| 2111 | else { |
| 2112 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2113 | Canon |
| 2114 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2115 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2116 | toe = Canon; |
| 2117 | } |
| 2118 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2119 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2120 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2121 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2122 | Types.push_back(toe); |
| 2123 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2126 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2127 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2128 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2129 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2130 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2131 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2132 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2133 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2134 | Types.push_back(tot); |
| 2135 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2138 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2139 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
| 2140 | static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2141 | if (e->isTypeDependent()) |
| 2142 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2143 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2144 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2145 | // as the type of the entity named by e. |
| 2146 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2147 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2148 | return VD->getType(); |
| 2149 | } |
| 2150 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2151 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2152 | return FD->getType(); |
| 2153 | } |
| 2154 | // If e is a function call or an invocation of an overloaded operator, |
| 2155 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2156 | // return type of that function. |
| 2157 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2158 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2159 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2160 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2161 | |
| 2162 | // 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] | 2163 | // defined as T&, otherwise decltype(e) is defined as T. |
| 2164 | if (e->isLvalue(Context) == Expr::LV_Valid) |
| 2165 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2166 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2167 | return T; |
| 2168 | } |
| 2169 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2170 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2171 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2172 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2173 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2174 | /// on canonical type's (which are always unique). |
| 2175 | QualType ASTContext::getDecltypeType(Expr *e) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2176 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2177 | if (e->isTypeDependent()) { |
| 2178 | llvm::FoldingSetNodeID ID; |
| 2179 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2180 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2181 | void *InsertPos = 0; |
| 2182 | DependentDecltypeType *Canon |
| 2183 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2184 | if (Canon) { |
| 2185 | // We already have a "canonical" version of an equivalent, dependent |
| 2186 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2187 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2188 | QualType((DecltypeType*)Canon, 0)); |
| 2189 | } |
| 2190 | else { |
| 2191 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2192 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2193 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2194 | dt = Canon; |
| 2195 | } |
| 2196 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2197 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2198 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2199 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2200 | Types.push_back(dt); |
| 2201 | return QualType(dt, 0); |
| 2202 | } |
| 2203 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2204 | /// getTagDeclType - Return the unique reference to the type for the |
| 2205 | /// specified TagDecl (struct/union/class/enum) decl. |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2206 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2207 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2208 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2209 | // away const? mutable? |
| 2210 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2213 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2214 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2215 | /// needs to agree with the definition in <stddef.h>. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2216 | QualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2217 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2220 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2221 | /// Used when in C++, as a GCC extension. |
| 2222 | QualType ASTContext::getSignedWCharType() const { |
| 2223 | // FIXME: derive from "Target" ? |
| 2224 | return WCharTy; |
| 2225 | } |
| 2226 | |
| 2227 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2228 | /// Used when in C++, as a GCC extension. |
| 2229 | QualType ASTContext::getUnsignedWCharType() const { |
| 2230 | // FIXME: derive from "Target" ? |
| 2231 | return UnsignedIntTy; |
| 2232 | } |
| 2233 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2234 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2235 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2236 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2237 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2240 | //===----------------------------------------------------------------------===// |
| 2241 | // Type Operators |
| 2242 | //===----------------------------------------------------------------------===// |
| 2243 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2244 | CanQualType ASTContext::getCanonicalParamType(QualType T) { |
| 2245 | // Push qualifiers into arrays, and then discard any remaining |
| 2246 | // qualifiers. |
| 2247 | T = getCanonicalType(T); |
| 2248 | const Type *Ty = T.getTypePtr(); |
| 2249 | |
| 2250 | QualType Result; |
| 2251 | if (isa<ArrayType>(Ty)) { |
| 2252 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2253 | } else if (isa<FunctionType>(Ty)) { |
| 2254 | Result = getPointerType(QualType(Ty, 0)); |
| 2255 | } else { |
| 2256 | Result = QualType(Ty, 0); |
| 2257 | } |
| 2258 | |
| 2259 | return CanQualType::CreateUnsafe(Result); |
| 2260 | } |
| 2261 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2262 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 2263 | /// the specified potentially non-canonical type. The non-canonical version |
| 2264 | /// of a type may have many "decorated" versions of types. Decorators can |
| 2265 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 2266 | /// to be free of any of these, allowing two canonical types to be compared |
| 2267 | /// for exact equality with a simple pointer comparison. |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2268 | CanQualType ASTContext::getCanonicalType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2269 | QualifierCollector Quals; |
| 2270 | const Type *Ptr = Quals.strip(T); |
| 2271 | QualType CanType = Ptr->getCanonicalTypeInternal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2273 | // The canonical internal type will be the canonical type *except* |
| 2274 | // that we push type qualifiers down through array types. |
| 2275 | |
| 2276 | // If there are no new qualifiers to push down, stop here. |
| 2277 | if (!Quals.hasQualifiers()) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2278 | return CanQualType::CreateUnsafe(CanType); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2279 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2280 | // If the type qualifiers are on an array type, get the canonical |
| 2281 | // type of the array with the qualifiers applied to the element |
| 2282 | // type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2283 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 2284 | if (!AT) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2285 | return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2286 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2287 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2288 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2289 | QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2290 | NewEltTy = getCanonicalType(NewEltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2291 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2292 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2293 | return CanQualType::CreateUnsafe( |
| 2294 | getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2295 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2296 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2297 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2298 | return CanQualType::CreateUnsafe( |
| 2299 | getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2300 | IAT->getIndexTypeCVRQualifiers())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2302 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2303 | return CanQualType::CreateUnsafe( |
| 2304 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2305 | DSAT->getSizeExpr() ? |
| 2306 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2307 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2308 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 87a924e | 2009-10-30 22:56:57 +0000 | [diff] [blame] | 2309 | DSAT->getBracketsRange())->getCanonicalTypeInternal()); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2310 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2311 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2312 | return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2313 | VAT->getSizeExpr() ? |
| 2314 | VAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2315 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2316 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2317 | VAT->getBracketsRange())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2320 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
| 2321 | // If this template name refers to a template, the canonical |
| 2322 | // template name merely stores the template itself. |
| 2323 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2324 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2325 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2326 | // If this template name refers to a set of overloaded function templates, |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 2327 | /// the canonical template name merely stores the set of function templates. |
Douglas Gregor | 6ebd15e | 2009-07-31 05:24:01 +0000 | [diff] [blame] | 2328 | if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) { |
| 2329 | OverloadedFunctionDecl *CanonOvl = 0; |
| 2330 | for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), |
| 2331 | FEnd = Ovl->function_end(); |
| 2332 | F != FEnd; ++F) { |
| 2333 | Decl *Canon = F->get()->getCanonicalDecl(); |
| 2334 | if (CanonOvl || Canon != F->get()) { |
| 2335 | if (!CanonOvl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2336 | CanonOvl = OverloadedFunctionDecl::Create(*this, |
| 2337 | Ovl->getDeclContext(), |
Douglas Gregor | 6ebd15e | 2009-07-31 05:24:01 +0000 | [diff] [blame] | 2338 | Ovl->getDeclName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2339 | |
Douglas Gregor | 6ebd15e | 2009-07-31 05:24:01 +0000 | [diff] [blame] | 2340 | CanonOvl->addOverload( |
| 2341 | AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon))); |
| 2342 | } |
| 2343 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2344 | |
Douglas Gregor | 6ebd15e | 2009-07-31 05:24:01 +0000 | [diff] [blame] | 2345 | return TemplateName(CanonOvl? CanonOvl : Ovl); |
| 2346 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2347 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2348 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2349 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2350 | return DTN->CanonicalTemplateName; |
| 2351 | } |
| 2352 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame^] | 2353 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2354 | X = getCanonicalTemplateName(X); |
| 2355 | Y = getCanonicalTemplateName(Y); |
| 2356 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2357 | } |
| 2358 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2359 | TemplateArgument |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2360 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { |
| 2361 | switch (Arg.getKind()) { |
| 2362 | case TemplateArgument::Null: |
| 2363 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2364 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2365 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2366 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2367 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2368 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2369 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2370 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2371 | case TemplateArgument::Template: |
| 2372 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
| 2373 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2374 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2375 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2376 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2377 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2378 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2379 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2380 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2381 | case TemplateArgument::Pack: { |
| 2382 | // FIXME: Allocate in ASTContext |
| 2383 | TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()]; |
| 2384 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2385 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2386 | AEnd = Arg.pack_end(); |
| 2387 | A != AEnd; (void)++A, ++Idx) |
| 2388 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2389 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2390 | TemplateArgument Result; |
| 2391 | Result.setArgumentPack(CanonArgs, Arg.pack_size(), false); |
| 2392 | return Result; |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | // Silence GCC warning |
| 2397 | assert(false && "Unhandled template argument kind"); |
| 2398 | return TemplateArgument(); |
| 2399 | } |
| 2400 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2401 | NestedNameSpecifier * |
| 2402 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2403 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2404 | return 0; |
| 2405 | |
| 2406 | switch (NNS->getKind()) { |
| 2407 | case NestedNameSpecifier::Identifier: |
| 2408 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2409 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2410 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2411 | NNS->getAsIdentifier()); |
| 2412 | |
| 2413 | case NestedNameSpecifier::Namespace: |
| 2414 | // A namespace is canonical; build a nested-name-specifier with |
| 2415 | // this namespace and no prefix. |
| 2416 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2417 | |
| 2418 | case NestedNameSpecifier::TypeSpec: |
| 2419 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2420 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2421 | return NestedNameSpecifier::Create(*this, 0, |
| 2422 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2423 | T.getTypePtr()); |
| 2424 | } |
| 2425 | |
| 2426 | case NestedNameSpecifier::Global: |
| 2427 | // The global specifier is canonical and unique. |
| 2428 | return NNS; |
| 2429 | } |
| 2430 | |
| 2431 | // Required to silence a GCC warning |
| 2432 | return 0; |
| 2433 | } |
| 2434 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2435 | |
| 2436 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 2437 | // Handle the non-qualified case efficiently. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2438 | if (!T.hasQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2439 | // Handle the common positive case fast. |
| 2440 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2441 | return AT; |
| 2442 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2443 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2444 | // Handle the common negative case fast. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2445 | QualType CType = T->getCanonicalTypeInternal(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2446 | if (!isa<ArrayType>(CType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2447 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2448 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2449 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2450 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2451 | // 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] | 2452 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2453 | // If we get here, we either have type qualifiers on the type, or we have |
| 2454 | // 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] | 2455 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2456 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2457 | QualifierCollector Qs; |
| 2458 | const Type *Ty = Qs.strip(T.getDesugaredType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2459 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2460 | // If we have a simple case, just return now. |
| 2461 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2462 | if (ATy == 0 || Qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2463 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2464 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2465 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 2466 | // qualifiers into the array element type and return a new array type. |
| 2467 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2468 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2469 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2470 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2471 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 2472 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2473 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2474 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2475 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 2476 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 2477 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2478 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2479 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2480 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2481 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 2482 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2483 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2484 | DSAT->getSizeExpr() ? |
| 2485 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2486 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2487 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2488 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2489 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2490 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2491 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2492 | VAT->getSizeExpr() ? |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2493 | VAT->getSizeExpr()->Retain() : 0, |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2494 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2495 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2496 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2500 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 2501 | /// specified array type to a pointer. This operation is non-trivial when |
| 2502 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 2503 | /// this returns a pointer to a properly qualified element of the array. |
| 2504 | /// |
| 2505 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 2506 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2507 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 2508 | // typedefs in the element type of the array. This also handles propagation |
| 2509 | // of type qualifiers from the array type into the element type if present |
| 2510 | // (C99 6.7.3p8). |
| 2511 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 2512 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2513 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2514 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2515 | |
| 2516 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2517 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2520 | QualType ASTContext::getBaseElementType(QualType QT) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2521 | QualifierCollector Qs; |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2522 | while (true) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2523 | const Type *UT = Qs.strip(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2524 | if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) { |
| 2525 | QT = AT->getElementType(); |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 2526 | } else { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2527 | return Qs.apply(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2528 | } |
| 2529 | } |
| 2530 | } |
| 2531 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2532 | QualType ASTContext::getBaseElementType(const ArrayType *AT) { |
| 2533 | QualType ElemTy = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2534 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2535 | if (const ArrayType *AT = getAsArrayType(ElemTy)) |
| 2536 | return getBaseElementType(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2537 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 2538 | return ElemTy; |
| 2539 | } |
| 2540 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2541 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2542 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2543 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 2544 | uint64_t ElementCount = 1; |
| 2545 | do { |
| 2546 | ElementCount *= CA->getSize().getZExtValue(); |
| 2547 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 2548 | } while (CA); |
| 2549 | return ElementCount; |
| 2550 | } |
| 2551 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2552 | /// getFloatingRank - Return a relative rank for floating point types. |
| 2553 | /// 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] | 2554 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2555 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2556 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2557 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2558 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 2559 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2560 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2561 | case BuiltinType::Float: return FloatRank; |
| 2562 | case BuiltinType::Double: return DoubleRank; |
| 2563 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 2564 | } |
| 2565 | } |
| 2566 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2567 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 2568 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2569 | /// 'typeDomain' is a real floating point or complex type. |
| 2570 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2571 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 2572 | QualType Domain) const { |
| 2573 | FloatingRank EltRank = getFloatingRank(Size); |
| 2574 | if (Domain->isComplexType()) { |
| 2575 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2576 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2577 | case FloatRank: return FloatComplexTy; |
| 2578 | case DoubleRank: return DoubleComplexTy; |
| 2579 | case LongDoubleRank: return LongDoubleComplexTy; |
| 2580 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2581 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2582 | |
| 2583 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 2584 | switch (EltRank) { |
| 2585 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 2586 | case FloatRank: return FloatTy; |
| 2587 | case DoubleRank: return DoubleTy; |
| 2588 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2589 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2592 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 2593 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 2594 | /// '_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] | 2595 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2596 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 2597 | FloatingRank LHSR = getFloatingRank(LHS); |
| 2598 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2599 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2600 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2601 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2602 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2603 | return 1; |
| 2604 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2607 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 2608 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 2609 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2610 | unsigned ASTContext::getIntegerRank(Type *T) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2611 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2612 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
| 2613 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
| 2614 | |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 2615 | if (T->isSpecificBuiltinType(BuiltinType::WChar)) |
| 2616 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 2617 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2618 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 2619 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 2620 | |
| 2621 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 2622 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 2623 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2624 | // There are two things which impact the integer rank: the width, and |
| 2625 | // the ordering of builtins. The builtin ordering is encoded in the |
| 2626 | // bottom three bits; the width is encoded in the bits above that. |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 2627 | if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2628 | return FWIT->getWidth() << 3; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2629 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2630 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2631 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 2632 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2633 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2634 | case BuiltinType::Char_S: |
| 2635 | case BuiltinType::Char_U: |
| 2636 | case BuiltinType::SChar: |
| 2637 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2638 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2639 | case BuiltinType::Short: |
| 2640 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2641 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2642 | case BuiltinType::Int: |
| 2643 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2644 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2645 | case BuiltinType::Long: |
| 2646 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2647 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2648 | case BuiltinType::LongLong: |
| 2649 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2650 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2651 | case BuiltinType::Int128: |
| 2652 | case BuiltinType::UInt128: |
| 2653 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2654 | } |
| 2655 | } |
| 2656 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2657 | /// \brief Whether this is a promotable bitfield reference according |
| 2658 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 2659 | /// |
| 2660 | /// \returns the type this bit-field will promote to, or NULL if no |
| 2661 | /// promotion occurs. |
| 2662 | QualType ASTContext::isPromotableBitField(Expr *E) { |
| 2663 | FieldDecl *Field = E->getBitField(); |
| 2664 | if (!Field) |
| 2665 | return QualType(); |
| 2666 | |
| 2667 | QualType FT = Field->getType(); |
| 2668 | |
| 2669 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 2670 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 2671 | uint64_t IntSize = getTypeSize(IntTy); |
| 2672 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 2673 | // to the size of int, it gets promoted no matter what its type is. |
| 2674 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 2675 | if (BitWidth < IntSize) |
| 2676 | return IntTy; |
| 2677 | |
| 2678 | if (BitWidth == IntSize) |
| 2679 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 2680 | |
| 2681 | // Types bigger than int are not subject to promotions, and therefore act |
| 2682 | // like the base type. |
| 2683 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 2684 | // is ridiculous. |
| 2685 | return QualType(); |
| 2686 | } |
| 2687 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2688 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 2689 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 2690 | /// integer type. |
| 2691 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) { |
| 2692 | assert(!Promotable.isNull()); |
| 2693 | assert(Promotable->isPromotableIntegerType()); |
| 2694 | if (Promotable->isSignedIntegerType()) |
| 2695 | return IntTy; |
| 2696 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 2697 | uint64_t IntSize = getTypeSize(IntTy); |
| 2698 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 2699 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 2700 | } |
| 2701 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2703 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2704 | /// LHS < RHS, return -1. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2705 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2706 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 2707 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2708 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2709 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2710 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 2711 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2713 | unsigned LHSRank = getIntegerRank(LHSC); |
| 2714 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2715 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2716 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 2717 | if (LHSRank == RHSRank) return 0; |
| 2718 | return LHSRank > RHSRank ? 1 : -1; |
| 2719 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2720 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2721 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 2722 | if (LHSUnsigned) { |
| 2723 | // If the unsigned [LHS] type is larger, return it. |
| 2724 | if (LHSRank >= RHSRank) |
| 2725 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2726 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2727 | // If the signed type can represent all values of the unsigned type, it |
| 2728 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2729 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2730 | return -1; |
| 2731 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2732 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2733 | // If the unsigned [RHS] type is larger, return it. |
| 2734 | if (RHSRank >= LHSRank) |
| 2735 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2736 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2737 | // If the signed type can represent all values of the unsigned type, it |
| 2738 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2739 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2740 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2741 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2742 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2743 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2744 | QualType ASTContext::getCFConstantStringType() { |
| 2745 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2746 | CFConstantStringTypeDecl = |
| 2747 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2748 | &Idents.get("NSConstantString")); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2749 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2750 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2751 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2752 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2753 | // int flags; |
| 2754 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2755 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2756 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2757 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2758 | FieldTypes[3] = LongTy; |
| 2759 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2760 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2761 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2762 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2763 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2764 | FieldTypes[i], /*DInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2765 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2766 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2767 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | CFConstantStringTypeDecl->completeDefinition(*this); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2771 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2772 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2773 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 2774 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2775 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2776 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2777 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2778 | assert(Rec && "Invalid CFConstantStringType"); |
| 2779 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 2780 | } |
| 2781 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | QualType ASTContext::getObjCFastEnumerationStateType() { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2783 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2784 | ObjCFastEnumerationStateTypeDecl = |
| 2785 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2786 | &Idents.get("__objcFastEnumerationState")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2787 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2788 | QualType FieldTypes[] = { |
| 2789 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2790 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2791 | getPointerType(UnsignedLongTy), |
| 2792 | getConstantArrayType(UnsignedLongTy, |
| 2793 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 2794 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2795 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2796 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2797 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2798 | ObjCFastEnumerationStateTypeDecl, |
| 2799 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2800 | FieldTypes[i], /*DInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2801 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2802 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2803 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2804 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2805 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2806 | ObjCFastEnumerationStateTypeDecl->completeDefinition(*this); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2807 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2808 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2809 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 2810 | } |
| 2811 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2812 | QualType ASTContext::getBlockDescriptorType() { |
| 2813 | if (BlockDescriptorType) |
| 2814 | return getTagDeclType(BlockDescriptorType); |
| 2815 | |
| 2816 | RecordDecl *T; |
| 2817 | // FIXME: Needs the FlagAppleBlock bit. |
| 2818 | T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2819 | &Idents.get("__block_descriptor")); |
| 2820 | |
| 2821 | QualType FieldTypes[] = { |
| 2822 | UnsignedLongTy, |
| 2823 | UnsignedLongTy, |
| 2824 | }; |
| 2825 | |
| 2826 | const char *FieldNames[] = { |
| 2827 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2828 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2829 | }; |
| 2830 | |
| 2831 | for (size_t i = 0; i < 2; ++i) { |
| 2832 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2833 | T, |
| 2834 | SourceLocation(), |
| 2835 | &Idents.get(FieldNames[i]), |
| 2836 | FieldTypes[i], /*DInfo=*/0, |
| 2837 | /*BitWidth=*/0, |
| 2838 | /*Mutable=*/false); |
| 2839 | T->addDecl(Field); |
| 2840 | } |
| 2841 | |
| 2842 | T->completeDefinition(*this); |
| 2843 | |
| 2844 | BlockDescriptorType = T; |
| 2845 | |
| 2846 | return getTagDeclType(BlockDescriptorType); |
| 2847 | } |
| 2848 | |
| 2849 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 2850 | const RecordType *Rec = T->getAs<RecordType>(); |
| 2851 | assert(Rec && "Invalid BlockDescriptorType"); |
| 2852 | BlockDescriptorType = Rec->getDecl(); |
| 2853 | } |
| 2854 | |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2855 | QualType ASTContext::getBlockDescriptorExtendedType() { |
| 2856 | if (BlockDescriptorExtendedType) |
| 2857 | return getTagDeclType(BlockDescriptorExtendedType); |
| 2858 | |
| 2859 | RecordDecl *T; |
| 2860 | // FIXME: Needs the FlagAppleBlock bit. |
| 2861 | T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2862 | &Idents.get("__block_descriptor_withcopydispose")); |
| 2863 | |
| 2864 | QualType FieldTypes[] = { |
| 2865 | UnsignedLongTy, |
| 2866 | UnsignedLongTy, |
| 2867 | getPointerType(VoidPtrTy), |
| 2868 | getPointerType(VoidPtrTy) |
| 2869 | }; |
| 2870 | |
| 2871 | const char *FieldNames[] = { |
| 2872 | "reserved", |
| 2873 | "Size", |
| 2874 | "CopyFuncPtr", |
| 2875 | "DestroyFuncPtr" |
| 2876 | }; |
| 2877 | |
| 2878 | for (size_t i = 0; i < 4; ++i) { |
| 2879 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2880 | T, |
| 2881 | SourceLocation(), |
| 2882 | &Idents.get(FieldNames[i]), |
| 2883 | FieldTypes[i], /*DInfo=*/0, |
| 2884 | /*BitWidth=*/0, |
| 2885 | /*Mutable=*/false); |
| 2886 | T->addDecl(Field); |
| 2887 | } |
| 2888 | |
| 2889 | T->completeDefinition(*this); |
| 2890 | |
| 2891 | BlockDescriptorExtendedType = T; |
| 2892 | |
| 2893 | return getTagDeclType(BlockDescriptorExtendedType); |
| 2894 | } |
| 2895 | |
| 2896 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 2897 | const RecordType *Rec = T->getAs<RecordType>(); |
| 2898 | assert(Rec && "Invalid BlockDescriptorType"); |
| 2899 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 2900 | } |
| 2901 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2902 | bool ASTContext::BlockRequiresCopying(QualType Ty) { |
| 2903 | if (Ty->isBlockPointerType()) |
| 2904 | return true; |
| 2905 | if (isObjCNSObjectType(Ty)) |
| 2906 | return true; |
| 2907 | if (Ty->isObjCObjectPointerType()) |
| 2908 | return true; |
| 2909 | return false; |
| 2910 | } |
| 2911 | |
| 2912 | QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) { |
| 2913 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2914 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2915 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2916 | // unsigned int __flags; |
| 2917 | // unsigned int __size; |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 2918 | // void *__copy_helper; // as needed |
| 2919 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2920 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2921 | // } * |
| 2922 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2923 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 2924 | |
| 2925 | // FIXME: Move up |
Fariborz Jahanian | 4d0d85c | 2009-10-24 00:16:42 +0000 | [diff] [blame] | 2926 | static unsigned int UniqueBlockByRefTypeID = 0; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 2927 | llvm::SmallString<36> Name; |
| 2928 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 2929 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2930 | RecordDecl *T; |
| 2931 | T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 2932 | &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2933 | T->startDefinition(); |
| 2934 | QualType Int32Ty = IntTy; |
| 2935 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 2936 | QualType FieldTypes[] = { |
| 2937 | getPointerType(VoidPtrTy), |
| 2938 | getPointerType(getTagDeclType(T)), |
| 2939 | Int32Ty, |
| 2940 | Int32Ty, |
| 2941 | getPointerType(VoidPtrTy), |
| 2942 | getPointerType(VoidPtrTy), |
| 2943 | Ty |
| 2944 | }; |
| 2945 | |
| 2946 | const char *FieldNames[] = { |
| 2947 | "__isa", |
| 2948 | "__forwarding", |
| 2949 | "__flags", |
| 2950 | "__size", |
| 2951 | "__copy_helper", |
| 2952 | "__destroy_helper", |
| 2953 | DeclName, |
| 2954 | }; |
| 2955 | |
| 2956 | for (size_t i = 0; i < 7; ++i) { |
| 2957 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 2958 | continue; |
| 2959 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 2960 | &Idents.get(FieldNames[i]), |
| 2961 | FieldTypes[i], /*DInfo=*/0, |
| 2962 | /*BitWidth=*/0, /*Mutable=*/false); |
| 2963 | T->addDecl(Field); |
| 2964 | } |
| 2965 | |
| 2966 | T->completeDefinition(*this); |
| 2967 | |
| 2968 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | |
| 2972 | QualType ASTContext::getBlockParmType( |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2973 | bool BlockHasCopyDispose, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2974 | llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2975 | // FIXME: Move up |
Fariborz Jahanian | 4d0d85c | 2009-10-24 00:16:42 +0000 | [diff] [blame] | 2976 | static unsigned int UniqueBlockParmTypeID = 0; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 2977 | llvm::SmallString<36> Name; |
| 2978 | llvm::raw_svector_ostream(Name) << "__block_literal_" |
| 2979 | << ++UniqueBlockParmTypeID; |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2980 | RecordDecl *T; |
| 2981 | T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 2982 | &Idents.get(Name.str())); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2983 | QualType FieldTypes[] = { |
| 2984 | getPointerType(VoidPtrTy), |
| 2985 | IntTy, |
| 2986 | IntTy, |
| 2987 | getPointerType(VoidPtrTy), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2988 | (BlockHasCopyDispose ? |
| 2989 | getPointerType(getBlockDescriptorExtendedType()) : |
| 2990 | getPointerType(getBlockDescriptorType())) |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2991 | }; |
| 2992 | |
| 2993 | const char *FieldNames[] = { |
| 2994 | "__isa", |
| 2995 | "__flags", |
| 2996 | "__reserved", |
| 2997 | "__FuncPtr", |
| 2998 | "__descriptor" |
| 2999 | }; |
| 3000 | |
| 3001 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3002 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3003 | &Idents.get(FieldNames[i]), |
| 3004 | FieldTypes[i], /*DInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3005 | /*BitWidth=*/0, /*Mutable=*/false); |
| 3006 | T->addDecl(Field); |
| 3007 | } |
| 3008 | |
| 3009 | for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) { |
| 3010 | const Expr *E = BlockDeclRefDecls[i]; |
| 3011 | const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 3012 | clang::IdentifierInfo *Name = 0; |
| 3013 | if (BDRE) { |
| 3014 | const ValueDecl *D = BDRE->getDecl(); |
| 3015 | Name = &Idents.get(D->getName()); |
| 3016 | } |
| 3017 | QualType FieldType = E->getType(); |
| 3018 | |
| 3019 | if (BDRE && BDRE->isByRef()) |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3020 | FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(), |
| 3021 | FieldType); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3022 | |
| 3023 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3024 | Name, FieldType, /*DInfo=*/0, |
| 3025 | /*BitWidth=*/0, /*Mutable=*/false); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3026 | T->addDecl(Field); |
| 3027 | } |
| 3028 | |
| 3029 | T->completeDefinition(*this); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3030 | |
| 3031 | return getPointerType(getTagDeclType(T)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3034 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3035 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3036 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3037 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3038 | } |
| 3039 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3040 | // This returns true if a type has been typedefed to BOOL: |
| 3041 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3042 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3043 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3044 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3045 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3046 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3047 | return false; |
| 3048 | } |
| 3049 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3050 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3051 | /// purpose. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3052 | int ASTContext::getObjCEncodingTypeSize(QualType type) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3053 | uint64_t sz = getTypeSize(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3054 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3055 | // Make all integer and enum types at least as large as an int |
| 3056 | if (sz > 0 && type->isIntegralType()) |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3057 | sz = std::max(sz, getTypeSize(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3058 | // Treat arrays as pointers, since that's how they're passed in. |
| 3059 | else if (type->isArrayType()) |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3060 | sz = getTypeSize(VoidPtrTy); |
| 3061 | return sz / getTypeSize(CharTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3064 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3065 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3066 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3067 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3068 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3069 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3070 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3071 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3072 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3073 | // Compute size of all parameters. |
| 3074 | // Start with computing size of a pointer in number of bytes. |
| 3075 | // FIXME: There might(should) be a better way of doing this computation! |
| 3076 | SourceLocation Loc; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3077 | int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3078 | // The first two arguments (self and _cmd) are pointers; account for |
| 3079 | // their size. |
| 3080 | int ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3081 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3082 | E = Decl->param_end(); PI != E; ++PI) { |
| 3083 | QualType PType = (*PI)->getType(); |
| 3084 | int sz = getObjCEncodingTypeSize(PType); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3085 | assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3086 | ParmOffset += sz; |
| 3087 | } |
| 3088 | S += llvm::utostr(ParmOffset); |
| 3089 | S += "@0:"; |
| 3090 | S += llvm::utostr(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3091 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3092 | // Argument types. |
| 3093 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3094 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3095 | E = Decl->param_end(); PI != E; ++PI) { |
| 3096 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3097 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3098 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3099 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3100 | // Use array's original type only if it has known number of |
| 3101 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3102 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3103 | PType = PVDecl->getType(); |
| 3104 | } else if (PType->isFunctionType()) |
| 3105 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3106 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3107 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3108 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3109 | getObjCEncodingForType(PType, S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3110 | S += llvm::utostr(ParmOffset); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3111 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3112 | } |
| 3113 | } |
| 3114 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3115 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3116 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3117 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3118 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3119 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3120 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3121 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3122 | /// encoded as single characters, followed by an identifier. Property types |
| 3123 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3124 | /// these attributes are defined by the following enumeration: |
| 3125 | /// @code |
| 3126 | /// enum PropertyAttributes { |
| 3127 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3128 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3129 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3130 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3131 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3132 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3133 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3134 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3135 | /// kPropertyWeak = 'W' // 'weak' property |
| 3136 | /// kPropertyStrong = 'P' // property GC'able |
| 3137 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3138 | /// }; |
| 3139 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3140 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3141 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3142 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3143 | // Collect information from the property implementation decl(s). |
| 3144 | bool Dynamic = false; |
| 3145 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3146 | |
| 3147 | // FIXME: Duplicated code due to poor abstraction. |
| 3148 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3149 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3150 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3151 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3152 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3153 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3154 | ObjCPropertyImplDecl *PID = *i; |
| 3155 | if (PID->getPropertyDecl() == PD) { |
| 3156 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3157 | Dynamic = true; |
| 3158 | } else { |
| 3159 | SynthesizePID = PID; |
| 3160 | } |
| 3161 | } |
| 3162 | } |
| 3163 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3164 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3165 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3166 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3167 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3168 | ObjCPropertyImplDecl *PID = *i; |
| 3169 | if (PID->getPropertyDecl() == PD) { |
| 3170 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3171 | Dynamic = true; |
| 3172 | } else { |
| 3173 | SynthesizePID = PID; |
| 3174 | } |
| 3175 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3176 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3177 | } |
| 3178 | } |
| 3179 | |
| 3180 | // FIXME: This is not very efficient. |
| 3181 | S = "T"; |
| 3182 | |
| 3183 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3184 | // GCC has some special rules regarding encoding of properties which |
| 3185 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3186 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3187 | true /* outermost type */, |
| 3188 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3189 | |
| 3190 | if (PD->isReadOnly()) { |
| 3191 | S += ",R"; |
| 3192 | } else { |
| 3193 | switch (PD->getSetterKind()) { |
| 3194 | case ObjCPropertyDecl::Assign: break; |
| 3195 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3196 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3197 | } |
| 3198 | } |
| 3199 | |
| 3200 | // It really isn't clear at all what this means, since properties |
| 3201 | // are "dynamic by default". |
| 3202 | if (Dynamic) |
| 3203 | S += ",D"; |
| 3204 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3205 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3206 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3207 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3208 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3209 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3210 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3214 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3215 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
| 3218 | if (SynthesizePID) { |
| 3219 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3220 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3221 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
| 3224 | // FIXME: OBJCGC: weak & strong |
| 3225 | } |
| 3226 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3227 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3228 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3229 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3230 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3231 | /// |
| 3232 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3233 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3234 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3235 | if (BT->getKind() == BuiltinType::ULong && |
| 3236 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3237 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3238 | else |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3239 | if (BT->getKind() == BuiltinType::Long && |
| 3240 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3241 | PointeeTy = IntTy; |
| 3242 | } |
| 3243 | } |
| 3244 | } |
| 3245 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3246 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3247 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3248 | // We follow the behavior of gcc, expanding structures which are |
| 3249 | // directly pointed to, and expanding embedded structures. Note that |
| 3250 | // these rules are sufficient to prevent recursive encoding of the |
| 3251 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3252 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3253 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3256 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3257 | const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3258 | const Expr *E = FD->getBitWidth(); |
| 3259 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 3260 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3261 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3262 | S += 'b'; |
| 3263 | S += llvm::utostr(N); |
| 3264 | } |
| 3265 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3266 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3267 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 3268 | bool ExpandPointedToStructures, |
| 3269 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3270 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3271 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3272 | bool EncodingProperty) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3273 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3274 | if (FD && FD->isBitField()) |
| 3275 | return EncodeBitField(this, S, FD); |
| 3276 | char encoding; |
| 3277 | switch (BT->getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3278 | default: assert(0 && "Unhandled builtin type kind"); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3279 | case BuiltinType::Void: encoding = 'v'; break; |
| 3280 | case BuiltinType::Bool: encoding = 'B'; break; |
| 3281 | case BuiltinType::Char_U: |
| 3282 | case BuiltinType::UChar: encoding = 'C'; break; |
| 3283 | case BuiltinType::UShort: encoding = 'S'; break; |
| 3284 | case BuiltinType::UInt: encoding = 'I'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | case BuiltinType::ULong: |
| 3286 | encoding = |
| 3287 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
Fariborz Jahanian | 72696e1 | 2009-02-11 22:31:45 +0000 | [diff] [blame] | 3288 | break; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3289 | case BuiltinType::UInt128: encoding = 'T'; break; |
| 3290 | case BuiltinType::ULongLong: encoding = 'Q'; break; |
| 3291 | case BuiltinType::Char_S: |
| 3292 | case BuiltinType::SChar: encoding = 'c'; break; |
| 3293 | case BuiltinType::Short: encoding = 's'; break; |
| 3294 | case BuiltinType::Int: encoding = 'i'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3295 | case BuiltinType::Long: |
| 3296 | encoding = |
| 3297 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3298 | break; |
| 3299 | case BuiltinType::LongLong: encoding = 'q'; break; |
| 3300 | case BuiltinType::Int128: encoding = 't'; break; |
| 3301 | case BuiltinType::Float: encoding = 'f'; break; |
| 3302 | case BuiltinType::Double: encoding = 'd'; break; |
| 3303 | case BuiltinType::LongDouble: encoding = 'd'; break; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3304 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3305 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3306 | S += encoding; |
| 3307 | return; |
| 3308 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3309 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3310 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3311 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3312 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3313 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3314 | return; |
| 3315 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3316 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3317 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3318 | QualType PointeeTy = PT->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3319 | bool isReadOnly = false; |
| 3320 | // For historical/compatibility reasons, the read-only qualifier of the |
| 3321 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 3322 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3323 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3324 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3325 | if (OutermostType && T.isConstQualified()) { |
| 3326 | isReadOnly = true; |
| 3327 | S += 'r'; |
| 3328 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3329 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3330 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3331 | while (P->getAs<PointerType>()) |
| 3332 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3333 | if (P.isConstQualified()) { |
| 3334 | isReadOnly = true; |
| 3335 | S += 'r'; |
| 3336 | } |
| 3337 | } |
| 3338 | if (isReadOnly) { |
| 3339 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 3340 | // combinations need to be rearranged. |
| 3341 | // Rewrite "in const" from "nr" to "rn" |
| 3342 | const char * s = S.c_str(); |
| 3343 | int len = S.length(); |
| 3344 | if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') { |
| 3345 | std::string replace = "rn"; |
| 3346 | S.replace(S.end()-2, S.end(), replace); |
| 3347 | } |
| 3348 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3349 | if (isObjCSelType(PointeeTy)) { |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 3350 | S += ':'; |
| 3351 | return; |
Fariborz Jahanian | c2939bc | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 3352 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3353 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3354 | if (PointeeTy->isCharType()) { |
| 3355 | // char pointer types should be encoded as '*' unless it is a |
| 3356 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3357 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3358 | S += '*'; |
| 3359 | return; |
| 3360 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3361 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 3362 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 3363 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 3364 | S += '#'; |
| 3365 | return; |
| 3366 | } |
| 3367 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 3368 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 3369 | S += '@'; |
| 3370 | return; |
| 3371 | } |
| 3372 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3373 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3374 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3375 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 3376 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3377 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3378 | NULL); |
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 (const ArrayType *AT = |
| 3383 | // Ignore type qualifiers etc. |
| 3384 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3385 | if (isa<IncompleteArrayType>(AT)) { |
| 3386 | // Incomplete arrays are encoded as a pointer to the array element. |
| 3387 | S += '^'; |
| 3388 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3389 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3390 | false, ExpandStructures, FD); |
| 3391 | } else { |
| 3392 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3393 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3394 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 3395 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 3396 | else { |
| 3397 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 3398 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 3399 | S += '0'; |
| 3400 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3401 | |
| 3402 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3403 | false, ExpandStructures, FD); |
| 3404 | S += ']'; |
| 3405 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3406 | return; |
| 3407 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3408 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3409 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 3410 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3411 | return; |
| 3412 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3414 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3415 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3416 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3417 | // Anonymous structures print as '?' |
| 3418 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 3419 | S += II->getName(); |
| 3420 | } else { |
| 3421 | S += '?'; |
| 3422 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3423 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3424 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3425 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 3426 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3427 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3428 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3429 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3430 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3431 | S += '"'; |
| 3432 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3433 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3434 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3435 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3436 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3437 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3438 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3439 | QualType qt = Field->getType(); |
| 3440 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3441 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3442 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3443 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3444 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 3445 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3446 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3447 | return; |
| 3448 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3450 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3451 | if (FD && FD->isBitField()) |
| 3452 | EncodeBitField(this, S, FD); |
| 3453 | else |
| 3454 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3455 | return; |
| 3456 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3457 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3458 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 3459 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3460 | return; |
| 3461 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3462 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3463 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3464 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3465 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3466 | S += '{'; |
| 3467 | const IdentifierInfo *II = OI->getIdentifier(); |
| 3468 | S += II->getName(); |
| 3469 | S += '='; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3470 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3471 | CollectObjCIvars(OI, RecFields); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3472 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3473 | if (RecFields[i]->isBitField()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3474 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3475 | RecFields[i]); |
| 3476 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3477 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3478 | FD); |
| 3479 | } |
| 3480 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3481 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3482 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3483 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3484 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3485 | if (OPT->isObjCIdType()) { |
| 3486 | S += '@'; |
| 3487 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3488 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3489 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3490 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 3491 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 3492 | // Since this is a binary compatibility issue, need to consult with runtime |
| 3493 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3494 | S += '#'; |
| 3495 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3496 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3497 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3498 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3499 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3500 | ExpandPointedToStructures, |
| 3501 | ExpandStructures, FD); |
| 3502 | if (FD || EncodingProperty) { |
| 3503 | // Note that we do extended encoding of protocol qualifer list |
| 3504 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3505 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3506 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3507 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3508 | S += '<'; |
| 3509 | S += (*I)->getNameAsString(); |
| 3510 | S += '>'; |
| 3511 | } |
| 3512 | S += '"'; |
| 3513 | } |
| 3514 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3515 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3516 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3517 | QualType PointeeTy = OPT->getPointeeType(); |
| 3518 | if (!EncodingProperty && |
| 3519 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 3520 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3521 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3522 | // {...}; |
| 3523 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3524 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 3525 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3526 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3527 | return; |
| 3528 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3529 | |
| 3530 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3531 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3532 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3533 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3534 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3535 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3536 | S += '<'; |
| 3537 | S += (*I)->getNameAsString(); |
| 3538 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3539 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3540 | S += '"'; |
| 3541 | } |
| 3542 | return; |
| 3543 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3544 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3545 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3548 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3549 | std::string& S) const { |
| 3550 | if (QT & Decl::OBJC_TQ_In) |
| 3551 | S += 'n'; |
| 3552 | if (QT & Decl::OBJC_TQ_Inout) |
| 3553 | S += 'N'; |
| 3554 | if (QT & Decl::OBJC_TQ_Out) |
| 3555 | S += 'o'; |
| 3556 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 3557 | S += 'O'; |
| 3558 | if (QT & Decl::OBJC_TQ_Byref) |
| 3559 | S += 'R'; |
| 3560 | if (QT & Decl::OBJC_TQ_Oneway) |
| 3561 | S += 'V'; |
| 3562 | } |
| 3563 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3564 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3565 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3566 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3567 | BuiltinVaListType = T; |
| 3568 | } |
| 3569 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3570 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3571 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 3572 | } |
| 3573 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3574 | void ASTContext::setObjCSelType(QualType T) { |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3575 | ObjCSelType = T; |
| 3576 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3577 | const TypedefType *TT = T->getAs<TypedefType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3578 | if (!TT) |
| 3579 | return; |
| 3580 | TypedefDecl *TD = TT->getDecl(); |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3581 | |
| 3582 | // typedef struct objc_selector *SEL; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3583 | const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>(); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 3584 | if (!ptr) |
| 3585 | return; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3586 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 3587 | if (!rec) |
| 3588 | return; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3589 | SelStructType = rec; |
| 3590 | } |
| 3591 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3592 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3593 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3596 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3597 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 3598 | } |
| 3599 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3600 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3601 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3602 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3603 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3604 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3605 | } |
| 3606 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3607 | /// \brief Retrieve the template name that represents a qualified |
| 3608 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3609 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3610 | bool TemplateKeyword, |
| 3611 | TemplateDecl *Template) { |
| 3612 | llvm::FoldingSetNodeID ID; |
| 3613 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 3614 | |
| 3615 | void *InsertPos = 0; |
| 3616 | QualifiedTemplateName *QTN = |
| 3617 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3618 | if (!QTN) { |
| 3619 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 3620 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 3621 | } |
| 3622 | |
| 3623 | return TemplateName(QTN); |
| 3624 | } |
| 3625 | |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 3626 | /// \brief Retrieve the template name that represents a qualified |
| 3627 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3628 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 3629 | bool TemplateKeyword, |
| 3630 | OverloadedFunctionDecl *Template) { |
| 3631 | llvm::FoldingSetNodeID ID; |
| 3632 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3633 | |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 3634 | void *InsertPos = 0; |
| 3635 | QualifiedTemplateName *QTN = |
| 3636 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3637 | if (!QTN) { |
| 3638 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 3639 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 3640 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3641 | |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 3642 | return TemplateName(QTN); |
| 3643 | } |
| 3644 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3645 | /// \brief Retrieve the template name that represents a dependent |
| 3646 | /// template name such as \c MetaFun::template apply. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3647 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3648 | const IdentifierInfo *Name) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3649 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 3650 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3651 | |
| 3652 | llvm::FoldingSetNodeID ID; |
| 3653 | DependentTemplateName::Profile(ID, NNS, Name); |
| 3654 | |
| 3655 | void *InsertPos = 0; |
| 3656 | DependentTemplateName *QTN = |
| 3657 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3658 | |
| 3659 | if (QTN) |
| 3660 | return TemplateName(QTN); |
| 3661 | |
| 3662 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 3663 | if (CanonNNS == NNS) { |
| 3664 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 3665 | } else { |
| 3666 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 3667 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
| 3668 | } |
| 3669 | |
| 3670 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3671 | return TemplateName(QTN); |
| 3672 | } |
| 3673 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3674 | /// \brief Retrieve the template name that represents a dependent |
| 3675 | /// template name such as \c MetaFun::template operator+. |
| 3676 | TemplateName |
| 3677 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 3678 | OverloadedOperatorKind Operator) { |
| 3679 | assert((!NNS || NNS->isDependent()) && |
| 3680 | "Nested name specifier must be dependent"); |
| 3681 | |
| 3682 | llvm::FoldingSetNodeID ID; |
| 3683 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 3684 | |
| 3685 | void *InsertPos = 0; |
| 3686 | DependentTemplateName *QTN = |
| 3687 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3688 | |
| 3689 | if (QTN) |
| 3690 | return TemplateName(QTN); |
| 3691 | |
| 3692 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 3693 | if (CanonNNS == NNS) { |
| 3694 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 3695 | } else { |
| 3696 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 3697 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
| 3698 | } |
| 3699 | |
| 3700 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3701 | return TemplateName(QTN); |
| 3702 | } |
| 3703 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3704 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 3705 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 3706 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3707 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3708 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3709 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3710 | case TargetInfo::SignedShort: return ShortTy; |
| 3711 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 3712 | case TargetInfo::SignedInt: return IntTy; |
| 3713 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 3714 | case TargetInfo::SignedLong: return LongTy; |
| 3715 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 3716 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 3717 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 3718 | } |
| 3719 | |
| 3720 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3721 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3722 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 3723 | |
| 3724 | //===----------------------------------------------------------------------===// |
| 3725 | // Type Predicates. |
| 3726 | //===----------------------------------------------------------------------===// |
| 3727 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3728 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 3729 | /// NSObject attribute on a c-style pointer type. |
| 3730 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3731 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3732 | /// |
| 3733 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 3734 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 3735 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3736 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3737 | return true; |
| 3738 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3739 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3740 | } |
| 3741 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3742 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 3743 | /// garbage collection attribute. |
| 3744 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3745 | Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
| 3746 | Qualifiers::GC GCAttrs = Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3747 | if (getLangOptions().ObjC1 && |
| 3748 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3749 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3750 | // Default behavious under objective-c's gc is for objective-c pointers |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3751 | // (or pointers to them) be treated as though they were declared |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3752 | // as __strong. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3753 | if (GCAttrs == Qualifiers::GCNone) { |
Fariborz Jahanian | 75212ee | 2009-09-10 23:38:45 +0000 | [diff] [blame] | 3754 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3755 | GCAttrs = Qualifiers::Strong; |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3756 | else if (Ty->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3757 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3758 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 3759 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 3760 | // set on them. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3761 | else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3762 | return Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3763 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3764 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3765 | } |
| 3766 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3767 | //===----------------------------------------------------------------------===// |
| 3768 | // Type Compatibility Testing |
| 3769 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 3770 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3771 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3772 | /// compatible. |
| 3773 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 3774 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3775 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3776 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3777 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3780 | //===----------------------------------------------------------------------===// |
| 3781 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 3782 | //===----------------------------------------------------------------------===// |
| 3783 | |
| 3784 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 3785 | /// inheritance hierarchy of 'rProto'. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3786 | bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 3787 | ObjCProtocolDecl *rProto) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3788 | if (lProto == rProto) |
| 3789 | return true; |
| 3790 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 3791 | E = rProto->protocol_end(); PI != E; ++PI) |
| 3792 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 3793 | return true; |
| 3794 | return false; |
| 3795 | } |
| 3796 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3797 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 3798 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 3799 | /// otherwise. |
| 3800 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 3801 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 3802 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 3803 | return false; |
| 3804 | } |
| 3805 | |
| 3806 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 3807 | /// ObjCQualifiedIDType. |
| 3808 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 3809 | bool compare) { |
| 3810 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3811 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3812 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 3813 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3814 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3815 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 3816 | return true; |
| 3817 | |
| 3818 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3819 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3820 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3821 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3822 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3823 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3824 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3825 | // make sure we check the class hierarchy. |
| 3826 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 3827 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3828 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3829 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3830 | // see if static class implements all of id's protocols, directly or |
| 3831 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3832 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3833 | return false; |
| 3834 | } |
| 3835 | } |
| 3836 | // If there are no qualifiers and no interface, we have an 'id'. |
| 3837 | return true; |
| 3838 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3839 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3840 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3841 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3842 | ObjCProtocolDecl *lhsProto = *I; |
| 3843 | bool match = false; |
| 3844 | |
| 3845 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3846 | // see if static class implements all of id's protocols, directly or |
| 3847 | // through its super class and categories. |
| 3848 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 3849 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 3850 | ObjCProtocolDecl *rhsProto = *J; |
| 3851 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 3852 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 3853 | match = true; |
| 3854 | break; |
| 3855 | } |
| 3856 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3857 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3858 | // make sure we check the class hierarchy. |
| 3859 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 3860 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3861 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3862 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3863 | // see if static class implements all of id's protocols, directly or |
| 3864 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3865 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3866 | match = true; |
| 3867 | break; |
| 3868 | } |
| 3869 | } |
| 3870 | } |
| 3871 | if (!match) |
| 3872 | return false; |
| 3873 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3874 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3875 | return true; |
| 3876 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3877 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3878 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 3879 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 3880 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3882 | lhs->getAsObjCInterfacePointerType()) { |
| 3883 | if (lhsOPT->qual_empty()) { |
| 3884 | bool match = false; |
| 3885 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 3886 | for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), |
| 3887 | E = rhsQID->qual_end(); I != E; ++I) { |
| 3888 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3889 | // see if static class implements all of id's protocols, directly or |
| 3890 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3891 | if (lhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3892 | match = true; |
| 3893 | break; |
| 3894 | } |
| 3895 | } |
| 3896 | if (!match) |
| 3897 | return false; |
| 3898 | } |
| 3899 | return true; |
| 3900 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3901 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3902 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 3903 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 3904 | ObjCProtocolDecl *lhsProto = *I; |
| 3905 | bool match = false; |
| 3906 | |
| 3907 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3908 | // see if static class implements all of id's protocols, directly or |
| 3909 | // through its super class and categories. |
| 3910 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 3911 | E = rhsQID->qual_end(); J != E; ++J) { |
| 3912 | ObjCProtocolDecl *rhsProto = *J; |
| 3913 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 3914 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 3915 | match = true; |
| 3916 | break; |
| 3917 | } |
| 3918 | } |
| 3919 | if (!match) |
| 3920 | return false; |
| 3921 | } |
| 3922 | return true; |
| 3923 | } |
| 3924 | return false; |
| 3925 | } |
| 3926 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3927 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3928 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 3929 | /// protocol qualifiers on the LHS or RHS. |
| 3930 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3931 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 3932 | const ObjCObjectPointerType *RHSOPT) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3933 | // If either type represents the built-in 'id' or 'Class' types, return true. |
| 3934 | if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3935 | return true; |
| 3936 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3937 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3938 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 3939 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3940 | false); |
| 3941 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3942 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 3943 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3944 | if (LHS && RHS) // We have 2 user-defined types. |
| 3945 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3946 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3947 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 3950 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 3951 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 3952 | /// It is used to build composite qualifier list of the composite type of |
| 3953 | /// the conditional expression involving two objective-c pointer objects. |
| 3954 | static |
| 3955 | void getIntersectionOfProtocols(ASTContext &Context, |
| 3956 | const ObjCObjectPointerType *LHSOPT, |
| 3957 | const ObjCObjectPointerType *RHSOPT, |
| 3958 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 3959 | |
| 3960 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 3961 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 3962 | |
| 3963 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 3964 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 3965 | if (LHSNumProtocols > 0) |
| 3966 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 3967 | else { |
| 3968 | llvm::SmallVector<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 3969 | Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols); |
| 3970 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 3971 | LHSInheritedProtocols.end()); |
| 3972 | } |
| 3973 | |
| 3974 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 3975 | if (RHSNumProtocols > 0) { |
| 3976 | ObjCProtocolDecl **RHSProtocols = (ObjCProtocolDecl **)RHS->qual_begin(); |
| 3977 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 3978 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 3979 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 3980 | } |
| 3981 | else { |
| 3982 | llvm::SmallVector<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
| 3983 | Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols); |
| 3984 | // FIXME. This may cause duplication of protocols in the list, but should |
| 3985 | // be harmless. |
| 3986 | for (unsigned i = 0, len = RHSInheritedProtocols.size(); i < len; ++i) |
| 3987 | if (InheritedProtocolSet.count(RHSInheritedProtocols[i])) |
| 3988 | IntersectionOfProtocols.push_back(RHSInheritedProtocols[i]); |
| 3989 | } |
| 3990 | } |
| 3991 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 3992 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 3993 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 3994 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 3995 | /// warning is issued. So, its invokation is extremely rare. |
| 3996 | QualType ASTContext::areCommonBaseCompatible( |
| 3997 | const ObjCObjectPointerType *LHSOPT, |
| 3998 | const ObjCObjectPointerType *RHSOPT) { |
| 3999 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4000 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4001 | if (!LHS || !RHS) |
| 4002 | return QualType(); |
| 4003 | |
| 4004 | while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) { |
| 4005 | QualType LHSTy = getObjCInterfaceType(LHSIDecl); |
| 4006 | LHS = LHSTy->getAs<ObjCInterfaceType>(); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4007 | if (canAssignObjCInterfaces(LHS, RHS)) { |
| 4008 | llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols; |
| 4009 | getIntersectionOfProtocols(*this, |
| 4010 | LHSOPT, RHSOPT, IntersectionOfProtocols); |
| 4011 | if (IntersectionOfProtocols.empty()) |
| 4012 | LHSTy = getObjCObjectPointerType(LHSTy); |
| 4013 | else |
| 4014 | LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0], |
| 4015 | IntersectionOfProtocols.size()); |
| 4016 | return LHSTy; |
| 4017 | } |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | return QualType(); |
| 4021 | } |
| 4022 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4023 | bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, |
| 4024 | const ObjCInterfaceType *RHS) { |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4025 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4026 | // the LHS. |
| 4027 | if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4028 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4029 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4030 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4031 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4032 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4033 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4034 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4035 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4036 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4037 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4038 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4039 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4040 | for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4041 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4042 | LHSPI != LHSPE; LHSPI++) { |
| 4043 | bool RHSImplementsProtocol = false; |
| 4044 | |
| 4045 | // If the RHS doesn't implement the protocol on the left, the types |
| 4046 | // are incompatible. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4047 | for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4048 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4049 | RHSPI != RHSPE; RHSPI++) { |
| 4050 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4051 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4052 | break; |
| 4053 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4054 | } |
| 4055 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4056 | if (!RHSImplementsProtocol) |
| 4057 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4058 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4059 | // The RHS implements all protocols listed on the LHS. |
| 4060 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4061 | } |
| 4062 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4063 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4064 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4065 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4066 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4067 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4068 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4069 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4070 | |
| 4071 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 4072 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4075 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4076 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4077 | /// 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] | 4078 | /// same. See 6.7.[2,3,5] for additional rules. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4079 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { |
| 4080 | return !mergeTypes(LHS, RHS).isNull(); |
| 4081 | } |
| 4082 | |
| 4083 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4084 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 4085 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4086 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 4087 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4088 | bool allLTypes = true; |
| 4089 | bool allRTypes = true; |
| 4090 | |
| 4091 | // Check return type |
| 4092 | QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); |
| 4093 | if (retType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4094 | if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) |
| 4095 | allLTypes = false; |
| 4096 | if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) |
| 4097 | allRTypes = false; |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 4098 | // FIXME: double check this |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4099 | bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr(); |
| 4100 | if (NoReturn != lbase->getNoReturnAttr()) |
| 4101 | allLTypes = false; |
| 4102 | if (NoReturn != rbase->getNoReturnAttr()) |
| 4103 | allRTypes = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4104 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4105 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4106 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 4107 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4108 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 4109 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 4110 | |
| 4111 | // Compatible functions must have the same number of arguments |
| 4112 | if (lproto_nargs != rproto_nargs) |
| 4113 | return QualType(); |
| 4114 | |
| 4115 | // Variadic and non-variadic functions aren't compatible |
| 4116 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 4117 | return QualType(); |
| 4118 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4119 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 4120 | return QualType(); |
| 4121 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4122 | // Check argument compatibility |
| 4123 | llvm::SmallVector<QualType, 10> types; |
| 4124 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 4125 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 4126 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
| 4127 | QualType argtype = mergeTypes(largtype, rargtype); |
| 4128 | if (argtype.isNull()) return QualType(); |
| 4129 | types.push_back(argtype); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4130 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 4131 | allLTypes = false; |
| 4132 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 4133 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4134 | } |
| 4135 | if (allLTypes) return lhs; |
| 4136 | if (allRTypes) return rhs; |
| 4137 | return getFunctionType(retType, types.begin(), types.size(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4138 | lproto->isVariadic(), lproto->getTypeQuals(), |
| 4139 | NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
| 4142 | if (lproto) allRTypes = false; |
| 4143 | if (rproto) allLTypes = false; |
| 4144 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4145 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4146 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4147 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4148 | if (proto->isVariadic()) return QualType(); |
| 4149 | // Check that the types are compatible with the types that |
| 4150 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 4151 | // The only types actually affected are promotable integer |
| 4152 | // types and floats, which would be passed as a different |
| 4153 | // type depending on whether the prototype is visible. |
| 4154 | unsigned proto_nargs = proto->getNumArgs(); |
| 4155 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 4156 | QualType argTy = proto->getArgType(i); |
| 4157 | if (argTy->isPromotableIntegerType() || |
| 4158 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 4159 | return QualType(); |
| 4160 | } |
| 4161 | |
| 4162 | if (allLTypes) return lhs; |
| 4163 | if (allRTypes) return rhs; |
| 4164 | return getFunctionType(retType, proto->arg_type_begin(), |
Mike Stump | 2d3c191 | 2009-07-27 00:44:23 +0000 | [diff] [blame] | 4165 | proto->getNumArgs(), proto->isVariadic(), |
| 4166 | proto->getTypeQuals(), NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | if (allLTypes) return lhs; |
| 4170 | if (allRTypes) return rhs; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4171 | return getFunctionNoProtoType(retType, NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 4175 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 4176 | // type is adjusted to "T" prior to any further analysis, the expression |
| 4177 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4178 | // expression is an lvalue unless the reference is an rvalue reference and |
| 4179 | // the expression is a function call (possibly inside parentheses). |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4180 | // FIXME: C++ shouldn't be going through here! The rules are different |
| 4181 | // enough that they should be handled separately. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4182 | // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really* |
| 4183 | // shouldn't be going through here! |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4184 | if (const ReferenceType *RT = LHS->getAs<ReferenceType>()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 4185 | LHS = RT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4186 | if (const ReferenceType *RT = RHS->getAs<ReferenceType>()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 4187 | RHS = RT->getPointeeType(); |
Chris Lattner | f3692dc | 2008-04-07 05:37:56 +0000 | [diff] [blame] | 4188 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4189 | QualType LHSCan = getCanonicalType(LHS), |
| 4190 | RHSCan = getCanonicalType(RHS); |
| 4191 | |
| 4192 | // If two types are identical, they are compatible. |
| 4193 | if (LHSCan == RHSCan) |
| 4194 | return LHS; |
| 4195 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4196 | // If the qualifiers are different, the types aren't compatible... mostly. |
| 4197 | Qualifiers LQuals = LHSCan.getQualifiers(); |
| 4198 | Qualifiers RQuals = RHSCan.getQualifiers(); |
| 4199 | if (LQuals != RQuals) { |
| 4200 | // If any of these qualifiers are different, we have a type |
| 4201 | // mismatch. |
| 4202 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 4203 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 4204 | return QualType(); |
| 4205 | |
| 4206 | // Exactly one GC qualifier difference is allowed: __strong is |
| 4207 | // okay if the other type has no GC qualifier but is an Objective |
| 4208 | // C object pointer (i.e. implicitly strong by default). We fix |
| 4209 | // this by pretending that the unqualified type was actually |
| 4210 | // qualified __strong. |
| 4211 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 4212 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 4213 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 4214 | |
| 4215 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 4216 | return QualType(); |
| 4217 | |
| 4218 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 4219 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 4220 | } |
| 4221 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 4222 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 4223 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4224 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4225 | } |
| 4226 | |
| 4227 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4228 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 4229 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 4230 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4231 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4232 | // We want to consider the two function types to be the same for these |
| 4233 | // comparisons, just force one to the other. |
| 4234 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 4235 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 4236 | |
| 4237 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4238 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 4239 | LHSClass = Type::ConstantArray; |
| 4240 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 4241 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4242 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 4243 | // Canonicalize ExtVector -> Vector. |
| 4244 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 4245 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4246 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4247 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4248 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4249 | // 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] | 4250 | // a signed integer type, or an unsigned integer type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4251 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4252 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 4253 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4254 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4255 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4256 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 4257 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4258 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4259 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4260 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4261 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4262 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 4263 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4264 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4265 | #define TYPE(Class, Base) |
| 4266 | #define ABSTRACT_TYPE(Class, Base) |
| 4267 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4268 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4269 | #include "clang/AST/TypeNodes.def" |
| 4270 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 4271 | return QualType(); |
| 4272 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4273 | case Type::LValueReference: |
| 4274 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4275 | case Type::MemberPointer: |
| 4276 | assert(false && "C++ should never be in mergeTypes"); |
| 4277 | return QualType(); |
| 4278 | |
| 4279 | case Type::IncompleteArray: |
| 4280 | case Type::VariableArray: |
| 4281 | case Type::FunctionProto: |
| 4282 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4283 | assert(false && "Types are eliminated above"); |
| 4284 | return QualType(); |
| 4285 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4286 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4287 | { |
| 4288 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4289 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 4290 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4291 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 4292 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4293 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4294 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4295 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4296 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4297 | return getPointerType(ResultType); |
| 4298 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4299 | case Type::BlockPointer: |
| 4300 | { |
| 4301 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4302 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 4303 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4304 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 4305 | if (ResultType.isNull()) return QualType(); |
| 4306 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 4307 | return LHS; |
| 4308 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 4309 | return RHS; |
| 4310 | return getBlockPointerType(ResultType); |
| 4311 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4312 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4313 | { |
| 4314 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 4315 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 4316 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 4317 | return QualType(); |
| 4318 | |
| 4319 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 4320 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
| 4321 | QualType ResultType = mergeTypes(LHSElem, RHSElem); |
| 4322 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4323 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4324 | return LHS; |
| 4325 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4326 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4327 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 4328 | ArrayType::ArraySizeModifier(), 0); |
| 4329 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 4330 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4331 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 4332 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4333 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4334 | return LHS; |
| 4335 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4336 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4337 | if (LVAT) { |
| 4338 | // FIXME: This isn't correct! But tricky to implement because |
| 4339 | // the array's size has to be the size of LHS, but the type |
| 4340 | // has to be different. |
| 4341 | return LHS; |
| 4342 | } |
| 4343 | if (RVAT) { |
| 4344 | // FIXME: This isn't correct! But tricky to implement because |
| 4345 | // the array's size has to be the size of RHS, but the type |
| 4346 | // has to be different. |
| 4347 | return RHS; |
| 4348 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4349 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 4350 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4351 | return getIncompleteArrayType(ResultType, |
| 4352 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4353 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4354 | case Type::FunctionNoProto: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4355 | return mergeFunctionTypes(LHS, RHS); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4356 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4357 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4358 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4359 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4360 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4361 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 4362 | case Type::Complex: |
| 4363 | // Distinct complex types are incompatible. |
| 4364 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4365 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4366 | // FIXME: The merged type should be an ExtVector! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4367 | if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4368 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4369 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4370 | case Type::ObjCInterface: { |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4371 | // Check if the interfaces are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4372 | // FIXME: This should be type compatibility, e.g. whether |
| 4373 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4374 | const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>(); |
| 4375 | const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>(); |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4376 | if (LHSIface && RHSIface && |
| 4377 | canAssignObjCInterfaces(LHSIface, RHSIface)) |
| 4378 | return LHS; |
| 4379 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4380 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4381 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4382 | case Type::ObjCObjectPointer: { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4383 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 4384 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4385 | return LHS; |
| 4386 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 4387 | return QualType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4388 | } |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4389 | case Type::FixedWidthInt: |
| 4390 | // Distinct fixed-width integers are not compatible. |
| 4391 | return QualType(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4392 | case Type::TemplateSpecialization: |
| 4393 | assert(false && "Dependent types have no size"); |
| 4394 | break; |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4395 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4396 | |
| 4397 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4398 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 4399 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 4400 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4401 | // Integer Predicates |
| 4402 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 4403 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4404 | unsigned ASTContext::getIntWidth(QualType T) { |
Sebastian Redl | 632d772 | 2009-11-05 21:10:57 +0000 | [diff] [blame] | 4405 | if (T->isBooleanType()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4406 | return 1; |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4407 | if (FixedWidthIntType *FWIT = dyn_cast<FixedWidthIntType>(T)) { |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4408 | return FWIT->getWidth(); |
| 4409 | } |
| 4410 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4411 | return (unsigned)getTypeSize(T); |
| 4412 | } |
| 4413 | |
| 4414 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
| 4415 | assert(T->isSignedIntegerType() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4416 | |
| 4417 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 4418 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 4419 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
| 4420 | VTy->getNumElements()); |
| 4421 | |
| 4422 | // For enums, we return the unsigned version of the base type. |
| 4423 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4424 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4425 | |
| 4426 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 4427 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4428 | switch (BTy->getKind()) { |
| 4429 | case BuiltinType::Char_S: |
| 4430 | case BuiltinType::SChar: |
| 4431 | return UnsignedCharTy; |
| 4432 | case BuiltinType::Short: |
| 4433 | return UnsignedShortTy; |
| 4434 | case BuiltinType::Int: |
| 4435 | return UnsignedIntTy; |
| 4436 | case BuiltinType::Long: |
| 4437 | return UnsignedLongTy; |
| 4438 | case BuiltinType::LongLong: |
| 4439 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4440 | case BuiltinType::Int128: |
| 4441 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4442 | default: |
| 4443 | assert(0 && "Unexpected signed integer type"); |
| 4444 | return QualType(); |
| 4445 | } |
| 4446 | } |
| 4447 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4448 | ExternalASTSource::~ExternalASTSource() { } |
| 4449 | |
| 4450 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4451 | |
| 4452 | |
| 4453 | //===----------------------------------------------------------------------===// |
| 4454 | // Builtin Type Computation |
| 4455 | //===----------------------------------------------------------------------===// |
| 4456 | |
| 4457 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
| 4458 | /// pointer over the consumed characters. This returns the resultant type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4459 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4460 | ASTContext::GetBuiltinTypeError &Error, |
| 4461 | bool AllowTypeModifiers = true) { |
| 4462 | // Modifiers. |
| 4463 | int HowLong = 0; |
| 4464 | bool Signed = false, Unsigned = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4465 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4466 | // Read the modifiers first. |
| 4467 | bool Done = false; |
| 4468 | while (!Done) { |
| 4469 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4470 | default: Done = true; --Str; break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4471 | case 'S': |
| 4472 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 4473 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 4474 | Signed = true; |
| 4475 | break; |
| 4476 | case 'U': |
| 4477 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 4478 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 4479 | Unsigned = true; |
| 4480 | break; |
| 4481 | case 'L': |
| 4482 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 4483 | ++HowLong; |
| 4484 | break; |
| 4485 | } |
| 4486 | } |
| 4487 | |
| 4488 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4489 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4490 | // Read the base type. |
| 4491 | switch (*Str++) { |
| 4492 | default: assert(0 && "Unknown builtin type letter!"); |
| 4493 | case 'v': |
| 4494 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4495 | "Bad modifiers used with 'v'!"); |
| 4496 | Type = Context.VoidTy; |
| 4497 | break; |
| 4498 | case 'f': |
| 4499 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4500 | "Bad modifiers used with 'f'!"); |
| 4501 | Type = Context.FloatTy; |
| 4502 | break; |
| 4503 | case 'd': |
| 4504 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 4505 | "Bad modifiers used with 'd'!"); |
| 4506 | if (HowLong) |
| 4507 | Type = Context.LongDoubleTy; |
| 4508 | else |
| 4509 | Type = Context.DoubleTy; |
| 4510 | break; |
| 4511 | case 's': |
| 4512 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 4513 | if (Unsigned) |
| 4514 | Type = Context.UnsignedShortTy; |
| 4515 | else |
| 4516 | Type = Context.ShortTy; |
| 4517 | break; |
| 4518 | case 'i': |
| 4519 | if (HowLong == 3) |
| 4520 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 4521 | else if (HowLong == 2) |
| 4522 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 4523 | else if (HowLong == 1) |
| 4524 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 4525 | else |
| 4526 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 4527 | break; |
| 4528 | case 'c': |
| 4529 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 4530 | if (Signed) |
| 4531 | Type = Context.SignedCharTy; |
| 4532 | else if (Unsigned) |
| 4533 | Type = Context.UnsignedCharTy; |
| 4534 | else |
| 4535 | Type = Context.CharTy; |
| 4536 | break; |
| 4537 | case 'b': // boolean |
| 4538 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 4539 | Type = Context.BoolTy; |
| 4540 | break; |
| 4541 | case 'z': // size_t. |
| 4542 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 4543 | Type = Context.getSizeType(); |
| 4544 | break; |
| 4545 | case 'F': |
| 4546 | Type = Context.getCFConstantStringType(); |
| 4547 | break; |
| 4548 | case 'a': |
| 4549 | Type = Context.getBuiltinVaListType(); |
| 4550 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4551 | break; |
| 4552 | case 'A': |
| 4553 | // This is a "reference" to a va_list; however, what exactly |
| 4554 | // this means depends on how va_list is defined. There are two |
| 4555 | // different kinds of va_list: ones passed by value, and ones |
| 4556 | // passed by reference. An example of a by-value va_list is |
| 4557 | // x86, where va_list is a char*. An example of by-ref va_list |
| 4558 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 4559 | // we want this argument to be a char*&; for x86-64, we want |
| 4560 | // it to be a __va_list_tag*. |
| 4561 | Type = Context.getBuiltinVaListType(); |
| 4562 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4563 | if (Type->isArrayType()) { |
| 4564 | Type = Context.getArrayDecayedType(Type); |
| 4565 | } else { |
| 4566 | Type = Context.getLValueReferenceType(Type); |
| 4567 | } |
| 4568 | break; |
| 4569 | case 'V': { |
| 4570 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4571 | unsigned NumElements = strtoul(Str, &End, 10); |
| 4572 | assert(End != Str && "Missing vector size"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4573 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4574 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4575 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4576 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 4577 | Type = Context.getVectorType(ElementType, NumElements); |
| 4578 | break; |
| 4579 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 4580 | case 'X': { |
| 4581 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 4582 | Type = Context.getComplexType(ElementType); |
| 4583 | break; |
| 4584 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4585 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4586 | Type = Context.getFILEType(); |
| 4587 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4588 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4589 | return QualType(); |
| 4590 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4591 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4592 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4593 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4594 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4595 | else |
| 4596 | Type = Context.getjmp_bufType(); |
| 4597 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4598 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4599 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4600 | return QualType(); |
| 4601 | } |
| 4602 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4603 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4604 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4605 | if (!AllowTypeModifiers) |
| 4606 | return Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4607 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4608 | Done = false; |
| 4609 | while (!Done) { |
| 4610 | switch (*Str++) { |
| 4611 | default: Done = true; --Str; break; |
| 4612 | case '*': |
| 4613 | Type = Context.getPointerType(Type); |
| 4614 | break; |
| 4615 | case '&': |
| 4616 | Type = Context.getLValueReferenceType(Type); |
| 4617 | break; |
| 4618 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 4619 | case 'C': |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4620 | Type = Type.withConst(); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4621 | break; |
| 4622 | } |
| 4623 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4624 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4625 | return Type; |
| 4626 | } |
| 4627 | |
| 4628 | /// GetBuiltinType - Return the type for the specified builtin. |
| 4629 | QualType ASTContext::GetBuiltinType(unsigned id, |
| 4630 | GetBuiltinTypeError &Error) { |
| 4631 | const char *TypeStr = BuiltinInfo.GetTypeString(id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4632 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4633 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4634 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4635 | Error = GE_None; |
| 4636 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4637 | if (Error != GE_None) |
| 4638 | return QualType(); |
| 4639 | while (TypeStr[0] && TypeStr[0] != '.') { |
| 4640 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4641 | if (Error != GE_None) |
| 4642 | return QualType(); |
| 4643 | |
| 4644 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 4645 | if (Ty->isArrayType()) |
| 4646 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4647 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4648 | ArgTypes.push_back(Ty); |
| 4649 | } |
| 4650 | |
| 4651 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 4652 | "'.' should only occur at end of builtin type list!"); |
| 4653 | |
| 4654 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 4655 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 4656 | return getFunctionNoProtoType(ResType); |
| 4657 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), |
| 4658 | TypeStr[0] == '.', 0); |
| 4659 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4660 | |
| 4661 | QualType |
| 4662 | ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 4663 | // Perform the usual unary conversions. We do this early so that |
| 4664 | // integral promotions to "int" can allow us to exit early, in the |
| 4665 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 4666 | // qualifiers. For example, "const float" and "float" are |
| 4667 | // equivalent. |
| 4668 | if (lhs->isPromotableIntegerType()) |
| 4669 | lhs = getPromotedIntegerType(lhs); |
| 4670 | else |
| 4671 | lhs = lhs.getUnqualifiedType(); |
| 4672 | if (rhs->isPromotableIntegerType()) |
| 4673 | rhs = getPromotedIntegerType(rhs); |
| 4674 | else |
| 4675 | rhs = rhs.getUnqualifiedType(); |
| 4676 | |
| 4677 | // If both types are identical, no conversion is needed. |
| 4678 | if (lhs == rhs) |
| 4679 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4680 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4681 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 4682 | // The caller can deal with this (e.g. pointer + int). |
| 4683 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 4684 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4685 | |
| 4686 | // At this point, we have two different arithmetic types. |
| 4687 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4688 | // Handle complex types first (C99 6.3.1.8p1). |
| 4689 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 4690 | // if we have an integer operand, the result is the complex type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4691 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4692 | // convert the rhs to the lhs complex type. |
| 4693 | return lhs; |
| 4694 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4695 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4696 | // convert the lhs to the rhs complex type. |
| 4697 | return rhs; |
| 4698 | } |
| 4699 | // This handles complex/complex, complex/float, or float/complex. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4700 | // When both operands are complex, the shorter operand is converted to the |
| 4701 | // type of the longer, and that is the type of the result. This corresponds |
| 4702 | // to what is done when combining two real floating-point operands. |
| 4703 | // The fun begins when size promotion occur across type domains. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4704 | // 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] | 4705 | // floating-point type, the less precise type is converted, within it's |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4706 | // 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] | 4707 | // when combining a "long double" with a "double _Complex", the |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4708 | // "double _Complex" is promoted to "long double _Complex". |
| 4709 | int result = getFloatingTypeOrder(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4710 | |
| 4711 | if (result > 0) { // The left side is bigger, convert rhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4712 | rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4713 | } else if (result < 0) { // The right side is bigger, convert lhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4714 | lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4715 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4716 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 4717 | // domains match. This is a requirement for our implementation, C99 |
| 4718 | // does not require this promotion. |
| 4719 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 4720 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
| 4721 | return rhs; |
| 4722 | } else { // handle "_Complex double, double". |
| 4723 | return lhs; |
| 4724 | } |
| 4725 | } |
| 4726 | return lhs; // The domain/size match exactly. |
| 4727 | } |
| 4728 | // Now handle "real" floating types (i.e. float, double, long double). |
| 4729 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 4730 | // if we have an integer operand, the result is the real floating type. |
| 4731 | if (rhs->isIntegerType()) { |
| 4732 | // convert rhs to the lhs floating point type. |
| 4733 | return lhs; |
| 4734 | } |
| 4735 | if (rhs->isComplexIntegerType()) { |
| 4736 | // convert rhs to the complex floating point type. |
| 4737 | return getComplexType(lhs); |
| 4738 | } |
| 4739 | if (lhs->isIntegerType()) { |
| 4740 | // convert lhs to the rhs floating point type. |
| 4741 | return rhs; |
| 4742 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4743 | if (lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4744 | // convert lhs to the complex floating point type. |
| 4745 | return getComplexType(rhs); |
| 4746 | } |
| 4747 | // We have two real floating types, float/complex combos were handled above. |
| 4748 | // Convert the smaller operand to the bigger result. |
| 4749 | int result = getFloatingTypeOrder(lhs, rhs); |
| 4750 | if (result > 0) // convert the rhs |
| 4751 | return lhs; |
| 4752 | assert(result < 0 && "illegal float comparison"); |
| 4753 | return rhs; // convert the lhs |
| 4754 | } |
| 4755 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 4756 | // Handle GCC complex int extension. |
| 4757 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 4758 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 4759 | |
| 4760 | if (lhsComplexInt && rhsComplexInt) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4761 | if (getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4762 | rhsComplexInt->getElementType()) >= 0) |
| 4763 | return lhs; // convert the rhs |
| 4764 | return rhs; |
| 4765 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 4766 | // convert the rhs to the lhs complex type. |
| 4767 | return lhs; |
| 4768 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 4769 | // convert the lhs to the rhs complex type. |
| 4770 | return rhs; |
| 4771 | } |
| 4772 | } |
| 4773 | // Finally, we have two differing integer types. |
| 4774 | // The rules for this case are in C99 6.3.1.8 |
| 4775 | int compare = getIntegerTypeOrder(lhs, rhs); |
| 4776 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 4777 | rhsSigned = rhs->isSignedIntegerType(); |
| 4778 | QualType destType; |
| 4779 | if (lhsSigned == rhsSigned) { |
| 4780 | // Same signedness; use the higher-ranked type |
| 4781 | destType = compare >= 0 ? lhs : rhs; |
| 4782 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 4783 | // The unsigned type has greater than or equal rank to the |
| 4784 | // signed type, so use the unsigned type |
| 4785 | destType = lhsSigned ? rhs : lhs; |
| 4786 | } else if (getIntWidth(lhs) != getIntWidth(rhs)) { |
| 4787 | // The two types are different widths; if we are here, that |
| 4788 | // means the signed type is larger than the unsigned type, so |
| 4789 | // use the signed type. |
| 4790 | destType = lhsSigned ? lhs : rhs; |
| 4791 | } else { |
| 4792 | // The signed type is higher-ranked than the unsigned type, |
| 4793 | // but isn't actually any bigger (like unsigned int and long |
| 4794 | // on most 32-bit systems). Use the unsigned type corresponding |
| 4795 | // to the signed type. |
| 4796 | destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 4797 | } |
| 4798 | return destType; |
| 4799 | } |