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" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MemoryBuffer.h" |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 28 | #include "RecordLayoutBuilder.h" |
| 29 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
| 32 | enum FloatingRank { |
| 33 | FloatRank, DoubleRank, LongDoubleRank |
| 34 | }; |
| 35 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 36 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
| 37 | TargetInfo &t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 38 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 39 | Builtin::Context &builtins, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | bool FreeMem, unsigned size_reserve) : |
| 41 | GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0), |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 42 | ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 43 | sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
| 44 | SourceMgr(SM), LangOpts(LOpts), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | LoadedExternalComments(false), FreeMemory(FreeMem), Target(t), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 46 | Idents(idents), Selectors(sels), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) { |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 48 | ObjCIdRedefinitionType = QualType(); |
| 49 | ObjCClassRedefinitionType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 51 | TUDecl = TranslationUnitDecl::Create(*this); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 52 | InitBuiltinTypes(); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | ASTContext::~ASTContext() { |
| 56 | // Deallocate all the types. |
| 57 | while (!Types.empty()) { |
Ted Kremenek | 4b05b1d | 2008-05-21 16:38:54 +0000 | [diff] [blame] | 58 | Types.back()->Destroy(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | Types.pop_back(); |
| 60 | } |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 61 | |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 62 | { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 63 | llvm::FoldingSet<ExtQuals>::iterator |
| 64 | I = ExtQualNodes.begin(), E = ExtQualNodes.end(); |
| 65 | while (I != E) |
| 66 | Deallocate(&*I++); |
| 67 | } |
| 68 | |
| 69 | { |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 70 | llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 71 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); |
| 72 | while (I != E) { |
| 73 | ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); |
| 74 | delete R; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 79 | llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator |
| 80 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 81 | while (I != E) { |
| 82 | ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); |
| 83 | delete R; |
| 84 | } |
| 85 | } |
| 86 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 87 | // Destroy nested-name-specifiers. |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 88 | for (llvm::FoldingSet<NestedNameSpecifier>::iterator |
| 89 | NNS = NestedNameSpecifiers.begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | NNSEnd = NestedNameSpecifiers.end(); |
| 91 | NNS != NNSEnd; |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 92 | /* Increment in loop */) |
| 93 | (*NNS++).Destroy(*this); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 94 | |
| 95 | if (GlobalNestedNameSpecifier) |
| 96 | GlobalNestedNameSpecifier->Destroy(*this); |
| 97 | |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 98 | TUDecl->Destroy(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 102 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 103 | ExternalSource.reset(Source.take()); |
| 104 | } |
| 105 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | void ASTContext::PrintStats() const { |
| 107 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 108 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 109 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 110 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 112 | #define ABSTRACT_TYPE(Name, Parent) |
| 113 | #include "clang/AST/TypeNodes.def" |
| 114 | 0 // Extra |
| 115 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 116 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 118 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 119 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 122 | unsigned Idx = 0; |
| 123 | unsigned TotalBytes = 0; |
| 124 | #define TYPE(Name, Parent) \ |
| 125 | if (counts[Idx]) \ |
| 126 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 127 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 128 | ++Idx; |
| 129 | #define ABSTRACT_TYPE(Name, Parent) |
| 130 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 132 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 133 | |
| 134 | if (ExternalSource.get()) { |
| 135 | fprintf(stderr, "\n"); |
| 136 | ExternalSource->PrintStats(); |
| 137 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame^] | 141 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 142 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame^] | 143 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 144 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 147 | void ASTContext::InitBuiltinTypes() { |
| 148 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 150 | // C99 6.2.5p19. |
| 151 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 153 | // C99 6.2.5p2. |
| 154 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 155 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 156 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 158 | else |
| 159 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 160 | // C99 6.2.5p4. |
| 161 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 162 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 163 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 164 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 165 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | // C99 6.2.5p6. |
| 168 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 169 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 170 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 171 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 172 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | // C99 6.2.5p10. |
| 175 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 176 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 177 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 179 | // GNU extension, 128-bit integers. |
| 180 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 181 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 182 | |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 183 | if (LangOpts.CPlusPlus) // C++ 3.9.1p5 |
| 184 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 185 | else // C99 |
| 186 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 187 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 188 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 189 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 190 | else // C99 |
| 191 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 192 | |
| 193 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 194 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 195 | else // C99 |
| 196 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 197 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 198 | // Placeholder type for functions. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 199 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 200 | |
| 201 | // Placeholder type for type-dependent expressions whose type is |
| 202 | // completely unknown. No code should ever check a type against |
| 203 | // DependentTy and users should never see it; however, it is here to |
| 204 | // help diagnose failures to properly check for type-dependent |
| 205 | // expressions. |
| 206 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 207 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | // Placeholder type for C++0x auto declarations whose real type has |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 209 | // not yet been deduced. |
| 210 | InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 212 | // C99 6.2.5p11. |
| 213 | FloatComplexTy = getComplexType(FloatTy); |
| 214 | DoubleComplexTy = getComplexType(DoubleTy); |
| 215 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 216 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 217 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 219 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 220 | ObjCIdTypedefType = QualType(); |
| 221 | ObjCClassTypedefType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 222 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 223 | // Builtin types for 'id' and 'Class'. |
| 224 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 225 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 227 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 229 | // void * type |
| 230 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 231 | |
| 232 | // nullptr type (C++0x 2.14.7) |
| 233 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 236 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 237 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 238 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 239 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 240 | = InstantiatedFromStaticDataMember.find(Var); |
| 241 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 242 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 244 | return Pos->second; |
| 245 | } |
| 246 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 248 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
| 249 | TemplateSpecializationKind TSK) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 250 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 251 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 252 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 253 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 254 | InstantiatedFromStaticDataMember[Inst] |
| 255 | = new (*this) MemberSpecializationInfo(Tmpl, TSK); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 258 | UnresolvedUsingDecl * |
| 259 | ASTContext::getInstantiatedFromUnresolvedUsingDecl(UsingDecl *UUD) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | llvm::DenseMap<UsingDecl *, UnresolvedUsingDecl *>::iterator Pos |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 261 | = InstantiatedFromUnresolvedUsingDecl.find(UUD); |
| 262 | if (Pos == InstantiatedFromUnresolvedUsingDecl.end()) |
| 263 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 265 | return Pos->second; |
| 266 | } |
| 267 | |
| 268 | void |
| 269 | ASTContext::setInstantiatedFromUnresolvedUsingDecl(UsingDecl *UD, |
| 270 | UnresolvedUsingDecl *UUD) { |
| 271 | assert(!InstantiatedFromUnresolvedUsingDecl[UD] && |
| 272 | "Already noted what using decl what instantiated from"); |
| 273 | InstantiatedFromUnresolvedUsingDecl[UD] = UUD; |
| 274 | } |
| 275 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 276 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 277 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 278 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 279 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 280 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 282 | return Pos->second; |
| 283 | } |
| 284 | |
| 285 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 286 | FieldDecl *Tmpl) { |
| 287 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 288 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 289 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 290 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 292 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 293 | } |
| 294 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 295 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | class BeforeInTranslationUnit |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 297 | : std::binary_function<SourceRange, SourceRange, bool> { |
| 298 | SourceManager *SourceMgr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 300 | public: |
| 301 | explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 302 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 303 | bool operator()(SourceRange X, SourceRange Y) { |
| 304 | return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin()); |
| 305 | } |
| 306 | }; |
| 307 | } |
| 308 | |
| 309 | /// \brief Determine whether the given comment is a Doxygen-style comment. |
| 310 | /// |
| 311 | /// \param Start the start of the comment text. |
| 312 | /// |
| 313 | /// \param End the end of the comment text. |
| 314 | /// |
| 315 | /// \param Member whether we want to check whether this is a member comment |
| 316 | /// (which requires a < after the Doxygen-comment delimiter). Otherwise, |
| 317 | /// we only return true when we find a non-member comment. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | static bool |
| 319 | isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 320 | bool Member = false) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | const char *BufferStart |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 322 | = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first; |
| 323 | const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin()); |
| 324 | const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 326 | if (End - Start < 4) |
| 327 | return false; |
| 328 | |
| 329 | assert(Start[0] == '/' && "Not a comment?"); |
| 330 | if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*')) |
| 331 | return false; |
| 332 | if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/')) |
| 333 | return false; |
| 334 | |
| 335 | return (Start[3] == '<') == Member; |
| 336 | } |
| 337 | |
| 338 | /// \brief Retrieve the comment associated with the given declaration, if |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | /// it has one. |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 340 | const char *ASTContext::getCommentForDecl(const Decl *D) { |
| 341 | if (!D) |
| 342 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 344 | // Check whether we have cached a comment string for this declaration |
| 345 | // already. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | llvm::DenseMap<const Decl *, std::string>::iterator Pos |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 347 | = DeclComments.find(D); |
| 348 | if (Pos != DeclComments.end()) |
| 349 | return Pos->second.c_str(); |
| 350 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | // 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] | 352 | // that source, do so now. |
| 353 | if (ExternalSource && !LoadedExternalComments) { |
| 354 | std::vector<SourceRange> LoadedComments; |
| 355 | ExternalSource->ReadComments(LoadedComments); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 357 | if (!LoadedComments.empty()) |
| 358 | Comments.insert(Comments.begin(), LoadedComments.begin(), |
| 359 | LoadedComments.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 361 | LoadedExternalComments = true; |
| 362 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | |
| 364 | // If there are no comments anywhere, we won't find anything. |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 365 | if (Comments.empty()) |
| 366 | return 0; |
| 367 | |
| 368 | // If the declaration doesn't map directly to a location in a file, we |
| 369 | // can't find the comment. |
| 370 | SourceLocation DeclStartLoc = D->getLocStart(); |
| 371 | if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID()) |
| 372 | return 0; |
| 373 | |
| 374 | // Find the comment that occurs just before this declaration. |
| 375 | std::vector<SourceRange>::iterator LastComment |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | = std::lower_bound(Comments.begin(), Comments.end(), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 377 | SourceRange(DeclStartLoc), |
| 378 | BeforeInTranslationUnit(&SourceMgr)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 380 | // Decompose the location for the start of the declaration and find the |
| 381 | // beginning of the file buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | std::pair<FileID, unsigned> DeclStartDecomp |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 383 | = SourceMgr.getDecomposedLoc(DeclStartLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | const char *FileBufferStart |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 385 | = SourceMgr.getBufferData(DeclStartDecomp.first).first; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 387 | // First check whether we have a comment for a member. |
| 388 | if (LastComment != Comments.end() && |
| 389 | !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) && |
| 390 | isDoxygenComment(SourceMgr, *LastComment, true)) { |
| 391 | std::pair<FileID, unsigned> LastCommentEndDecomp |
| 392 | = SourceMgr.getDecomposedLoc(LastComment->getEnd()); |
| 393 | if (DeclStartDecomp.first == LastCommentEndDecomp.first && |
| 394 | SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | == SourceMgr.getLineNumber(LastCommentEndDecomp.first, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 396 | LastCommentEndDecomp.second)) { |
| 397 | // The Doxygen member comment comes after the declaration starts and |
| 398 | // is on the same line and in the same file as the declaration. This |
| 399 | // is the comment we want. |
| 400 | std::string &Result = DeclComments[D]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | Result.append(FileBufferStart + |
| 402 | SourceMgr.getFileOffset(LastComment->getBegin()), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 403 | FileBufferStart + LastCommentEndDecomp.second + 1); |
| 404 | return Result.c_str(); |
| 405 | } |
| 406 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 408 | if (LastComment == Comments.begin()) |
| 409 | return 0; |
| 410 | --LastComment; |
| 411 | |
| 412 | // Decompose the end of the comment. |
| 413 | std::pair<FileID, unsigned> LastCommentEndDecomp |
| 414 | = SourceMgr.getDecomposedLoc(LastComment->getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 416 | // If the comment and the declaration aren't in the same file, then they |
| 417 | // aren't related. |
| 418 | if (DeclStartDecomp.first != LastCommentEndDecomp.first) |
| 419 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 420 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 421 | // Check that we actually have a Doxygen comment. |
| 422 | if (!isDoxygenComment(SourceMgr, *LastComment)) |
| 423 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 425 | // Compute the starting line for the declaration and for the end of the |
| 426 | // comment (this is expensive). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | unsigned DeclStartLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 428 | = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second); |
| 429 | unsigned CommentEndLine |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | = SourceMgr.getLineNumber(LastCommentEndDecomp.first, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 431 | LastCommentEndDecomp.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 433 | // If the comment does not end on the line prior to the declaration, then |
| 434 | // the comment is not associated with the declaration at all. |
| 435 | if (CommentEndLine + 1 != DeclStartLine) |
| 436 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 438 | // We have a comment, but there may be more comments on the previous lines. |
| 439 | // Keep looking so long as the comments are still Doxygen comments and are |
| 440 | // still adjacent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | unsigned ExpectedLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 442 | = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1; |
| 443 | std::vector<SourceRange>::iterator FirstComment = LastComment; |
| 444 | while (FirstComment != Comments.begin()) { |
| 445 | // Look at the previous comment |
| 446 | --FirstComment; |
| 447 | std::pair<FileID, unsigned> Decomp |
| 448 | = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 449 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 450 | // If this previous comment is in a different file, we're done. |
| 451 | if (Decomp.first != DeclStartDecomp.first) { |
| 452 | ++FirstComment; |
| 453 | break; |
| 454 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 456 | // If this comment is not a Doxygen comment, we're done. |
| 457 | if (!isDoxygenComment(SourceMgr, *FirstComment)) { |
| 458 | ++FirstComment; |
| 459 | break; |
| 460 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 462 | // If the line number is not what we expected, we're done. |
| 463 | unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second); |
| 464 | if (Line != ExpectedLine) { |
| 465 | ++FirstComment; |
| 466 | break; |
| 467 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 469 | // Set the next expected line number. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 470 | ExpectedLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 471 | = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1; |
| 472 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 474 | // The iterator range [FirstComment, LastComment] contains all of the |
| 475 | // BCPL comments that, together, are associated with this declaration. |
| 476 | // Form a single comment block string for this declaration that concatenates |
| 477 | // all of these comments. |
| 478 | std::string &Result = DeclComments[D]; |
| 479 | while (FirstComment != LastComment) { |
| 480 | std::pair<FileID, unsigned> DecompStart |
| 481 | = SourceMgr.getDecomposedLoc(FirstComment->getBegin()); |
| 482 | std::pair<FileID, unsigned> DecompEnd |
| 483 | = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); |
| 484 | Result.append(FileBufferStart + DecompStart.second, |
| 485 | FileBufferStart + DecompEnd.second + 1); |
| 486 | ++FirstComment; |
| 487 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 489 | // Append the last comment line. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | Result.append(FileBufferStart + |
| 491 | SourceMgr.getFileOffset(LastComment->getBegin()), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 492 | FileBufferStart + LastCommentEndDecomp.second + 1); |
| 493 | return Result.c_str(); |
| 494 | } |
| 495 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 496 | //===----------------------------------------------------------------------===// |
| 497 | // Type Sizing and Analysis |
| 498 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 499 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 500 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 501 | /// scalar floating point type. |
| 502 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 503 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 504 | assert(BT && "Not a floating point type!"); |
| 505 | switch (BT->getKind()) { |
| 506 | default: assert(0 && "Not a floating point type!"); |
| 507 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 508 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 509 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 510 | } |
| 511 | } |
| 512 | |
Mike Stump | 196efbf | 2009-09-22 02:43:44 +0000 | [diff] [blame] | 513 | /// getDeclAlignInBytes - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 514 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 515 | /// this method will assert on them. |
Daniel Dunbar | b7d0844 | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 516 | unsigned ASTContext::getDeclAlignInBytes(const Decl *D) { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 517 | unsigned Align = Target.getCharWidth(); |
| 518 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 519 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 520 | Align = std::max(Align, AA->getAlignment()); |
| 521 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 522 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 523 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 524 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 525 | unsigned AS = RT->getPointeeType().getAddressSpace(); |
Anders Carlsson | f093023 | 2009-04-10 04:52:36 +0000 | [diff] [blame] | 526 | Align = Target.getPointerAlign(AS); |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 527 | } else if (!T->isIncompleteType() && !T->isFunctionType()) { |
| 528 | // Incomplete or function types default to 1. |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 529 | while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) |
| 530 | T = cast<ArrayType>(T)->getElementType(); |
| 531 | |
| 532 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 533 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 534 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 535 | |
| 536 | return Align / Target.getCharWidth(); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 537 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 538 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 539 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 540 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 541 | /// |
| 542 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 543 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 544 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 545 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 546 | ASTContext::getTypeInfo(const Type *T) { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 547 | uint64_t Width=0; |
| 548 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 549 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 550 | #define TYPE(Class, Base) |
| 551 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 552 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 553 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 554 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 555 | assert(false && "Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 556 | break; |
| 557 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 558 | case Type::FunctionNoProto: |
| 559 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 560 | // GCC extension: alignof(function) = 32 bits |
| 561 | Width = 0; |
| 562 | Align = 32; |
| 563 | break; |
| 564 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 565 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 566 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 567 | Width = 0; |
| 568 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 569 | break; |
| 570 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 571 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 572 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 574 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 575 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 576 | Align = EltInfo.second; |
| 577 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 578 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 579 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 580 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 581 | const VectorType *VT = cast<VectorType>(T); |
| 582 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 583 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 584 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 585 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 586 | // This happens for non-power-of-2 length vectors. |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 587 | if (VT->getNumElements() & (VT->getNumElements()-1)) { |
| 588 | Align = llvm::NextPowerOf2(Align); |
| 589 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 590 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 591 | break; |
| 592 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 593 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 594 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 595 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 596 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 597 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 598 | // GCC extension: alignof(void) = 8 bits. |
| 599 | Width = 0; |
| 600 | Align = 8; |
| 601 | break; |
| 602 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 603 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 604 | Width = Target.getBoolWidth(); |
| 605 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 606 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 607 | case BuiltinType::Char_S: |
| 608 | case BuiltinType::Char_U: |
| 609 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 610 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 611 | Width = Target.getCharWidth(); |
| 612 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 613 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 614 | case BuiltinType::WChar: |
| 615 | Width = Target.getWCharWidth(); |
| 616 | Align = Target.getWCharAlign(); |
| 617 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 618 | case BuiltinType::Char16: |
| 619 | Width = Target.getChar16Width(); |
| 620 | Align = Target.getChar16Align(); |
| 621 | break; |
| 622 | case BuiltinType::Char32: |
| 623 | Width = Target.getChar32Width(); |
| 624 | Align = Target.getChar32Align(); |
| 625 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 626 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 627 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 628 | Width = Target.getShortWidth(); |
| 629 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 630 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 631 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 632 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 633 | Width = Target.getIntWidth(); |
| 634 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 635 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 636 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 637 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 638 | Width = Target.getLongWidth(); |
| 639 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 640 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 641 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 642 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 643 | Width = Target.getLongLongWidth(); |
| 644 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 645 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 646 | case BuiltinType::Int128: |
| 647 | case BuiltinType::UInt128: |
| 648 | Width = 128; |
| 649 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 650 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 651 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 652 | Width = Target.getFloatWidth(); |
| 653 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 654 | break; |
| 655 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 656 | Width = Target.getDoubleWidth(); |
| 657 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 658 | break; |
| 659 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 660 | Width = Target.getLongDoubleWidth(); |
| 661 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 662 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 663 | case BuiltinType::NullPtr: |
| 664 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 665 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 666 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 667 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 668 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 669 | case Type::FixedWidthInt: |
| 670 | // FIXME: This isn't precisely correct; the width/alignment should depend |
| 671 | // on the available types for the target |
| 672 | Width = cast<FixedWidthIntType>(T)->getWidth(); |
Chris Lattner | 736166b | 2009-02-15 21:20:13 +0000 | [diff] [blame] | 673 | Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 674 | Align = Width; |
| 675 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 676 | case Type::ObjCObjectPointer: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 677 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 678 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 679 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 680 | case Type::BlockPointer: { |
| 681 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 682 | Width = Target.getPointerWidth(AS); |
| 683 | Align = Target.getPointerAlign(AS); |
| 684 | break; |
| 685 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 686 | case Type::Pointer: { |
| 687 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 688 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 689 | Align = Target.getPointerAlign(AS); |
| 690 | break; |
| 691 | } |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 692 | case Type::LValueReference: |
| 693 | case Type::RValueReference: |
Chris Lattner | 7ab2ed8 | 2007-07-13 22:16:13 +0000 | [diff] [blame] | 694 | // "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] | 695 | // of the referenced type." C++98 5.3.3p2: expr.sizeof. |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 696 | // FIXME: This is wrong for struct layout: a reference in a struct has |
| 697 | // pointer size. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 698 | return getTypeInfo(cast<ReferenceType>(T)->getPointeeType()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 699 | case Type::MemberPointer: { |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 700 | // FIXME: This is ABI dependent. We use the Itanium C++ ABI. |
| 701 | // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers |
| 702 | // If we ever want to support other ABIs this needs to be abstracted. |
| 703 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 704 | QualType Pointee = cast<MemberPointerType>(T)->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 705 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 706 | getTypeInfo(getPointerDiffType()); |
| 707 | Width = PtrDiffInfo.first; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 708 | if (Pointee->isFunctionType()) |
| 709 | Width *= 2; |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 710 | Align = PtrDiffInfo.second; |
| 711 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 712 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 713 | case Type::Complex: { |
| 714 | // Complex types have the same alignment as their elements, but twice the |
| 715 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 717 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 718 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 719 | Align = EltInfo.second; |
| 720 | break; |
| 721 | } |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 722 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 723 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 724 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 725 | Width = Layout.getSize(); |
| 726 | Align = Layout.getAlignment(); |
| 727 | break; |
| 728 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 729 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 730 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 731 | const TagType *TT = cast<TagType>(T); |
| 732 | |
| 733 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 734 | Width = 1; |
| 735 | Align = 1; |
| 736 | break; |
| 737 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 738 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 739 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 740 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 741 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 742 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 743 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 744 | Width = Layout.getSize(); |
| 745 | Align = Layout.getAlignment(); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 746 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 747 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 748 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 749 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 750 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 751 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 752 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 753 | case Type::Elaborated: |
| 754 | return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType() |
| 755 | .getTypePtr()); |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 756 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 757 | case Type::Typedef: { |
| 758 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 759 | if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) { |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 760 | Align = Aligned->getAlignment(); |
| 761 | Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr()); |
| 762 | } else |
| 763 | return getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 764 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 765 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 766 | |
| 767 | case Type::TypeOfExpr: |
| 768 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 769 | .getTypePtr()); |
| 770 | |
| 771 | case Type::TypeOf: |
| 772 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 773 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 774 | case Type::Decltype: |
| 775 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 776 | .getTypePtr()); |
| 777 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 778 | case Type::QualifiedName: |
| 779 | return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 781 | case Type::TemplateSpecialization: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 782 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 783 | "Cannot request the size of a dependent type"); |
| 784 | // FIXME: this is likely to be wrong once we support template |
| 785 | // aliases, since a template alias could refer to a typedef that |
| 786 | // has an __aligned__ attribute on it. |
| 787 | return getTypeInfo(getCanonicalType(T)); |
| 788 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 789 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 790 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 791 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 794 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 795 | /// type for the current target in bits. This can be different than the ABI |
| 796 | /// alignment in cases where it is beneficial for performance to overalign |
| 797 | /// a data type. |
| 798 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) { |
| 799 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 800 | |
| 801 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 802 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 803 | T = CT->getElementType().getTypePtr(); |
| 804 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 805 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 806 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 807 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 808 | return ABIAlign; |
| 809 | } |
| 810 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 811 | static void CollectLocalObjCIvars(ASTContext *Ctx, |
| 812 | const ObjCInterfaceDecl *OI, |
| 813 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 814 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 815 | E = OI->ivar_end(); I != E; ++I) { |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 816 | ObjCIvarDecl *IVDecl = *I; |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 817 | if (!IVDecl->isInvalidDecl()) |
| 818 | Fields.push_back(cast<FieldDecl>(IVDecl)); |
| 819 | } |
| 820 | } |
| 821 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 822 | void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 823 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
| 824 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 825 | CollectObjCIvars(SuperClass, Fields); |
| 826 | CollectLocalObjCIvars(this, OI, Fields); |
| 827 | } |
| 828 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 829 | /// ShallowCollectObjCIvars - |
| 830 | /// Collect all ivars, including those synthesized, in the current class. |
| 831 | /// |
| 832 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 833 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars, |
| 834 | bool CollectSynthesized) { |
| 835 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 836 | E = OI->ivar_end(); I != E; ++I) { |
| 837 | Ivars.push_back(*I); |
| 838 | } |
| 839 | if (CollectSynthesized) |
| 840 | CollectSynthesizedIvars(OI, Ivars); |
| 841 | } |
| 842 | |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 843 | void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD, |
| 844 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 845 | for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), |
| 846 | E = PD->prop_end(); I != E; ++I) |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 847 | if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) |
| 848 | Ivars.push_back(Ivar); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 849 | |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 850 | // Also look into nested protocols. |
| 851 | for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), |
| 852 | E = PD->protocol_end(); P != E; ++P) |
| 853 | CollectProtocolSynthesizedIvars(*P, Ivars); |
| 854 | } |
| 855 | |
| 856 | /// CollectSynthesizedIvars - |
| 857 | /// This routine collect synthesized ivars for the designated class. |
| 858 | /// |
| 859 | void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI, |
| 860 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 861 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), |
| 862 | E = OI->prop_end(); I != E; ++I) { |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 863 | if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) |
| 864 | Ivars.push_back(Ivar); |
| 865 | } |
| 866 | // Also look into interface's protocol list for properties declared |
| 867 | // in the protocol and whose ivars are synthesized. |
| 868 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 869 | PE = OI->protocol_end(); P != PE; ++P) { |
| 870 | ObjCProtocolDecl *PD = (*P); |
| 871 | CollectProtocolSynthesizedIvars(PD, Ivars); |
| 872 | } |
| 873 | } |
| 874 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 875 | unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) { |
| 876 | unsigned count = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 877 | for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), |
| 878 | E = PD->prop_end(); I != E; ++I) |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 879 | if ((*I)->getPropertyIvarDecl()) |
| 880 | ++count; |
| 881 | |
| 882 | // Also look into nested protocols. |
| 883 | for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), |
| 884 | E = PD->protocol_end(); P != E; ++P) |
| 885 | count += CountProtocolSynthesizedIvars(*P); |
| 886 | return count; |
| 887 | } |
| 888 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 889 | unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 890 | unsigned count = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 891 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), |
| 892 | E = OI->prop_end(); I != E; ++I) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 893 | if ((*I)->getPropertyIvarDecl()) |
| 894 | ++count; |
| 895 | } |
| 896 | // Also look into interface's protocol list for properties declared |
| 897 | // in the protocol and whose ivars are synthesized. |
| 898 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 899 | PE = OI->protocol_end(); P != PE; ++P) { |
| 900 | ObjCProtocolDecl *PD = (*P); |
| 901 | count += CountProtocolSynthesizedIvars(PD); |
| 902 | } |
| 903 | return count; |
| 904 | } |
| 905 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 906 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 907 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 908 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 909 | I = ObjCImpls.find(D); |
| 910 | if (I != ObjCImpls.end()) |
| 911 | return cast<ObjCImplementationDecl>(I->second); |
| 912 | return 0; |
| 913 | } |
| 914 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 915 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 916 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 917 | I = ObjCImpls.find(D); |
| 918 | if (I != ObjCImpls.end()) |
| 919 | return cast<ObjCCategoryImplDecl>(I->second); |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 924 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 925 | ObjCImplementationDecl *ImplD) { |
| 926 | assert(IFaceD && ImplD && "Passed null params"); |
| 927 | ObjCImpls[IFaceD] = ImplD; |
| 928 | } |
| 929 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 930 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 931 | ObjCCategoryImplDecl *ImplD) { |
| 932 | assert(CatD && ImplD && "Passed null params"); |
| 933 | ObjCImpls[CatD] = ImplD; |
| 934 | } |
| 935 | |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 936 | /// \brief Allocate an uninitialized DeclaratorInfo. |
| 937 | /// |
| 938 | /// The caller should initialize the memory held by DeclaratorInfo using |
| 939 | /// the TypeLoc wrappers. |
| 940 | /// |
| 941 | /// \param T the type that will be the basis for type source info. This type |
| 942 | /// should refer to how the declarator was written in source code, not to |
| 943 | /// what type semantic analysis resolved the declarator to. |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 944 | DeclaratorInfo *ASTContext::CreateDeclaratorInfo(QualType T, |
| 945 | unsigned DataSize) { |
| 946 | if (!DataSize) |
| 947 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 948 | else |
| 949 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
| 950 | "incorrect data size provided to CreateDeclaratorInfo!"); |
| 951 | |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 952 | DeclaratorInfo *DInfo = |
| 953 | (DeclaratorInfo*)BumpAlloc.Allocate(sizeof(DeclaratorInfo) + DataSize, 8); |
| 954 | new (DInfo) DeclaratorInfo(T); |
| 955 | return DInfo; |
| 956 | } |
| 957 | |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 958 | DeclaratorInfo *ASTContext::getTrivialDeclaratorInfo(QualType T, |
| 959 | SourceLocation L) { |
| 960 | DeclaratorInfo *DI = CreateDeclaratorInfo(T); |
| 961 | DI->getTypeLoc().initialize(L); |
| 962 | return DI; |
| 963 | } |
| 964 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 965 | /// getInterfaceLayoutImpl - Get or compute information about the |
| 966 | /// layout of the given interface. |
| 967 | /// |
| 968 | /// \param Impl - If given, also include the layout of the interface's |
| 969 | /// implementation. This may differ by including synthesized ivars. |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 970 | const ASTRecordLayout & |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 971 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
| 972 | const ObjCImplementationDecl *Impl) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 973 | assert(!D->isForwardDecl() && "Invalid interface decl!"); |
| 974 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 975 | // Look up this layout, if already laid out, return what we have. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | ObjCContainerDecl *Key = |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 977 | Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; |
| 978 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 979 | return *Entry; |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 980 | |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 981 | // Add in synthesized ivar count if laying out an implementation. |
| 982 | if (Impl) { |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 983 | unsigned FieldCount = D->ivar_size(); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 984 | unsigned SynthCount = CountSynthesizedIvars(D); |
| 985 | FieldCount += SynthCount; |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 986 | // If there aren't any sythesized ivars then reuse the interface |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 987 | // entry. Note we can't cache this because we simply free all |
| 988 | // entries later; however we shouldn't look up implementations |
| 989 | // frequently. |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 990 | if (SynthCount == 0) |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 991 | return getObjCLayout(D, 0); |
| 992 | } |
| 993 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 995 | ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl); |
| 996 | ObjCLayouts[Key] = NewEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 998 | return *NewEntry; |
| 999 | } |
| 1000 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1001 | const ASTRecordLayout & |
| 1002 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 1003 | return getObjCLayout(D, 0); |
| 1004 | } |
| 1005 | |
| 1006 | const ASTRecordLayout & |
| 1007 | ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { |
| 1008 | return getObjCLayout(D->getClassInterface(), D); |
| 1009 | } |
| 1010 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 1011 | /// getASTRecordLayout - Get or compute information about the layout of the |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1012 | /// specified record (struct/union/class), which indicates its size and field |
| 1013 | /// position information. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1014 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1015 | D = D->getDefinition(*this); |
| 1016 | assert(D && "Cannot get layout of forward declarations!"); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1017 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1018 | // Look up this layout, if already laid out, return what we have. |
Eli Friedman | ab22c43 | 2009-07-22 20:29:16 +0000 | [diff] [blame] | 1019 | // Note that we can't save a reference to the entry because this function |
| 1020 | // is recursive. |
| 1021 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1022 | if (Entry) return *Entry; |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1023 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1024 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 1025 | ASTRecordLayoutBuilder::ComputeLayout(*this, D); |
Eli Friedman | ab22c43 | 2009-07-22 20:29:16 +0000 | [diff] [blame] | 1026 | ASTRecordLayouts[D] = NewEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1028 | return *NewEntry; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1031 | //===----------------------------------------------------------------------===// |
| 1032 | // Type creation/memoization methods |
| 1033 | //===----------------------------------------------------------------------===// |
| 1034 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1035 | QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) { |
| 1036 | unsigned Fast = Quals.getFastQualifiers(); |
| 1037 | Quals.removeFastQualifiers(); |
| 1038 | |
| 1039 | // Check if we've already instantiated this type. |
| 1040 | llvm::FoldingSetNodeID ID; |
| 1041 | ExtQuals::Profile(ID, TypeNode, Quals); |
| 1042 | void *InsertPos = 0; |
| 1043 | if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 1044 | assert(EQ->getQualifiers() == Quals); |
| 1045 | QualType T = QualType(EQ, Fast); |
| 1046 | return T; |
| 1047 | } |
| 1048 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1049 | ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1050 | ExtQualNodes.InsertNode(New, InsertPos); |
| 1051 | QualType T = QualType(New, Fast); |
| 1052 | return T; |
| 1053 | } |
| 1054 | |
| 1055 | QualType ASTContext::getVolatileType(QualType T) { |
| 1056 | QualType CanT = getCanonicalType(T); |
| 1057 | if (CanT.isVolatileQualified()) return T; |
| 1058 | |
| 1059 | QualifierCollector Quals; |
| 1060 | const Type *TypeNode = Quals.strip(T); |
| 1061 | Quals.addVolatile(); |
| 1062 | |
| 1063 | return getExtQualType(TypeNode, Quals); |
| 1064 | } |
| 1065 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1066 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1067 | QualType CanT = getCanonicalType(T); |
| 1068 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1069 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1070 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1071 | // If we are composing extended qualifiers together, merge together |
| 1072 | // into one ExtQuals node. |
| 1073 | QualifierCollector Quals; |
| 1074 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1075 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1076 | // If this type already has an address space specified, it cannot get |
| 1077 | // another one. |
| 1078 | assert(!Quals.hasAddressSpace() && |
| 1079 | "Type cannot be in multiple addr spaces!"); |
| 1080 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1082 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1085 | QualType ASTContext::getObjCGCQualType(QualType T, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1086 | Qualifiers::GC GCAttr) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1087 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1088 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1089 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1090 | |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1091 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1092 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1093 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1094 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1095 | return getPointerType(ResultType); |
| 1096 | } |
| 1097 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1099 | // If we are composing extended qualifiers together, merge together |
| 1100 | // into one ExtQuals node. |
| 1101 | QualifierCollector Quals; |
| 1102 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1104 | // If this type already has an ObjCGC specified, it cannot get |
| 1105 | // another one. |
| 1106 | assert(!Quals.hasObjCGCAttr() && |
| 1107 | "Type cannot have multiple ObjCGCs!"); |
| 1108 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1109 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1110 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1111 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1112 | |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1113 | QualType ASTContext::getNoReturnType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1114 | QualType ResultType; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1115 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1116 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1117 | ResultType = getNoReturnType(Pointee); |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 1118 | ResultType = getPointerType(ResultType); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1119 | } else if (T->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1120 | QualType Pointee = T->getAs<BlockPointerType>()->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1121 | ResultType = getNoReturnType(Pointee); |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 1122 | ResultType = getBlockPointerType(ResultType); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1123 | } else { |
| 1124 | assert (T->isFunctionType() |
| 1125 | && "can't noreturn qualify non-pointer to function or block type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1126 | |
Benjamin Kramer | bdbeeb5 | 2009-09-25 11:47:22 +0000 | [diff] [blame] | 1127 | if (const FunctionNoProtoType *FNPT = T->getAs<FunctionNoProtoType>()) { |
| 1128 | ResultType = getFunctionNoProtoType(FNPT->getResultType(), true); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1129 | } else { |
| 1130 | const FunctionProtoType *F = T->getAs<FunctionProtoType>(); |
| 1131 | ResultType |
| 1132 | = getFunctionType(F->getResultType(), F->arg_type_begin(), |
| 1133 | F->getNumArgs(), F->isVariadic(), F->getTypeQuals(), |
| 1134 | F->hasExceptionSpec(), F->hasAnyExceptionSpec(), |
| 1135 | F->getNumExceptions(), F->exception_begin(), true); |
| 1136 | } |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1137 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1138 | |
| 1139 | return getQualifiedType(ResultType, T.getQualifiers()); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1142 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1143 | /// number with the specified element type. |
| 1144 | QualType ASTContext::getComplexType(QualType T) { |
| 1145 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1146 | // structure. |
| 1147 | llvm::FoldingSetNodeID ID; |
| 1148 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1150 | void *InsertPos = 0; |
| 1151 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1152 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1154 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1155 | // so fill in the canonical type field. |
| 1156 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1157 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1158 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1160 | // Get the new insert position for the node we care about. |
| 1161 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1162 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1163 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1164 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1165 | Types.push_back(New); |
| 1166 | ComplexTypes.InsertNode(New, InsertPos); |
| 1167 | return QualType(New, 0); |
| 1168 | } |
| 1169 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1170 | QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) { |
| 1171 | llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ? |
| 1172 | SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes; |
| 1173 | FixedWidthIntType *&Entry = Map[Width]; |
| 1174 | if (!Entry) |
| 1175 | Entry = new FixedWidthIntType(Width, Signed); |
| 1176 | return QualType(Entry, 0); |
| 1177 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1178 | |
| 1179 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1180 | /// the specified type. |
| 1181 | QualType ASTContext::getPointerType(QualType T) { |
| 1182 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1183 | // structure. |
| 1184 | llvm::FoldingSetNodeID ID; |
| 1185 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1187 | void *InsertPos = 0; |
| 1188 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1189 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1190 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1191 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1192 | // so fill in the canonical type field. |
| 1193 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1194 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1195 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1197 | // Get the new insert position for the node we care about. |
| 1198 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1199 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1200 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1201 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1202 | Types.push_back(New); |
| 1203 | PointerTypes.InsertNode(New, InsertPos); |
| 1204 | return QualType(New, 0); |
| 1205 | } |
| 1206 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1208 | /// a pointer to the specified block. |
| 1209 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1210 | assert(T->isFunctionType() && "block of function types only"); |
| 1211 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1212 | // structure. |
| 1213 | llvm::FoldingSetNodeID ID; |
| 1214 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1215 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1216 | void *InsertPos = 0; |
| 1217 | if (BlockPointerType *PT = |
| 1218 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1219 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | |
| 1221 | // 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] | 1222 | // type either so fill in the canonical type field. |
| 1223 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1224 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1225 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1227 | // Get the new insert position for the node we care about. |
| 1228 | BlockPointerType *NewIP = |
| 1229 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1230 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1231 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1232 | BlockPointerType *New |
| 1233 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1234 | Types.push_back(New); |
| 1235 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1236 | return QualType(New, 0); |
| 1237 | } |
| 1238 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1239 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1240 | /// lvalue reference to the specified type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1241 | QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1242 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1243 | // structure. |
| 1244 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1245 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1246 | |
| 1247 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1248 | if (LValueReferenceType *RT = |
| 1249 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1250 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1251 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1252 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1253 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1254 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1255 | // either, so fill in the canonical type field. |
| 1256 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1257 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1258 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1259 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1260 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1261 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1262 | LValueReferenceType *NewIP = |
| 1263 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1264 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1267 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1268 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1269 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1270 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1271 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1272 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1273 | return QualType(New, 0); |
| 1274 | } |
| 1275 | |
| 1276 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1277 | /// rvalue reference to the specified type. |
| 1278 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1279 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1280 | // structure. |
| 1281 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1282 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1283 | |
| 1284 | void *InsertPos = 0; |
| 1285 | if (RValueReferenceType *RT = |
| 1286 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1287 | return QualType(RT, 0); |
| 1288 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1289 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1290 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1291 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1292 | // either, so fill in the canonical type field. |
| 1293 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1294 | if (InnerRef || !T.isCanonical()) { |
| 1295 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1296 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1297 | |
| 1298 | // Get the new insert position for the node we care about. |
| 1299 | RValueReferenceType *NewIP = |
| 1300 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1301 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1302 | } |
| 1303 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1304 | RValueReferenceType *New |
| 1305 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1306 | Types.push_back(New); |
| 1307 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1308 | return QualType(New, 0); |
| 1309 | } |
| 1310 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1311 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1312 | /// member pointer to the specified type, in the specified class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1314 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1315 | // structure. |
| 1316 | llvm::FoldingSetNodeID ID; |
| 1317 | MemberPointerType::Profile(ID, T, Cls); |
| 1318 | |
| 1319 | void *InsertPos = 0; |
| 1320 | if (MemberPointerType *PT = |
| 1321 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1322 | return QualType(PT, 0); |
| 1323 | |
| 1324 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1325 | // type either, so fill in the canonical type field. |
| 1326 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1327 | if (!T.isCanonical()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1328 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1329 | |
| 1330 | // Get the new insert position for the node we care about. |
| 1331 | MemberPointerType *NewIP = |
| 1332 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1333 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1334 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1335 | MemberPointerType *New |
| 1336 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1337 | Types.push_back(New); |
| 1338 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1339 | return QualType(New, 0); |
| 1340 | } |
| 1341 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1343 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1344 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1345 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1346 | ArrayType::ArraySizeModifier ASM, |
| 1347 | unsigned EltTypeQuals) { |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1348 | assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) && |
| 1349 | "Constant array of VLAs is illegal!"); |
| 1350 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1351 | // Convert the array size into a canonical width matching the pointer size for |
| 1352 | // the target. |
| 1353 | llvm::APInt ArySize(ArySizeIn); |
| 1354 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1355 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1356 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1357 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1358 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1359 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1361 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1362 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1364 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1365 | // so fill in the canonical type field. |
| 1366 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1367 | if (!EltTy.isCanonical()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1369 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1370 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1372 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1373 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1374 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1375 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1376 | ConstantArrayType *New = new(*this,TypeAlignment) |
| 1377 | ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1378 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1379 | Types.push_back(New); |
| 1380 | return QualType(New, 0); |
| 1381 | } |
| 1382 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1383 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1384 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1385 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1386 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1387 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1388 | unsigned EltTypeQuals, |
| 1389 | SourceRange Brackets) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1390 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1391 | // that have an expression provided for their size. |
| 1392 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1393 | VariableArrayType *New = new(*this, TypeAlignment) |
| 1394 | VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1395 | |
| 1396 | VariableArrayTypes.push_back(New); |
| 1397 | Types.push_back(New); |
| 1398 | return QualType(New, 0); |
| 1399 | } |
| 1400 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1401 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1402 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1403 | /// type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1404 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1405 | Expr *NumElts, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1406 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1407 | unsigned EltTypeQuals, |
| 1408 | SourceRange Brackets) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1409 | assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1410 | "Size must be type- or value-dependent!"); |
| 1411 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1412 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1413 | DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM, |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1414 | EltTypeQuals, NumElts); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1415 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1416 | void *InsertPos = 0; |
| 1417 | DependentSizedArrayType *Canon |
| 1418 | = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1419 | DependentSizedArrayType *New; |
| 1420 | if (Canon) { |
| 1421 | // We already have a canonical version of this array type; use it as |
| 1422 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1423 | New = new (*this, TypeAlignment) |
| 1424 | DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), |
| 1425 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1426 | } else { |
| 1427 | QualType CanonEltTy = getCanonicalType(EltTy); |
| 1428 | if (CanonEltTy == EltTy) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1429 | New = new (*this, TypeAlignment) |
| 1430 | DependentSizedArrayType(*this, EltTy, QualType(), |
| 1431 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1432 | DependentSizedArrayTypes.InsertNode(New, InsertPos); |
| 1433 | } else { |
| 1434 | QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts, |
| 1435 | ASM, EltTypeQuals, |
| 1436 | SourceRange()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1437 | New = new (*this, TypeAlignment) |
| 1438 | DependentSizedArrayType(*this, EltTy, Canon, |
| 1439 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1440 | } |
| 1441 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1443 | Types.push_back(New); |
| 1444 | return QualType(New, 0); |
| 1445 | } |
| 1446 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1447 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1448 | ArrayType::ArraySizeModifier ASM, |
| 1449 | unsigned EltTypeQuals) { |
| 1450 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1451 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1452 | |
| 1453 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1454 | if (IncompleteArrayType *ATP = |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1455 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1456 | return QualType(ATP, 0); |
| 1457 | |
| 1458 | // If the element type isn't canonical, this won't be a canonical type |
| 1459 | // either, so fill in the canonical type field. |
| 1460 | QualType Canonical; |
| 1461 | |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1462 | if (!EltTy.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1463 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1464 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1465 | |
| 1466 | // Get the new insert position for the node we care about. |
| 1467 | IncompleteArrayType *NewIP = |
| 1468 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1469 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1470 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1471 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1472 | IncompleteArrayType *New = new (*this, TypeAlignment) |
| 1473 | IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1474 | |
| 1475 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1476 | Types.push_back(New); |
| 1477 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1480 | /// getVectorType - Return the unique reference to a vector type of |
| 1481 | /// the specified element type and size. VectorType must be a built-in type. |
| 1482 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1483 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1485 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1486 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1488 | // Check if we've already instantiated a vector of this type. |
| 1489 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1490 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1491 | void *InsertPos = 0; |
| 1492 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1493 | return QualType(VTP, 0); |
| 1494 | |
| 1495 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1496 | // so fill in the canonical type field. |
| 1497 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1498 | if (!vecType.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1499 | Canonical = getVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1501 | // Get the new insert position for the node we care about. |
| 1502 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1503 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1504 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1505 | VectorType *New = new (*this, TypeAlignment) |
| 1506 | VectorType(vecType, NumElts, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1507 | VectorTypes.InsertNode(New, InsertPos); |
| 1508 | Types.push_back(New); |
| 1509 | return QualType(New, 0); |
| 1510 | } |
| 1511 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1512 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1513 | /// 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] | 1514 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1515 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1517 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1518 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1520 | // Check if we've already instantiated a vector of this type. |
| 1521 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1523 | void *InsertPos = 0; |
| 1524 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1525 | return QualType(VTP, 0); |
| 1526 | |
| 1527 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1528 | // so fill in the canonical type field. |
| 1529 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1530 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1531 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1533 | // Get the new insert position for the node we care about. |
| 1534 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1535 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1536 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1537 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1538 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1539 | VectorTypes.InsertNode(New, InsertPos); |
| 1540 | Types.push_back(New); |
| 1541 | return QualType(New, 0); |
| 1542 | } |
| 1543 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | QualType ASTContext::getDependentSizedExtVectorType(QualType vecType, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1545 | Expr *SizeExpr, |
| 1546 | SourceLocation AttrLoc) { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1547 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1549 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1551 | void *InsertPos = 0; |
| 1552 | DependentSizedExtVectorType *Canon |
| 1553 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1554 | DependentSizedExtVectorType *New; |
| 1555 | if (Canon) { |
| 1556 | // We already have a canonical version of this array type; use it as |
| 1557 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1558 | New = new (*this, TypeAlignment) |
| 1559 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1560 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1561 | } else { |
| 1562 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1563 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1564 | New = new (*this, TypeAlignment) |
| 1565 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1566 | AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1567 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1568 | } else { |
| 1569 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1570 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1571 | New = new (*this, TypeAlignment) |
| 1572 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1573 | } |
| 1574 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1575 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1576 | Types.push_back(New); |
| 1577 | return QualType(New, 0); |
| 1578 | } |
| 1579 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1580 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1581 | /// |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1582 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1583 | // Unique functions, to guarantee there is only one function of a particular |
| 1584 | // structure. |
| 1585 | llvm::FoldingSetNodeID ID; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1586 | FunctionNoProtoType::Profile(ID, ResultTy, NoReturn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1588 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1589 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1590 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1591 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1593 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1594 | if (!ResultTy.isCanonical()) { |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1595 | Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1597 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1598 | FunctionNoProtoType *NewIP = |
| 1599 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1600 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1601 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1603 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
| 1604 | FunctionNoProtoType(ResultTy, Canonical, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1605 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1606 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1607 | return QualType(New, 0); |
| 1608 | } |
| 1609 | |
| 1610 | /// getFunctionType - Return a normal function type with a typed argument |
| 1611 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1612 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1613 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1614 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1615 | bool hasAnyExceptionSpec, unsigned NumExs, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1616 | const QualType *ExArray, bool NoReturn) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1617 | // Unique functions, to guarantee there is only one function of a particular |
| 1618 | // structure. |
| 1619 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1620 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1621 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1622 | NumExs, ExArray, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1623 | |
| 1624 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1626 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1627 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1628 | |
| 1629 | // Determine whether the type being created is already canonical or not. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1630 | bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1631 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1632 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1633 | isCanonical = false; |
| 1634 | |
| 1635 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1636 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1637 | QualType Canonical; |
| 1638 | if (!isCanonical) { |
| 1639 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1640 | CanonicalArgs.reserve(NumArgs); |
| 1641 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1642 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1643 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1644 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1645 | CanonicalArgs.data(), NumArgs, |
Douglas Gregor | 47259d9 | 2009-08-05 19:03:35 +0000 | [diff] [blame] | 1646 | isVariadic, TypeQuals, false, |
| 1647 | false, 0, 0, NoReturn); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1648 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1649 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1650 | FunctionProtoType *NewIP = |
| 1651 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1652 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1653 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1654 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1655 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1656 | // for two variable size arrays (for parameter and exception types) at the |
| 1657 | // end of them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | FunctionProtoType *FTP = |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1659 | (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + |
| 1660 | NumArgs*sizeof(QualType) + |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1661 | NumExs*sizeof(QualType), TypeAlignment); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1662 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1663 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1664 | ExArray, NumExs, Canonical, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1665 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1666 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1667 | return QualType(FTP, 0); |
| 1668 | } |
| 1669 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1670 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1671 | /// specified type declaration. |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1672 | QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1673 | assert(Decl && "Passed null for Decl param"); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1674 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1675 | |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1676 | if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1677 | return getTypedefType(Typedef); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1678 | else if (isa<TemplateTypeParmDecl>(Decl)) { |
| 1679 | assert(false && "Template type parameter types are always available."); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1680 | } else if (ObjCInterfaceDecl *ObjCInterface |
| 1681 | = dyn_cast<ObjCInterfaceDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1682 | return getObjCInterfaceType(ObjCInterface); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1683 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1684 | if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1685 | if (PrevDecl) |
| 1686 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1687 | else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1688 | Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1689 | } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1690 | if (PrevDecl) |
| 1691 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1692 | else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1693 | Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1694 | } else |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1695 | assert(false && "TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1696 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1697 | if (!PrevDecl) Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1698 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1701 | /// getTypedefType - Return the unique reference to the type for the |
| 1702 | /// specified typename decl. |
| 1703 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 1704 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1706 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1707 | Decl->TypeForDecl = new(*this, TypeAlignment) |
| 1708 | TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1709 | Types.push_back(Decl->TypeForDecl); |
| 1710 | return QualType(Decl->TypeForDecl, 0); |
| 1711 | } |
| 1712 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1713 | /// \brief Retrieve a substitution-result type. |
| 1714 | QualType |
| 1715 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
| 1716 | QualType Replacement) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1717 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1718 | && "replacement types must always be canonical"); |
| 1719 | |
| 1720 | llvm::FoldingSetNodeID ID; |
| 1721 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 1722 | void *InsertPos = 0; |
| 1723 | SubstTemplateTypeParmType *SubstParm |
| 1724 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1725 | |
| 1726 | if (!SubstParm) { |
| 1727 | SubstParm = new (*this, TypeAlignment) |
| 1728 | SubstTemplateTypeParmType(Parm, Replacement); |
| 1729 | Types.push_back(SubstParm); |
| 1730 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 1731 | } |
| 1732 | |
| 1733 | return QualType(SubstParm, 0); |
| 1734 | } |
| 1735 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1736 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1738 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1739 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1740 | bool ParameterPack, |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1741 | IdentifierInfo *Name) { |
| 1742 | llvm::FoldingSetNodeID ID; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1743 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1744 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1745 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1746 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1747 | |
| 1748 | if (TypeParm) |
| 1749 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1751 | if (Name) { |
| 1752 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1753 | TypeParm = new (*this, TypeAlignment) |
| 1754 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1755 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1756 | TypeParm = new (*this, TypeAlignment) |
| 1757 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1758 | |
| 1759 | Types.push_back(TypeParm); |
| 1760 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1761 | |
| 1762 | return QualType(TypeParm, 0); |
| 1763 | } |
| 1764 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1766 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
| 1767 | const TemplateArgument *Args, |
| 1768 | unsigned NumArgs, |
| 1769 | QualType Canon) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1770 | if (!Canon.isNull()) |
| 1771 | Canon = getCanonicalType(Canon); |
| 1772 | else { |
| 1773 | // Build the canonical template specialization type. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1774 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 1775 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 1776 | CanonArgs.reserve(NumArgs); |
| 1777 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1778 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 1779 | |
| 1780 | // Determine whether this canonical template specialization type already |
| 1781 | // exists. |
| 1782 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1784 | CanonArgs.data(), NumArgs, *this); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1785 | |
| 1786 | void *InsertPos = 0; |
| 1787 | TemplateSpecializationType *Spec |
| 1788 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1789 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1790 | if (!Spec) { |
| 1791 | // Allocate a new canonical template specialization type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1792 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1793 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1794 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1795 | Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1796 | CanonArgs.data(), NumArgs, |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1797 | Canon); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1798 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1800 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1801 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1802 | if (Canon.isNull()) |
| 1803 | Canon = QualType(Spec, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1804 | assert(Canon->isDependentType() && |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1805 | "Non-dependent template-id type must have a canonical type"); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1806 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 1807 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1808 | // Allocate the (non-canonical) template specialization type, but don't |
| 1809 | // try to unique it: these types typically have location information that |
| 1810 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1812 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1813 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1814 | TemplateSpecializationType *Spec |
| 1815 | = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1816 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1817 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1818 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1819 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 | QualType |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1823 | ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS, |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1824 | QualType NamedType) { |
| 1825 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1826 | QualifiedNameType::Profile(ID, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1827 | |
| 1828 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1829 | QualifiedNameType *T |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1830 | = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1831 | if (T) |
| 1832 | return QualType(T, 0); |
| 1833 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1834 | T = new (*this) QualifiedNameType(NNS, NamedType, |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1835 | getCanonicalType(NamedType)); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1836 | Types.push_back(T); |
| 1837 | QualifiedNameTypes.InsertNode(T, InsertPos); |
| 1838 | return QualType(T, 0); |
| 1839 | } |
| 1840 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1842 | const IdentifierInfo *Name, |
| 1843 | QualType Canon) { |
| 1844 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1845 | |
| 1846 | if (Canon.isNull()) { |
| 1847 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 1848 | if (CanonNNS != NNS) |
| 1849 | Canon = getTypenameType(CanonNNS, Name); |
| 1850 | } |
| 1851 | |
| 1852 | llvm::FoldingSetNodeID ID; |
| 1853 | TypenameType::Profile(ID, NNS, Name); |
| 1854 | |
| 1855 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | TypenameType *T |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1857 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1858 | if (T) |
| 1859 | return QualType(T, 0); |
| 1860 | |
| 1861 | T = new (*this) TypenameType(NNS, Name, Canon); |
| 1862 | Types.push_back(T); |
| 1863 | TypenameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1864 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | QualType |
| 1868 | ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1869 | const TemplateSpecializationType *TemplateId, |
| 1870 | QualType Canon) { |
| 1871 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1872 | |
| 1873 | if (Canon.isNull()) { |
| 1874 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 1875 | QualType CanonType = getCanonicalType(QualType(TemplateId, 0)); |
| 1876 | if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) { |
| 1877 | const TemplateSpecializationType *CanonTemplateId |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1878 | = CanonType->getAs<TemplateSpecializationType>(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1879 | assert(CanonTemplateId && |
| 1880 | "Canonical type must also be a template specialization type"); |
| 1881 | Canon = getTypenameType(CanonNNS, CanonTemplateId); |
| 1882 | } |
| 1883 | } |
| 1884 | |
| 1885 | llvm::FoldingSetNodeID ID; |
| 1886 | TypenameType::Profile(ID, NNS, TemplateId); |
| 1887 | |
| 1888 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1889 | TypenameType *T |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1890 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1891 | if (T) |
| 1892 | return QualType(T, 0); |
| 1893 | |
| 1894 | T = new (*this) TypenameType(NNS, TemplateId, Canon); |
| 1895 | Types.push_back(T); |
| 1896 | TypenameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1897 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 1900 | QualType |
| 1901 | ASTContext::getElaboratedType(QualType UnderlyingType, |
| 1902 | ElaboratedType::TagKind Tag) { |
| 1903 | llvm::FoldingSetNodeID ID; |
| 1904 | ElaboratedType::Profile(ID, UnderlyingType, Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 1906 | void *InsertPos = 0; |
| 1907 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1908 | if (T) |
| 1909 | return QualType(T, 0); |
| 1910 | |
| 1911 | QualType Canon = getCanonicalType(UnderlyingType); |
| 1912 | |
| 1913 | T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon); |
| 1914 | Types.push_back(T); |
| 1915 | ElaboratedTypes.InsertNode(T, InsertPos); |
| 1916 | return QualType(T, 0); |
| 1917 | } |
| 1918 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1919 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 1920 | /// alphabetically. |
| 1921 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 1922 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1923 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1926 | static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols, |
| 1927 | unsigned NumProtocols) { |
| 1928 | if (NumProtocols == 0) return true; |
| 1929 | |
| 1930 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 1931 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 1932 | return false; |
| 1933 | return true; |
| 1934 | } |
| 1935 | |
| 1936 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1937 | unsigned &NumProtocols) { |
| 1938 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1939 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1940 | // Sort protocols, keyed by name. |
| 1941 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 1942 | |
| 1943 | // Remove duplicates. |
| 1944 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 1945 | NumProtocols = ProtocolsEnd-Protocols; |
| 1946 | } |
| 1947 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1948 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 1949 | /// the given interface decl and the conforming protocol list. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1950 | QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | ObjCProtocolDecl **Protocols, |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1952 | unsigned NumProtocols) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1953 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1954 | ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1955 | |
| 1956 | void *InsertPos = 0; |
| 1957 | if (ObjCObjectPointerType *QT = |
| 1958 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1959 | return QualType(QT, 0); |
| 1960 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1961 | // Sort the protocol list alphabetically to canonicalize it. |
| 1962 | QualType Canonical; |
| 1963 | if (!InterfaceT.isCanonical() || |
| 1964 | !areSortedAndUniqued(Protocols, NumProtocols)) { |
| 1965 | if (!areSortedAndUniqued(Protocols, NumProtocols)) { |
| 1966 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); |
| 1967 | unsigned UniqueCount = NumProtocols; |
| 1968 | |
| 1969 | std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); |
| 1970 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
| 1971 | |
| 1972 | Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), |
| 1973 | &Sorted[0], UniqueCount); |
| 1974 | } else { |
| 1975 | Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), |
| 1976 | Protocols, NumProtocols); |
| 1977 | } |
| 1978 | |
| 1979 | // Regenerate InsertPos. |
| 1980 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1981 | } |
| 1982 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1983 | // No Match; |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1984 | ObjCObjectPointerType *QType = new (*this, TypeAlignment) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1985 | ObjCObjectPointerType(Canonical, InterfaceT, Protocols, NumProtocols); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1987 | Types.push_back(QType); |
| 1988 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
| 1989 | return QualType(QType, 0); |
| 1990 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1991 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1992 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 1993 | /// specified ObjC interface decl. The list of protocols is optional. |
| 1994 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1995 | ObjCProtocolDecl **Protocols, unsigned NumProtocols) { |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1996 | llvm::FoldingSetNodeID ID; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1997 | ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1999 | void *InsertPos = 0; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2000 | if (ObjCInterfaceType *QT = |
| 2001 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2002 | return QualType(QT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2004 | // Sort the protocol list alphabetically to canonicalize it. |
| 2005 | QualType Canonical; |
| 2006 | if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2007 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); |
| 2008 | std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); |
| 2009 | |
| 2010 | unsigned UniqueCount = NumProtocols; |
| 2011 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
| 2012 | |
| 2013 | Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount); |
| 2014 | |
| 2015 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2016 | } |
| 2017 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2018 | ObjCInterfaceType *QType = new (*this, TypeAlignment) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2019 | ObjCInterfaceType(Canonical, const_cast<ObjCInterfaceDecl*>(Decl), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2020 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2021 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2022 | Types.push_back(QType); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2023 | ObjCInterfaceTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2024 | return QualType(QType, 0); |
| 2025 | } |
| 2026 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2027 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2028 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2029 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2031 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2032 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2033 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2034 | if (tofExpr->isTypeDependent()) { |
| 2035 | llvm::FoldingSetNodeID ID; |
| 2036 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2037 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2038 | void *InsertPos = 0; |
| 2039 | DependentTypeOfExprType *Canon |
| 2040 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2041 | if (Canon) { |
| 2042 | // We already have a "canonical" version of an identical, dependent |
| 2043 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2044 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2045 | QualType((TypeOfExprType*)Canon, 0)); |
| 2046 | } |
| 2047 | else { |
| 2048 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2049 | Canon |
| 2050 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2051 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2052 | toe = Canon; |
| 2053 | } |
| 2054 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2055 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2056 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2057 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2058 | Types.push_back(toe); |
| 2059 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2062 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2063 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2064 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2065 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2066 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2067 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2068 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2069 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2070 | Types.push_back(tot); |
| 2071 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2074 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2075 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
| 2076 | static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2077 | if (e->isTypeDependent()) |
| 2078 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2080 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2081 | // as the type of the entity named by e. |
| 2082 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2083 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2084 | return VD->getType(); |
| 2085 | } |
| 2086 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2087 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2088 | return FD->getType(); |
| 2089 | } |
| 2090 | // If e is a function call or an invocation of an overloaded operator, |
| 2091 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2092 | // return type of that function. |
| 2093 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2094 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2095 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2096 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2097 | |
| 2098 | // 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] | 2099 | // defined as T&, otherwise decltype(e) is defined as T. |
| 2100 | if (e->isLvalue(Context) == Expr::LV_Valid) |
| 2101 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2102 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2103 | return T; |
| 2104 | } |
| 2105 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2106 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2107 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2108 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2109 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2110 | /// on canonical type's (which are always unique). |
| 2111 | QualType ASTContext::getDecltypeType(Expr *e) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2112 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2113 | if (e->isTypeDependent()) { |
| 2114 | llvm::FoldingSetNodeID ID; |
| 2115 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2117 | void *InsertPos = 0; |
| 2118 | DependentDecltypeType *Canon |
| 2119 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2120 | if (Canon) { |
| 2121 | // We already have a "canonical" version of an equivalent, dependent |
| 2122 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2123 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2124 | QualType((DecltypeType*)Canon, 0)); |
| 2125 | } |
| 2126 | else { |
| 2127 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2128 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2129 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2130 | dt = Canon; |
| 2131 | } |
| 2132 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2133 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2134 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2135 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2136 | Types.push_back(dt); |
| 2137 | return QualType(dt, 0); |
| 2138 | } |
| 2139 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2140 | /// getTagDeclType - Return the unique reference to the type for the |
| 2141 | /// specified TagDecl (struct/union/class/enum) decl. |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2142 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2143 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2144 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2145 | // away const? mutable? |
| 2146 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2149 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2150 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2151 | /// needs to agree with the definition in <stddef.h>. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2152 | QualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2153 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2156 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2157 | /// Used when in C++, as a GCC extension. |
| 2158 | QualType ASTContext::getSignedWCharType() const { |
| 2159 | // FIXME: derive from "Target" ? |
| 2160 | return WCharTy; |
| 2161 | } |
| 2162 | |
| 2163 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2164 | /// Used when in C++, as a GCC extension. |
| 2165 | QualType ASTContext::getUnsignedWCharType() const { |
| 2166 | // FIXME: derive from "Target" ? |
| 2167 | return UnsignedIntTy; |
| 2168 | } |
| 2169 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2170 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2171 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2172 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2173 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2174 | } |
| 2175 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2176 | //===----------------------------------------------------------------------===// |
| 2177 | // Type Operators |
| 2178 | //===----------------------------------------------------------------------===// |
| 2179 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2180 | CanQualType ASTContext::getCanonicalParamType(QualType T) { |
| 2181 | // Push qualifiers into arrays, and then discard any remaining |
| 2182 | // qualifiers. |
| 2183 | T = getCanonicalType(T); |
| 2184 | const Type *Ty = T.getTypePtr(); |
| 2185 | |
| 2186 | QualType Result; |
| 2187 | if (isa<ArrayType>(Ty)) { |
| 2188 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2189 | } else if (isa<FunctionType>(Ty)) { |
| 2190 | Result = getPointerType(QualType(Ty, 0)); |
| 2191 | } else { |
| 2192 | Result = QualType(Ty, 0); |
| 2193 | } |
| 2194 | |
| 2195 | return CanQualType::CreateUnsafe(Result); |
| 2196 | } |
| 2197 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2198 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 2199 | /// the specified potentially non-canonical type. The non-canonical version |
| 2200 | /// of a type may have many "decorated" versions of types. Decorators can |
| 2201 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 2202 | /// to be free of any of these, allowing two canonical types to be compared |
| 2203 | /// for exact equality with a simple pointer comparison. |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2204 | CanQualType ASTContext::getCanonicalType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2205 | QualifierCollector Quals; |
| 2206 | const Type *Ptr = Quals.strip(T); |
| 2207 | QualType CanType = Ptr->getCanonicalTypeInternal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2208 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2209 | // The canonical internal type will be the canonical type *except* |
| 2210 | // that we push type qualifiers down through array types. |
| 2211 | |
| 2212 | // If there are no new qualifiers to push down, stop here. |
| 2213 | if (!Quals.hasQualifiers()) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2214 | return CanQualType::CreateUnsafe(CanType); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2215 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2216 | // If the type qualifiers are on an array type, get the canonical |
| 2217 | // type of the array with the qualifiers applied to the element |
| 2218 | // type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2219 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 2220 | if (!AT) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2221 | return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2222 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2223 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2224 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2225 | QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2226 | NewEltTy = getCanonicalType(NewEltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2227 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2228 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2229 | return CanQualType::CreateUnsafe( |
| 2230 | getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2231 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2232 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2233 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2234 | return CanQualType::CreateUnsafe( |
| 2235 | getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2236 | IAT->getIndexTypeCVRQualifiers())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2237 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2238 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2239 | return CanQualType::CreateUnsafe( |
| 2240 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2241 | DSAT->getSizeExpr() ? |
| 2242 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2243 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2244 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2245 | DSAT->getBracketsRange())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2246 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2247 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2248 | return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2249 | VAT->getSizeExpr() ? |
| 2250 | VAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2251 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2252 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2253 | VAT->getBracketsRange())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2256 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
| 2257 | // If this template name refers to a template, the canonical |
| 2258 | // template name merely stores the template itself. |
| 2259 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2260 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2261 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2262 | // If this template name refers to a set of overloaded function templates, |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 2263 | /// the canonical template name merely stores the set of function templates. |
Douglas Gregor | 6ebd15e | 2009-07-31 05:24:01 +0000 | [diff] [blame] | 2264 | if (OverloadedFunctionDecl *Ovl = Name.getAsOverloadedFunctionDecl()) { |
| 2265 | OverloadedFunctionDecl *CanonOvl = 0; |
| 2266 | for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), |
| 2267 | FEnd = Ovl->function_end(); |
| 2268 | F != FEnd; ++F) { |
| 2269 | Decl *Canon = F->get()->getCanonicalDecl(); |
| 2270 | if (CanonOvl || Canon != F->get()) { |
| 2271 | if (!CanonOvl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | CanonOvl = OverloadedFunctionDecl::Create(*this, |
| 2273 | Ovl->getDeclContext(), |
Douglas Gregor | 6ebd15e | 2009-07-31 05:24:01 +0000 | [diff] [blame] | 2274 | Ovl->getDeclName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2275 | |
Douglas Gregor | 6ebd15e | 2009-07-31 05:24:01 +0000 | [diff] [blame] | 2276 | CanonOvl->addOverload( |
| 2277 | AnyFunctionDecl::getFromNamedDecl(cast<NamedDecl>(Canon))); |
| 2278 | } |
| 2279 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2280 | |
Douglas Gregor | 6ebd15e | 2009-07-31 05:24:01 +0000 | [diff] [blame] | 2281 | return TemplateName(CanonOvl? CanonOvl : Ovl); |
| 2282 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2283 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2284 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2285 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2286 | return DTN->CanonicalTemplateName; |
| 2287 | } |
| 2288 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2289 | TemplateArgument |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2290 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { |
| 2291 | switch (Arg.getKind()) { |
| 2292 | case TemplateArgument::Null: |
| 2293 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2294 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2295 | case TemplateArgument::Expression: |
| 2296 | // FIXME: Build canonical expression? |
| 2297 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2298 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2299 | case TemplateArgument::Declaration: |
| 2300 | return TemplateArgument(SourceLocation(), |
| 2301 | Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2302 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2303 | case TemplateArgument::Integral: |
| 2304 | return TemplateArgument(SourceLocation(), |
| 2305 | *Arg.getAsIntegral(), |
| 2306 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2307 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2308 | case TemplateArgument::Type: |
| 2309 | return TemplateArgument(SourceLocation(), |
| 2310 | getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2311 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2312 | case TemplateArgument::Pack: { |
| 2313 | // FIXME: Allocate in ASTContext |
| 2314 | TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()]; |
| 2315 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2316 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2317 | AEnd = Arg.pack_end(); |
| 2318 | A != AEnd; (void)++A, ++Idx) |
| 2319 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2320 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2321 | TemplateArgument Result; |
| 2322 | Result.setArgumentPack(CanonArgs, Arg.pack_size(), false); |
| 2323 | return Result; |
| 2324 | } |
| 2325 | } |
| 2326 | |
| 2327 | // Silence GCC warning |
| 2328 | assert(false && "Unhandled template argument kind"); |
| 2329 | return TemplateArgument(); |
| 2330 | } |
| 2331 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2332 | NestedNameSpecifier * |
| 2333 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2335 | return 0; |
| 2336 | |
| 2337 | switch (NNS->getKind()) { |
| 2338 | case NestedNameSpecifier::Identifier: |
| 2339 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2341 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2342 | NNS->getAsIdentifier()); |
| 2343 | |
| 2344 | case NestedNameSpecifier::Namespace: |
| 2345 | // A namespace is canonical; build a nested-name-specifier with |
| 2346 | // this namespace and no prefix. |
| 2347 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2348 | |
| 2349 | case NestedNameSpecifier::TypeSpec: |
| 2350 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2351 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2352 | return NestedNameSpecifier::Create(*this, 0, |
| 2353 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2354 | T.getTypePtr()); |
| 2355 | } |
| 2356 | |
| 2357 | case NestedNameSpecifier::Global: |
| 2358 | // The global specifier is canonical and unique. |
| 2359 | return NNS; |
| 2360 | } |
| 2361 | |
| 2362 | // Required to silence a GCC warning |
| 2363 | return 0; |
| 2364 | } |
| 2365 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2366 | |
| 2367 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 2368 | // Handle the non-qualified case efficiently. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2369 | if (!T.hasQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2370 | // Handle the common positive case fast. |
| 2371 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2372 | return AT; |
| 2373 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2374 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2375 | // Handle the common negative case fast. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2376 | QualType CType = T->getCanonicalTypeInternal(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2377 | if (!isa<ArrayType>(CType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2378 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2379 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2380 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2381 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2382 | // 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] | 2383 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2384 | // If we get here, we either have type qualifiers on the type, or we have |
| 2385 | // 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] | 2386 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2387 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2388 | QualifierCollector Qs; |
| 2389 | const Type *Ty = Qs.strip(T.getDesugaredType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2390 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2391 | // If we have a simple case, just return now. |
| 2392 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2393 | if (ATy == 0 || Qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2394 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2395 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2396 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 2397 | // qualifiers into the array element type and return a new array type. |
| 2398 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2399 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2400 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2401 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2402 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 2403 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2404 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2405 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2406 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 2407 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 2408 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2409 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2410 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2411 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2412 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 2413 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2414 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2415 | DSAT->getSizeExpr() ? |
| 2416 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2417 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2418 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2419 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2420 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2421 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2422 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2423 | VAT->getSizeExpr() ? |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2424 | VAT->getSizeExpr()->Retain() : 0, |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2425 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2426 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2427 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2431 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 2432 | /// specified array type to a pointer. This operation is non-trivial when |
| 2433 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 2434 | /// this returns a pointer to a properly qualified element of the array. |
| 2435 | /// |
| 2436 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 2437 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2438 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 2439 | // typedefs in the element type of the array. This also handles propagation |
| 2440 | // of type qualifiers from the array type into the element type if present |
| 2441 | // (C99 6.7.3p8). |
| 2442 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 2443 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2444 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2445 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2446 | |
| 2447 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2448 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2451 | QualType ASTContext::getBaseElementType(QualType QT) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2452 | QualifierCollector Qs; |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2453 | while (true) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2454 | const Type *UT = Qs.strip(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2455 | if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) { |
| 2456 | QT = AT->getElementType(); |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 2457 | } else { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2458 | return Qs.apply(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2459 | } |
| 2460 | } |
| 2461 | } |
| 2462 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2463 | QualType ASTContext::getBaseElementType(const ArrayType *AT) { |
| 2464 | QualType ElemTy = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2465 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2466 | if (const ArrayType *AT = getAsArrayType(ElemTy)) |
| 2467 | return getBaseElementType(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2468 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 2469 | return ElemTy; |
| 2470 | } |
| 2471 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2472 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2473 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2474 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 2475 | uint64_t ElementCount = 1; |
| 2476 | do { |
| 2477 | ElementCount *= CA->getSize().getZExtValue(); |
| 2478 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 2479 | } while (CA); |
| 2480 | return ElementCount; |
| 2481 | } |
| 2482 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2483 | /// getFloatingRank - Return a relative rank for floating point types. |
| 2484 | /// 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] | 2485 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2486 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2487 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2488 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2489 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 2490 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2491 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2492 | case BuiltinType::Float: return FloatRank; |
| 2493 | case BuiltinType::Double: return DoubleRank; |
| 2494 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 2495 | } |
| 2496 | } |
| 2497 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2498 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 2499 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2500 | /// 'typeDomain' is a real floating point or complex type. |
| 2501 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2502 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 2503 | QualType Domain) const { |
| 2504 | FloatingRank EltRank = getFloatingRank(Size); |
| 2505 | if (Domain->isComplexType()) { |
| 2506 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2507 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2508 | case FloatRank: return FloatComplexTy; |
| 2509 | case DoubleRank: return DoubleComplexTy; |
| 2510 | case LongDoubleRank: return LongDoubleComplexTy; |
| 2511 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2512 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2513 | |
| 2514 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 2515 | switch (EltRank) { |
| 2516 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 2517 | case FloatRank: return FloatTy; |
| 2518 | case DoubleRank: return DoubleTy; |
| 2519 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2520 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2523 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 2524 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 2525 | /// '_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] | 2526 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2527 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 2528 | FloatingRank LHSR = getFloatingRank(LHS); |
| 2529 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2530 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2531 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2532 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2533 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2534 | return 1; |
| 2535 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2538 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 2539 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 2540 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2541 | unsigned ASTContext::getIntegerRank(Type *T) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2542 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2543 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
| 2544 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
| 2545 | |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 2546 | if (T->isSpecificBuiltinType(BuiltinType::WChar)) |
| 2547 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 2548 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2549 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 2550 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 2551 | |
| 2552 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 2553 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 2554 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2555 | // There are two things which impact the integer rank: the width, and |
| 2556 | // the ordering of builtins. The builtin ordering is encoded in the |
| 2557 | // bottom three bits; the width is encoded in the bits above that. |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 2558 | if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2559 | return FWIT->getWidth() << 3; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2560 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2561 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2562 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 2563 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2564 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2565 | case BuiltinType::Char_S: |
| 2566 | case BuiltinType::Char_U: |
| 2567 | case BuiltinType::SChar: |
| 2568 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2569 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2570 | case BuiltinType::Short: |
| 2571 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2572 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2573 | case BuiltinType::Int: |
| 2574 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2575 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2576 | case BuiltinType::Long: |
| 2577 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2578 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2579 | case BuiltinType::LongLong: |
| 2580 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2581 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2582 | case BuiltinType::Int128: |
| 2583 | case BuiltinType::UInt128: |
| 2584 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2585 | } |
| 2586 | } |
| 2587 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2588 | /// \brief Whether this is a promotable bitfield reference according |
| 2589 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 2590 | /// |
| 2591 | /// \returns the type this bit-field will promote to, or NULL if no |
| 2592 | /// promotion occurs. |
| 2593 | QualType ASTContext::isPromotableBitField(Expr *E) { |
| 2594 | FieldDecl *Field = E->getBitField(); |
| 2595 | if (!Field) |
| 2596 | return QualType(); |
| 2597 | |
| 2598 | QualType FT = Field->getType(); |
| 2599 | |
| 2600 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 2601 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 2602 | uint64_t IntSize = getTypeSize(IntTy); |
| 2603 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 2604 | // to the size of int, it gets promoted no matter what its type is. |
| 2605 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 2606 | if (BitWidth < IntSize) |
| 2607 | return IntTy; |
| 2608 | |
| 2609 | if (BitWidth == IntSize) |
| 2610 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 2611 | |
| 2612 | // Types bigger than int are not subject to promotions, and therefore act |
| 2613 | // like the base type. |
| 2614 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 2615 | // is ridiculous. |
| 2616 | return QualType(); |
| 2617 | } |
| 2618 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2619 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 2620 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 2621 | /// integer type. |
| 2622 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) { |
| 2623 | assert(!Promotable.isNull()); |
| 2624 | assert(Promotable->isPromotableIntegerType()); |
| 2625 | if (Promotable->isSignedIntegerType()) |
| 2626 | return IntTy; |
| 2627 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 2628 | uint64_t IntSize = getTypeSize(IntTy); |
| 2629 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 2630 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 2631 | } |
| 2632 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2633 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2634 | /// 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] | 2635 | /// LHS < RHS, return -1. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2636 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2637 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 2638 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2639 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2640 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2641 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 2642 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2643 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2644 | unsigned LHSRank = getIntegerRank(LHSC); |
| 2645 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2646 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2647 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 2648 | if (LHSRank == RHSRank) return 0; |
| 2649 | return LHSRank > RHSRank ? 1 : -1; |
| 2650 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2651 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2652 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 2653 | if (LHSUnsigned) { |
| 2654 | // If the unsigned [LHS] type is larger, return it. |
| 2655 | if (LHSRank >= RHSRank) |
| 2656 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2658 | // If the signed type can represent all values of the unsigned type, it |
| 2659 | // 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] | 2660 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2661 | return -1; |
| 2662 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2663 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2664 | // If the unsigned [RHS] type is larger, return it. |
| 2665 | if (RHSRank >= LHSRank) |
| 2666 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2667 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2668 | // If the signed type can represent all values of the unsigned type, it |
| 2669 | // 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] | 2670 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2671 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2672 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2673 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2674 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2675 | QualType ASTContext::getCFConstantStringType() { |
| 2676 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2677 | CFConstantStringTypeDecl = |
| 2678 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2679 | &Idents.get("NSConstantString")); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2680 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2681 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2682 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2683 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2684 | // int flags; |
| 2685 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2686 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2687 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2688 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2689 | FieldTypes[3] = LongTy; |
| 2690 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2691 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2692 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2693 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2694 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2695 | FieldTypes[i], /*DInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2696 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2697 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2698 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | CFConstantStringTypeDecl->completeDefinition(*this); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2702 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2703 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2704 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 2705 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2706 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2707 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2708 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2709 | assert(Rec && "Invalid CFConstantStringType"); |
| 2710 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 2711 | } |
| 2712 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2713 | QualType ASTContext::getObjCFastEnumerationStateType() { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2714 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2715 | ObjCFastEnumerationStateTypeDecl = |
| 2716 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2717 | &Idents.get("__objcFastEnumerationState")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2718 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2719 | QualType FieldTypes[] = { |
| 2720 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2721 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2722 | getPointerType(UnsignedLongTy), |
| 2723 | getConstantArrayType(UnsignedLongTy, |
| 2724 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 2725 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2726 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2727 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2728 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2729 | ObjCFastEnumerationStateTypeDecl, |
| 2730 | SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 2731 | FieldTypes[i], /*DInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2732 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2733 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2734 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2735 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2736 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2737 | ObjCFastEnumerationStateTypeDecl->completeDefinition(*this); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2738 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2739 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2740 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 2741 | } |
| 2742 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2743 | QualType ASTContext::getBlockDescriptorType() { |
| 2744 | if (BlockDescriptorType) |
| 2745 | return getTagDeclType(BlockDescriptorType); |
| 2746 | |
| 2747 | RecordDecl *T; |
| 2748 | // FIXME: Needs the FlagAppleBlock bit. |
| 2749 | T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2750 | &Idents.get("__block_descriptor")); |
| 2751 | |
| 2752 | QualType FieldTypes[] = { |
| 2753 | UnsignedLongTy, |
| 2754 | UnsignedLongTy, |
| 2755 | }; |
| 2756 | |
| 2757 | const char *FieldNames[] = { |
| 2758 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2759 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2760 | }; |
| 2761 | |
| 2762 | for (size_t i = 0; i < 2; ++i) { |
| 2763 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2764 | T, |
| 2765 | SourceLocation(), |
| 2766 | &Idents.get(FieldNames[i]), |
| 2767 | FieldTypes[i], /*DInfo=*/0, |
| 2768 | /*BitWidth=*/0, |
| 2769 | /*Mutable=*/false); |
| 2770 | T->addDecl(Field); |
| 2771 | } |
| 2772 | |
| 2773 | T->completeDefinition(*this); |
| 2774 | |
| 2775 | BlockDescriptorType = T; |
| 2776 | |
| 2777 | return getTagDeclType(BlockDescriptorType); |
| 2778 | } |
| 2779 | |
| 2780 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 2781 | const RecordType *Rec = T->getAs<RecordType>(); |
| 2782 | assert(Rec && "Invalid BlockDescriptorType"); |
| 2783 | BlockDescriptorType = Rec->getDecl(); |
| 2784 | } |
| 2785 | |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2786 | QualType ASTContext::getBlockDescriptorExtendedType() { |
| 2787 | if (BlockDescriptorExtendedType) |
| 2788 | return getTagDeclType(BlockDescriptorExtendedType); |
| 2789 | |
| 2790 | RecordDecl *T; |
| 2791 | // FIXME: Needs the FlagAppleBlock bit. |
| 2792 | T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2793 | &Idents.get("__block_descriptor_withcopydispose")); |
| 2794 | |
| 2795 | QualType FieldTypes[] = { |
| 2796 | UnsignedLongTy, |
| 2797 | UnsignedLongTy, |
| 2798 | getPointerType(VoidPtrTy), |
| 2799 | getPointerType(VoidPtrTy) |
| 2800 | }; |
| 2801 | |
| 2802 | const char *FieldNames[] = { |
| 2803 | "reserved", |
| 2804 | "Size", |
| 2805 | "CopyFuncPtr", |
| 2806 | "DestroyFuncPtr" |
| 2807 | }; |
| 2808 | |
| 2809 | for (size_t i = 0; i < 4; ++i) { |
| 2810 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2811 | T, |
| 2812 | SourceLocation(), |
| 2813 | &Idents.get(FieldNames[i]), |
| 2814 | FieldTypes[i], /*DInfo=*/0, |
| 2815 | /*BitWidth=*/0, |
| 2816 | /*Mutable=*/false); |
| 2817 | T->addDecl(Field); |
| 2818 | } |
| 2819 | |
| 2820 | T->completeDefinition(*this); |
| 2821 | |
| 2822 | BlockDescriptorExtendedType = T; |
| 2823 | |
| 2824 | return getTagDeclType(BlockDescriptorExtendedType); |
| 2825 | } |
| 2826 | |
| 2827 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 2828 | const RecordType *Rec = T->getAs<RecordType>(); |
| 2829 | assert(Rec && "Invalid BlockDescriptorType"); |
| 2830 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 2831 | } |
| 2832 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2833 | bool ASTContext::BlockRequiresCopying(QualType Ty) { |
| 2834 | if (Ty->isBlockPointerType()) |
| 2835 | return true; |
| 2836 | if (isObjCNSObjectType(Ty)) |
| 2837 | return true; |
| 2838 | if (Ty->isObjCObjectPointerType()) |
| 2839 | return true; |
| 2840 | return false; |
| 2841 | } |
| 2842 | |
| 2843 | QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) { |
| 2844 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2845 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2846 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2847 | // unsigned int __flags; |
| 2848 | // unsigned int __size; |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 2849 | // void *__copy_helper; // as needed |
| 2850 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2851 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2852 | // } * |
| 2853 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2854 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 2855 | |
| 2856 | // FIXME: Move up |
| 2857 | static int UniqueBlockByRefTypeID = 0; |
| 2858 | char Name[36]; |
| 2859 | sprintf(Name, "__Block_byref_%d_%s", ++UniqueBlockByRefTypeID, DeclName); |
| 2860 | RecordDecl *T; |
| 2861 | T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2862 | &Idents.get(Name)); |
| 2863 | T->startDefinition(); |
| 2864 | QualType Int32Ty = IntTy; |
| 2865 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 2866 | QualType FieldTypes[] = { |
| 2867 | getPointerType(VoidPtrTy), |
| 2868 | getPointerType(getTagDeclType(T)), |
| 2869 | Int32Ty, |
| 2870 | Int32Ty, |
| 2871 | getPointerType(VoidPtrTy), |
| 2872 | getPointerType(VoidPtrTy), |
| 2873 | Ty |
| 2874 | }; |
| 2875 | |
| 2876 | const char *FieldNames[] = { |
| 2877 | "__isa", |
| 2878 | "__forwarding", |
| 2879 | "__flags", |
| 2880 | "__size", |
| 2881 | "__copy_helper", |
| 2882 | "__destroy_helper", |
| 2883 | DeclName, |
| 2884 | }; |
| 2885 | |
| 2886 | for (size_t i = 0; i < 7; ++i) { |
| 2887 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 2888 | continue; |
| 2889 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 2890 | &Idents.get(FieldNames[i]), |
| 2891 | FieldTypes[i], /*DInfo=*/0, |
| 2892 | /*BitWidth=*/0, /*Mutable=*/false); |
| 2893 | T->addDecl(Field); |
| 2894 | } |
| 2895 | |
| 2896 | T->completeDefinition(*this); |
| 2897 | |
| 2898 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
| 2901 | |
| 2902 | QualType ASTContext::getBlockParmType( |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2903 | bool BlockHasCopyDispose, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2904 | llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2905 | // FIXME: Move up |
| 2906 | static int UniqueBlockParmTypeID = 0; |
| 2907 | char Name[36]; |
| 2908 | sprintf(Name, "__block_literal_%u", ++UniqueBlockParmTypeID); |
| 2909 | RecordDecl *T; |
| 2910 | T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2911 | &Idents.get(Name)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2912 | QualType FieldTypes[] = { |
| 2913 | getPointerType(VoidPtrTy), |
| 2914 | IntTy, |
| 2915 | IntTy, |
| 2916 | getPointerType(VoidPtrTy), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2917 | (BlockHasCopyDispose ? |
| 2918 | getPointerType(getBlockDescriptorExtendedType()) : |
| 2919 | getPointerType(getBlockDescriptorType())) |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2920 | }; |
| 2921 | |
| 2922 | const char *FieldNames[] = { |
| 2923 | "__isa", |
| 2924 | "__flags", |
| 2925 | "__reserved", |
| 2926 | "__FuncPtr", |
| 2927 | "__descriptor" |
| 2928 | }; |
| 2929 | |
| 2930 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2931 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2932 | &Idents.get(FieldNames[i]), |
| 2933 | FieldTypes[i], /*DInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2934 | /*BitWidth=*/0, /*Mutable=*/false); |
| 2935 | T->addDecl(Field); |
| 2936 | } |
| 2937 | |
| 2938 | for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) { |
| 2939 | const Expr *E = BlockDeclRefDecls[i]; |
| 2940 | const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 2941 | clang::IdentifierInfo *Name = 0; |
| 2942 | if (BDRE) { |
| 2943 | const ValueDecl *D = BDRE->getDecl(); |
| 2944 | Name = &Idents.get(D->getName()); |
| 2945 | } |
| 2946 | QualType FieldType = E->getType(); |
| 2947 | |
| 2948 | if (BDRE && BDRE->isByRef()) |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2949 | FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(), |
| 2950 | FieldType); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2951 | |
| 2952 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 2953 | Name, FieldType, /*DInfo=*/0, |
| 2954 | /*BitWidth=*/0, /*Mutable=*/false); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2955 | T->addDecl(Field); |
| 2956 | } |
| 2957 | |
| 2958 | T->completeDefinition(*this); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2959 | |
| 2960 | return getPointerType(getTagDeclType(T)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2963 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2964 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2965 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 2966 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 2967 | } |
| 2968 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 2969 | // This returns true if a type has been typedefed to BOOL: |
| 2970 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 2971 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 2972 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 2973 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 2974 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2975 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2976 | return false; |
| 2977 | } |
| 2978 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2979 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2980 | /// purpose. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2981 | int ASTContext::getObjCEncodingTypeSize(QualType type) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2982 | uint64_t sz = getTypeSize(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2983 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2984 | // Make all integer and enum types at least as large as an int |
| 2985 | if (sz > 0 && type->isIntegralType()) |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2986 | sz = std::max(sz, getTypeSize(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2987 | // Treat arrays as pointers, since that's how they're passed in. |
| 2988 | else if (type->isArrayType()) |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2989 | sz = getTypeSize(VoidPtrTy); |
| 2990 | return sz / getTypeSize(CharTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2993 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2994 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 2996 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2997 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 2998 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2999 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3000 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3001 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3002 | // Compute size of all parameters. |
| 3003 | // Start with computing size of a pointer in number of bytes. |
| 3004 | // FIXME: There might(should) be a better way of doing this computation! |
| 3005 | SourceLocation Loc; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3006 | int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3007 | // The first two arguments (self and _cmd) are pointers; account for |
| 3008 | // their size. |
| 3009 | int ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3010 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3011 | E = Decl->param_end(); PI != E; ++PI) { |
| 3012 | QualType PType = (*PI)->getType(); |
| 3013 | int sz = getObjCEncodingTypeSize(PType); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3014 | assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3015 | ParmOffset += sz; |
| 3016 | } |
| 3017 | S += llvm::utostr(ParmOffset); |
| 3018 | S += "@0:"; |
| 3019 | S += llvm::utostr(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3020 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3021 | // Argument types. |
| 3022 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3023 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3024 | E = Decl->param_end(); PI != E; ++PI) { |
| 3025 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3026 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3027 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3028 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3029 | // Use array's original type only if it has known number of |
| 3030 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3031 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3032 | PType = PVDecl->getType(); |
| 3033 | } else if (PType->isFunctionType()) |
| 3034 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3035 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3036 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3037 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3038 | getObjCEncodingForType(PType, S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3039 | S += llvm::utostr(ParmOffset); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3040 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3041 | } |
| 3042 | } |
| 3043 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3044 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3045 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3046 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3047 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3048 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3049 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3050 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3051 | /// encoded as single characters, followed by an identifier. Property types |
| 3052 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3053 | /// these attributes are defined by the following enumeration: |
| 3054 | /// @code |
| 3055 | /// enum PropertyAttributes { |
| 3056 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3057 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3058 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3059 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3060 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3061 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3062 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3063 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3064 | /// kPropertyWeak = 'W' // 'weak' property |
| 3065 | /// kPropertyStrong = 'P' // property GC'able |
| 3066 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3067 | /// }; |
| 3068 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3069 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3070 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3071 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3072 | // Collect information from the property implementation decl(s). |
| 3073 | bool Dynamic = false; |
| 3074 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3075 | |
| 3076 | // FIXME: Duplicated code due to poor abstraction. |
| 3077 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3078 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3079 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3080 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3081 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3082 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3083 | ObjCPropertyImplDecl *PID = *i; |
| 3084 | if (PID->getPropertyDecl() == PD) { |
| 3085 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3086 | Dynamic = true; |
| 3087 | } else { |
| 3088 | SynthesizePID = PID; |
| 3089 | } |
| 3090 | } |
| 3091 | } |
| 3092 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3093 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3094 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3095 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3096 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3097 | ObjCPropertyImplDecl *PID = *i; |
| 3098 | if (PID->getPropertyDecl() == PD) { |
| 3099 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3100 | Dynamic = true; |
| 3101 | } else { |
| 3102 | SynthesizePID = PID; |
| 3103 | } |
| 3104 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3105 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3106 | } |
| 3107 | } |
| 3108 | |
| 3109 | // FIXME: This is not very efficient. |
| 3110 | S = "T"; |
| 3111 | |
| 3112 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3113 | // GCC has some special rules regarding encoding of properties which |
| 3114 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3115 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3116 | true /* outermost type */, |
| 3117 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3118 | |
| 3119 | if (PD->isReadOnly()) { |
| 3120 | S += ",R"; |
| 3121 | } else { |
| 3122 | switch (PD->getSetterKind()) { |
| 3123 | case ObjCPropertyDecl::Assign: break; |
| 3124 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3125 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3126 | } |
| 3127 | } |
| 3128 | |
| 3129 | // It really isn't clear at all what this means, since properties |
| 3130 | // are "dynamic by default". |
| 3131 | if (Dynamic) |
| 3132 | S += ",D"; |
| 3133 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3134 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3135 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3136 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3137 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3138 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3139 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
| 3142 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3143 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3144 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
| 3147 | if (SynthesizePID) { |
| 3148 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3149 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3150 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | // FIXME: OBJCGC: weak & strong |
| 3154 | } |
| 3155 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3156 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3157 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3158 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3159 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3160 | /// |
| 3161 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3162 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3163 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3164 | if (BT->getKind() == BuiltinType::ULong && |
| 3165 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3166 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3167 | else |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3168 | if (BT->getKind() == BuiltinType::Long && |
| 3169 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3170 | PointeeTy = IntTy; |
| 3171 | } |
| 3172 | } |
| 3173 | } |
| 3174 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3175 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3176 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3177 | // We follow the behavior of gcc, expanding structures which are |
| 3178 | // directly pointed to, and expanding embedded structures. Note that |
| 3179 | // these rules are sufficient to prevent recursive encoding of the |
| 3180 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3181 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3182 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3185 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3186 | const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3187 | const Expr *E = FD->getBitWidth(); |
| 3188 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 3189 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3190 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3191 | S += 'b'; |
| 3192 | S += llvm::utostr(N); |
| 3193 | } |
| 3194 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3195 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3196 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 3197 | bool ExpandPointedToStructures, |
| 3198 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3199 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3200 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3201 | bool EncodingProperty) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3202 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3203 | if (FD && FD->isBitField()) |
| 3204 | return EncodeBitField(this, S, FD); |
| 3205 | char encoding; |
| 3206 | switch (BT->getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3207 | default: assert(0 && "Unhandled builtin type kind"); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3208 | case BuiltinType::Void: encoding = 'v'; break; |
| 3209 | case BuiltinType::Bool: encoding = 'B'; break; |
| 3210 | case BuiltinType::Char_U: |
| 3211 | case BuiltinType::UChar: encoding = 'C'; break; |
| 3212 | case BuiltinType::UShort: encoding = 'S'; break; |
| 3213 | case BuiltinType::UInt: encoding = 'I'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3214 | case BuiltinType::ULong: |
| 3215 | encoding = |
| 3216 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
Fariborz Jahanian | 72696e1 | 2009-02-11 22:31:45 +0000 | [diff] [blame] | 3217 | break; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3218 | case BuiltinType::UInt128: encoding = 'T'; break; |
| 3219 | case BuiltinType::ULongLong: encoding = 'Q'; break; |
| 3220 | case BuiltinType::Char_S: |
| 3221 | case BuiltinType::SChar: encoding = 'c'; break; |
| 3222 | case BuiltinType::Short: encoding = 's'; break; |
| 3223 | case BuiltinType::Int: encoding = 'i'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3224 | case BuiltinType::Long: |
| 3225 | encoding = |
| 3226 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3227 | break; |
| 3228 | case BuiltinType::LongLong: encoding = 'q'; break; |
| 3229 | case BuiltinType::Int128: encoding = 't'; break; |
| 3230 | case BuiltinType::Float: encoding = 'f'; break; |
| 3231 | case BuiltinType::Double: encoding = 'd'; break; |
| 3232 | case BuiltinType::LongDouble: encoding = 'd'; break; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3233 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3234 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3235 | S += encoding; |
| 3236 | return; |
| 3237 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3238 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3239 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3240 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3241 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3242 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3243 | return; |
| 3244 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3246 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3247 | QualType PointeeTy = PT->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3248 | bool isReadOnly = false; |
| 3249 | // For historical/compatibility reasons, the read-only qualifier of the |
| 3250 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 3251 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3252 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3253 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3254 | if (OutermostType && T.isConstQualified()) { |
| 3255 | isReadOnly = true; |
| 3256 | S += 'r'; |
| 3257 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3258 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3259 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3260 | while (P->getAs<PointerType>()) |
| 3261 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3262 | if (P.isConstQualified()) { |
| 3263 | isReadOnly = true; |
| 3264 | S += 'r'; |
| 3265 | } |
| 3266 | } |
| 3267 | if (isReadOnly) { |
| 3268 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 3269 | // combinations need to be rearranged. |
| 3270 | // Rewrite "in const" from "nr" to "rn" |
| 3271 | const char * s = S.c_str(); |
| 3272 | int len = S.length(); |
| 3273 | if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') { |
| 3274 | std::string replace = "rn"; |
| 3275 | S.replace(S.end()-2, S.end(), replace); |
| 3276 | } |
| 3277 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3278 | if (isObjCSelType(PointeeTy)) { |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 3279 | S += ':'; |
| 3280 | return; |
Fariborz Jahanian | c2939bc | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 3281 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3282 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3283 | if (PointeeTy->isCharType()) { |
| 3284 | // char pointer types should be encoded as '*' unless it is a |
| 3285 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3286 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3287 | S += '*'; |
| 3288 | return; |
| 3289 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3290 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 3291 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 3292 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 3293 | S += '#'; |
| 3294 | return; |
| 3295 | } |
| 3296 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 3297 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 3298 | S += '@'; |
| 3299 | return; |
| 3300 | } |
| 3301 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3302 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3303 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3304 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 3305 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3306 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3307 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3308 | return; |
| 3309 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3310 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3311 | if (const ArrayType *AT = |
| 3312 | // Ignore type qualifiers etc. |
| 3313 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3314 | if (isa<IncompleteArrayType>(AT)) { |
| 3315 | // Incomplete arrays are encoded as a pointer to the array element. |
| 3316 | S += '^'; |
| 3317 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3318 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3319 | false, ExpandStructures, FD); |
| 3320 | } else { |
| 3321 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3322 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3323 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 3324 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 3325 | else { |
| 3326 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 3327 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 3328 | S += '0'; |
| 3329 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3330 | |
| 3331 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3332 | false, ExpandStructures, FD); |
| 3333 | S += ']'; |
| 3334 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3335 | return; |
| 3336 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3337 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3338 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 3339 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3340 | return; |
| 3341 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3342 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3343 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3344 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3345 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3346 | // Anonymous structures print as '?' |
| 3347 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 3348 | S += II->getName(); |
| 3349 | } else { |
| 3350 | S += '?'; |
| 3351 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3352 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3353 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3354 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 3355 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3356 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3357 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3358 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3359 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3360 | S += '"'; |
| 3361 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3362 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3363 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3364 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3365 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3366 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3367 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3368 | QualType qt = Field->getType(); |
| 3369 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3370 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3371 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3372 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3373 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 3374 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3375 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3376 | return; |
| 3377 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3378 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3379 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3380 | if (FD && FD->isBitField()) |
| 3381 | EncodeBitField(this, S, FD); |
| 3382 | else |
| 3383 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3384 | return; |
| 3385 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3386 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3387 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 3388 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3389 | return; |
| 3390 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3391 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3392 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3393 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3394 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3395 | S += '{'; |
| 3396 | const IdentifierInfo *II = OI->getIdentifier(); |
| 3397 | S += II->getName(); |
| 3398 | S += '='; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3399 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3400 | CollectObjCIvars(OI, RecFields); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3401 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3402 | if (RecFields[i]->isBitField()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3403 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3404 | RecFields[i]); |
| 3405 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3406 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3407 | FD); |
| 3408 | } |
| 3409 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3410 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3411 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3412 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3413 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3414 | if (OPT->isObjCIdType()) { |
| 3415 | S += '@'; |
| 3416 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3417 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3418 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3419 | if (OPT->isObjCClassType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3420 | S += '#'; |
| 3421 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3422 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3423 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3424 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3425 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3426 | ExpandPointedToStructures, |
| 3427 | ExpandStructures, FD); |
| 3428 | if (FD || EncodingProperty) { |
| 3429 | // Note that we do extended encoding of protocol qualifer list |
| 3430 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3431 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3432 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3433 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3434 | S += '<'; |
| 3435 | S += (*I)->getNameAsString(); |
| 3436 | S += '>'; |
| 3437 | } |
| 3438 | S += '"'; |
| 3439 | } |
| 3440 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3441 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3442 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3443 | QualType PointeeTy = OPT->getPointeeType(); |
| 3444 | if (!EncodingProperty && |
| 3445 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 3446 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3447 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3448 | // {...}; |
| 3449 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3450 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 3451 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3452 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3453 | return; |
| 3454 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3455 | |
| 3456 | S += '@'; |
| 3457 | if (FD || EncodingProperty) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3458 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3459 | S += OPT->getInterfaceDecl()->getNameAsCString(); |
| 3460 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3461 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3462 | S += '<'; |
| 3463 | S += (*I)->getNameAsString(); |
| 3464 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3465 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3466 | S += '"'; |
| 3467 | } |
| 3468 | return; |
| 3469 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3470 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3471 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3472 | } |
| 3473 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3474 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3475 | std::string& S) const { |
| 3476 | if (QT & Decl::OBJC_TQ_In) |
| 3477 | S += 'n'; |
| 3478 | if (QT & Decl::OBJC_TQ_Inout) |
| 3479 | S += 'N'; |
| 3480 | if (QT & Decl::OBJC_TQ_Out) |
| 3481 | S += 'o'; |
| 3482 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 3483 | S += 'O'; |
| 3484 | if (QT & Decl::OBJC_TQ_Byref) |
| 3485 | S += 'R'; |
| 3486 | if (QT & Decl::OBJC_TQ_Oneway) |
| 3487 | S += 'V'; |
| 3488 | } |
| 3489 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3490 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3491 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3492 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3493 | BuiltinVaListType = T; |
| 3494 | } |
| 3495 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3496 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3497 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3500 | void ASTContext::setObjCSelType(QualType T) { |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3501 | ObjCSelType = T; |
| 3502 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3503 | const TypedefType *TT = T->getAs<TypedefType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3504 | if (!TT) |
| 3505 | return; |
| 3506 | TypedefDecl *TD = TT->getDecl(); |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3507 | |
| 3508 | // typedef struct objc_selector *SEL; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3509 | const PointerType *ptr = TD->getUnderlyingType()->getAs<PointerType>(); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 3510 | if (!ptr) |
| 3511 | return; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3512 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 3513 | if (!rec) |
| 3514 | return; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3515 | SelStructType = rec; |
| 3516 | } |
| 3517 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3518 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3519 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3522 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3523 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3526 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3527 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3528 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3529 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3530 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3533 | /// \brief Retrieve the template name that represents a qualified |
| 3534 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3535 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3536 | bool TemplateKeyword, |
| 3537 | TemplateDecl *Template) { |
| 3538 | llvm::FoldingSetNodeID ID; |
| 3539 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 3540 | |
| 3541 | void *InsertPos = 0; |
| 3542 | QualifiedTemplateName *QTN = |
| 3543 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3544 | if (!QTN) { |
| 3545 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 3546 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 3547 | } |
| 3548 | |
| 3549 | return TemplateName(QTN); |
| 3550 | } |
| 3551 | |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 3552 | /// \brief Retrieve the template name that represents a qualified |
| 3553 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3554 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 3555 | bool TemplateKeyword, |
| 3556 | OverloadedFunctionDecl *Template) { |
| 3557 | llvm::FoldingSetNodeID ID; |
| 3558 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3559 | |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 3560 | void *InsertPos = 0; |
| 3561 | QualifiedTemplateName *QTN = |
| 3562 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3563 | if (!QTN) { |
| 3564 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 3565 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 3566 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3567 | |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 3568 | return TemplateName(QTN); |
| 3569 | } |
| 3570 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3571 | /// \brief Retrieve the template name that represents a dependent |
| 3572 | /// template name such as \c MetaFun::template apply. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3573 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3574 | const IdentifierInfo *Name) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3575 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 3576 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3577 | |
| 3578 | llvm::FoldingSetNodeID ID; |
| 3579 | DependentTemplateName::Profile(ID, NNS, Name); |
| 3580 | |
| 3581 | void *InsertPos = 0; |
| 3582 | DependentTemplateName *QTN = |
| 3583 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3584 | |
| 3585 | if (QTN) |
| 3586 | return TemplateName(QTN); |
| 3587 | |
| 3588 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 3589 | if (CanonNNS == NNS) { |
| 3590 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 3591 | } else { |
| 3592 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 3593 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
| 3594 | } |
| 3595 | |
| 3596 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3597 | return TemplateName(QTN); |
| 3598 | } |
| 3599 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3600 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 3601 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 3602 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame^] | 3603 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3604 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame^] | 3605 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3606 | case TargetInfo::SignedShort: return ShortTy; |
| 3607 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 3608 | case TargetInfo::SignedInt: return IntTy; |
| 3609 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 3610 | case TargetInfo::SignedLong: return LongTy; |
| 3611 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 3612 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 3613 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 3614 | } |
| 3615 | |
| 3616 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame^] | 3617 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3618 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 3619 | |
| 3620 | //===----------------------------------------------------------------------===// |
| 3621 | // Type Predicates. |
| 3622 | //===----------------------------------------------------------------------===// |
| 3623 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3624 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 3625 | /// NSObject attribute on a c-style pointer type. |
| 3626 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3627 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3628 | /// |
| 3629 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 3630 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 3631 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3632 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3633 | return true; |
| 3634 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3635 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3636 | } |
| 3637 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3638 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 3639 | /// garbage collection attribute. |
| 3640 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3641 | Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
| 3642 | Qualifiers::GC GCAttrs = Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3643 | if (getLangOptions().ObjC1 && |
| 3644 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3645 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3646 | // Default behavious under objective-c's gc is for objective-c pointers |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3647 | // (or pointers to them) be treated as though they were declared |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3648 | // as __strong. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3649 | if (GCAttrs == Qualifiers::GCNone) { |
Fariborz Jahanian | 75212ee | 2009-09-10 23:38:45 +0000 | [diff] [blame] | 3650 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3651 | GCAttrs = Qualifiers::Strong; |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3652 | else if (Ty->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3653 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3654 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 3655 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 3656 | // set on them. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3657 | else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3658 | return Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3659 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3660 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3661 | } |
| 3662 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3663 | //===----------------------------------------------------------------------===// |
| 3664 | // Type Compatibility Testing |
| 3665 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 3666 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3667 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3668 | /// compatible. |
| 3669 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 3670 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3671 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3672 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3673 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3676 | //===----------------------------------------------------------------------===// |
| 3677 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 3678 | //===----------------------------------------------------------------------===// |
| 3679 | |
| 3680 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 3681 | /// inheritance hierarchy of 'rProto'. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3682 | bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 3683 | ObjCProtocolDecl *rProto) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3684 | if (lProto == rProto) |
| 3685 | return true; |
| 3686 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 3687 | E = rProto->protocol_end(); PI != E; ++PI) |
| 3688 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 3689 | return true; |
| 3690 | return false; |
| 3691 | } |
| 3692 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3693 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 3694 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 3695 | /// otherwise. |
| 3696 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 3697 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 3698 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 3699 | return false; |
| 3700 | } |
| 3701 | |
| 3702 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 3703 | /// ObjCQualifiedIDType. |
| 3704 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 3705 | bool compare) { |
| 3706 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3707 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3708 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 3709 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3710 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3711 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 3712 | return true; |
| 3713 | |
| 3714 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3715 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3716 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3717 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3718 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3719 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3720 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3721 | // make sure we check the class hierarchy. |
| 3722 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 3723 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3724 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3725 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3726 | // see if static class implements all of id's protocols, directly or |
| 3727 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3728 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3729 | return false; |
| 3730 | } |
| 3731 | } |
| 3732 | // If there are no qualifiers and no interface, we have an 'id'. |
| 3733 | return true; |
| 3734 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3735 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3736 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3737 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3738 | ObjCProtocolDecl *lhsProto = *I; |
| 3739 | bool match = false; |
| 3740 | |
| 3741 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3742 | // see if static class implements all of id's protocols, directly or |
| 3743 | // through its super class and categories. |
| 3744 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 3745 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 3746 | ObjCProtocolDecl *rhsProto = *J; |
| 3747 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 3748 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 3749 | match = true; |
| 3750 | break; |
| 3751 | } |
| 3752 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3753 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3754 | // make sure we check the class hierarchy. |
| 3755 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 3756 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3757 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3758 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3759 | // see if static class implements all of id's protocols, directly or |
| 3760 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3761 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3762 | match = true; |
| 3763 | break; |
| 3764 | } |
| 3765 | } |
| 3766 | } |
| 3767 | if (!match) |
| 3768 | return false; |
| 3769 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3770 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3771 | return true; |
| 3772 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3773 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3774 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 3775 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 3776 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3777 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3778 | lhs->getAsObjCInterfacePointerType()) { |
| 3779 | if (lhsOPT->qual_empty()) { |
| 3780 | bool match = false; |
| 3781 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 3782 | for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), |
| 3783 | E = rhsQID->qual_end(); I != E; ++I) { |
| 3784 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3785 | // see if static class implements all of id's protocols, directly or |
| 3786 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3787 | if (lhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3788 | match = true; |
| 3789 | break; |
| 3790 | } |
| 3791 | } |
| 3792 | if (!match) |
| 3793 | return false; |
| 3794 | } |
| 3795 | return true; |
| 3796 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3797 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3798 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 3799 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 3800 | ObjCProtocolDecl *lhsProto = *I; |
| 3801 | bool match = false; |
| 3802 | |
| 3803 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3804 | // see if static class implements all of id's protocols, directly or |
| 3805 | // through its super class and categories. |
| 3806 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 3807 | E = rhsQID->qual_end(); J != E; ++J) { |
| 3808 | ObjCProtocolDecl *rhsProto = *J; |
| 3809 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 3810 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 3811 | match = true; |
| 3812 | break; |
| 3813 | } |
| 3814 | } |
| 3815 | if (!match) |
| 3816 | return false; |
| 3817 | } |
| 3818 | return true; |
| 3819 | } |
| 3820 | return false; |
| 3821 | } |
| 3822 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3823 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3824 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 3825 | /// protocol qualifiers on the LHS or RHS. |
| 3826 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3827 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 3828 | const ObjCObjectPointerType *RHSOPT) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3829 | // If either type represents the built-in 'id' or 'Class' types, return true. |
| 3830 | if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3831 | return true; |
| 3832 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3833 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3834 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 3835 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3836 | false); |
| 3837 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3838 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 3839 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3840 | if (LHS && RHS) // We have 2 user-defined types. |
| 3841 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3842 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3843 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3846 | bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, |
| 3847 | const ObjCInterfaceType *RHS) { |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3848 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 3849 | // the LHS. |
| 3850 | if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 3851 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3852 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3853 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 3854 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 3855 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3856 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3857 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3858 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 3859 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 3860 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3861 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3862 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 3863 | for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(), |
| 3864 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 3865 | LHSPI != LHSPE; LHSPI++) { |
| 3866 | bool RHSImplementsProtocol = false; |
| 3867 | |
| 3868 | // If the RHS doesn't implement the protocol on the left, the types |
| 3869 | // are incompatible. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 3870 | for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3871 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 3872 | RHSPI != RHSPE; RHSPI++) { |
| 3873 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 3874 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 3875 | break; |
| 3876 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 3877 | } |
| 3878 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 3879 | if (!RHSImplementsProtocol) |
| 3880 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3881 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 3882 | // The RHS implements all protocols listed on the LHS. |
| 3883 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3886 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 3887 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3888 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 3889 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3890 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3891 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3892 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3893 | |
| 3894 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 3895 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3898 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 3899 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3900 | /// 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] | 3901 | /// same. See 6.7.[2,3,5] for additional rules. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3902 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { |
| 3903 | return !mergeTypes(LHS, RHS).isNull(); |
| 3904 | } |
| 3905 | |
| 3906 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3907 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 3908 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3909 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 3910 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3911 | bool allLTypes = true; |
| 3912 | bool allRTypes = true; |
| 3913 | |
| 3914 | // Check return type |
| 3915 | QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); |
| 3916 | if (retType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3917 | if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) |
| 3918 | allLTypes = false; |
| 3919 | if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) |
| 3920 | allRTypes = false; |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 3921 | // FIXME: double check this |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 3922 | bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr(); |
| 3923 | if (NoReturn != lbase->getNoReturnAttr()) |
| 3924 | allLTypes = false; |
| 3925 | if (NoReturn != rbase->getNoReturnAttr()) |
| 3926 | allRTypes = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3927 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3928 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3929 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 3930 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3931 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 3932 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 3933 | |
| 3934 | // Compatible functions must have the same number of arguments |
| 3935 | if (lproto_nargs != rproto_nargs) |
| 3936 | return QualType(); |
| 3937 | |
| 3938 | // Variadic and non-variadic functions aren't compatible |
| 3939 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 3940 | return QualType(); |
| 3941 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 3942 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 3943 | return QualType(); |
| 3944 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3945 | // Check argument compatibility |
| 3946 | llvm::SmallVector<QualType, 10> types; |
| 3947 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 3948 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 3949 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
| 3950 | QualType argtype = mergeTypes(largtype, rargtype); |
| 3951 | if (argtype.isNull()) return QualType(); |
| 3952 | types.push_back(argtype); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3953 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 3954 | allLTypes = false; |
| 3955 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 3956 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3957 | } |
| 3958 | if (allLTypes) return lhs; |
| 3959 | if (allRTypes) return rhs; |
| 3960 | return getFunctionType(retType, types.begin(), types.size(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 3961 | lproto->isVariadic(), lproto->getTypeQuals(), |
| 3962 | NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
| 3965 | if (lproto) allRTypes = false; |
| 3966 | if (rproto) allLTypes = false; |
| 3967 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3968 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3969 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3970 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3971 | if (proto->isVariadic()) return QualType(); |
| 3972 | // Check that the types are compatible with the types that |
| 3973 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 3974 | // The only types actually affected are promotable integer |
| 3975 | // types and floats, which would be passed as a different |
| 3976 | // type depending on whether the prototype is visible. |
| 3977 | unsigned proto_nargs = proto->getNumArgs(); |
| 3978 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 3979 | QualType argTy = proto->getArgType(i); |
| 3980 | if (argTy->isPromotableIntegerType() || |
| 3981 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 3982 | return QualType(); |
| 3983 | } |
| 3984 | |
| 3985 | if (allLTypes) return lhs; |
| 3986 | if (allRTypes) return rhs; |
| 3987 | return getFunctionType(retType, proto->arg_type_begin(), |
Mike Stump | 2d3c191 | 2009-07-27 00:44:23 +0000 | [diff] [blame] | 3988 | proto->getNumArgs(), proto->isVariadic(), |
| 3989 | proto->getTypeQuals(), NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3990 | } |
| 3991 | |
| 3992 | if (allLTypes) return lhs; |
| 3993 | if (allRTypes) return rhs; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 3994 | return getFunctionNoProtoType(retType, NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
| 3997 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 3998 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 3999 | // type is adjusted to "T" prior to any further analysis, the expression |
| 4000 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4001 | // expression is an lvalue unless the reference is an rvalue reference and |
| 4002 | // the expression is a function call (possibly inside parentheses). |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4003 | // FIXME: C++ shouldn't be going through here! The rules are different |
| 4004 | // enough that they should be handled separately. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4005 | // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really* |
| 4006 | // shouldn't be going through here! |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4007 | if (const ReferenceType *RT = LHS->getAs<ReferenceType>()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 4008 | LHS = RT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4009 | if (const ReferenceType *RT = RHS->getAs<ReferenceType>()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 4010 | RHS = RT->getPointeeType(); |
Chris Lattner | f3692dc | 2008-04-07 05:37:56 +0000 | [diff] [blame] | 4011 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4012 | QualType LHSCan = getCanonicalType(LHS), |
| 4013 | RHSCan = getCanonicalType(RHS); |
| 4014 | |
| 4015 | // If two types are identical, they are compatible. |
| 4016 | if (LHSCan == RHSCan) |
| 4017 | return LHS; |
| 4018 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4019 | // If the qualifiers are different, the types aren't compatible... mostly. |
| 4020 | Qualifiers LQuals = LHSCan.getQualifiers(); |
| 4021 | Qualifiers RQuals = RHSCan.getQualifiers(); |
| 4022 | if (LQuals != RQuals) { |
| 4023 | // If any of these qualifiers are different, we have a type |
| 4024 | // mismatch. |
| 4025 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 4026 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 4027 | return QualType(); |
| 4028 | |
| 4029 | // Exactly one GC qualifier difference is allowed: __strong is |
| 4030 | // okay if the other type has no GC qualifier but is an Objective |
| 4031 | // C object pointer (i.e. implicitly strong by default). We fix |
| 4032 | // this by pretending that the unqualified type was actually |
| 4033 | // qualified __strong. |
| 4034 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 4035 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 4036 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 4037 | |
| 4038 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 4039 | return QualType(); |
| 4040 | |
| 4041 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 4042 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 4043 | } |
| 4044 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 4045 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 4046 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4047 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4051 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 4052 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 4053 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4054 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4055 | // We want to consider the two function types to be the same for these |
| 4056 | // comparisons, just force one to the other. |
| 4057 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 4058 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 4059 | |
| 4060 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4061 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 4062 | LHSClass = Type::ConstantArray; |
| 4063 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 4064 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4065 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 4066 | // Canonicalize ExtVector -> Vector. |
| 4067 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 4068 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4069 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4070 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4071 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4072 | // 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] | 4073 | // a signed integer type, or an unsigned integer type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4074 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4075 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 4076 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4077 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4078 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4079 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 4080 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4081 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4082 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4083 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4084 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4085 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 4086 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4087 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4088 | #define TYPE(Class, Base) |
| 4089 | #define ABSTRACT_TYPE(Class, Base) |
| 4090 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4091 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4092 | #include "clang/AST/TypeNodes.def" |
| 4093 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 4094 | return QualType(); |
| 4095 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4096 | case Type::LValueReference: |
| 4097 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4098 | case Type::MemberPointer: |
| 4099 | assert(false && "C++ should never be in mergeTypes"); |
| 4100 | return QualType(); |
| 4101 | |
| 4102 | case Type::IncompleteArray: |
| 4103 | case Type::VariableArray: |
| 4104 | case Type::FunctionProto: |
| 4105 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4106 | assert(false && "Types are eliminated above"); |
| 4107 | return QualType(); |
| 4108 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4109 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4110 | { |
| 4111 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4112 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 4113 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4114 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 4115 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4116 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4117 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4118 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4119 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4120 | return getPointerType(ResultType); |
| 4121 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4122 | case Type::BlockPointer: |
| 4123 | { |
| 4124 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4125 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 4126 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4127 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 4128 | if (ResultType.isNull()) return QualType(); |
| 4129 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 4130 | return LHS; |
| 4131 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 4132 | return RHS; |
| 4133 | return getBlockPointerType(ResultType); |
| 4134 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4135 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4136 | { |
| 4137 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 4138 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 4139 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 4140 | return QualType(); |
| 4141 | |
| 4142 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 4143 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
| 4144 | QualType ResultType = mergeTypes(LHSElem, RHSElem); |
| 4145 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4146 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4147 | return LHS; |
| 4148 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4149 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4150 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 4151 | ArrayType::ArraySizeModifier(), 0); |
| 4152 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 4153 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4154 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 4155 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4156 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4157 | return LHS; |
| 4158 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4159 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4160 | if (LVAT) { |
| 4161 | // FIXME: This isn't correct! But tricky to implement because |
| 4162 | // the array's size has to be the size of LHS, but the type |
| 4163 | // has to be different. |
| 4164 | return LHS; |
| 4165 | } |
| 4166 | if (RVAT) { |
| 4167 | // FIXME: This isn't correct! But tricky to implement because |
| 4168 | // the array's size has to be the size of RHS, but the type |
| 4169 | // has to be different. |
| 4170 | return RHS; |
| 4171 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4172 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 4173 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4174 | return getIncompleteArrayType(ResultType, |
| 4175 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4176 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4177 | case Type::FunctionNoProto: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4178 | return mergeFunctionTypes(LHS, RHS); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4179 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4180 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4181 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4182 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4183 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4184 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 4185 | case Type::Complex: |
| 4186 | // Distinct complex types are incompatible. |
| 4187 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4188 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4189 | // FIXME: The merged type should be an ExtVector! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4190 | if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4191 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4192 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4193 | case Type::ObjCInterface: { |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4194 | // Check if the interfaces are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4195 | // FIXME: This should be type compatibility, e.g. whether |
| 4196 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4197 | const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>(); |
| 4198 | const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>(); |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4199 | if (LHSIface && RHSIface && |
| 4200 | canAssignObjCInterfaces(LHSIface, RHSIface)) |
| 4201 | return LHS; |
| 4202 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4203 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4204 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4205 | case Type::ObjCObjectPointer: { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4206 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 4207 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4208 | return LHS; |
| 4209 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 4210 | return QualType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4211 | } |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4212 | case Type::FixedWidthInt: |
| 4213 | // Distinct fixed-width integers are not compatible. |
| 4214 | return QualType(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4215 | case Type::TemplateSpecialization: |
| 4216 | assert(false && "Dependent types have no size"); |
| 4217 | break; |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4218 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4219 | |
| 4220 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4221 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 4222 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 4223 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4224 | // Integer Predicates |
| 4225 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 4226 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4227 | unsigned ASTContext::getIntWidth(QualType T) { |
| 4228 | if (T == BoolTy) |
| 4229 | return 1; |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4230 | if (FixedWidthIntType *FWIT = dyn_cast<FixedWidthIntType>(T)) { |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4231 | return FWIT->getWidth(); |
| 4232 | } |
| 4233 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4234 | return (unsigned)getTypeSize(T); |
| 4235 | } |
| 4236 | |
| 4237 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
| 4238 | assert(T->isSignedIntegerType() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4239 | |
| 4240 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 4241 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 4242 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
| 4243 | VTy->getNumElements()); |
| 4244 | |
| 4245 | // For enums, we return the unsigned version of the base type. |
| 4246 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4247 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4248 | |
| 4249 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 4250 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4251 | switch (BTy->getKind()) { |
| 4252 | case BuiltinType::Char_S: |
| 4253 | case BuiltinType::SChar: |
| 4254 | return UnsignedCharTy; |
| 4255 | case BuiltinType::Short: |
| 4256 | return UnsignedShortTy; |
| 4257 | case BuiltinType::Int: |
| 4258 | return UnsignedIntTy; |
| 4259 | case BuiltinType::Long: |
| 4260 | return UnsignedLongTy; |
| 4261 | case BuiltinType::LongLong: |
| 4262 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4263 | case BuiltinType::Int128: |
| 4264 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4265 | default: |
| 4266 | assert(0 && "Unexpected signed integer type"); |
| 4267 | return QualType(); |
| 4268 | } |
| 4269 | } |
| 4270 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4271 | ExternalASTSource::~ExternalASTSource() { } |
| 4272 | |
| 4273 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4274 | |
| 4275 | |
| 4276 | //===----------------------------------------------------------------------===// |
| 4277 | // Builtin Type Computation |
| 4278 | //===----------------------------------------------------------------------===// |
| 4279 | |
| 4280 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
| 4281 | /// pointer over the consumed characters. This returns the resultant type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4282 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4283 | ASTContext::GetBuiltinTypeError &Error, |
| 4284 | bool AllowTypeModifiers = true) { |
| 4285 | // Modifiers. |
| 4286 | int HowLong = 0; |
| 4287 | bool Signed = false, Unsigned = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4288 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4289 | // Read the modifiers first. |
| 4290 | bool Done = false; |
| 4291 | while (!Done) { |
| 4292 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4293 | default: Done = true; --Str; break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4294 | case 'S': |
| 4295 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 4296 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 4297 | Signed = true; |
| 4298 | break; |
| 4299 | case 'U': |
| 4300 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 4301 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 4302 | Unsigned = true; |
| 4303 | break; |
| 4304 | case 'L': |
| 4305 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 4306 | ++HowLong; |
| 4307 | break; |
| 4308 | } |
| 4309 | } |
| 4310 | |
| 4311 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4312 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4313 | // Read the base type. |
| 4314 | switch (*Str++) { |
| 4315 | default: assert(0 && "Unknown builtin type letter!"); |
| 4316 | case 'v': |
| 4317 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4318 | "Bad modifiers used with 'v'!"); |
| 4319 | Type = Context.VoidTy; |
| 4320 | break; |
| 4321 | case 'f': |
| 4322 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4323 | "Bad modifiers used with 'f'!"); |
| 4324 | Type = Context.FloatTy; |
| 4325 | break; |
| 4326 | case 'd': |
| 4327 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 4328 | "Bad modifiers used with 'd'!"); |
| 4329 | if (HowLong) |
| 4330 | Type = Context.LongDoubleTy; |
| 4331 | else |
| 4332 | Type = Context.DoubleTy; |
| 4333 | break; |
| 4334 | case 's': |
| 4335 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 4336 | if (Unsigned) |
| 4337 | Type = Context.UnsignedShortTy; |
| 4338 | else |
| 4339 | Type = Context.ShortTy; |
| 4340 | break; |
| 4341 | case 'i': |
| 4342 | if (HowLong == 3) |
| 4343 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 4344 | else if (HowLong == 2) |
| 4345 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 4346 | else if (HowLong == 1) |
| 4347 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 4348 | else |
| 4349 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 4350 | break; |
| 4351 | case 'c': |
| 4352 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 4353 | if (Signed) |
| 4354 | Type = Context.SignedCharTy; |
| 4355 | else if (Unsigned) |
| 4356 | Type = Context.UnsignedCharTy; |
| 4357 | else |
| 4358 | Type = Context.CharTy; |
| 4359 | break; |
| 4360 | case 'b': // boolean |
| 4361 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 4362 | Type = Context.BoolTy; |
| 4363 | break; |
| 4364 | case 'z': // size_t. |
| 4365 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 4366 | Type = Context.getSizeType(); |
| 4367 | break; |
| 4368 | case 'F': |
| 4369 | Type = Context.getCFConstantStringType(); |
| 4370 | break; |
| 4371 | case 'a': |
| 4372 | Type = Context.getBuiltinVaListType(); |
| 4373 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4374 | break; |
| 4375 | case 'A': |
| 4376 | // This is a "reference" to a va_list; however, what exactly |
| 4377 | // this means depends on how va_list is defined. There are two |
| 4378 | // different kinds of va_list: ones passed by value, and ones |
| 4379 | // passed by reference. An example of a by-value va_list is |
| 4380 | // x86, where va_list is a char*. An example of by-ref va_list |
| 4381 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 4382 | // we want this argument to be a char*&; for x86-64, we want |
| 4383 | // it to be a __va_list_tag*. |
| 4384 | Type = Context.getBuiltinVaListType(); |
| 4385 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4386 | if (Type->isArrayType()) { |
| 4387 | Type = Context.getArrayDecayedType(Type); |
| 4388 | } else { |
| 4389 | Type = Context.getLValueReferenceType(Type); |
| 4390 | } |
| 4391 | break; |
| 4392 | case 'V': { |
| 4393 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4394 | unsigned NumElements = strtoul(Str, &End, 10); |
| 4395 | assert(End != Str && "Missing vector size"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4396 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4397 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4398 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4399 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 4400 | Type = Context.getVectorType(ElementType, NumElements); |
| 4401 | break; |
| 4402 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 4403 | case 'X': { |
| 4404 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 4405 | Type = Context.getComplexType(ElementType); |
| 4406 | break; |
| 4407 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4408 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4409 | Type = Context.getFILEType(); |
| 4410 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4411 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4412 | return QualType(); |
| 4413 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4414 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4415 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4416 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4417 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4418 | else |
| 4419 | Type = Context.getjmp_bufType(); |
| 4420 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4421 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4422 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4423 | return QualType(); |
| 4424 | } |
| 4425 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4426 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4427 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4428 | if (!AllowTypeModifiers) |
| 4429 | return Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4430 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4431 | Done = false; |
| 4432 | while (!Done) { |
| 4433 | switch (*Str++) { |
| 4434 | default: Done = true; --Str; break; |
| 4435 | case '*': |
| 4436 | Type = Context.getPointerType(Type); |
| 4437 | break; |
| 4438 | case '&': |
| 4439 | Type = Context.getLValueReferenceType(Type); |
| 4440 | break; |
| 4441 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 4442 | case 'C': |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4443 | Type = Type.withConst(); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4444 | break; |
| 4445 | } |
| 4446 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4447 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4448 | return Type; |
| 4449 | } |
| 4450 | |
| 4451 | /// GetBuiltinType - Return the type for the specified builtin. |
| 4452 | QualType ASTContext::GetBuiltinType(unsigned id, |
| 4453 | GetBuiltinTypeError &Error) { |
| 4454 | const char *TypeStr = BuiltinInfo.GetTypeString(id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4455 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4456 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4457 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4458 | Error = GE_None; |
| 4459 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4460 | if (Error != GE_None) |
| 4461 | return QualType(); |
| 4462 | while (TypeStr[0] && TypeStr[0] != '.') { |
| 4463 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4464 | if (Error != GE_None) |
| 4465 | return QualType(); |
| 4466 | |
| 4467 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 4468 | if (Ty->isArrayType()) |
| 4469 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4470 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4471 | ArgTypes.push_back(Ty); |
| 4472 | } |
| 4473 | |
| 4474 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 4475 | "'.' should only occur at end of builtin type list!"); |
| 4476 | |
| 4477 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 4478 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 4479 | return getFunctionNoProtoType(ResType); |
| 4480 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), |
| 4481 | TypeStr[0] == '.', 0); |
| 4482 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4483 | |
| 4484 | QualType |
| 4485 | ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 4486 | // Perform the usual unary conversions. We do this early so that |
| 4487 | // integral promotions to "int" can allow us to exit early, in the |
| 4488 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 4489 | // qualifiers. For example, "const float" and "float" are |
| 4490 | // equivalent. |
| 4491 | if (lhs->isPromotableIntegerType()) |
| 4492 | lhs = getPromotedIntegerType(lhs); |
| 4493 | else |
| 4494 | lhs = lhs.getUnqualifiedType(); |
| 4495 | if (rhs->isPromotableIntegerType()) |
| 4496 | rhs = getPromotedIntegerType(rhs); |
| 4497 | else |
| 4498 | rhs = rhs.getUnqualifiedType(); |
| 4499 | |
| 4500 | // If both types are identical, no conversion is needed. |
| 4501 | if (lhs == rhs) |
| 4502 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4503 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4504 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 4505 | // The caller can deal with this (e.g. pointer + int). |
| 4506 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 4507 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4508 | |
| 4509 | // At this point, we have two different arithmetic types. |
| 4510 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4511 | // Handle complex types first (C99 6.3.1.8p1). |
| 4512 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 4513 | // if we have an integer operand, the result is the complex type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4514 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4515 | // convert the rhs to the lhs complex type. |
| 4516 | return lhs; |
| 4517 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4518 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4519 | // convert the lhs to the rhs complex type. |
| 4520 | return rhs; |
| 4521 | } |
| 4522 | // This handles complex/complex, complex/float, or float/complex. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4523 | // When both operands are complex, the shorter operand is converted to the |
| 4524 | // type of the longer, and that is the type of the result. This corresponds |
| 4525 | // to what is done when combining two real floating-point operands. |
| 4526 | // The fun begins when size promotion occur across type domains. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4527 | // 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] | 4528 | // floating-point type, the less precise type is converted, within it's |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4529 | // 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] | 4530 | // when combining a "long double" with a "double _Complex", the |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4531 | // "double _Complex" is promoted to "long double _Complex". |
| 4532 | int result = getFloatingTypeOrder(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4533 | |
| 4534 | if (result > 0) { // The left side is bigger, convert rhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4535 | rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4536 | } else if (result < 0) { // The right side is bigger, convert lhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4537 | lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4538 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4539 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 4540 | // domains match. This is a requirement for our implementation, C99 |
| 4541 | // does not require this promotion. |
| 4542 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 4543 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
| 4544 | return rhs; |
| 4545 | } else { // handle "_Complex double, double". |
| 4546 | return lhs; |
| 4547 | } |
| 4548 | } |
| 4549 | return lhs; // The domain/size match exactly. |
| 4550 | } |
| 4551 | // Now handle "real" floating types (i.e. float, double, long double). |
| 4552 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 4553 | // if we have an integer operand, the result is the real floating type. |
| 4554 | if (rhs->isIntegerType()) { |
| 4555 | // convert rhs to the lhs floating point type. |
| 4556 | return lhs; |
| 4557 | } |
| 4558 | if (rhs->isComplexIntegerType()) { |
| 4559 | // convert rhs to the complex floating point type. |
| 4560 | return getComplexType(lhs); |
| 4561 | } |
| 4562 | if (lhs->isIntegerType()) { |
| 4563 | // convert lhs to the rhs floating point type. |
| 4564 | return rhs; |
| 4565 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4566 | if (lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4567 | // convert lhs to the complex floating point type. |
| 4568 | return getComplexType(rhs); |
| 4569 | } |
| 4570 | // We have two real floating types, float/complex combos were handled above. |
| 4571 | // Convert the smaller operand to the bigger result. |
| 4572 | int result = getFloatingTypeOrder(lhs, rhs); |
| 4573 | if (result > 0) // convert the rhs |
| 4574 | return lhs; |
| 4575 | assert(result < 0 && "illegal float comparison"); |
| 4576 | return rhs; // convert the lhs |
| 4577 | } |
| 4578 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 4579 | // Handle GCC complex int extension. |
| 4580 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 4581 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 4582 | |
| 4583 | if (lhsComplexInt && rhsComplexInt) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4584 | if (getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4585 | rhsComplexInt->getElementType()) >= 0) |
| 4586 | return lhs; // convert the rhs |
| 4587 | return rhs; |
| 4588 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 4589 | // convert the rhs to the lhs complex type. |
| 4590 | return lhs; |
| 4591 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 4592 | // convert the lhs to the rhs complex type. |
| 4593 | return rhs; |
| 4594 | } |
| 4595 | } |
| 4596 | // Finally, we have two differing integer types. |
| 4597 | // The rules for this case are in C99 6.3.1.8 |
| 4598 | int compare = getIntegerTypeOrder(lhs, rhs); |
| 4599 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 4600 | rhsSigned = rhs->isSignedIntegerType(); |
| 4601 | QualType destType; |
| 4602 | if (lhsSigned == rhsSigned) { |
| 4603 | // Same signedness; use the higher-ranked type |
| 4604 | destType = compare >= 0 ? lhs : rhs; |
| 4605 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 4606 | // The unsigned type has greater than or equal rank to the |
| 4607 | // signed type, so use the unsigned type |
| 4608 | destType = lhsSigned ? rhs : lhs; |
| 4609 | } else if (getIntWidth(lhs) != getIntWidth(rhs)) { |
| 4610 | // The two types are different widths; if we are here, that |
| 4611 | // means the signed type is larger than the unsigned type, so |
| 4612 | // use the signed type. |
| 4613 | destType = lhsSigned ? lhs : rhs; |
| 4614 | } else { |
| 4615 | // The signed type is higher-ranked than the unsigned type, |
| 4616 | // but isn't actually any bigger (like unsigned int and long |
| 4617 | // on most 32-bit systems). Use the unsigned type corresponding |
| 4618 | // to the signed type. |
| 4619 | destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 4620 | } |
| 4621 | return destType; |
| 4622 | } |