Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 980e508 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 18 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExternalASTSource.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Builtins.h" |
Chris Lattner | a9376d4 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 29 | #include "RecordLayoutBuilder.h" |
| 30 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | |
| 33 | enum FloatingRank { |
| 34 | FloatRank, DoubleRank, LongDoubleRank |
| 35 | }; |
| 36 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 37 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
Daniel Dunbar | 444be73 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 38 | const TargetInfo &t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 39 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 40 | Builtin::Context &builtins, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | bool FreeMem, unsigned size_reserve) : |
| 42 | GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0), |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 43 | ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 44 | sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
| 45 | SourceMgr(SM), LangOpts(LOpts), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | LoadedExternalComments(false), FreeMemory(FreeMem), Target(t), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 47 | Idents(idents), Selectors(sels), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) { |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 49 | ObjCIdRedefinitionType = QualType(); |
| 50 | ObjCClassRedefinitionType = QualType(); |
Fariborz Jahanian | 369a3bd | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 51 | ObjCSelRedefinitionType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 53 | TUDecl = TranslationUnitDecl::Create(*this); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 54 | InitBuiltinTypes(); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | ASTContext::~ASTContext() { |
| 58 | // Deallocate all the types. |
| 59 | while (!Types.empty()) { |
Ted Kremenek | 4b05b1d | 2008-05-21 16:38:54 +0000 | [diff] [blame] | 60 | Types.back()->Destroy(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | Types.pop_back(); |
| 62 | } |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 63 | |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 64 | { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 65 | llvm::FoldingSet<ExtQuals>::iterator |
| 66 | I = ExtQualNodes.begin(), E = ExtQualNodes.end(); |
| 67 | while (I != E) |
| 68 | Deallocate(&*I++); |
| 69 | } |
| 70 | |
| 71 | { |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 72 | llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 73 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); |
| 74 | while (I != E) { |
| 75 | ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); |
| 76 | delete R; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 81 | llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator |
| 82 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 83 | while (I != E) { |
| 84 | ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); |
| 85 | delete R; |
| 86 | } |
| 87 | } |
| 88 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 89 | // Destroy nested-name-specifiers. |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 90 | for (llvm::FoldingSet<NestedNameSpecifier>::iterator |
| 91 | NNS = NestedNameSpecifiers.begin(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | NNSEnd = NestedNameSpecifiers.end(); |
| 93 | NNS != NNSEnd; |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 94 | /* Increment in loop */) |
| 95 | (*NNS++).Destroy(*this); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 96 | |
| 97 | if (GlobalNestedNameSpecifier) |
| 98 | GlobalNestedNameSpecifier->Destroy(*this); |
| 99 | |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 100 | TUDecl->Destroy(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | void |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 104 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 105 | ExternalSource.reset(Source.take()); |
| 106 | } |
| 107 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | void ASTContext::PrintStats() const { |
| 109 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 110 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 111 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 112 | unsigned counts[] = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 114 | #define ABSTRACT_TYPE(Name, Parent) |
| 115 | #include "clang/AST/TypeNodes.def" |
| 116 | 0 // Extra |
| 117 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 118 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 120 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 121 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 124 | unsigned Idx = 0; |
| 125 | unsigned TotalBytes = 0; |
| 126 | #define TYPE(Name, Parent) \ |
| 127 | if (counts[Idx]) \ |
| 128 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 129 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 130 | ++Idx; |
| 131 | #define ABSTRACT_TYPE(Name, Parent) |
| 132 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 134 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 135 | |
| 136 | if (ExternalSource.get()) { |
| 137 | fprintf(stderr, "\n"); |
| 138 | ExternalSource->PrintStats(); |
| 139 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 143 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 144 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 145 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 146 | Types.push_back(Ty); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 149 | void ASTContext::InitBuiltinTypes() { |
| 150 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 152 | // C99 6.2.5p19. |
| 153 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | // C99 6.2.5p2. |
| 156 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 157 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 158 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 160 | else |
| 161 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 162 | // C99 6.2.5p4. |
| 163 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 164 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 165 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 166 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 167 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | // C99 6.2.5p6. |
| 170 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 171 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 172 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 173 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 174 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | // C99 6.2.5p10. |
| 177 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 178 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 179 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 181 | // GNU extension, 128-bit integers. |
| 182 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 183 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 184 | |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 185 | if (LangOpts.CPlusPlus) // C++ 3.9.1p5 |
| 186 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 187 | else // C99 |
| 188 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 189 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 190 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 191 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 192 | else // C99 |
| 193 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 194 | |
| 195 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 196 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 197 | else // C99 |
| 198 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 199 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 200 | // Placeholder type for functions. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 201 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 202 | |
| 203 | // Placeholder type for type-dependent expressions whose type is |
| 204 | // completely unknown. No code should ever check a type against |
| 205 | // DependentTy and users should never see it; however, it is here to |
| 206 | // help diagnose failures to properly check for type-dependent |
| 207 | // expressions. |
| 208 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 209 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | // Placeholder type for C++0x auto declarations whose real type has |
Anders Carlsson | e89d159 | 2009-06-26 18:41:36 +0000 | [diff] [blame] | 211 | // not yet been deduced. |
| 212 | InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | // C99 6.2.5p11. |
| 215 | FloatComplexTy = getComplexType(FloatTy); |
| 216 | DoubleComplexTy = getComplexType(DoubleTy); |
| 217 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 218 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 219 | BuiltinVaListType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 221 | // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope(). |
| 222 | ObjCIdTypedefType = QualType(); |
| 223 | ObjCClassTypedefType = QualType(); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 224 | ObjCSelTypedefType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 225 | |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 226 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 227 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 228 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 229 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 231 | ObjCConstantStringType = QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 233 | // void * type |
| 234 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 235 | |
| 236 | // nullptr type (C++0x 2.14.7) |
| 237 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 240 | MemberSpecializationInfo * |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 241 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 242 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 243 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 244 | = InstantiatedFromStaticDataMember.find(Var); |
| 245 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 246 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 248 | return Pos->second; |
| 249 | } |
| 250 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | void |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 252 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
| 253 | TemplateSpecializationKind TSK) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 254 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 255 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 256 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 257 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 258 | InstantiatedFromStaticDataMember[Inst] |
| 259 | = new (*this) MemberSpecializationInfo(Tmpl, TSK); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 260 | } |
| 261 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 262 | NamedDecl * |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 263 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 264 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 265 | = InstantiatedFromUsingDecl.find(UUD); |
| 266 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 267 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 269 | return Pos->second; |
| 270 | } |
| 271 | |
| 272 | void |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 273 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 274 | assert((isa<UsingDecl>(Pattern) || |
| 275 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 276 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 277 | "pattern decl is not a using decl"); |
| 278 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 279 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 280 | } |
| 281 | |
| 282 | UsingShadowDecl * |
| 283 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 284 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 285 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 286 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 287 | return 0; |
| 288 | |
| 289 | return Pos->second; |
| 290 | } |
| 291 | |
| 292 | void |
| 293 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 294 | UsingShadowDecl *Pattern) { |
| 295 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 296 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 299 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 300 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 301 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 302 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 303 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 305 | return Pos->second; |
| 306 | } |
| 307 | |
| 308 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 309 | FieldDecl *Tmpl) { |
| 310 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 311 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 312 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 313 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 315 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 316 | } |
| 317 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 318 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | class BeforeInTranslationUnit |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 320 | : std::binary_function<SourceRange, SourceRange, bool> { |
| 321 | SourceManager *SourceMgr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 323 | public: |
| 324 | explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 326 | bool operator()(SourceRange X, SourceRange Y) { |
| 327 | return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin()); |
| 328 | } |
| 329 | }; |
| 330 | } |
| 331 | |
| 332 | /// \brief Determine whether the given comment is a Doxygen-style comment. |
| 333 | /// |
| 334 | /// \param Start the start of the comment text. |
| 335 | /// |
| 336 | /// \param End the end of the comment text. |
| 337 | /// |
| 338 | /// \param Member whether we want to check whether this is a member comment |
| 339 | /// (which requires a < after the Doxygen-comment delimiter). Otherwise, |
| 340 | /// we only return true when we find a non-member comment. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | static bool |
| 342 | isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 343 | bool Member = false) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | const char *BufferStart |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 345 | = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first; |
| 346 | const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin()); |
| 347 | const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 349 | if (End - Start < 4) |
| 350 | return false; |
| 351 | |
| 352 | assert(Start[0] == '/' && "Not a comment?"); |
| 353 | if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*')) |
| 354 | return false; |
| 355 | if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/')) |
| 356 | return false; |
| 357 | |
| 358 | return (Start[3] == '<') == Member; |
| 359 | } |
| 360 | |
| 361 | /// \brief Retrieve the comment associated with the given declaration, if |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | /// it has one. |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 363 | const char *ASTContext::getCommentForDecl(const Decl *D) { |
| 364 | if (!D) |
| 365 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 367 | // Check whether we have cached a comment string for this declaration |
| 368 | // already. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | llvm::DenseMap<const Decl *, std::string>::iterator Pos |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 370 | = DeclComments.find(D); |
| 371 | if (Pos != DeclComments.end()) |
| 372 | return Pos->second.c_str(); |
| 373 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | // If we have an external AST source and have not yet loaded comments from |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 375 | // that source, do so now. |
| 376 | if (ExternalSource && !LoadedExternalComments) { |
| 377 | std::vector<SourceRange> LoadedComments; |
| 378 | ExternalSource->ReadComments(LoadedComments); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 380 | if (!LoadedComments.empty()) |
| 381 | Comments.insert(Comments.begin(), LoadedComments.begin(), |
| 382 | LoadedComments.end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 383 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 384 | LoadedExternalComments = true; |
| 385 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
| 387 | // If there are no comments anywhere, we won't find anything. |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 388 | if (Comments.empty()) |
| 389 | return 0; |
| 390 | |
| 391 | // If the declaration doesn't map directly to a location in a file, we |
| 392 | // can't find the comment. |
| 393 | SourceLocation DeclStartLoc = D->getLocStart(); |
| 394 | if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID()) |
| 395 | return 0; |
| 396 | |
| 397 | // Find the comment that occurs just before this declaration. |
| 398 | std::vector<SourceRange>::iterator LastComment |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | = std::lower_bound(Comments.begin(), Comments.end(), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 400 | SourceRange(DeclStartLoc), |
| 401 | BeforeInTranslationUnit(&SourceMgr)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 403 | // Decompose the location for the start of the declaration and find the |
| 404 | // beginning of the file buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | std::pair<FileID, unsigned> DeclStartDecomp |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 406 | = SourceMgr.getDecomposedLoc(DeclStartLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | const char *FileBufferStart |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 408 | = SourceMgr.getBufferData(DeclStartDecomp.first).first; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 410 | // First check whether we have a comment for a member. |
| 411 | if (LastComment != Comments.end() && |
| 412 | !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) && |
| 413 | isDoxygenComment(SourceMgr, *LastComment, true)) { |
| 414 | std::pair<FileID, unsigned> LastCommentEndDecomp |
| 415 | = SourceMgr.getDecomposedLoc(LastComment->getEnd()); |
| 416 | if (DeclStartDecomp.first == LastCommentEndDecomp.first && |
| 417 | SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | == SourceMgr.getLineNumber(LastCommentEndDecomp.first, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 419 | LastCommentEndDecomp.second)) { |
| 420 | // The Doxygen member comment comes after the declaration starts and |
| 421 | // is on the same line and in the same file as the declaration. This |
| 422 | // is the comment we want. |
| 423 | std::string &Result = DeclComments[D]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | Result.append(FileBufferStart + |
| 425 | SourceMgr.getFileOffset(LastComment->getBegin()), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 426 | FileBufferStart + LastCommentEndDecomp.second + 1); |
| 427 | return Result.c_str(); |
| 428 | } |
| 429 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 431 | if (LastComment == Comments.begin()) |
| 432 | return 0; |
| 433 | --LastComment; |
| 434 | |
| 435 | // Decompose the end of the comment. |
| 436 | std::pair<FileID, unsigned> LastCommentEndDecomp |
| 437 | = SourceMgr.getDecomposedLoc(LastComment->getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 439 | // If the comment and the declaration aren't in the same file, then they |
| 440 | // aren't related. |
| 441 | if (DeclStartDecomp.first != LastCommentEndDecomp.first) |
| 442 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 444 | // Check that we actually have a Doxygen comment. |
| 445 | if (!isDoxygenComment(SourceMgr, *LastComment)) |
| 446 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 448 | // Compute the starting line for the declaration and for the end of the |
| 449 | // comment (this is expensive). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | unsigned DeclStartLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 451 | = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second); |
| 452 | unsigned CommentEndLine |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | = SourceMgr.getLineNumber(LastCommentEndDecomp.first, |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 454 | LastCommentEndDecomp.second); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 456 | // If the comment does not end on the line prior to the declaration, then |
| 457 | // the comment is not associated with the declaration at all. |
| 458 | if (CommentEndLine + 1 != DeclStartLine) |
| 459 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 461 | // We have a comment, but there may be more comments on the previous lines. |
| 462 | // Keep looking so long as the comments are still Doxygen comments and are |
| 463 | // still adjacent. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | unsigned ExpectedLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 465 | = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1; |
| 466 | std::vector<SourceRange>::iterator FirstComment = LastComment; |
| 467 | while (FirstComment != Comments.begin()) { |
| 468 | // Look at the previous comment |
| 469 | --FirstComment; |
| 470 | std::pair<FileID, unsigned> Decomp |
| 471 | = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 473 | // If this previous comment is in a different file, we're done. |
| 474 | if (Decomp.first != DeclStartDecomp.first) { |
| 475 | ++FirstComment; |
| 476 | break; |
| 477 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 479 | // If this comment is not a Doxygen comment, we're done. |
| 480 | if (!isDoxygenComment(SourceMgr, *FirstComment)) { |
| 481 | ++FirstComment; |
| 482 | break; |
| 483 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 485 | // If the line number is not what we expected, we're done. |
| 486 | unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second); |
| 487 | if (Line != ExpectedLine) { |
| 488 | ++FirstComment; |
| 489 | break; |
| 490 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 492 | // Set the next expected line number. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | ExpectedLine |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 494 | = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1; |
| 495 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 497 | // The iterator range [FirstComment, LastComment] contains all of the |
| 498 | // BCPL comments that, together, are associated with this declaration. |
| 499 | // Form a single comment block string for this declaration that concatenates |
| 500 | // all of these comments. |
| 501 | std::string &Result = DeclComments[D]; |
| 502 | while (FirstComment != LastComment) { |
| 503 | std::pair<FileID, unsigned> DecompStart |
| 504 | = SourceMgr.getDecomposedLoc(FirstComment->getBegin()); |
| 505 | std::pair<FileID, unsigned> DecompEnd |
| 506 | = SourceMgr.getDecomposedLoc(FirstComment->getEnd()); |
| 507 | Result.append(FileBufferStart + DecompStart.second, |
| 508 | FileBufferStart + DecompEnd.second + 1); |
| 509 | ++FirstComment; |
| 510 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 511 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 512 | // Append the last comment line. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | Result.append(FileBufferStart + |
| 514 | SourceMgr.getFileOffset(LastComment->getBegin()), |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 515 | FileBufferStart + LastCommentEndDecomp.second + 1); |
| 516 | return Result.c_str(); |
| 517 | } |
| 518 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 519 | //===----------------------------------------------------------------------===// |
| 520 | // Type Sizing and Analysis |
| 521 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 522 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 523 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 524 | /// scalar floating point type. |
| 525 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 526 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 527 | assert(BT && "Not a floating point type!"); |
| 528 | switch (BT->getKind()) { |
| 529 | default: assert(0 && "Not a floating point type!"); |
| 530 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 531 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 532 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 533 | } |
| 534 | } |
| 535 | |
Mike Stump | 196efbf | 2009-09-22 02:43:44 +0000 | [diff] [blame] | 536 | /// getDeclAlignInBytes - Return a conservative estimate of the alignment of the |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 537 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 538 | /// this method will assert on them. |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 539 | /// If @p RefAsPointee, references are treated like their underlying type |
| 540 | /// (for alignof), else they're treated like pointers (for CodeGen). |
| 541 | unsigned ASTContext::getDeclAlignInBytes(const Decl *D, bool RefAsPointee) { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 542 | unsigned Align = Target.getCharWidth(); |
| 543 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 544 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 545 | Align = std::max(Align, AA->getMaxAlignment()); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 546 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 547 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 548 | QualType T = VD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 549 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 550 | if (RefAsPointee) |
| 551 | T = RT->getPointeeType(); |
| 552 | else |
| 553 | T = getPointerType(RT->getPointeeType()); |
| 554 | } |
| 555 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 556 | // Incomplete or function types default to 1. |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 557 | while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) |
| 558 | T = cast<ArrayType>(T)->getElementType(); |
| 559 | |
| 560 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 561 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 562 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 563 | |
| 564 | return Align / Target.getCharWidth(); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 565 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 566 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 567 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 568 | /// does not work on incomplete types. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 569 | /// |
| 570 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 571 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 572 | /// should take a QualType, &c. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 573 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 574 | ASTContext::getTypeInfo(const Type *T) { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 575 | uint64_t Width=0; |
| 576 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 577 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 578 | #define TYPE(Class, Base) |
| 579 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 580 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 581 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 582 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 583 | assert(false && "Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 584 | break; |
| 585 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 586 | case Type::FunctionNoProto: |
| 587 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 588 | // GCC extension: alignof(function) = 32 bits |
| 589 | Width = 0; |
| 590 | Align = 32; |
| 591 | break; |
| 592 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 593 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 594 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 595 | Width = 0; |
| 596 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 597 | break; |
| 598 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 599 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 600 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 602 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 603 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 604 | Align = EltInfo.second; |
| 605 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 606 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 607 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 608 | case Type::Vector: { |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 609 | const VectorType *VT = cast<VectorType>(T); |
| 610 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 611 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 612 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 613 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 614 | // This happens for non-power-of-2 length vectors. |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 615 | if (VT->getNumElements() & (VT->getNumElements()-1)) { |
| 616 | Align = llvm::NextPowerOf2(Align); |
| 617 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 618 | } |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 619 | break; |
| 620 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 621 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 622 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 623 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 624 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 625 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 626 | // GCC extension: alignof(void) = 8 bits. |
| 627 | Width = 0; |
| 628 | Align = 8; |
| 629 | break; |
| 630 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 631 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 632 | Width = Target.getBoolWidth(); |
| 633 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 634 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 635 | case BuiltinType::Char_S: |
| 636 | case BuiltinType::Char_U: |
| 637 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 638 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 639 | Width = Target.getCharWidth(); |
| 640 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 641 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 642 | case BuiltinType::WChar: |
| 643 | Width = Target.getWCharWidth(); |
| 644 | Align = Target.getWCharAlign(); |
| 645 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 646 | case BuiltinType::Char16: |
| 647 | Width = Target.getChar16Width(); |
| 648 | Align = Target.getChar16Align(); |
| 649 | break; |
| 650 | case BuiltinType::Char32: |
| 651 | Width = Target.getChar32Width(); |
| 652 | Align = Target.getChar32Align(); |
| 653 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 654 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 655 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 656 | Width = Target.getShortWidth(); |
| 657 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 658 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 659 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 660 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 661 | Width = Target.getIntWidth(); |
| 662 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 663 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 664 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 665 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 666 | Width = Target.getLongWidth(); |
| 667 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 668 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 669 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 670 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 671 | Width = Target.getLongLongWidth(); |
| 672 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 673 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 674 | case BuiltinType::Int128: |
| 675 | case BuiltinType::UInt128: |
| 676 | Width = 128; |
| 677 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 678 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 679 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 680 | Width = Target.getFloatWidth(); |
| 681 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 682 | break; |
| 683 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 684 | Width = Target.getDoubleWidth(); |
| 685 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 686 | break; |
| 687 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 688 | Width = Target.getLongDoubleWidth(); |
| 689 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 690 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 691 | case BuiltinType::NullPtr: |
| 692 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 693 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 694 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 695 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 696 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 697 | case Type::FixedWidthInt: |
| 698 | // FIXME: This isn't precisely correct; the width/alignment should depend |
| 699 | // on the available types for the target |
| 700 | Width = cast<FixedWidthIntType>(T)->getWidth(); |
Chris Lattner | 736166b | 2009-02-15 21:20:13 +0000 | [diff] [blame] | 701 | Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 702 | Align = Width; |
| 703 | break; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 704 | case Type::ObjCObjectPointer: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 705 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 706 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 707 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 708 | case Type::BlockPointer: { |
| 709 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 710 | Width = Target.getPointerWidth(AS); |
| 711 | Align = Target.getPointerAlign(AS); |
| 712 | break; |
| 713 | } |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 714 | case Type::LValueReference: |
| 715 | case Type::RValueReference: { |
| 716 | // alignof and sizeof should never enter this code path here, so we go |
| 717 | // the pointer route. |
| 718 | unsigned AS = cast<ReferenceType>(T)->getPointeeType().getAddressSpace(); |
| 719 | Width = Target.getPointerWidth(AS); |
| 720 | Align = Target.getPointerAlign(AS); |
| 721 | break; |
| 722 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 723 | case Type::Pointer: { |
| 724 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 725 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 726 | Align = Target.getPointerAlign(AS); |
| 727 | break; |
| 728 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 729 | case Type::MemberPointer: { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 730 | QualType Pointee = cast<MemberPointerType>(T)->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 731 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 732 | getTypeInfo(getPointerDiffType()); |
| 733 | Width = PtrDiffInfo.first; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 734 | if (Pointee->isFunctionType()) |
| 735 | Width *= 2; |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 736 | Align = PtrDiffInfo.second; |
| 737 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 738 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 739 | case Type::Complex: { |
| 740 | // Complex types have the same alignment as their elements, but twice the |
| 741 | // size. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 743 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 744 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 745 | Align = EltInfo.second; |
| 746 | break; |
| 747 | } |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 748 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 749 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 750 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 751 | Width = Layout.getSize(); |
| 752 | Align = Layout.getAlignment(); |
| 753 | break; |
| 754 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 755 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 756 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 757 | const TagType *TT = cast<TagType>(T); |
| 758 | |
| 759 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 760 | Width = 1; |
| 761 | Align = 1; |
| 762 | break; |
| 763 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 765 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 766 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 767 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 768 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 769 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 770 | Width = Layout.getSize(); |
| 771 | Align = Layout.getAlignment(); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 772 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 773 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 774 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 775 | case Type::SubstTemplateTypeParm: |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 776 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 777 | getReplacementType().getTypePtr()); |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 778 | |
Chris Lattner | 9fcfe92 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 779 | case Type::Elaborated: |
| 780 | return getTypeInfo(cast<ElaboratedType>(T)->getUnderlyingType() |
| 781 | .getTypePtr()); |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 782 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 783 | case Type::Typedef: { |
| 784 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 785 | if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 786 | Align = std::max(Aligned->getMaxAlignment(), |
| 787 | getTypeAlign(Typedef->getUnderlyingType().getTypePtr())); |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 788 | Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr()); |
| 789 | } else |
| 790 | return getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 791 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 792 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 793 | |
| 794 | case Type::TypeOfExpr: |
| 795 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 796 | .getTypePtr()); |
| 797 | |
| 798 | case Type::TypeOf: |
| 799 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 800 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 801 | case Type::Decltype: |
| 802 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 803 | .getTypePtr()); |
| 804 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 805 | case Type::QualifiedName: |
| 806 | return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 808 | case Type::TemplateSpecialization: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | assert(getCanonicalType(T) != T && |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 810 | "Cannot request the size of a dependent type"); |
| 811 | // FIXME: this is likely to be wrong once we support template |
| 812 | // aliases, since a template alias could refer to a typedef that |
| 813 | // has an __aligned__ attribute on it. |
| 814 | return getTypeInfo(getCanonicalType(T)); |
| 815 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 817 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 818 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 821 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 822 | /// type for the current target in bits. This can be different than the ABI |
| 823 | /// alignment in cases where it is beneficial for performance to overalign |
| 824 | /// a data type. |
| 825 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) { |
| 826 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 827 | |
| 828 | // Double and long long should be naturally aligned if possible. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 829 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 830 | T = CT->getElementType().getTypePtr(); |
| 831 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 832 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 833 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 834 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 835 | return ABIAlign; |
| 836 | } |
| 837 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 838 | static void CollectLocalObjCIvars(ASTContext *Ctx, |
| 839 | const ObjCInterfaceDecl *OI, |
| 840 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 841 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 842 | E = OI->ivar_end(); I != E; ++I) { |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 843 | ObjCIvarDecl *IVDecl = *I; |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 844 | if (!IVDecl->isInvalidDecl()) |
| 845 | Fields.push_back(cast<FieldDecl>(IVDecl)); |
| 846 | } |
| 847 | } |
| 848 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 849 | void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 850 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
| 851 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 852 | CollectObjCIvars(SuperClass, Fields); |
| 853 | CollectLocalObjCIvars(this, OI, Fields); |
| 854 | } |
| 855 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 856 | /// ShallowCollectObjCIvars - |
| 857 | /// Collect all ivars, including those synthesized, in the current class. |
| 858 | /// |
| 859 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 860 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars, |
| 861 | bool CollectSynthesized) { |
| 862 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 863 | E = OI->ivar_end(); I != E; ++I) { |
| 864 | Ivars.push_back(*I); |
| 865 | } |
| 866 | if (CollectSynthesized) |
| 867 | CollectSynthesizedIvars(OI, Ivars); |
| 868 | } |
| 869 | |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 870 | void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD, |
| 871 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 872 | for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), |
| 873 | E = PD->prop_end(); I != E; ++I) |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 874 | if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) |
| 875 | Ivars.push_back(Ivar); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 876 | |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 877 | // Also look into nested protocols. |
| 878 | for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), |
| 879 | E = PD->protocol_end(); P != E; ++P) |
| 880 | CollectProtocolSynthesizedIvars(*P, Ivars); |
| 881 | } |
| 882 | |
| 883 | /// CollectSynthesizedIvars - |
| 884 | /// This routine collect synthesized ivars for the designated class. |
| 885 | /// |
| 886 | void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI, |
| 887 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 888 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), |
| 889 | E = OI->prop_end(); I != E; ++I) { |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 890 | if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) |
| 891 | Ivars.push_back(Ivar); |
| 892 | } |
| 893 | // Also look into interface's protocol list for properties declared |
| 894 | // in the protocol and whose ivars are synthesized. |
| 895 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 896 | PE = OI->protocol_end(); P != PE; ++P) { |
| 897 | ObjCProtocolDecl *PD = (*P); |
| 898 | CollectProtocolSynthesizedIvars(PD, Ivars); |
| 899 | } |
| 900 | } |
| 901 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 902 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 903 | /// those inherited by it. |
| 904 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
| 905 | llvm::SmallVectorImpl<ObjCProtocolDecl*> &Protocols) { |
| 906 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 907 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 908 | PE = OI->protocol_end(); P != PE; ++P) { |
| 909 | ObjCProtocolDecl *Proto = (*P); |
| 910 | Protocols.push_back(Proto); |
| 911 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 912 | PE = Proto->protocol_end(); P != PE; ++P) |
| 913 | CollectInheritedProtocols(*P, Protocols); |
| 914 | } |
| 915 | |
| 916 | // Categories of this Interface. |
| 917 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 918 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 919 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 920 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 921 | while (SD) { |
| 922 | CollectInheritedProtocols(SD, Protocols); |
| 923 | SD = SD->getSuperClass(); |
| 924 | } |
| 925 | return; |
| 926 | } |
| 927 | if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 928 | for (ObjCInterfaceDecl::protocol_iterator P = OC->protocol_begin(), |
| 929 | PE = OC->protocol_end(); P != PE; ++P) { |
| 930 | ObjCProtocolDecl *Proto = (*P); |
| 931 | Protocols.push_back(Proto); |
| 932 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 933 | PE = Proto->protocol_end(); P != PE; ++P) |
| 934 | CollectInheritedProtocols(*P, Protocols); |
| 935 | } |
| 936 | return; |
| 937 | } |
| 938 | if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 939 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 940 | PE = OP->protocol_end(); P != PE; ++P) { |
| 941 | ObjCProtocolDecl *Proto = (*P); |
| 942 | Protocols.push_back(Proto); |
| 943 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 944 | PE = Proto->protocol_end(); P != PE; ++P) |
| 945 | CollectInheritedProtocols(*P, Protocols); |
| 946 | } |
| 947 | return; |
| 948 | } |
| 949 | } |
| 950 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 951 | unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) { |
| 952 | unsigned count = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 953 | for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(), |
| 954 | E = PD->prop_end(); I != E; ++I) |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 955 | if ((*I)->getPropertyIvarDecl()) |
| 956 | ++count; |
| 957 | |
| 958 | // Also look into nested protocols. |
| 959 | for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), |
| 960 | E = PD->protocol_end(); P != E; ++P) |
| 961 | count += CountProtocolSynthesizedIvars(*P); |
| 962 | return count; |
| 963 | } |
| 964 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 966 | unsigned count = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 967 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(), |
| 968 | E = OI->prop_end(); I != E; ++I) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 969 | if ((*I)->getPropertyIvarDecl()) |
| 970 | ++count; |
| 971 | } |
| 972 | // Also look into interface's protocol list for properties declared |
| 973 | // in the protocol and whose ivars are synthesized. |
| 974 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 975 | PE = OI->protocol_end(); P != PE; ++P) { |
| 976 | ObjCProtocolDecl *PD = (*P); |
| 977 | count += CountProtocolSynthesizedIvars(PD); |
| 978 | } |
| 979 | return count; |
| 980 | } |
| 981 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 982 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 983 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 984 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 985 | I = ObjCImpls.find(D); |
| 986 | if (I != ObjCImpls.end()) |
| 987 | return cast<ObjCImplementationDecl>(I->second); |
| 988 | return 0; |
| 989 | } |
| 990 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 991 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 992 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 993 | I = ObjCImpls.find(D); |
| 994 | if (I != ObjCImpls.end()) |
| 995 | return cast<ObjCCategoryImplDecl>(I->second); |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1000 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1001 | ObjCImplementationDecl *ImplD) { |
| 1002 | assert(IFaceD && ImplD && "Passed null params"); |
| 1003 | ObjCImpls[IFaceD] = ImplD; |
| 1004 | } |
| 1005 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1006 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1007 | ObjCCategoryImplDecl *ImplD) { |
| 1008 | assert(CatD && ImplD && "Passed null params"); |
| 1009 | ObjCImpls[CatD] = ImplD; |
| 1010 | } |
| 1011 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1012 | /// \brief Allocate an uninitialized TypeSourceInfo. |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1013 | /// |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1014 | /// The caller should initialize the memory held by TypeSourceInfo using |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1015 | /// the TypeLoc wrappers. |
| 1016 | /// |
| 1017 | /// \param T the type that will be the basis for type source info. This type |
| 1018 | /// should refer to how the declarator was written in source code, not to |
| 1019 | /// what type semantic analysis resolved the declarator to. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1020 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1021 | unsigned DataSize) { |
| 1022 | if (!DataSize) |
| 1023 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1024 | else |
| 1025 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1026 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 109de5e | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1027 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1028 | TypeSourceInfo *TInfo = |
| 1029 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1030 | new (TInfo) TypeSourceInfo(T); |
| 1031 | return TInfo; |
Argyrios Kyrtzidis | b17166c | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1034 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1035 | SourceLocation L) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1036 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
John McCall | a4eb74d | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1037 | DI->getTypeLoc().initialize(L); |
| 1038 | return DI; |
| 1039 | } |
| 1040 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1041 | /// getInterfaceLayoutImpl - Get or compute information about the |
| 1042 | /// layout of the given interface. |
| 1043 | /// |
| 1044 | /// \param Impl - If given, also include the layout of the interface's |
| 1045 | /// implementation. This may differ by including synthesized ivars. |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1046 | const ASTRecordLayout & |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1047 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
| 1048 | const ObjCImplementationDecl *Impl) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 1049 | assert(!D->isForwardDecl() && "Invalid interface decl!"); |
| 1050 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1051 | // Look up this layout, if already laid out, return what we have. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | ObjCContainerDecl *Key = |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 1053 | Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; |
| 1054 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 1055 | return *Entry; |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1056 | |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 1057 | // Add in synthesized ivar count if laying out an implementation. |
| 1058 | if (Impl) { |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 1059 | unsigned FieldCount = D->ivar_size(); |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1060 | unsigned SynthCount = CountSynthesizedIvars(D); |
| 1061 | FieldCount += SynthCount; |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 1062 | // If there aren't any sythesized ivars then reuse the interface |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 1063 | // entry. Note we can't cache this because we simply free all |
| 1064 | // entries later; however we shouldn't look up implementations |
| 1065 | // frequently. |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1066 | if (SynthCount == 0) |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 1067 | return getObjCLayout(D, 0); |
| 1068 | } |
| 1069 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1070 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 1071 | ASTRecordLayoutBuilder::ComputeLayout(*this, D, Impl); |
| 1072 | ObjCLayouts[Key] = NewEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1074 | return *NewEntry; |
| 1075 | } |
| 1076 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1077 | const ASTRecordLayout & |
| 1078 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 1079 | return getObjCLayout(D, 0); |
| 1080 | } |
| 1081 | |
| 1082 | const ASTRecordLayout & |
| 1083 | ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { |
| 1084 | return getObjCLayout(D->getClassInterface(), D); |
| 1085 | } |
| 1086 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 1087 | /// getASTRecordLayout - Get or compute information about the layout of the |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1088 | /// specified record (struct/union/class), which indicates its size and field |
| 1089 | /// position information. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1090 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1091 | D = D->getDefinition(*this); |
| 1092 | assert(D && "Cannot get layout of forward declarations!"); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1093 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1094 | // Look up this layout, if already laid out, return what we have. |
Eli Friedman | ab22c43 | 2009-07-22 20:29:16 +0000 | [diff] [blame] | 1095 | // Note that we can't save a reference to the entry because this function |
| 1096 | // is recursive. |
| 1097 | const ASTRecordLayout *Entry = ASTRecordLayouts[D]; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1098 | if (Entry) return *Entry; |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1099 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | const ASTRecordLayout *NewEntry = |
Anders Carlsson | 29445a0 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 1101 | ASTRecordLayoutBuilder::ComputeLayout(*this, D); |
Eli Friedman | ab22c43 | 2009-07-22 20:29:16 +0000 | [diff] [blame] | 1102 | ASTRecordLayouts[D] = NewEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1104 | return *NewEntry; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Anders Carlsson | f53df23 | 2009-12-07 04:35:11 +0000 | [diff] [blame] | 1107 | const CXXMethodDecl *ASTContext::getKeyFunction(const CXXRecordDecl *RD) { |
| 1108 | RD = cast<CXXRecordDecl>(RD->getDefinition(*this)); |
| 1109 | assert(RD && "Cannot get key function for forward declarations!"); |
| 1110 | |
| 1111 | const CXXMethodDecl *&Entry = KeyFunctions[RD]; |
| 1112 | if (!Entry) |
| 1113 | Entry = ASTRecordLayoutBuilder::ComputeKeyFunction(RD); |
| 1114 | else |
| 1115 | assert(Entry == ASTRecordLayoutBuilder::ComputeKeyFunction(RD) && |
| 1116 | "Key function changed!"); |
| 1117 | |
| 1118 | return Entry; |
| 1119 | } |
| 1120 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1121 | //===----------------------------------------------------------------------===// |
| 1122 | // Type creation/memoization methods |
| 1123 | //===----------------------------------------------------------------------===// |
| 1124 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1125 | QualType ASTContext::getExtQualType(const Type *TypeNode, Qualifiers Quals) { |
| 1126 | unsigned Fast = Quals.getFastQualifiers(); |
| 1127 | Quals.removeFastQualifiers(); |
| 1128 | |
| 1129 | // Check if we've already instantiated this type. |
| 1130 | llvm::FoldingSetNodeID ID; |
| 1131 | ExtQuals::Profile(ID, TypeNode, Quals); |
| 1132 | void *InsertPos = 0; |
| 1133 | if (ExtQuals *EQ = ExtQualNodes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 1134 | assert(EQ->getQualifiers() == Quals); |
| 1135 | QualType T = QualType(EQ, Fast); |
| 1136 | return T; |
| 1137 | } |
| 1138 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1139 | ExtQuals *New = new (*this, TypeAlignment) ExtQuals(*this, TypeNode, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1140 | ExtQualNodes.InsertNode(New, InsertPos); |
| 1141 | QualType T = QualType(New, Fast); |
| 1142 | return T; |
| 1143 | } |
| 1144 | |
| 1145 | QualType ASTContext::getVolatileType(QualType T) { |
| 1146 | QualType CanT = getCanonicalType(T); |
| 1147 | if (CanT.isVolatileQualified()) return T; |
| 1148 | |
| 1149 | QualifierCollector Quals; |
| 1150 | const Type *TypeNode = Quals.strip(T); |
| 1151 | Quals.addVolatile(); |
| 1152 | |
| 1153 | return getExtQualType(TypeNode, Quals); |
| 1154 | } |
| 1155 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1156 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1157 | QualType CanT = getCanonicalType(T); |
| 1158 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1159 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1160 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1161 | // If we are composing extended qualifiers together, merge together |
| 1162 | // into one ExtQuals node. |
| 1163 | QualifierCollector Quals; |
| 1164 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1165 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1166 | // If this type already has an address space specified, it cannot get |
| 1167 | // another one. |
| 1168 | assert(!Quals.hasAddressSpace() && |
| 1169 | "Type cannot be in multiple addr spaces!"); |
| 1170 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1171 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1172 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1175 | QualType ASTContext::getObjCGCQualType(QualType T, |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1176 | Qualifiers::GC GCAttr) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1177 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1178 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1179 | return T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1181 | if (T->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1182 | QualType Pointee = T->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1183 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1184 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1185 | return getPointerType(ResultType); |
| 1186 | } |
| 1187 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1189 | // If we are composing extended qualifiers together, merge together |
| 1190 | // into one ExtQuals node. |
| 1191 | QualifierCollector Quals; |
| 1192 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1194 | // If this type already has an ObjCGC specified, it cannot get |
| 1195 | // another one. |
| 1196 | assert(!Quals.hasObjCGCAttr() && |
| 1197 | "Type cannot have multiple ObjCGCs!"); |
| 1198 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1200 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1201 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1202 | |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1203 | QualType ASTContext::getNoReturnType(QualType T, bool AddNoReturn) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1204 | QualType ResultType; |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1205 | if (const PointerType *Pointer = T->getAs<PointerType>()) { |
| 1206 | QualType Pointee = Pointer->getPointeeType(); |
| 1207 | ResultType = getNoReturnType(Pointee, AddNoReturn); |
| 1208 | if (ResultType == Pointee) |
| 1209 | return T; |
| 1210 | |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 1211 | ResultType = getPointerType(ResultType); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1212 | } else if (const BlockPointerType *BlockPointer |
| 1213 | = T->getAs<BlockPointerType>()) { |
| 1214 | QualType Pointee = BlockPointer->getPointeeType(); |
| 1215 | ResultType = getNoReturnType(Pointee, AddNoReturn); |
| 1216 | if (ResultType == Pointee) |
| 1217 | return T; |
| 1218 | |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 1219 | ResultType = getBlockPointerType(ResultType); |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1220 | } else if (const FunctionType *F = T->getAs<FunctionType>()) { |
| 1221 | if (F->getNoReturnAttr() == AddNoReturn) |
| 1222 | return T; |
| 1223 | |
| 1224 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(F)) { |
| 1225 | ResultType = getFunctionNoProtoType(FNPT->getResultType(), AddNoReturn); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1226 | } else { |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1227 | const FunctionProtoType *FPT = cast<FunctionProtoType>(F); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1228 | ResultType |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1229 | = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1230 | FPT->getNumArgs(), FPT->isVariadic(), |
| 1231 | FPT->getTypeQuals(), |
| 1232 | FPT->hasExceptionSpec(), FPT->hasAnyExceptionSpec(), |
| 1233 | FPT->getNumExceptions(), FPT->exception_begin(), |
| 1234 | AddNoReturn); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1235 | } |
Douglas Gregor | 43c79c2 | 2009-12-09 00:47:37 +0000 | [diff] [blame] | 1236 | } else |
| 1237 | return T; |
| 1238 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1239 | return getQualifiedType(ResultType, T.getLocalQualifiers()); |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1242 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1243 | /// number with the specified element type. |
| 1244 | QualType ASTContext::getComplexType(QualType T) { |
| 1245 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1246 | // structure. |
| 1247 | llvm::FoldingSetNodeID ID; |
| 1248 | ComplexType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1250 | void *InsertPos = 0; |
| 1251 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1252 | return QualType(CT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1253 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1254 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1255 | // so fill in the canonical type field. |
| 1256 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1257 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1258 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1260 | // Get the new insert position for the node we care about. |
| 1261 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1262 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1263 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1264 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1265 | Types.push_back(New); |
| 1266 | ComplexTypes.InsertNode(New, InsertPos); |
| 1267 | return QualType(New, 0); |
| 1268 | } |
| 1269 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1270 | QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) { |
| 1271 | llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ? |
| 1272 | SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes; |
| 1273 | FixedWidthIntType *&Entry = Map[Width]; |
| 1274 | if (!Entry) |
| 1275 | Entry = new FixedWidthIntType(Width, Signed); |
| 1276 | return QualType(Entry, 0); |
| 1277 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1278 | |
| 1279 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1280 | /// the specified type. |
| 1281 | QualType ASTContext::getPointerType(QualType T) { |
| 1282 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1283 | // structure. |
| 1284 | llvm::FoldingSetNodeID ID; |
| 1285 | PointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1287 | void *InsertPos = 0; |
| 1288 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1289 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1291 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1292 | // so fill in the canonical type field. |
| 1293 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1294 | if (!T.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1295 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1296 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1297 | // Get the new insert position for the node we care about. |
| 1298 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1299 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1300 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1301 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1302 | Types.push_back(New); |
| 1303 | PointerTypes.InsertNode(New, InsertPos); |
| 1304 | return QualType(New, 0); |
| 1305 | } |
| 1306 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1308 | /// a pointer to the specified block. |
| 1309 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1310 | assert(T->isFunctionType() && "block of function types only"); |
| 1311 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1312 | // structure. |
| 1313 | llvm::FoldingSetNodeID ID; |
| 1314 | BlockPointerType::Profile(ID, T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1316 | void *InsertPos = 0; |
| 1317 | if (BlockPointerType *PT = |
| 1318 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1319 | return QualType(PT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1320 | |
| 1321 | // 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] | 1322 | // type either so fill in the canonical type field. |
| 1323 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1324 | if (!T.isCanonical()) { |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1325 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1326 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1327 | // Get the new insert position for the node we care about. |
| 1328 | BlockPointerType *NewIP = |
| 1329 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1330 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1331 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1332 | BlockPointerType *New |
| 1333 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1334 | Types.push_back(New); |
| 1335 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1336 | return QualType(New, 0); |
| 1337 | } |
| 1338 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1339 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1340 | /// lvalue reference to the specified type. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1341 | QualType ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1342 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1343 | // structure. |
| 1344 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1345 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1346 | |
| 1347 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1348 | if (LValueReferenceType *RT = |
| 1349 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1350 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1351 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1352 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1353 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1354 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1355 | // either, so fill in the canonical type field. |
| 1356 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1357 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1358 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1359 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1360 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1361 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1362 | LValueReferenceType *NewIP = |
| 1363 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1364 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1367 | LValueReferenceType *New |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1368 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1369 | SpelledAsLValue); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1370 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1371 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1372 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1373 | return QualType(New, 0); |
| 1374 | } |
| 1375 | |
| 1376 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1377 | /// rvalue reference to the specified type. |
| 1378 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1379 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1380 | // structure. |
| 1381 | llvm::FoldingSetNodeID ID; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1382 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1383 | |
| 1384 | void *InsertPos = 0; |
| 1385 | if (RValueReferenceType *RT = |
| 1386 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1387 | return QualType(RT, 0); |
| 1388 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1389 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1390 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1391 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1392 | // either, so fill in the canonical type field. |
| 1393 | QualType Canonical; |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1394 | if (InnerRef || !T.isCanonical()) { |
| 1395 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1396 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1397 | |
| 1398 | // Get the new insert position for the node we care about. |
| 1399 | RValueReferenceType *NewIP = |
| 1400 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1401 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1402 | } |
| 1403 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1404 | RValueReferenceType *New |
| 1405 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1406 | Types.push_back(New); |
| 1407 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1408 | return QualType(New, 0); |
| 1409 | } |
| 1410 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1411 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1412 | /// member pointer to the specified type, in the specified class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1413 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1414 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1415 | // structure. |
| 1416 | llvm::FoldingSetNodeID ID; |
| 1417 | MemberPointerType::Profile(ID, T, Cls); |
| 1418 | |
| 1419 | void *InsertPos = 0; |
| 1420 | if (MemberPointerType *PT = |
| 1421 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1422 | return QualType(PT, 0); |
| 1423 | |
| 1424 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1425 | // type either, so fill in the canonical type field. |
| 1426 | QualType Canonical; |
Douglas Gregor | 87c12c4 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1427 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1428 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1429 | |
| 1430 | // Get the new insert position for the node we care about. |
| 1431 | MemberPointerType *NewIP = |
| 1432 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1433 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1434 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1435 | MemberPointerType *New |
| 1436 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1437 | Types.push_back(New); |
| 1438 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1439 | return QualType(New, 0); |
| 1440 | } |
| 1441 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1443 | /// array of the specified element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1445 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1446 | ArrayType::ArraySizeModifier ASM, |
| 1447 | unsigned EltTypeQuals) { |
Sebastian Redl | 923d56d | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1448 | assert((EltTy->isDependentType() || |
| 1449 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1450 | "Constant array of VLAs is illegal!"); |
| 1451 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1452 | // Convert the array size into a canonical width matching the pointer size for |
| 1453 | // the target. |
| 1454 | llvm::APInt ArySize(ArySizeIn); |
| 1455 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1456 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1457 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1458 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1460 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | if (ConstantArrayType *ATP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1462 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1463 | return QualType(ATP, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1465 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1466 | // so fill in the canonical type field. |
| 1467 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1468 | if (!EltTy.isCanonical()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1470 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1471 | // Get the new insert position for the node we care about. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | ConstantArrayType *NewIP = |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1473 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1474 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1475 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1477 | ConstantArrayType *New = new(*this,TypeAlignment) |
| 1478 | ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1479 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1480 | Types.push_back(New); |
| 1481 | return QualType(New, 0); |
| 1482 | } |
| 1483 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1484 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1485 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1486 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1487 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1488 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1489 | unsigned EltTypeQuals, |
| 1490 | SourceRange Brackets) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1491 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1492 | // that have an expression provided for their size. |
| 1493 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1494 | VariableArrayType *New = new(*this, TypeAlignment) |
| 1495 | VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1496 | |
| 1497 | VariableArrayTypes.push_back(New); |
| 1498 | Types.push_back(New); |
| 1499 | return QualType(New, 0); |
| 1500 | } |
| 1501 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1502 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1503 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1504 | /// type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1505 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1506 | Expr *NumElts, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1507 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1508 | unsigned EltTypeQuals, |
| 1509 | SourceRange Brackets) { |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1510 | assert((!NumElts || NumElts->isTypeDependent() || |
| 1511 | NumElts->isValueDependent()) && |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1512 | "Size must be type- or value-dependent!"); |
| 1513 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1514 | void *InsertPos = 0; |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1515 | DependentSizedArrayType *Canon = 0; |
| 1516 | |
| 1517 | if (NumElts) { |
| 1518 | // Dependently-sized array types that do not have a specified |
| 1519 | // number of elements will have their sizes deduced from an |
| 1520 | // initializer. |
| 1521 | llvm::FoldingSetNodeID ID; |
| 1522 | DependentSizedArrayType::Profile(ID, *this, getCanonicalType(EltTy), ASM, |
| 1523 | EltTypeQuals, NumElts); |
| 1524 | |
| 1525 | Canon = DependentSizedArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1526 | } |
| 1527 | |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1528 | DependentSizedArrayType *New; |
| 1529 | if (Canon) { |
| 1530 | // We already have a canonical version of this array type; use it as |
| 1531 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1532 | New = new (*this, TypeAlignment) |
| 1533 | DependentSizedArrayType(*this, EltTy, QualType(Canon, 0), |
| 1534 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1535 | } else { |
| 1536 | QualType CanonEltTy = getCanonicalType(EltTy); |
| 1537 | if (CanonEltTy == EltTy) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1538 | New = new (*this, TypeAlignment) |
| 1539 | DependentSizedArrayType(*this, EltTy, QualType(), |
| 1540 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | cb78d88 | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 1541 | |
| 1542 | if (NumElts) |
| 1543 | DependentSizedArrayTypes.InsertNode(New, InsertPos); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1544 | } else { |
| 1545 | QualType Canon = getDependentSizedArrayType(CanonEltTy, NumElts, |
| 1546 | ASM, EltTypeQuals, |
| 1547 | SourceRange()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1548 | New = new (*this, TypeAlignment) |
| 1549 | DependentSizedArrayType(*this, EltTy, Canon, |
| 1550 | NumElts, ASM, EltTypeQuals, Brackets); |
Douglas Gregor | 04d4bee | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 1551 | } |
| 1552 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1553 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1554 | Types.push_back(New); |
| 1555 | return QualType(New, 0); |
| 1556 | } |
| 1557 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1558 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1559 | ArrayType::ArraySizeModifier ASM, |
| 1560 | unsigned EltTypeQuals) { |
| 1561 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1562 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1563 | |
| 1564 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | if (IncompleteArrayType *ATP = |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1566 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1567 | return QualType(ATP, 0); |
| 1568 | |
| 1569 | // If the element type isn't canonical, this won't be a canonical type |
| 1570 | // either, so fill in the canonical type field. |
| 1571 | QualType Canonical; |
| 1572 | |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1573 | if (!EltTy.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1574 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1575 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1576 | |
| 1577 | // Get the new insert position for the node we care about. |
| 1578 | IncompleteArrayType *NewIP = |
| 1579 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1580 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1581 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1582 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1583 | IncompleteArrayType *New = new (*this, TypeAlignment) |
| 1584 | IncompleteArrayType(EltTy, Canonical, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1585 | |
| 1586 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1587 | Types.push_back(New); |
| 1588 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1591 | /// getVectorType - Return the unique reference to a vector type of |
| 1592 | /// the specified element type and size. VectorType must be a built-in type. |
| 1593 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1594 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1596 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1597 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1598 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1599 | // Check if we've already instantiated a vector of this type. |
| 1600 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1602 | void *InsertPos = 0; |
| 1603 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1604 | return QualType(VTP, 0); |
| 1605 | |
| 1606 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1607 | // so fill in the canonical type field. |
| 1608 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1609 | if (!vecType.isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1610 | Canonical = getVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1612 | // Get the new insert position for the node we care about. |
| 1613 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1614 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1615 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1616 | VectorType *New = new (*this, TypeAlignment) |
| 1617 | VectorType(vecType, NumElts, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1618 | VectorTypes.InsertNode(New, InsertPos); |
| 1619 | Types.push_back(New); |
| 1620 | return QualType(New, 0); |
| 1621 | } |
| 1622 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1623 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1624 | /// 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] | 1625 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1626 | BuiltinType *baseType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1628 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1629 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1630 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1631 | // Check if we've already instantiated a vector of this type. |
| 1632 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1633 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1634 | void *InsertPos = 0; |
| 1635 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1636 | return QualType(VTP, 0); |
| 1637 | |
| 1638 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1639 | // so fill in the canonical type field. |
| 1640 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1641 | if (!vecType.isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1642 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1644 | // Get the new insert position for the node we care about. |
| 1645 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1646 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1647 | } |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1648 | ExtVectorType *New = new (*this, TypeAlignment) |
| 1649 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1650 | VectorTypes.InsertNode(New, InsertPos); |
| 1651 | Types.push_back(New); |
| 1652 | return QualType(New, 0); |
| 1653 | } |
| 1654 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | QualType ASTContext::getDependentSizedExtVectorType(QualType vecType, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1656 | Expr *SizeExpr, |
| 1657 | SourceLocation AttrLoc) { |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1658 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1660 | SizeExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1661 | |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1662 | void *InsertPos = 0; |
| 1663 | DependentSizedExtVectorType *Canon |
| 1664 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1665 | DependentSizedExtVectorType *New; |
| 1666 | if (Canon) { |
| 1667 | // We already have a canonical version of this array type; use it as |
| 1668 | // the canonical type for a newly-built type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1669 | New = new (*this, TypeAlignment) |
| 1670 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 1671 | SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1672 | } else { |
| 1673 | QualType CanonVecTy = getCanonicalType(vecType); |
| 1674 | if (CanonVecTy == vecType) { |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1675 | New = new (*this, TypeAlignment) |
| 1676 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 1677 | AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1678 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 1679 | } else { |
| 1680 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 1681 | SourceLocation()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1682 | New = new (*this, TypeAlignment) |
| 1683 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 2ec09f1 | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 1684 | } |
| 1685 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1686 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1687 | Types.push_back(New); |
| 1688 | return QualType(New, 0); |
| 1689 | } |
| 1690 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1691 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1692 | /// |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1693 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1694 | // Unique functions, to guarantee there is only one function of a particular |
| 1695 | // structure. |
| 1696 | llvm::FoldingSetNodeID ID; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1697 | FunctionNoProtoType::Profile(ID, ResultTy, NoReturn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1698 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1699 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1700 | if (FunctionNoProtoType *FT = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1701 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1702 | return QualType(FT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1703 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1704 | QualType Canonical; |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1705 | if (!ResultTy.isCanonical()) { |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1706 | Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1708 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1709 | FunctionNoProtoType *NewIP = |
| 1710 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1711 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1712 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1713 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1714 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
| 1715 | FunctionNoProtoType(ResultTy, Canonical, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1716 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1717 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1718 | return QualType(New, 0); |
| 1719 | } |
| 1720 | |
| 1721 | /// getFunctionType - Return a normal function type with a typed argument |
| 1722 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1723 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1724 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1725 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1726 | bool hasAnyExceptionSpec, unsigned NumExs, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1727 | const QualType *ExArray, bool NoReturn) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1728 | // Unique functions, to guarantee there is only one function of a particular |
| 1729 | // structure. |
| 1730 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1731 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1732 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1733 | NumExs, ExArray, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1734 | |
| 1735 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1736 | if (FunctionProtoType *FTP = |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1737 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1738 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1739 | |
| 1740 | // Determine whether the type being created is already canonical or not. |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1741 | bool isCanonical = !hasExceptionSpec && ResultTy.isCanonical(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1742 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1743 | if (!ArgArray[i].isCanonicalAsParam()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1744 | isCanonical = false; |
| 1745 | |
| 1746 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1747 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1748 | QualType Canonical; |
| 1749 | if (!isCanonical) { |
| 1750 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1751 | CanonicalArgs.reserve(NumArgs); |
| 1752 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1753 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1754 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1755 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1756 | CanonicalArgs.data(), NumArgs, |
Douglas Gregor | 47259d9 | 2009-08-05 19:03:35 +0000 | [diff] [blame] | 1757 | isVariadic, TypeQuals, false, |
| 1758 | false, 0, 0, NoReturn); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1759 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1760 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1761 | FunctionProtoType *NewIP = |
| 1762 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1763 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1764 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1765 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1766 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1767 | // for two variable size arrays (for parameter and exception types) at the |
| 1768 | // end of them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | FunctionProtoType *FTP = |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1770 | (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + |
| 1771 | NumArgs*sizeof(QualType) + |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1772 | NumExs*sizeof(QualType), TypeAlignment); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1773 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1774 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 1775 | ExArray, NumExs, Canonical, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1776 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1777 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1778 | return QualType(FTP, 0); |
| 1779 | } |
| 1780 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1781 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1782 | /// specified type declaration. |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1783 | QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1784 | assert(Decl && "Passed null for Decl param"); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1785 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1787 | if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1788 | return getTypedefType(Typedef); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1789 | else if (isa<TemplateTypeParmDecl>(Decl)) { |
| 1790 | assert(false && "Template type parameter types are always available."); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1791 | } else if (ObjCInterfaceDecl *ObjCInterface |
| 1792 | = dyn_cast<ObjCInterfaceDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1793 | return getObjCInterfaceType(ObjCInterface); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1794 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1795 | if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1796 | if (PrevDecl) |
| 1797 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1798 | else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1799 | Decl->TypeForDecl = new (*this, TypeAlignment) RecordType(Record); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1800 | } else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1801 | if (PrevDecl) |
| 1802 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1803 | else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1804 | Decl->TypeForDecl = new (*this, TypeAlignment) EnumType(Enum); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1805 | } else if (UnresolvedUsingTypenameDecl *Using = |
| 1806 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
| 1807 | Decl->TypeForDecl = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 1808 | } else |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1809 | assert(false && "TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1810 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1811 | if (!PrevDecl) Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1812 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1815 | /// getTypedefType - Return the unique reference to the type for the |
| 1816 | /// specified typename decl. |
| 1817 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 1818 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1819 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1820 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1821 | Decl->TypeForDecl = new(*this, TypeAlignment) |
| 1822 | TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1823 | Types.push_back(Decl->TypeForDecl); |
| 1824 | return QualType(Decl->TypeForDecl, 0); |
| 1825 | } |
| 1826 | |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1827 | /// \brief Retrieve a substitution-result type. |
| 1828 | QualType |
| 1829 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
| 1830 | QualType Replacement) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1831 | assert(Replacement.isCanonical() |
John McCall | 49a832b | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1832 | && "replacement types must always be canonical"); |
| 1833 | |
| 1834 | llvm::FoldingSetNodeID ID; |
| 1835 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 1836 | void *InsertPos = 0; |
| 1837 | SubstTemplateTypeParmType *SubstParm |
| 1838 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1839 | |
| 1840 | if (!SubstParm) { |
| 1841 | SubstParm = new (*this, TypeAlignment) |
| 1842 | SubstTemplateTypeParmType(Parm, Replacement); |
| 1843 | Types.push_back(SubstParm); |
| 1844 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 1845 | } |
| 1846 | |
| 1847 | return QualType(SubstParm, 0); |
| 1848 | } |
| 1849 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1850 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1851 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1852 | /// name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1854 | bool ParameterPack, |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1855 | IdentifierInfo *Name) { |
| 1856 | llvm::FoldingSetNodeID ID; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1857 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1858 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | TemplateTypeParmType *TypeParm |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1860 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1861 | |
| 1862 | if (TypeParm) |
| 1863 | return QualType(TypeParm, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1864 | |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1865 | if (Name) { |
| 1866 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1867 | TypeParm = new (*this, TypeAlignment) |
| 1868 | TemplateTypeParmType(Depth, Index, ParameterPack, Name, Canon); |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1869 | } else |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1870 | TypeParm = new (*this, TypeAlignment) |
| 1871 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1872 | |
| 1873 | Types.push_back(TypeParm); |
| 1874 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1875 | |
| 1876 | return QualType(TypeParm, 0); |
| 1877 | } |
| 1878 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1880 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1881 | const TemplateArgumentListInfo &Args, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1882 | QualType Canon) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1883 | unsigned NumArgs = Args.size(); |
| 1884 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1885 | llvm::SmallVector<TemplateArgument, 4> ArgVec; |
| 1886 | ArgVec.reserve(NumArgs); |
| 1887 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1888 | ArgVec.push_back(Args[i].getArgument()); |
| 1889 | |
| 1890 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, Canon); |
| 1891 | } |
| 1892 | |
| 1893 | QualType |
| 1894 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1895 | const TemplateArgument *Args, |
| 1896 | unsigned NumArgs, |
| 1897 | QualType Canon) { |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1898 | if (!Canon.isNull()) |
| 1899 | Canon = getCanonicalType(Canon); |
| 1900 | else { |
| 1901 | // Build the canonical template specialization type. |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1902 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
| 1903 | llvm::SmallVector<TemplateArgument, 4> CanonArgs; |
| 1904 | CanonArgs.reserve(NumArgs); |
| 1905 | for (unsigned I = 0; I != NumArgs; ++I) |
| 1906 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 1907 | |
| 1908 | // Determine whether this canonical template specialization type already |
| 1909 | // exists. |
| 1910 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1911 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1912 | CanonArgs.data(), NumArgs, *this); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1913 | |
| 1914 | void *InsertPos = 0; |
| 1915 | TemplateSpecializationType *Spec |
| 1916 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1917 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1918 | if (!Spec) { |
| 1919 | // Allocate a new canonical template specialization type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1920 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1921 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1922 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | Spec = new (Mem) TemplateSpecializationType(*this, CanonTemplate, |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1924 | CanonArgs.data(), NumArgs, |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1925 | Canon); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1926 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1927 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1928 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1930 | if (Canon.isNull()) |
| 1931 | Canon = QualType(Spec, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | assert(Canon->isDependentType() && |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1933 | "Non-dependent template-id type must have a canonical type"); |
Douglas Gregor | b88e888 | 2009-07-30 17:40:51 +0000 | [diff] [blame] | 1934 | } |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 1935 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 1936 | // Allocate the (non-canonical) template specialization type, but don't |
| 1937 | // try to unique it: these types typically have location information that |
| 1938 | // we don't unique and don't want to lose. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1939 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1940 | sizeof(TemplateArgument) * NumArgs), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1941 | TypeAlignment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | TemplateSpecializationType *Spec |
| 1943 | = new (Mem) TemplateSpecializationType(*this, Template, Args, NumArgs, |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 1944 | Canon); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1946 | Types.push_back(Spec); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1947 | return QualType(Spec, 0); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | QualType |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1951 | ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS, |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1952 | QualType NamedType) { |
| 1953 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1954 | QualifiedNameType::Profile(ID, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1955 | |
| 1956 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1957 | QualifiedNameType *T |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1958 | = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1959 | if (T) |
| 1960 | return QualType(T, 0); |
| 1961 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | T = new (*this) QualifiedNameType(NNS, NamedType, |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1963 | getCanonicalType(NamedType)); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1964 | Types.push_back(T); |
| 1965 | QualifiedNameTypes.InsertNode(T, InsertPos); |
| 1966 | return QualType(T, 0); |
| 1967 | } |
| 1968 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1969 | QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1970 | const IdentifierInfo *Name, |
| 1971 | QualType Canon) { |
| 1972 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1973 | |
| 1974 | if (Canon.isNull()) { |
| 1975 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 1976 | if (CanonNNS != NNS) |
| 1977 | Canon = getTypenameType(CanonNNS, Name); |
| 1978 | } |
| 1979 | |
| 1980 | llvm::FoldingSetNodeID ID; |
| 1981 | TypenameType::Profile(ID, NNS, Name); |
| 1982 | |
| 1983 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1984 | TypenameType *T |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1985 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1986 | if (T) |
| 1987 | return QualType(T, 0); |
| 1988 | |
| 1989 | T = new (*this) TypenameType(NNS, Name, Canon); |
| 1990 | Types.push_back(T); |
| 1991 | TypenameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1992 | return QualType(T, 0); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | QualType |
| 1996 | ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1997 | const TemplateSpecializationType *TemplateId, |
| 1998 | QualType Canon) { |
| 1999 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2000 | |
| 2001 | if (Canon.isNull()) { |
| 2002 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 2003 | QualType CanonType = getCanonicalType(QualType(TemplateId, 0)); |
| 2004 | if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) { |
| 2005 | const TemplateSpecializationType *CanonTemplateId |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2006 | = CanonType->getAs<TemplateSpecializationType>(); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2007 | assert(CanonTemplateId && |
| 2008 | "Canonical type must also be a template specialization type"); |
| 2009 | Canon = getTypenameType(CanonNNS, CanonTemplateId); |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | llvm::FoldingSetNodeID ID; |
| 2014 | TypenameType::Profile(ID, NNS, TemplateId); |
| 2015 | |
| 2016 | void *InsertPos = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | TypenameType *T |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2018 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2019 | if (T) |
| 2020 | return QualType(T, 0); |
| 2021 | |
| 2022 | T = new (*this) TypenameType(NNS, TemplateId, Canon); |
| 2023 | Types.push_back(T); |
| 2024 | TypenameTypes.InsertNode(T, InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | return QualType(T, 0); |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 2028 | QualType |
| 2029 | ASTContext::getElaboratedType(QualType UnderlyingType, |
| 2030 | ElaboratedType::TagKind Tag) { |
| 2031 | llvm::FoldingSetNodeID ID; |
| 2032 | ElaboratedType::Profile(ID, UnderlyingType, Tag); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | |
John McCall | 7da2431 | 2009-09-05 00:15:47 +0000 | [diff] [blame] | 2034 | void *InsertPos = 0; |
| 2035 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2036 | if (T) |
| 2037 | return QualType(T, 0); |
| 2038 | |
| 2039 | QualType Canon = getCanonicalType(UnderlyingType); |
| 2040 | |
| 2041 | T = new (*this) ElaboratedType(UnderlyingType, Tag, Canon); |
| 2042 | Types.push_back(T); |
| 2043 | ElaboratedTypes.InsertNode(T, InsertPos); |
| 2044 | return QualType(T, 0); |
| 2045 | } |
| 2046 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2047 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2048 | /// alphabetically. |
| 2049 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2050 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2051 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2054 | static bool areSortedAndUniqued(ObjCProtocolDecl **Protocols, |
| 2055 | unsigned NumProtocols) { |
| 2056 | if (NumProtocols == 0) return true; |
| 2057 | |
| 2058 | for (unsigned i = 1; i != NumProtocols; ++i) |
| 2059 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i])) |
| 2060 | return false; |
| 2061 | return true; |
| 2062 | } |
| 2063 | |
| 2064 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2065 | unsigned &NumProtocols) { |
| 2066 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2068 | // Sort protocols, keyed by name. |
| 2069 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2070 | |
| 2071 | // Remove duplicates. |
| 2072 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2073 | NumProtocols = ProtocolsEnd-Protocols; |
| 2074 | } |
| 2075 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2076 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2077 | /// the given interface decl and the conforming protocol list. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2078 | QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | ObjCProtocolDecl **Protocols, |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2080 | unsigned NumProtocols) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2081 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2082 | ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2083 | |
| 2084 | void *InsertPos = 0; |
| 2085 | if (ObjCObjectPointerType *QT = |
| 2086 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2087 | return QualType(QT, 0); |
| 2088 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2089 | // Sort the protocol list alphabetically to canonicalize it. |
| 2090 | QualType Canonical; |
| 2091 | if (!InterfaceT.isCanonical() || |
| 2092 | !areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2093 | if (!areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2094 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); |
| 2095 | unsigned UniqueCount = NumProtocols; |
| 2096 | |
| 2097 | std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); |
| 2098 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
| 2099 | |
| 2100 | Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), |
| 2101 | &Sorted[0], UniqueCount); |
| 2102 | } else { |
| 2103 | Canonical = getObjCObjectPointerType(getCanonicalType(InterfaceT), |
| 2104 | Protocols, NumProtocols); |
| 2105 | } |
| 2106 | |
| 2107 | // Regenerate InsertPos. |
| 2108 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2109 | } |
| 2110 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2111 | // No Match; |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2112 | ObjCObjectPointerType *QType = new (*this, TypeAlignment) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2113 | ObjCObjectPointerType(Canonical, InterfaceT, Protocols, NumProtocols); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2115 | Types.push_back(QType); |
| 2116 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
| 2117 | return QualType(QType, 0); |
| 2118 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2119 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2120 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 2121 | /// specified ObjC interface decl. The list of protocols is optional. |
| 2122 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2123 | ObjCProtocolDecl **Protocols, unsigned NumProtocols) { |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2124 | llvm::FoldingSetNodeID ID; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2125 | ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2127 | void *InsertPos = 0; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2128 | if (ObjCInterfaceType *QT = |
| 2129 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2130 | return QualType(QT, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2131 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2132 | // Sort the protocol list alphabetically to canonicalize it. |
| 2133 | QualType Canonical; |
| 2134 | if (NumProtocols && !areSortedAndUniqued(Protocols, NumProtocols)) { |
| 2135 | llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(NumProtocols); |
| 2136 | std::copy(Protocols, Protocols + NumProtocols, Sorted.begin()); |
| 2137 | |
| 2138 | unsigned UniqueCount = NumProtocols; |
| 2139 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
| 2140 | |
| 2141 | Canonical = getObjCInterfaceType(Decl, &Sorted[0], UniqueCount); |
| 2142 | |
| 2143 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2144 | } |
| 2145 | |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2146 | ObjCInterfaceType *QType = new (*this, TypeAlignment) |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2147 | ObjCInterfaceType(Canonical, const_cast<ObjCInterfaceDecl*>(Decl), |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2148 | Protocols, NumProtocols); |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2149 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2150 | Types.push_back(QType); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 2151 | ObjCInterfaceTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 2152 | return QualType(QType, 0); |
| 2153 | } |
| 2154 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2155 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 2156 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2157 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2159 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2160 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2161 | TypeOfExprType *toe; |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2162 | if (tofExpr->isTypeDependent()) { |
| 2163 | llvm::FoldingSetNodeID ID; |
| 2164 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2166 | void *InsertPos = 0; |
| 2167 | DependentTypeOfExprType *Canon |
| 2168 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2169 | if (Canon) { |
| 2170 | // We already have a "canonical" version of an identical, dependent |
| 2171 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2172 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2173 | QualType((TypeOfExprType*)Canon, 0)); |
| 2174 | } |
| 2175 | else { |
| 2176 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2177 | Canon |
| 2178 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | b197572 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 2179 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 2180 | toe = Canon; |
| 2181 | } |
| 2182 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2183 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2184 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2185 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2186 | Types.push_back(toe); |
| 2187 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2190 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 2191 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 2192 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2193 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2194 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2195 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2196 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2197 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 2198 | Types.push_back(tot); |
| 2199 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2202 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 2203 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
| 2204 | static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 2205 | if (e->isTypeDependent()) |
| 2206 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2207 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2208 | // If e is an id expression or a class member access, decltype(e) is defined |
| 2209 | // as the type of the entity named by e. |
| 2210 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 2211 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 2212 | return VD->getType(); |
| 2213 | } |
| 2214 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 2215 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 2216 | return FD->getType(); |
| 2217 | } |
| 2218 | // If e is a function call or an invocation of an overloaded operator, |
| 2219 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 2220 | // return type of that function. |
| 2221 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 2222 | return CE->getCallReturnType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2223 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2224 | QualType T = e->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2225 | |
| 2226 | // 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] | 2227 | // defined as T&, otherwise decltype(e) is defined as T. |
| 2228 | if (e->isLvalue(Context) == Expr::LV_Valid) |
| 2229 | T = Context.getLValueReferenceType(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2230 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 2231 | return T; |
| 2232 | } |
| 2233 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2234 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 2235 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 2236 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2237 | /// an issue. This doesn't effect the type checker, since it operates |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2238 | /// on canonical type's (which are always unique). |
| 2239 | QualType ASTContext::getDecltypeType(Expr *e) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2240 | DecltypeType *dt; |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2241 | if (e->isTypeDependent()) { |
| 2242 | llvm::FoldingSetNodeID ID; |
| 2243 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2244 | |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2245 | void *InsertPos = 0; |
| 2246 | DependentDecltypeType *Canon |
| 2247 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2248 | if (Canon) { |
| 2249 | // We already have a "canonical" version of an equivalent, dependent |
| 2250 | // decltype type. Use that as our canonical type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2251 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2252 | QualType((DecltypeType*)Canon, 0)); |
| 2253 | } |
| 2254 | else { |
| 2255 | // Build a new, canonical typeof(expr) type. |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2256 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | 9d702ae | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 2257 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 2258 | dt = Canon; |
| 2259 | } |
| 2260 | } else { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2261 | QualType T = getDecltypeForExpr(e, *this); |
John McCall | 6b304a0 | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2262 | dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 2263 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 2264 | Types.push_back(dt); |
| 2265 | return QualType(dt, 0); |
| 2266 | } |
| 2267 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2268 | /// getTagDeclType - Return the unique reference to the type for the |
| 2269 | /// specified TagDecl (struct/union/class/enum) decl. |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2270 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 2271 | assert (Decl); |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 2272 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 2273 | // away const? mutable? |
| 2274 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2277 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 2278 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 2279 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | a3ccda5 | 2009-12-12 00:26:23 +0000 | [diff] [blame^] | 2280 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2281 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 2284 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 2285 | /// Used when in C++, as a GCC extension. |
| 2286 | QualType ASTContext::getSignedWCharType() const { |
| 2287 | // FIXME: derive from "Target" ? |
| 2288 | return WCharTy; |
| 2289 | } |
| 2290 | |
| 2291 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 2292 | /// Used when in C++, as a GCC extension. |
| 2293 | QualType ASTContext::getUnsignedWCharType() const { |
| 2294 | // FIXME: derive from "Target" ? |
| 2295 | return UnsignedIntTy; |
| 2296 | } |
| 2297 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2298 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 2299 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 2300 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2301 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2304 | //===----------------------------------------------------------------------===// |
| 2305 | // Type Operators |
| 2306 | //===----------------------------------------------------------------------===// |
| 2307 | |
John McCall | 54e14c4 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2308 | CanQualType ASTContext::getCanonicalParamType(QualType T) { |
| 2309 | // Push qualifiers into arrays, and then discard any remaining |
| 2310 | // qualifiers. |
| 2311 | T = getCanonicalType(T); |
| 2312 | const Type *Ty = T.getTypePtr(); |
| 2313 | |
| 2314 | QualType Result; |
| 2315 | if (isa<ArrayType>(Ty)) { |
| 2316 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 2317 | } else if (isa<FunctionType>(Ty)) { |
| 2318 | Result = getPointerType(QualType(Ty, 0)); |
| 2319 | } else { |
| 2320 | Result = QualType(Ty, 0); |
| 2321 | } |
| 2322 | |
| 2323 | return CanQualType::CreateUnsafe(Result); |
| 2324 | } |
| 2325 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2326 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 2327 | /// the specified potentially non-canonical type. The non-canonical version |
| 2328 | /// of a type may have many "decorated" versions of types. Decorators can |
| 2329 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 2330 | /// to be free of any of these, allowing two canonical types to be compared |
| 2331 | /// for exact equality with a simple pointer comparison. |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2332 | CanQualType ASTContext::getCanonicalType(QualType T) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2333 | QualifierCollector Quals; |
| 2334 | const Type *Ptr = Quals.strip(T); |
| 2335 | QualType CanType = Ptr->getCanonicalTypeInternal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2336 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2337 | // The canonical internal type will be the canonical type *except* |
| 2338 | // that we push type qualifiers down through array types. |
| 2339 | |
| 2340 | // If there are no new qualifiers to push down, stop here. |
| 2341 | if (!Quals.hasQualifiers()) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2342 | return CanQualType::CreateUnsafe(CanType); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2343 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2344 | // If the type qualifiers are on an array type, get the canonical |
| 2345 | // type of the array with the qualifiers applied to the element |
| 2346 | // type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2347 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 2348 | if (!AT) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2349 | return CanQualType::CreateUnsafe(getQualifiedType(CanType, Quals)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2350 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2351 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2352 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2353 | QualType NewEltTy = getQualifiedType(AT->getElementType(), Quals); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2354 | NewEltTy = getCanonicalType(NewEltTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2355 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2356 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2357 | return CanQualType::CreateUnsafe( |
| 2358 | getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2359 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2360 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2361 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2362 | return CanQualType::CreateUnsafe( |
| 2363 | getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2364 | IAT->getIndexTypeCVRQualifiers())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2365 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2366 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2367 | return CanQualType::CreateUnsafe( |
| 2368 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2369 | DSAT->getSizeExpr() ? |
| 2370 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2371 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2372 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 87a924e | 2009-10-30 22:56:57 +0000 | [diff] [blame] | 2373 | DSAT->getBracketsRange())->getCanonicalTypeInternal()); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2374 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2375 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2376 | return CanQualType::CreateUnsafe(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2377 | VAT->getSizeExpr() ? |
| 2378 | VAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2379 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2380 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 2381 | VAT->getBracketsRange())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2384 | DeclarationName ASTContext::getNameForTemplate(TemplateName Name) { |
| 2385 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) |
| 2386 | return TD->getDeclName(); |
| 2387 | |
| 2388 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
| 2389 | if (DTN->isIdentifier()) { |
| 2390 | return DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 2391 | } else { |
| 2392 | return DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2393 | } |
| 2394 | } |
| 2395 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2396 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 2397 | assert(Storage); |
| 2398 | return (*Storage->begin())->getDeclName(); |
John McCall | 80ad16f | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2401 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
| 2402 | // If this template name refers to a template, the canonical |
| 2403 | // template name merely stores the template itself. |
| 2404 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2405 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2406 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 2407 | assert(!Name.getAsOverloadedTemplate()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2408 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2409 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2410 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2411 | return DTN->CanonicalTemplateName; |
| 2412 | } |
| 2413 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2414 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 2415 | X = getCanonicalTemplateName(X); |
| 2416 | Y = getCanonicalTemplateName(Y); |
| 2417 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 2418 | } |
| 2419 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2420 | TemplateArgument |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2421 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) { |
| 2422 | switch (Arg.getKind()) { |
| 2423 | case TemplateArgument::Null: |
| 2424 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2425 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2426 | case TemplateArgument::Expression: |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2427 | return Arg; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2428 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2429 | case TemplateArgument::Declaration: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2430 | return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2431 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2432 | case TemplateArgument::Template: |
| 2433 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
| 2434 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2435 | case TemplateArgument::Integral: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2436 | return TemplateArgument(*Arg.getAsIntegral(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2437 | getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2438 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2439 | case TemplateArgument::Type: |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2440 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2441 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2442 | case TemplateArgument::Pack: { |
| 2443 | // FIXME: Allocate in ASTContext |
| 2444 | TemplateArgument *CanonArgs = new TemplateArgument[Arg.pack_size()]; |
| 2445 | unsigned Idx = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2446 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2447 | AEnd = Arg.pack_end(); |
| 2448 | A != AEnd; (void)++A, ++Idx) |
| 2449 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2450 | |
Douglas Gregor | 1275ae0 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2451 | TemplateArgument Result; |
| 2452 | Result.setArgumentPack(CanonArgs, Arg.pack_size(), false); |
| 2453 | return Result; |
| 2454 | } |
| 2455 | } |
| 2456 | |
| 2457 | // Silence GCC warning |
| 2458 | assert(false && "Unhandled template argument kind"); |
| 2459 | return TemplateArgument(); |
| 2460 | } |
| 2461 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2462 | NestedNameSpecifier * |
| 2463 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2464 | if (!NNS) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2465 | return 0; |
| 2466 | |
| 2467 | switch (NNS->getKind()) { |
| 2468 | case NestedNameSpecifier::Identifier: |
| 2469 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2470 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2471 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2472 | NNS->getAsIdentifier()); |
| 2473 | |
| 2474 | case NestedNameSpecifier::Namespace: |
| 2475 | // A namespace is canonical; build a nested-name-specifier with |
| 2476 | // this namespace and no prefix. |
| 2477 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2478 | |
| 2479 | case NestedNameSpecifier::TypeSpec: |
| 2480 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2481 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2482 | return NestedNameSpecifier::Create(*this, 0, |
| 2483 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2484 | T.getTypePtr()); |
| 2485 | } |
| 2486 | |
| 2487 | case NestedNameSpecifier::Global: |
| 2488 | // The global specifier is canonical and unique. |
| 2489 | return NNS; |
| 2490 | } |
| 2491 | |
| 2492 | // Required to silence a GCC warning |
| 2493 | return 0; |
| 2494 | } |
| 2495 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2496 | |
| 2497 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 2498 | // Handle the non-qualified case efficiently. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 2499 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2500 | // Handle the common positive case fast. |
| 2501 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2502 | return AT; |
| 2503 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2504 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2505 | // Handle the common negative case fast. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2506 | QualType CType = T->getCanonicalTypeInternal(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2507 | if (!isa<ArrayType>(CType)) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2508 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2509 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2510 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2511 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2512 | // 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] | 2513 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2514 | // If we get here, we either have type qualifiers on the type, or we have |
| 2515 | // 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] | 2516 | // we must propagate them down into the element type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2517 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2518 | QualifierCollector Qs; |
| 2519 | const Type *Ty = Qs.strip(T.getDesugaredType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2520 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2521 | // If we have a simple case, just return now. |
| 2522 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2523 | if (ATy == 0 || Qs.empty()) |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2524 | return ATy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2525 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2526 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 2527 | // qualifiers into the array element type and return a new array type. |
| 2528 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2529 | // This can recursively sink qualifiers through multiple levels of arrays. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2530 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), Qs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2531 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2532 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 2533 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2534 | CAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2535 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2536 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 2537 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 2538 | IAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2539 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2540 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2541 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2542 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 2543 | return cast<ArrayType>( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2544 | getDependentSizedArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2545 | DSAT->getSizeExpr() ? |
| 2546 | DSAT->getSizeExpr()->Retain() : 0, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2547 | DSAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2548 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2549 | DSAT->getBracketsRange())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2550 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2551 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2552 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
Eli Friedman | bbed6b9 | 2009-08-15 02:50:32 +0000 | [diff] [blame] | 2553 | VAT->getSizeExpr() ? |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2554 | VAT->getSizeExpr()->Retain() : 0, |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2555 | VAT->getSizeModifier(), |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2556 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2557 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2561 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 2562 | /// specified array type to a pointer. This operation is non-trivial when |
| 2563 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 2564 | /// this returns a pointer to a properly qualified element of the array. |
| 2565 | /// |
| 2566 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 2567 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2568 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 2569 | // typedefs in the element type of the array. This also handles propagation |
| 2570 | // of type qualifiers from the array type into the element type if present |
| 2571 | // (C99 6.7.3p8). |
| 2572 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 2573 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2574 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2575 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2576 | |
| 2577 | // int x[restrict 4] -> int *restrict |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2578 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2581 | QualType ASTContext::getBaseElementType(QualType QT) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2582 | QualifierCollector Qs; |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2583 | while (true) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2584 | const Type *UT = Qs.strip(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2585 | if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) { |
| 2586 | QT = AT->getElementType(); |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 2587 | } else { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2588 | return Qs.apply(QT); |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2589 | } |
| 2590 | } |
| 2591 | } |
| 2592 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2593 | QualType ASTContext::getBaseElementType(const ArrayType *AT) { |
| 2594 | QualType ElemTy = AT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 | |
Anders Carlsson | fbbce49 | 2009-09-25 01:23:32 +0000 | [diff] [blame] | 2596 | if (const ArrayType *AT = getAsArrayType(ElemTy)) |
| 2597 | return getBaseElementType(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2598 | |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 2599 | return ElemTy; |
| 2600 | } |
| 2601 | |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2602 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2603 | uint64_t |
Fariborz Jahanian | 0de7899 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 2604 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 2605 | uint64_t ElementCount = 1; |
| 2606 | do { |
| 2607 | ElementCount *= CA->getSize().getZExtValue(); |
| 2608 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 2609 | } while (CA); |
| 2610 | return ElementCount; |
| 2611 | } |
| 2612 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2613 | /// getFloatingRank - Return a relative rank for floating point types. |
| 2614 | /// 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] | 2615 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2616 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2617 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2618 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2619 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 2620 | switch (T->getAs<BuiltinType>()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2621 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2622 | case BuiltinType::Float: return FloatRank; |
| 2623 | case BuiltinType::Double: return DoubleRank; |
| 2624 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 2625 | } |
| 2626 | } |
| 2627 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2628 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 2629 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2630 | /// 'typeDomain' is a real floating point or complex type. |
| 2631 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2632 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 2633 | QualType Domain) const { |
| 2634 | FloatingRank EltRank = getFloatingRank(Size); |
| 2635 | if (Domain->isComplexType()) { |
| 2636 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2637 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2638 | case FloatRank: return FloatComplexTy; |
| 2639 | case DoubleRank: return DoubleComplexTy; |
| 2640 | case LongDoubleRank: return LongDoubleComplexTy; |
| 2641 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2642 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2643 | |
| 2644 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 2645 | switch (EltRank) { |
| 2646 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 2647 | case FloatRank: return FloatTy; |
| 2648 | case DoubleRank: return DoubleTy; |
| 2649 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2650 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2653 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 2654 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 2655 | /// '_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] | 2656 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2657 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 2658 | FloatingRank LHSR = getFloatingRank(LHS); |
| 2659 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2661 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2662 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2663 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2664 | return 1; |
| 2665 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2668 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 2669 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 2670 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2671 | unsigned ASTContext::getIntegerRank(Type *T) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2672 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2673 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2674 | T = ET->getDecl()->getPromotionType().getTypePtr(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2675 | |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 2676 | if (T->isSpecificBuiltinType(BuiltinType::WChar)) |
| 2677 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 2678 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2679 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 2680 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 2681 | |
| 2682 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 2683 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 2684 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2685 | // There are two things which impact the integer rank: the width, and |
| 2686 | // the ordering of builtins. The builtin ordering is encoded in the |
| 2687 | // bottom three bits; the width is encoded in the bits above that. |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 2688 | if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2689 | return FWIT->getWidth() << 3; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2690 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2691 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2692 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 2693 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2694 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2695 | case BuiltinType::Char_S: |
| 2696 | case BuiltinType::Char_U: |
| 2697 | case BuiltinType::SChar: |
| 2698 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2699 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2700 | case BuiltinType::Short: |
| 2701 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2702 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2703 | case BuiltinType::Int: |
| 2704 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2705 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2706 | case BuiltinType::Long: |
| 2707 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2708 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2709 | case BuiltinType::LongLong: |
| 2710 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2711 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2712 | case BuiltinType::Int128: |
| 2713 | case BuiltinType::UInt128: |
| 2714 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2715 | } |
| 2716 | } |
| 2717 | |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 2718 | /// \brief Whether this is a promotable bitfield reference according |
| 2719 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 2720 | /// |
| 2721 | /// \returns the type this bit-field will promote to, or NULL if no |
| 2722 | /// promotion occurs. |
| 2723 | QualType ASTContext::isPromotableBitField(Expr *E) { |
| 2724 | FieldDecl *Field = E->getBitField(); |
| 2725 | if (!Field) |
| 2726 | return QualType(); |
| 2727 | |
| 2728 | QualType FT = Field->getType(); |
| 2729 | |
| 2730 | llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this); |
| 2731 | uint64_t BitWidth = BitWidthAP.getZExtValue(); |
| 2732 | uint64_t IntSize = getTypeSize(IntTy); |
| 2733 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 2734 | // to the size of int, it gets promoted no matter what its type is. |
| 2735 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 2736 | if (BitWidth < IntSize) |
| 2737 | return IntTy; |
| 2738 | |
| 2739 | if (BitWidth == IntSize) |
| 2740 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 2741 | |
| 2742 | // Types bigger than int are not subject to promotions, and therefore act |
| 2743 | // like the base type. |
| 2744 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 2745 | // is ridiculous. |
| 2746 | return QualType(); |
| 2747 | } |
| 2748 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2749 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 2750 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 2751 | /// integer type. |
| 2752 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) { |
| 2753 | assert(!Promotable.isNull()); |
| 2754 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 2755 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 2756 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 2757 | if (Promotable->isSignedIntegerType()) |
| 2758 | return IntTy; |
| 2759 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 2760 | uint64_t IntSize = getTypeSize(IntTy); |
| 2761 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 2762 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 2763 | } |
| 2764 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2765 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2766 | /// 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] | 2767 | /// LHS < RHS, return -1. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2768 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2769 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 2770 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2771 | if (LHSC == RHSC) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2772 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2773 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 2774 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2775 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2776 | unsigned LHSRank = getIntegerRank(LHSC); |
| 2777 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2778 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2779 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 2780 | if (LHSRank == RHSRank) return 0; |
| 2781 | return LHSRank > RHSRank ? 1 : -1; |
| 2782 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2783 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2784 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 2785 | if (LHSUnsigned) { |
| 2786 | // If the unsigned [LHS] type is larger, return it. |
| 2787 | if (LHSRank >= RHSRank) |
| 2788 | return 1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2789 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2790 | // If the signed type can represent all values of the unsigned type, it |
| 2791 | // 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] | 2792 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2793 | return -1; |
| 2794 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2795 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2796 | // If the unsigned [RHS] type is larger, return it. |
| 2797 | if (RHSRank >= LHSRank) |
| 2798 | return -1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2799 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2800 | // If the signed type can represent all values of the unsigned type, it |
| 2801 | // 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] | 2802 | // powers of two larger than each other, this is always safe. |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2803 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2804 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2805 | |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2806 | static RecordDecl * |
| 2807 | CreateRecordDecl(ASTContext &Ctx, RecordDecl::TagKind TK, DeclContext *DC, |
| 2808 | SourceLocation L, IdentifierInfo *Id) { |
| 2809 | if (Ctx.getLangOptions().CPlusPlus) |
| 2810 | return CXXRecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2811 | else |
| 2812 | return RecordDecl::Create(Ctx, TK, DC, L, Id); |
| 2813 | } |
| 2814 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2815 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2816 | QualType ASTContext::getCFConstantStringType() { |
| 2817 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | CFConstantStringTypeDecl = |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2819 | CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2820 | &Idents.get("NSConstantString")); |
| 2821 | |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2822 | QualType FieldTypes[4]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2823 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2824 | // const int *isa; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2825 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2826 | // int flags; |
| 2827 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2828 | // const char *str; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2829 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2830 | // long length; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2831 | FieldTypes[3] = LongTy; |
| 2832 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2833 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2834 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2835 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2836 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2837 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2838 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2839 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2840 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | CFConstantStringTypeDecl->completeDefinition(*this); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2845 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2846 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 2847 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2848 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2849 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2850 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2851 | assert(Rec && "Invalid CFConstantStringType"); |
| 2852 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 2853 | } |
| 2854 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | QualType ASTContext::getObjCFastEnumerationStateType() { |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2856 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2857 | ObjCFastEnumerationStateTypeDecl = |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2858 | CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2859 | &Idents.get("__objcFastEnumerationState")); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2860 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2861 | QualType FieldTypes[] = { |
| 2862 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2863 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2864 | getPointerType(UnsignedLongTy), |
| 2865 | getConstantArrayType(UnsignedLongTy, |
| 2866 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 2867 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2868 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2869 | for (size_t i = 0; i < 4; ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2870 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2871 | ObjCFastEnumerationStateTypeDecl, |
| 2872 | SourceLocation(), 0, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2873 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2874 | /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2875 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2876 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2877 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2878 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2879 | ObjCFastEnumerationStateTypeDecl->completeDefinition(*this); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2880 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2881 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2882 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 2883 | } |
| 2884 | |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2885 | QualType ASTContext::getBlockDescriptorType() { |
| 2886 | if (BlockDescriptorType) |
| 2887 | return getTagDeclType(BlockDescriptorType); |
| 2888 | |
| 2889 | RecordDecl *T; |
| 2890 | // FIXME: Needs the FlagAppleBlock bit. |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2891 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2892 | &Idents.get("__block_descriptor")); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2893 | |
| 2894 | QualType FieldTypes[] = { |
| 2895 | UnsignedLongTy, |
| 2896 | UnsignedLongTy, |
| 2897 | }; |
| 2898 | |
| 2899 | const char *FieldNames[] = { |
| 2900 | "reserved", |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2901 | "Size" |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2902 | }; |
| 2903 | |
| 2904 | for (size_t i = 0; i < 2; ++i) { |
| 2905 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2906 | T, |
| 2907 | SourceLocation(), |
| 2908 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2909 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 2910 | /*BitWidth=*/0, |
| 2911 | /*Mutable=*/false); |
| 2912 | T->addDecl(Field); |
| 2913 | } |
| 2914 | |
| 2915 | T->completeDefinition(*this); |
| 2916 | |
| 2917 | BlockDescriptorType = T; |
| 2918 | |
| 2919 | return getTagDeclType(BlockDescriptorType); |
| 2920 | } |
| 2921 | |
| 2922 | void ASTContext::setBlockDescriptorType(QualType T) { |
| 2923 | const RecordType *Rec = T->getAs<RecordType>(); |
| 2924 | assert(Rec && "Invalid BlockDescriptorType"); |
| 2925 | BlockDescriptorType = Rec->getDecl(); |
| 2926 | } |
| 2927 | |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2928 | QualType ASTContext::getBlockDescriptorExtendedType() { |
| 2929 | if (BlockDescriptorExtendedType) |
| 2930 | return getTagDeclType(BlockDescriptorExtendedType); |
| 2931 | |
| 2932 | RecordDecl *T; |
| 2933 | // FIXME: Needs the FlagAppleBlock bit. |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 2934 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2935 | &Idents.get("__block_descriptor_withcopydispose")); |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2936 | |
| 2937 | QualType FieldTypes[] = { |
| 2938 | UnsignedLongTy, |
| 2939 | UnsignedLongTy, |
| 2940 | getPointerType(VoidPtrTy), |
| 2941 | getPointerType(VoidPtrTy) |
| 2942 | }; |
| 2943 | |
| 2944 | const char *FieldNames[] = { |
| 2945 | "reserved", |
| 2946 | "Size", |
| 2947 | "CopyFuncPtr", |
| 2948 | "DestroyFuncPtr" |
| 2949 | }; |
| 2950 | |
| 2951 | for (size_t i = 0; i < 4; ++i) { |
| 2952 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2953 | T, |
| 2954 | SourceLocation(), |
| 2955 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2956 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 2957 | /*BitWidth=*/0, |
| 2958 | /*Mutable=*/false); |
| 2959 | T->addDecl(Field); |
| 2960 | } |
| 2961 | |
| 2962 | T->completeDefinition(*this); |
| 2963 | |
| 2964 | BlockDescriptorExtendedType = T; |
| 2965 | |
| 2966 | return getTagDeclType(BlockDescriptorExtendedType); |
| 2967 | } |
| 2968 | |
| 2969 | void ASTContext::setBlockDescriptorExtendedType(QualType T) { |
| 2970 | const RecordType *Rec = T->getAs<RecordType>(); |
| 2971 | assert(Rec && "Invalid BlockDescriptorType"); |
| 2972 | BlockDescriptorExtendedType = Rec->getDecl(); |
| 2973 | } |
| 2974 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2975 | bool ASTContext::BlockRequiresCopying(QualType Ty) { |
| 2976 | if (Ty->isBlockPointerType()) |
| 2977 | return true; |
| 2978 | if (isObjCNSObjectType(Ty)) |
| 2979 | return true; |
| 2980 | if (Ty->isObjCObjectPointerType()) |
| 2981 | return true; |
| 2982 | return false; |
| 2983 | } |
| 2984 | |
| 2985 | QualType ASTContext::BuildByRefType(const char *DeclName, QualType Ty) { |
| 2986 | // type = struct __Block_byref_1_X { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2987 | // void *__isa; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2988 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2989 | // unsigned int __flags; |
| 2990 | // unsigned int __size; |
Mike Stump | 38e1627 | 2009-10-21 22:01:24 +0000 | [diff] [blame] | 2991 | // void *__copy_helper; // as needed |
| 2992 | // void *__destroy_help // as needed |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2993 | // int X; |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 2994 | // } * |
| 2995 | |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 2996 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 2997 | |
| 2998 | // FIXME: Move up |
Fariborz Jahanian | 4d0d85c | 2009-10-24 00:16:42 +0000 | [diff] [blame] | 2999 | static unsigned int UniqueBlockByRefTypeID = 0; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3000 | llvm::SmallString<36> Name; |
| 3001 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 3002 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3003 | RecordDecl *T; |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3004 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 3005 | &Idents.get(Name.str())); |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3006 | T->startDefinition(); |
| 3007 | QualType Int32Ty = IntTy; |
| 3008 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 3009 | QualType FieldTypes[] = { |
| 3010 | getPointerType(VoidPtrTy), |
| 3011 | getPointerType(getTagDeclType(T)), |
| 3012 | Int32Ty, |
| 3013 | Int32Ty, |
| 3014 | getPointerType(VoidPtrTy), |
| 3015 | getPointerType(VoidPtrTy), |
| 3016 | Ty |
| 3017 | }; |
| 3018 | |
| 3019 | const char *FieldNames[] = { |
| 3020 | "__isa", |
| 3021 | "__forwarding", |
| 3022 | "__flags", |
| 3023 | "__size", |
| 3024 | "__copy_helper", |
| 3025 | "__destroy_helper", |
| 3026 | DeclName, |
| 3027 | }; |
| 3028 | |
| 3029 | for (size_t i = 0; i < 7; ++i) { |
| 3030 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 3031 | continue; |
| 3032 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
| 3033 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3034 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3035 | /*BitWidth=*/0, /*Mutable=*/false); |
| 3036 | T->addDecl(Field); |
| 3037 | } |
| 3038 | |
| 3039 | T->completeDefinition(*this); |
| 3040 | |
| 3041 | return getPointerType(getTagDeclType(T)); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | |
| 3045 | QualType ASTContext::getBlockParmType( |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3046 | bool BlockHasCopyDispose, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3047 | llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) { |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3048 | // FIXME: Move up |
Fariborz Jahanian | 4d0d85c | 2009-10-24 00:16:42 +0000 | [diff] [blame] | 3049 | static unsigned int UniqueBlockParmTypeID = 0; |
Benjamin Kramer | f5942a4 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 3050 | llvm::SmallString<36> Name; |
| 3051 | llvm::raw_svector_ostream(Name) << "__block_literal_" |
| 3052 | << ++UniqueBlockParmTypeID; |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3053 | RecordDecl *T; |
Anders Carlsson | 79cbc7d | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3054 | T = CreateRecordDecl(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 3055 | &Idents.get(Name.str())); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3056 | QualType FieldTypes[] = { |
| 3057 | getPointerType(VoidPtrTy), |
| 3058 | IntTy, |
| 3059 | IntTy, |
| 3060 | getPointerType(VoidPtrTy), |
Mike Stump | 083c25e | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3061 | (BlockHasCopyDispose ? |
| 3062 | getPointerType(getBlockDescriptorExtendedType()) : |
| 3063 | getPointerType(getBlockDescriptorType())) |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3064 | }; |
| 3065 | |
| 3066 | const char *FieldNames[] = { |
| 3067 | "__isa", |
| 3068 | "__flags", |
| 3069 | "__reserved", |
| 3070 | "__FuncPtr", |
| 3071 | "__descriptor" |
| 3072 | }; |
| 3073 | |
| 3074 | for (size_t i = 0; i < 5; ++i) { |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3075 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3076 | &Idents.get(FieldNames[i]), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3077 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3078 | /*BitWidth=*/0, /*Mutable=*/false); |
| 3079 | T->addDecl(Field); |
| 3080 | } |
| 3081 | |
| 3082 | for (size_t i = 0; i < BlockDeclRefDecls.size(); ++i) { |
| 3083 | const Expr *E = BlockDeclRefDecls[i]; |
| 3084 | const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E); |
| 3085 | clang::IdentifierInfo *Name = 0; |
| 3086 | if (BDRE) { |
| 3087 | const ValueDecl *D = BDRE->getDecl(); |
| 3088 | Name = &Idents.get(D->getName()); |
| 3089 | } |
| 3090 | QualType FieldType = E->getType(); |
| 3091 | |
| 3092 | if (BDRE && BDRE->isByRef()) |
Mike Stump | af7b44d | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 3093 | FieldType = BuildByRefType(BDRE->getDecl()->getNameAsCString(), |
| 3094 | FieldType); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3095 | |
| 3096 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3097 | Name, FieldType, /*TInfo=*/0, |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3098 | /*BitWidth=*/0, /*Mutable=*/false); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3099 | T->addDecl(Field); |
| 3100 | } |
| 3101 | |
| 3102 | T->completeDefinition(*this); |
Mike Stump | ea26cb5 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 3103 | |
| 3104 | return getPointerType(getTagDeclType(T)); |
Mike Stump | adaaad3 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3107 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3108 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3109 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 3110 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 3111 | } |
| 3112 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3113 | // This returns true if a type has been typedefed to BOOL: |
| 3114 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 3115 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3116 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 3117 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 3118 | return II->isStr("BOOL"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3119 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3120 | return false; |
| 3121 | } |
| 3122 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3123 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3124 | /// purpose. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3125 | int ASTContext::getObjCEncodingTypeSize(QualType type) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3126 | uint64_t sz = getTypeSize(type); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3127 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3128 | // Make all integer and enum types at least as large as an int |
| 3129 | if (sz > 0 && type->isIntegralType()) |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3130 | sz = std::max(sz, getTypeSize(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3131 | // Treat arrays as pointers, since that's how they're passed in. |
| 3132 | else if (type->isArrayType()) |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3133 | sz = getTypeSize(VoidPtrTy); |
| 3134 | return sz / getTypeSize(CharTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3135 | } |
| 3136 | |
David Chisnall | 5e530af | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 3137 | /// getObjCEncodingForBlockDecl - Return the encoded type for this method |
| 3138 | /// declaration. |
| 3139 | void ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr, |
| 3140 | std::string& S) { |
| 3141 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 3142 | QualType BlockTy = |
| 3143 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 3144 | // Encode result type. |
| 3145 | getObjCEncodingForType(cast<FunctionType>(BlockTy)->getResultType(), S); |
| 3146 | // Compute size of all parameters. |
| 3147 | // Start with computing size of a pointer in number of bytes. |
| 3148 | // FIXME: There might(should) be a better way of doing this computation! |
| 3149 | SourceLocation Loc; |
| 3150 | int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); |
| 3151 | int ParmOffset = PtrSize; |
| 3152 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3153 | E = Decl->param_end(); PI != E; ++PI) { |
| 3154 | QualType PType = (*PI)->getType(); |
| 3155 | int sz = getObjCEncodingTypeSize(PType); |
| 3156 | assert (sz > 0 && "BlockExpr - Incomplete param type"); |
| 3157 | ParmOffset += sz; |
| 3158 | } |
| 3159 | // Size of the argument frame |
| 3160 | S += llvm::utostr(ParmOffset); |
| 3161 | // Block pointer and offset. |
| 3162 | S += "@?0"; |
| 3163 | ParmOffset = PtrSize; |
| 3164 | |
| 3165 | // Argument types. |
| 3166 | ParmOffset = PtrSize; |
| 3167 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 3168 | Decl->param_end(); PI != E; ++PI) { |
| 3169 | ParmVarDecl *PVDecl = *PI; |
| 3170 | QualType PType = PVDecl->getOriginalType(); |
| 3171 | if (const ArrayType *AT = |
| 3172 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3173 | // Use array's original type only if it has known number of |
| 3174 | // elements. |
| 3175 | if (!isa<ConstantArrayType>(AT)) |
| 3176 | PType = PVDecl->getType(); |
| 3177 | } else if (PType->isFunctionType()) |
| 3178 | PType = PVDecl->getType(); |
| 3179 | getObjCEncodingForType(PType, S); |
| 3180 | S += llvm::utostr(ParmOffset); |
| 3181 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 3182 | } |
| 3183 | } |
| 3184 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3185 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3186 | /// declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3187 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3188 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3189 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3190 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3191 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3192 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3193 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3194 | // Compute size of all parameters. |
| 3195 | // Start with computing size of a pointer in number of bytes. |
| 3196 | // FIXME: There might(should) be a better way of doing this computation! |
| 3197 | SourceLocation Loc; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3198 | int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3199 | // The first two arguments (self and _cmd) are pointers; account for |
| 3200 | // their size. |
| 3201 | int ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3202 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3203 | E = Decl->param_end(); PI != E; ++PI) { |
| 3204 | QualType PType = (*PI)->getType(); |
| 3205 | int sz = getObjCEncodingTypeSize(PType); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3206 | assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3207 | ParmOffset += sz; |
| 3208 | } |
| 3209 | S += llvm::utostr(ParmOffset); |
| 3210 | S += "@0:"; |
| 3211 | S += llvm::utostr(PtrSize); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3212 | |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3213 | // Argument types. |
| 3214 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 3215 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 3216 | E = Decl->param_end(); PI != E; ++PI) { |
| 3217 | ParmVarDecl *PVDecl = *PI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3218 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3219 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3220 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 3221 | // Use array's original type only if it has known number of |
| 3222 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 3223 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 3224 | PType = PVDecl->getType(); |
| 3225 | } else if (PType->isFunctionType()) |
| 3226 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3227 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3228 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 3229 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3230 | getObjCEncodingForType(PType, S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3231 | S += llvm::utostr(ParmOffset); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3232 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 3233 | } |
| 3234 | } |
| 3235 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3236 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3237 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3238 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 3239 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3240 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 3241 | /// attributes readonly and bycopy are encoded as single characters. The |
| 3242 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 3243 | /// encoded as single characters, followed by an identifier. Property types |
| 3244 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 3245 | /// these attributes are defined by the following enumeration: |
| 3246 | /// @code |
| 3247 | /// enum PropertyAttributes { |
| 3248 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 3249 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 3250 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 3251 | /// kPropertyDynamic = 'D', // property is dynamic |
| 3252 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 3253 | /// kPropertySetter = 'S', // followed by setter selector name |
| 3254 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 3255 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 3256 | /// kPropertyWeak = 'W' // 'weak' property |
| 3257 | /// kPropertyStrong = 'P' // property GC'able |
| 3258 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 3259 | /// }; |
| 3260 | /// @endcode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3261 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3262 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 3263 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3264 | // Collect information from the property implementation decl(s). |
| 3265 | bool Dynamic = false; |
| 3266 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 3267 | |
| 3268 | // FIXME: Duplicated code due to poor abstraction. |
| 3269 | if (Container) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3270 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3271 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 3272 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3273 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3274 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3275 | ObjCPropertyImplDecl *PID = *i; |
| 3276 | if (PID->getPropertyDecl() == PD) { |
| 3277 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3278 | Dynamic = true; |
| 3279 | } else { |
| 3280 | SynthesizePID = PID; |
| 3281 | } |
| 3282 | } |
| 3283 | } |
| 3284 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3285 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3286 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3287 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 3288 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3289 | ObjCPropertyImplDecl *PID = *i; |
| 3290 | if (PID->getPropertyDecl() == PD) { |
| 3291 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 3292 | Dynamic = true; |
| 3293 | } else { |
| 3294 | SynthesizePID = PID; |
| 3295 | } |
| 3296 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3297 | } |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | // FIXME: This is not very efficient. |
| 3302 | S = "T"; |
| 3303 | |
| 3304 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3305 | // GCC has some special rules regarding encoding of properties which |
| 3306 | // closely resembles encoding of ivars. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3307 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3308 | true /* outermost type */, |
| 3309 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3310 | |
| 3311 | if (PD->isReadOnly()) { |
| 3312 | S += ",R"; |
| 3313 | } else { |
| 3314 | switch (PD->getSetterKind()) { |
| 3315 | case ObjCPropertyDecl::Assign: break; |
| 3316 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3317 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3318 | } |
| 3319 | } |
| 3320 | |
| 3321 | // It really isn't clear at all what this means, since properties |
| 3322 | // are "dynamic by default". |
| 3323 | if (Dynamic) |
| 3324 | S += ",D"; |
| 3325 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3326 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 3327 | S += ",N"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3328 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3329 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 3330 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3331 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 3335 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3336 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | if (SynthesizePID) { |
| 3340 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 3341 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 3342 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | // FIXME: OBJCGC: weak & strong |
| 3346 | } |
| 3347 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3348 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3349 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 3350 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3351 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 3352 | /// |
| 3353 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3354 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3355 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3356 | if (BT->getKind() == BuiltinType::ULong && |
| 3357 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3358 | PointeeTy = UnsignedIntTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3359 | else |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 3360 | if (BT->getKind() == BuiltinType::Long && |
| 3361 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3362 | PointeeTy = IntTy; |
| 3363 | } |
| 3364 | } |
| 3365 | } |
| 3366 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3367 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3368 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3369 | // We follow the behavior of gcc, expanding structures which are |
| 3370 | // directly pointed to, and expanding embedded structures. Note that |
| 3371 | // these rules are sufficient to prevent recursive encoding of the |
| 3372 | // same type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3373 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 3374 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3377 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3378 | const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3379 | const Expr *E = FD->getBitWidth(); |
| 3380 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 3381 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3382 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3383 | S += 'b'; |
| 3384 | S += llvm::utostr(N); |
| 3385 | } |
| 3386 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3387 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3388 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 3389 | bool ExpandPointedToStructures, |
| 3390 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 3391 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 3392 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3393 | bool EncodingProperty) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3394 | if (const BuiltinType *BT = T->getAs<BuiltinType>()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3395 | if (FD && FD->isBitField()) |
| 3396 | return EncodeBitField(this, S, FD); |
| 3397 | char encoding; |
| 3398 | switch (BT->getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3399 | default: assert(0 && "Unhandled builtin type kind"); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3400 | case BuiltinType::Void: encoding = 'v'; break; |
| 3401 | case BuiltinType::Bool: encoding = 'B'; break; |
| 3402 | case BuiltinType::Char_U: |
| 3403 | case BuiltinType::UChar: encoding = 'C'; break; |
| 3404 | case BuiltinType::UShort: encoding = 'S'; break; |
| 3405 | case BuiltinType::UInt: encoding = 'I'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3406 | case BuiltinType::ULong: |
| 3407 | encoding = |
| 3408 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
Fariborz Jahanian | 72696e1 | 2009-02-11 22:31:45 +0000 | [diff] [blame] | 3409 | break; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3410 | case BuiltinType::UInt128: encoding = 'T'; break; |
| 3411 | case BuiltinType::ULongLong: encoding = 'Q'; break; |
| 3412 | case BuiltinType::Char_S: |
| 3413 | case BuiltinType::SChar: encoding = 'c'; break; |
| 3414 | case BuiltinType::Short: encoding = 's'; break; |
| 3415 | case BuiltinType::Int: encoding = 'i'; break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3416 | case BuiltinType::Long: |
| 3417 | encoding = |
| 3418 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3419 | break; |
| 3420 | case BuiltinType::LongLong: encoding = 'q'; break; |
| 3421 | case BuiltinType::Int128: encoding = 't'; break; |
| 3422 | case BuiltinType::Float: encoding = 'f'; break; |
| 3423 | case BuiltinType::Double: encoding = 'd'; break; |
| 3424 | case BuiltinType::LongDouble: encoding = 'd'; break; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3425 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3426 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3427 | S += encoding; |
| 3428 | return; |
| 3429 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3430 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3431 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3432 | S += 'j'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3433 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 3434 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3435 | return; |
| 3436 | } |
Fariborz Jahanian | 60bce3e | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 3437 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3438 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 3439 | if (PT->isObjCSelType()) { |
| 3440 | S += ':'; |
| 3441 | return; |
| 3442 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3443 | QualType PointeeTy = PT->getPointeeType(); |
Fariborz Jahanian | 8d2c0a9 | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 3444 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3445 | bool isReadOnly = false; |
| 3446 | // For historical/compatibility reasons, the read-only qualifier of the |
| 3447 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 3448 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3450 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3451 | if (OutermostType && T.isConstQualified()) { |
| 3452 | isReadOnly = true; |
| 3453 | S += 'r'; |
| 3454 | } |
Mike Stump | 9fdbab3 | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3455 | } else if (OutermostType) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3456 | QualType P = PointeeTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3457 | while (P->getAs<PointerType>()) |
| 3458 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3459 | if (P.isConstQualified()) { |
| 3460 | isReadOnly = true; |
| 3461 | S += 'r'; |
| 3462 | } |
| 3463 | } |
| 3464 | if (isReadOnly) { |
| 3465 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 3466 | // combinations need to be rearranged. |
| 3467 | // Rewrite "in const" from "nr" to "rn" |
| 3468 | const char * s = S.c_str(); |
| 3469 | int len = S.length(); |
| 3470 | if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') { |
| 3471 | std::string replace = "rn"; |
| 3472 | S.replace(S.end()-2, S.end(), replace); |
| 3473 | } |
| 3474 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3475 | |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3476 | if (PointeeTy->isCharType()) { |
| 3477 | // char pointer types should be encoded as '*' unless it is a |
| 3478 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 3479 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3480 | S += '*'; |
| 3481 | return; |
| 3482 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3483 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 3484 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 3485 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 3486 | S += '#'; |
| 3487 | return; |
| 3488 | } |
| 3489 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 3490 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 3491 | S += '@'; |
| 3492 | return; |
| 3493 | } |
| 3494 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3495 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3496 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3497 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 3498 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3499 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3500 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3501 | return; |
| 3502 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3503 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3504 | if (const ArrayType *AT = |
| 3505 | // Ignore type qualifiers etc. |
| 3506 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3507 | if (isa<IncompleteArrayType>(AT)) { |
| 3508 | // Incomplete arrays are encoded as a pointer to the array element. |
| 3509 | S += '^'; |
| 3510 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3511 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3512 | false, ExpandStructures, FD); |
| 3513 | } else { |
| 3514 | S += '['; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3515 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3516 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 3517 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 3518 | else { |
| 3519 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 3520 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 3521 | S += '0'; |
| 3522 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3523 | |
| 3524 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 3525 | false, ExpandStructures, FD); |
| 3526 | S += ']'; |
| 3527 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3528 | return; |
| 3529 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3530 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3531 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 3532 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3533 | return; |
| 3534 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3535 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3536 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 3537 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3538 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 3539 | // Anonymous structures print as '?' |
| 3540 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 3541 | S += II->getName(); |
| 3542 | } else { |
| 3543 | S += '?'; |
| 3544 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 3545 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3546 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3547 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 3548 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3549 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3550 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3551 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3552 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3553 | S += '"'; |
| 3554 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3555 | |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3556 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3557 | if (Field->isBitField()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3558 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3559 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3560 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 3561 | QualType qt = Field->getType(); |
| 3562 | getLegacyIntegralTypeEncoding(qt); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3563 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3564 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3565 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 3566 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 3567 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 3568 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3569 | return; |
| 3570 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3571 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3572 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 3573 | if (FD && FD->isBitField()) |
| 3574 | EncodeBitField(this, S, FD); |
| 3575 | else |
| 3576 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3577 | return; |
| 3578 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3579 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3580 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 3581 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3582 | return; |
| 3583 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3584 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3585 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3586 | // @encode(class_name) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3587 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3588 | S += '{'; |
| 3589 | const IdentifierInfo *II = OI->getIdentifier(); |
| 3590 | S += II->getName(); |
| 3591 | S += '='; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3592 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3593 | CollectObjCIvars(OI, RecFields); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3594 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3595 | if (RecFields[i]->isBitField()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3596 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3597 | RecFields[i]); |
| 3598 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3599 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3600 | FD); |
| 3601 | } |
| 3602 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3603 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 3604 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3605 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3606 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3607 | if (OPT->isObjCIdType()) { |
| 3608 | S += '@'; |
| 3609 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3610 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3611 | |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3612 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 3613 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 3614 | // Since this is a binary compatibility issue, need to consult with runtime |
| 3615 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3616 | S += '#'; |
| 3617 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3618 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3619 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3620 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3621 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3622 | ExpandPointedToStructures, |
| 3623 | ExpandStructures, FD); |
| 3624 | if (FD || EncodingProperty) { |
| 3625 | // Note that we do extended encoding of protocol qualifer list |
| 3626 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3627 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3628 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3629 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3630 | S += '<'; |
| 3631 | S += (*I)->getNameAsString(); |
| 3632 | S += '>'; |
| 3633 | } |
| 3634 | S += '"'; |
| 3635 | } |
| 3636 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3637 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3638 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3639 | QualType PointeeTy = OPT->getPointeeType(); |
| 3640 | if (!EncodingProperty && |
| 3641 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 3642 | // Another historical/compatibility reason. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3643 | // We encode the underlying type which comes out as |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3644 | // {...}; |
| 3645 | S += '^'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3646 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 3647 | false, ExpandPointedToStructures, |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3648 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3649 | return; |
| 3650 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3651 | |
| 3652 | S += '@'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3653 | if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3654 | S += '"'; |
Steve Naroff | 27d20a2 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 3655 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3656 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3657 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3658 | S += '<'; |
| 3659 | S += (*I)->getNameAsString(); |
| 3660 | S += '>'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3661 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3662 | S += '"'; |
| 3663 | } |
| 3664 | return; |
| 3665 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3666 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3667 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3670 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 3671 | std::string& S) const { |
| 3672 | if (QT & Decl::OBJC_TQ_In) |
| 3673 | S += 'n'; |
| 3674 | if (QT & Decl::OBJC_TQ_Inout) |
| 3675 | S += 'N'; |
| 3676 | if (QT & Decl::OBJC_TQ_Out) |
| 3677 | S += 'o'; |
| 3678 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 3679 | S += 'O'; |
| 3680 | if (QT & Decl::OBJC_TQ_Byref) |
| 3681 | S += 'R'; |
| 3682 | if (QT & Decl::OBJC_TQ_Oneway) |
| 3683 | S += 'V'; |
| 3684 | } |
| 3685 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3686 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3687 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3688 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3689 | BuiltinVaListType = T; |
| 3690 | } |
| 3691 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3692 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3693 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3696 | void ASTContext::setObjCSelType(QualType T) { |
Fariborz Jahanian | 13dcd00 | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 3697 | ObjCSelTypedefType = T; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3700 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3701 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 3704 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3705 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3708 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3709 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3710 | "'NSConstantString' type already set!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3711 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 3712 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 3715 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 3716 | /// lookup. |
| 3717 | TemplateName ASTContext::getOverloadedTemplateName(NamedDecl * const *Begin, |
| 3718 | NamedDecl * const *End) { |
| 3719 | unsigned size = End - Begin; |
| 3720 | assert(size > 1 && "set is not overloaded!"); |
| 3721 | |
| 3722 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 3723 | size * sizeof(FunctionTemplateDecl*)); |
| 3724 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 3725 | |
| 3726 | NamedDecl **Storage = OT->getStorage(); |
| 3727 | for (NamedDecl * const *I = Begin; I != End; ++I) { |
| 3728 | NamedDecl *D = *I; |
| 3729 | assert(isa<FunctionTemplateDecl>(D) || |
| 3730 | (isa<UsingShadowDecl>(D) && |
| 3731 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 3732 | *Storage++ = D; |
| 3733 | } |
| 3734 | |
| 3735 | return TemplateName(OT); |
| 3736 | } |
| 3737 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3738 | /// \brief Retrieve the template name that represents a qualified |
| 3739 | /// template name such as \c std::vector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3740 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3741 | bool TemplateKeyword, |
| 3742 | TemplateDecl *Template) { |
| 3743 | llvm::FoldingSetNodeID ID; |
| 3744 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 3745 | |
| 3746 | void *InsertPos = 0; |
| 3747 | QualifiedTemplateName *QTN = |
| 3748 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3749 | if (!QTN) { |
| 3750 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 3751 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 3752 | } |
| 3753 | |
| 3754 | return TemplateName(QTN); |
| 3755 | } |
| 3756 | |
| 3757 | /// \brief Retrieve the template name that represents a dependent |
| 3758 | /// template name such as \c MetaFun::template apply. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3759 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3760 | const IdentifierInfo *Name) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3761 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 3762 | "Nested name specifier must be dependent"); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3763 | |
| 3764 | llvm::FoldingSetNodeID ID; |
| 3765 | DependentTemplateName::Profile(ID, NNS, Name); |
| 3766 | |
| 3767 | void *InsertPos = 0; |
| 3768 | DependentTemplateName *QTN = |
| 3769 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3770 | |
| 3771 | if (QTN) |
| 3772 | return TemplateName(QTN); |
| 3773 | |
| 3774 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 3775 | if (CanonNNS == NNS) { |
| 3776 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 3777 | } else { |
| 3778 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 3779 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
| 3780 | } |
| 3781 | |
| 3782 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3783 | return TemplateName(QTN); |
| 3784 | } |
| 3785 | |
Douglas Gregor | ca1bdd7 | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 3786 | /// \brief Retrieve the template name that represents a dependent |
| 3787 | /// template name such as \c MetaFun::template operator+. |
| 3788 | TemplateName |
| 3789 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 3790 | OverloadedOperatorKind Operator) { |
| 3791 | assert((!NNS || NNS->isDependent()) && |
| 3792 | "Nested name specifier must be dependent"); |
| 3793 | |
| 3794 | llvm::FoldingSetNodeID ID; |
| 3795 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 3796 | |
| 3797 | void *InsertPos = 0; |
| 3798 | DependentTemplateName *QTN = |
| 3799 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 3800 | |
| 3801 | if (QTN) |
| 3802 | return TemplateName(QTN); |
| 3803 | |
| 3804 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 3805 | if (CanonNNS == NNS) { |
| 3806 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 3807 | } else { |
| 3808 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 3809 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
| 3810 | } |
| 3811 | |
| 3812 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3813 | return TemplateName(QTN); |
| 3814 | } |
| 3815 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3816 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 3817 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 3818 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3819 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3820 | switch (Type) { |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3821 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3822 | case TargetInfo::SignedShort: return ShortTy; |
| 3823 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 3824 | case TargetInfo::SignedInt: return IntTy; |
| 3825 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 3826 | case TargetInfo::SignedLong: return LongTy; |
| 3827 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 3828 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 3829 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 3830 | } |
| 3831 | |
| 3832 | assert(false && "Unhandled TargetInfo::IntType value"); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 3833 | return CanQualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3834 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 3835 | |
| 3836 | //===----------------------------------------------------------------------===// |
| 3837 | // Type Predicates. |
| 3838 | //===----------------------------------------------------------------------===// |
| 3839 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3840 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 3841 | /// NSObject attribute on a c-style pointer type. |
| 3842 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3843 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3844 | /// |
| 3845 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 3846 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 3847 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3848 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3849 | return true; |
| 3850 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3851 | return false; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3854 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 3855 | /// garbage collection attribute. |
| 3856 | /// |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3857 | Qualifiers::GC ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
| 3858 | Qualifiers::GC GCAttrs = Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3859 | if (getLangOptions().ObjC1 && |
| 3860 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3861 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3862 | // Default behavious under objective-c's gc is for objective-c pointers |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3863 | // (or pointers to them) be treated as though they were declared |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3864 | // as __strong. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3865 | if (GCAttrs == Qualifiers::GCNone) { |
Fariborz Jahanian | 75212ee | 2009-09-10 23:38:45 +0000 | [diff] [blame] | 3866 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3867 | GCAttrs = Qualifiers::Strong; |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3868 | else if (Ty->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3869 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3870 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 3871 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 3872 | // set on them. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3873 | else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3874 | return Qualifiers::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3875 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3876 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3879 | //===----------------------------------------------------------------------===// |
| 3880 | // Type Compatibility Testing |
| 3881 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 3882 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3883 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3884 | /// compatible. |
| 3885 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 3886 | const VectorType *RHS) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3887 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3888 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3889 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3890 | } |
| 3891 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3892 | //===----------------------------------------------------------------------===// |
| 3893 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 3894 | //===----------------------------------------------------------------------===// |
| 3895 | |
| 3896 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 3897 | /// inheritance hierarchy of 'rProto'. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3898 | bool ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 3899 | ObjCProtocolDecl *rProto) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3900 | if (lProto == rProto) |
| 3901 | return true; |
| 3902 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 3903 | E = rProto->protocol_end(); PI != E; ++PI) |
| 3904 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 3905 | return true; |
| 3906 | return false; |
| 3907 | } |
| 3908 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3909 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 3910 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 3911 | /// otherwise. |
| 3912 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 3913 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 3914 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 3915 | return false; |
| 3916 | } |
| 3917 | |
| 3918 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 3919 | /// ObjCQualifiedIDType. |
| 3920 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 3921 | bool compare) { |
| 3922 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3923 | if (lhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3924 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 3925 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3926 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3927 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 3928 | return true; |
| 3929 | |
| 3930 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3931 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3932 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3933 | if (!rhsOPT) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3935 | if (rhsOPT->qual_empty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3936 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3937 | // make sure we check the class hierarchy. |
| 3938 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 3939 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3940 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3941 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3942 | // see if static class implements all of id's protocols, directly or |
| 3943 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3944 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3945 | return false; |
| 3946 | } |
| 3947 | } |
| 3948 | // If there are no qualifiers and no interface, we have an 'id'. |
| 3949 | return true; |
| 3950 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3951 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3952 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3953 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3954 | ObjCProtocolDecl *lhsProto = *I; |
| 3955 | bool match = false; |
| 3956 | |
| 3957 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3958 | // see if static class implements all of id's protocols, directly or |
| 3959 | // through its super class and categories. |
| 3960 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 3961 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 3962 | ObjCProtocolDecl *rhsProto = *J; |
| 3963 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 3964 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 3965 | match = true; |
| 3966 | break; |
| 3967 | } |
| 3968 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3969 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3970 | // make sure we check the class hierarchy. |
| 3971 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 3972 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3973 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3974 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3975 | // see if static class implements all of id's protocols, directly or |
| 3976 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 3977 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3978 | match = true; |
| 3979 | break; |
| 3980 | } |
| 3981 | } |
| 3982 | } |
| 3983 | if (!match) |
| 3984 | return false; |
| 3985 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3986 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3987 | return true; |
| 3988 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3989 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3990 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 3991 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 3992 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3993 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3994 | lhs->getAsObjCInterfacePointerType()) { |
| 3995 | if (lhsOPT->qual_empty()) { |
| 3996 | bool match = false; |
| 3997 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 3998 | for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), |
| 3999 | E = rhsQID->qual_end(); I != E; ++I) { |
| 4000 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4001 | // see if static class implements all of id's protocols, directly or |
| 4002 | // through its super class and categories. |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 4003 | if (lhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4004 | match = true; |
| 4005 | break; |
| 4006 | } |
| 4007 | } |
| 4008 | if (!match) |
| 4009 | return false; |
| 4010 | } |
| 4011 | return true; |
| 4012 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4013 | // Both the right and left sides have qualifiers. |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4014 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 4015 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 4016 | ObjCProtocolDecl *lhsProto = *I; |
| 4017 | bool match = false; |
| 4018 | |
| 4019 | // when comparing an id<P> on lhs with a static type on rhs, |
| 4020 | // see if static class implements all of id's protocols, directly or |
| 4021 | // through its super class and categories. |
| 4022 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 4023 | E = rhsQID->qual_end(); J != E; ++J) { |
| 4024 | ObjCProtocolDecl *rhsProto = *J; |
| 4025 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 4026 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 4027 | match = true; |
| 4028 | break; |
| 4029 | } |
| 4030 | } |
| 4031 | if (!match) |
| 4032 | return false; |
| 4033 | } |
| 4034 | return true; |
| 4035 | } |
| 4036 | return false; |
| 4037 | } |
| 4038 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4039 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4040 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 4041 | /// protocol qualifiers on the LHS or RHS. |
| 4042 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4043 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 4044 | const ObjCObjectPointerType *RHSOPT) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 4045 | // If either type represents the built-in 'id' or 'Class' types, return true. |
| 4046 | if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4047 | return true; |
| 4048 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4049 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4050 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 4051 | QualType(RHSOPT,0), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4052 | false); |
| 4053 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4054 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4055 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4056 | if (LHS && RHS) // We have 2 user-defined types. |
| 4057 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4058 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4059 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4062 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 4063 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 4064 | /// It is used to build composite qualifier list of the composite type of |
| 4065 | /// the conditional expression involving two objective-c pointer objects. |
| 4066 | static |
| 4067 | void getIntersectionOfProtocols(ASTContext &Context, |
| 4068 | const ObjCObjectPointerType *LHSOPT, |
| 4069 | const ObjCObjectPointerType *RHSOPT, |
| 4070 | llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
| 4071 | |
| 4072 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4073 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4074 | |
| 4075 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 4076 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 4077 | if (LHSNumProtocols > 0) |
| 4078 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 4079 | else { |
| 4080 | llvm::SmallVector<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 4081 | Context.CollectInheritedProtocols(LHS->getDecl(), LHSInheritedProtocols); |
| 4082 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 4083 | LHSInheritedProtocols.end()); |
| 4084 | } |
| 4085 | |
| 4086 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 4087 | if (RHSNumProtocols > 0) { |
| 4088 | ObjCProtocolDecl **RHSProtocols = (ObjCProtocolDecl **)RHS->qual_begin(); |
| 4089 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 4090 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 4091 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
| 4092 | } |
| 4093 | else { |
| 4094 | llvm::SmallVector<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
| 4095 | Context.CollectInheritedProtocols(RHS->getDecl(), RHSInheritedProtocols); |
| 4096 | // FIXME. This may cause duplication of protocols in the list, but should |
| 4097 | // be harmless. |
| 4098 | for (unsigned i = 0, len = RHSInheritedProtocols.size(); i < len; ++i) |
| 4099 | if (InheritedProtocolSet.count(RHSInheritedProtocols[i])) |
| 4100 | IntersectionOfProtocols.push_back(RHSInheritedProtocols[i]); |
| 4101 | } |
| 4102 | } |
| 4103 | |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4104 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 4105 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 4106 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 4107 | /// warning is issued. So, its invokation is extremely rare. |
| 4108 | QualType ASTContext::areCommonBaseCompatible( |
| 4109 | const ObjCObjectPointerType *LHSOPT, |
| 4110 | const ObjCObjectPointerType *RHSOPT) { |
| 4111 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 4112 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 4113 | if (!LHS || !RHS) |
| 4114 | return QualType(); |
| 4115 | |
| 4116 | while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) { |
| 4117 | QualType LHSTy = getObjCInterfaceType(LHSIDecl); |
| 4118 | LHS = LHSTy->getAs<ObjCInterfaceType>(); |
Fariborz Jahanian | e23fa2d | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 4119 | if (canAssignObjCInterfaces(LHS, RHS)) { |
| 4120 | llvm::SmallVector<ObjCProtocolDecl *, 8> IntersectionOfProtocols; |
| 4121 | getIntersectionOfProtocols(*this, |
| 4122 | LHSOPT, RHSOPT, IntersectionOfProtocols); |
| 4123 | if (IntersectionOfProtocols.empty()) |
| 4124 | LHSTy = getObjCObjectPointerType(LHSTy); |
| 4125 | else |
| 4126 | LHSTy = getObjCObjectPointerType(LHSTy, &IntersectionOfProtocols[0], |
| 4127 | IntersectionOfProtocols.size()); |
| 4128 | return LHSTy; |
| 4129 | } |
Fariborz Jahanian | db07b3f | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
| 4132 | return QualType(); |
| 4133 | } |
| 4134 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4135 | bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, |
| 4136 | const ObjCInterfaceType *RHS) { |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4137 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 4138 | // the LHS. |
| 4139 | if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 4140 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4141 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4142 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 4143 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4144 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4145 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4146 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4147 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 4148 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4149 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4150 | return true; // FIXME: should return false! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4151 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4152 | for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(), |
| 4153 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4154 | LHSPI != LHSPE; LHSPI++) { |
| 4155 | bool RHSImplementsProtocol = false; |
| 4156 | |
| 4157 | // If the RHS doesn't implement the protocol on the left, the types |
| 4158 | // are incompatible. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 4159 | for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 4160 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4161 | RHSPI != RHSPE; RHSPI++) { |
| 4162 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4163 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 4164 | break; |
| 4165 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4166 | } |
| 4167 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 4168 | if (!RHSImplementsProtocol) |
| 4169 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4170 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 4171 | // The RHS implements all protocols listed on the LHS. |
| 4172 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4175 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 4176 | // get the "pointed to" types |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4177 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 4178 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4179 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4180 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4181 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4182 | |
| 4183 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 4184 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4187 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4188 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4189 | /// 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] | 4190 | /// same. See 6.7.[2,3,5] for additional rules. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4191 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { |
| 4192 | return !mergeTypes(LHS, RHS).isNull(); |
| 4193 | } |
| 4194 | |
| 4195 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4196 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 4197 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4198 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 4199 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4200 | bool allLTypes = true; |
| 4201 | bool allRTypes = true; |
| 4202 | |
| 4203 | // Check return type |
| 4204 | QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); |
| 4205 | if (retType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4206 | if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) |
| 4207 | allLTypes = false; |
| 4208 | if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) |
| 4209 | allRTypes = false; |
Mike Stump | 6dcbc29 | 2009-07-25 23:24:03 +0000 | [diff] [blame] | 4210 | // FIXME: double check this |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4211 | bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr(); |
| 4212 | if (NoReturn != lbase->getNoReturnAttr()) |
| 4213 | allLTypes = false; |
| 4214 | if (NoReturn != rbase->getNoReturnAttr()) |
| 4215 | allRTypes = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4216 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4217 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4218 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 4219 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4220 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 4221 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 4222 | |
| 4223 | // Compatible functions must have the same number of arguments |
| 4224 | if (lproto_nargs != rproto_nargs) |
| 4225 | return QualType(); |
| 4226 | |
| 4227 | // Variadic and non-variadic functions aren't compatible |
| 4228 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 4229 | return QualType(); |
| 4230 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 4231 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 4232 | return QualType(); |
| 4233 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4234 | // Check argument compatibility |
| 4235 | llvm::SmallVector<QualType, 10> types; |
| 4236 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 4237 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 4238 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
| 4239 | QualType argtype = mergeTypes(largtype, rargtype); |
| 4240 | if (argtype.isNull()) return QualType(); |
| 4241 | types.push_back(argtype); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4242 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 4243 | allLTypes = false; |
| 4244 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 4245 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4246 | } |
| 4247 | if (allLTypes) return lhs; |
| 4248 | if (allRTypes) return rhs; |
| 4249 | return getFunctionType(retType, types.begin(), types.size(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4250 | lproto->isVariadic(), lproto->getTypeQuals(), |
| 4251 | NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | if (lproto) allRTypes = false; |
| 4255 | if (rproto) allLTypes = false; |
| 4256 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4257 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4258 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 4259 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4260 | if (proto->isVariadic()) return QualType(); |
| 4261 | // Check that the types are compatible with the types that |
| 4262 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 4263 | // The only types actually affected are promotable integer |
| 4264 | // types and floats, which would be passed as a different |
| 4265 | // type depending on whether the prototype is visible. |
| 4266 | unsigned proto_nargs = proto->getNumArgs(); |
| 4267 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 4268 | QualType argTy = proto->getArgType(i); |
| 4269 | if (argTy->isPromotableIntegerType() || |
| 4270 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 4271 | return QualType(); |
| 4272 | } |
| 4273 | |
| 4274 | if (allLTypes) return lhs; |
| 4275 | if (allRTypes) return rhs; |
| 4276 | return getFunctionType(retType, proto->arg_type_begin(), |
Mike Stump | 2d3c191 | 2009-07-27 00:44:23 +0000 | [diff] [blame] | 4277 | proto->getNumArgs(), proto->isVariadic(), |
| 4278 | proto->getTypeQuals(), NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | if (allLTypes) return lhs; |
| 4282 | if (allRTypes) return rhs; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame] | 4283 | return getFunctionNoProtoType(retType, NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4284 | } |
| 4285 | |
| 4286 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 4287 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 4288 | // type is adjusted to "T" prior to any further analysis, the expression |
| 4289 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4290 | // expression is an lvalue unless the reference is an rvalue reference and |
| 4291 | // the expression is a function call (possibly inside parentheses). |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4292 | // FIXME: C++ shouldn't be going through here! The rules are different |
| 4293 | // enough that they should be handled separately. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4294 | // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really* |
| 4295 | // shouldn't be going through here! |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4296 | if (const ReferenceType *RT = LHS->getAs<ReferenceType>()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 4297 | LHS = RT->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4298 | if (const ReferenceType *RT = RHS->getAs<ReferenceType>()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 4299 | RHS = RT->getPointeeType(); |
Chris Lattner | f3692dc | 2008-04-07 05:37:56 +0000 | [diff] [blame] | 4300 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4301 | QualType LHSCan = getCanonicalType(LHS), |
| 4302 | RHSCan = getCanonicalType(RHS); |
| 4303 | |
| 4304 | // If two types are identical, they are compatible. |
| 4305 | if (LHSCan == RHSCan) |
| 4306 | return LHS; |
| 4307 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4308 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4309 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 4310 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4311 | if (LQuals != RQuals) { |
| 4312 | // If any of these qualifiers are different, we have a type |
| 4313 | // mismatch. |
| 4314 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 4315 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 4316 | return QualType(); |
| 4317 | |
| 4318 | // Exactly one GC qualifier difference is allowed: __strong is |
| 4319 | // okay if the other type has no GC qualifier but is an Objective |
| 4320 | // C object pointer (i.e. implicitly strong by default). We fix |
| 4321 | // this by pretending that the unqualified type was actually |
| 4322 | // qualified __strong. |
| 4323 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 4324 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 4325 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 4326 | |
| 4327 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 4328 | return QualType(); |
| 4329 | |
| 4330 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 4331 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 4332 | } |
| 4333 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 4334 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 4335 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4336 | return QualType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4337 | } |
| 4338 | |
| 4339 | // Okay, qualifiers are equal. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4340 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 4341 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 4342 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4343 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4344 | // We want to consider the two function types to be the same for these |
| 4345 | // comparisons, just force one to the other. |
| 4346 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 4347 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 4348 | |
| 4349 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4350 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 4351 | LHSClass = Type::ConstantArray; |
| 4352 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 4353 | RHSClass = Type::ConstantArray; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4354 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 4355 | // Canonicalize ExtVector -> Vector. |
| 4356 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 4357 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4358 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 4359 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4360 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4361 | // 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] | 4362 | // a signed integer type, or an unsigned integer type. |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4363 | // Compatibility is based on the underlying type, not the promotion |
| 4364 | // type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4365 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4366 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 4367 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4368 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4369 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4370 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 4371 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 4372 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4373 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4374 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4375 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4376 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 4377 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4378 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4379 | #define TYPE(Class, Base) |
| 4380 | #define ABSTRACT_TYPE(Class, Base) |
| 4381 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4382 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 4383 | #include "clang/AST/TypeNodes.def" |
| 4384 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 4385 | return QualType(); |
| 4386 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 4387 | case Type::LValueReference: |
| 4388 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4389 | case Type::MemberPointer: |
| 4390 | assert(false && "C++ should never be in mergeTypes"); |
| 4391 | return QualType(); |
| 4392 | |
| 4393 | case Type::IncompleteArray: |
| 4394 | case Type::VariableArray: |
| 4395 | case Type::FunctionProto: |
| 4396 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4397 | assert(false && "Types are eliminated above"); |
| 4398 | return QualType(); |
| 4399 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4400 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4401 | { |
| 4402 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4403 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 4404 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4405 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 4406 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4407 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4408 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 4409 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4410 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4411 | return getPointerType(ResultType); |
| 4412 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4413 | case Type::BlockPointer: |
| 4414 | { |
| 4415 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4416 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 4417 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 4418 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 4419 | if (ResultType.isNull()) return QualType(); |
| 4420 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 4421 | return LHS; |
| 4422 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 4423 | return RHS; |
| 4424 | return getBlockPointerType(ResultType); |
| 4425 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4426 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4427 | { |
| 4428 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 4429 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 4430 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 4431 | return QualType(); |
| 4432 | |
| 4433 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 4434 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
| 4435 | QualType ResultType = mergeTypes(LHSElem, RHSElem); |
| 4436 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4437 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4438 | return LHS; |
| 4439 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4440 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4441 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 4442 | ArrayType::ArraySizeModifier(), 0); |
| 4443 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 4444 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4445 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 4446 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4447 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 4448 | return LHS; |
| 4449 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 4450 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4451 | if (LVAT) { |
| 4452 | // FIXME: This isn't correct! But tricky to implement because |
| 4453 | // the array's size has to be the size of LHS, but the type |
| 4454 | // has to be different. |
| 4455 | return LHS; |
| 4456 | } |
| 4457 | if (RVAT) { |
| 4458 | // FIXME: This isn't correct! But tricky to implement because |
| 4459 | // the array's size has to be the size of RHS, but the type |
| 4460 | // has to be different. |
| 4461 | return RHS; |
| 4462 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 4463 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 4464 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4465 | return getIncompleteArrayType(ResultType, |
| 4466 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4467 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4468 | case Type::FunctionNoProto: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4469 | return mergeFunctionTypes(LHS, RHS); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4470 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4471 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4472 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 4473 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4474 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4475 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 4476 | case Type::Complex: |
| 4477 | // Distinct complex types are incompatible. |
| 4478 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 4479 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4480 | // FIXME: The merged type should be an ExtVector! |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4481 | if (areCompatVectorTypes(LHS->getAs<VectorType>(), RHS->getAs<VectorType>())) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4482 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4483 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4484 | case Type::ObjCInterface: { |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4485 | // Check if the interfaces are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4486 | // FIXME: This should be type compatibility, e.g. whether |
| 4487 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4488 | const ObjCInterfaceType* LHSIface = LHS->getAs<ObjCInterfaceType>(); |
| 4489 | const ObjCInterfaceType* RHSIface = RHS->getAs<ObjCInterfaceType>(); |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 4490 | if (LHSIface && RHSIface && |
| 4491 | canAssignObjCInterfaces(LHSIface, RHSIface)) |
| 4492 | return LHS; |
| 4493 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 4494 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 4495 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4496 | case Type::ObjCObjectPointer: { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4497 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 4498 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4499 | return LHS; |
| 4500 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 4501 | return QualType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4502 | } |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 4503 | case Type::FixedWidthInt: |
| 4504 | // Distinct fixed-width integers are not compatible. |
| 4505 | return QualType(); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4506 | case Type::TemplateSpecialization: |
| 4507 | assert(false && "Dependent types have no size"); |
| 4508 | break; |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4509 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4510 | |
| 4511 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 4512 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 4513 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 4514 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4515 | // Integer Predicates |
| 4516 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 4517 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4518 | unsigned ASTContext::getIntWidth(QualType T) { |
Sebastian Redl | 632d772 | 2009-11-05 21:10:57 +0000 | [diff] [blame] | 4519 | if (T->isBooleanType()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4520 | return 1; |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4521 | if (FixedWidthIntType *FWIT = dyn_cast<FixedWidthIntType>(T)) { |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4522 | return FWIT->getWidth(); |
| 4523 | } |
John McCall | 842aef8 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4524 | if (EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | 29a7f33 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 4525 | T = ET->getDecl()->getIntegerType(); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4526 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4527 | return (unsigned)getTypeSize(T); |
| 4528 | } |
| 4529 | |
| 4530 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
| 4531 | assert(T->isSignedIntegerType() && "Unexpected type"); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4532 | |
| 4533 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 4534 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 4535 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
| 4536 | VTy->getNumElements()); |
| 4537 | |
| 4538 | // For enums, we return the unsigned version of the base type. |
| 4539 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4540 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4541 | |
| 4542 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 4543 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4544 | switch (BTy->getKind()) { |
| 4545 | case BuiltinType::Char_S: |
| 4546 | case BuiltinType::SChar: |
| 4547 | return UnsignedCharTy; |
| 4548 | case BuiltinType::Short: |
| 4549 | return UnsignedShortTy; |
| 4550 | case BuiltinType::Int: |
| 4551 | return UnsignedIntTy; |
| 4552 | case BuiltinType::Long: |
| 4553 | return UnsignedLongTy; |
| 4554 | case BuiltinType::LongLong: |
| 4555 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4556 | case BuiltinType::Int128: |
| 4557 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 4558 | default: |
| 4559 | assert(0 && "Unexpected signed integer type"); |
| 4560 | return QualType(); |
| 4561 | } |
| 4562 | } |
| 4563 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 4564 | ExternalASTSource::~ExternalASTSource() { } |
| 4565 | |
| 4566 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4567 | |
| 4568 | |
| 4569 | //===----------------------------------------------------------------------===// |
| 4570 | // Builtin Type Computation |
| 4571 | //===----------------------------------------------------------------------===// |
| 4572 | |
| 4573 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
| 4574 | /// pointer over the consumed characters. This returns the resultant type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4575 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4576 | ASTContext::GetBuiltinTypeError &Error, |
| 4577 | bool AllowTypeModifiers = true) { |
| 4578 | // Modifiers. |
| 4579 | int HowLong = 0; |
| 4580 | bool Signed = false, Unsigned = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4581 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4582 | // Read the modifiers first. |
| 4583 | bool Done = false; |
| 4584 | while (!Done) { |
| 4585 | switch (*Str++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4586 | default: Done = true; --Str; break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4587 | case 'S': |
| 4588 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 4589 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 4590 | Signed = true; |
| 4591 | break; |
| 4592 | case 'U': |
| 4593 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 4594 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 4595 | Unsigned = true; |
| 4596 | break; |
| 4597 | case 'L': |
| 4598 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 4599 | ++HowLong; |
| 4600 | break; |
| 4601 | } |
| 4602 | } |
| 4603 | |
| 4604 | QualType Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4605 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4606 | // Read the base type. |
| 4607 | switch (*Str++) { |
| 4608 | default: assert(0 && "Unknown builtin type letter!"); |
| 4609 | case 'v': |
| 4610 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4611 | "Bad modifiers used with 'v'!"); |
| 4612 | Type = Context.VoidTy; |
| 4613 | break; |
| 4614 | case 'f': |
| 4615 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 4616 | "Bad modifiers used with 'f'!"); |
| 4617 | Type = Context.FloatTy; |
| 4618 | break; |
| 4619 | case 'd': |
| 4620 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 4621 | "Bad modifiers used with 'd'!"); |
| 4622 | if (HowLong) |
| 4623 | Type = Context.LongDoubleTy; |
| 4624 | else |
| 4625 | Type = Context.DoubleTy; |
| 4626 | break; |
| 4627 | case 's': |
| 4628 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 4629 | if (Unsigned) |
| 4630 | Type = Context.UnsignedShortTy; |
| 4631 | else |
| 4632 | Type = Context.ShortTy; |
| 4633 | break; |
| 4634 | case 'i': |
| 4635 | if (HowLong == 3) |
| 4636 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 4637 | else if (HowLong == 2) |
| 4638 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 4639 | else if (HowLong == 1) |
| 4640 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 4641 | else |
| 4642 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 4643 | break; |
| 4644 | case 'c': |
| 4645 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 4646 | if (Signed) |
| 4647 | Type = Context.SignedCharTy; |
| 4648 | else if (Unsigned) |
| 4649 | Type = Context.UnsignedCharTy; |
| 4650 | else |
| 4651 | Type = Context.CharTy; |
| 4652 | break; |
| 4653 | case 'b': // boolean |
| 4654 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 4655 | Type = Context.BoolTy; |
| 4656 | break; |
| 4657 | case 'z': // size_t. |
| 4658 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 4659 | Type = Context.getSizeType(); |
| 4660 | break; |
| 4661 | case 'F': |
| 4662 | Type = Context.getCFConstantStringType(); |
| 4663 | break; |
| 4664 | case 'a': |
| 4665 | Type = Context.getBuiltinVaListType(); |
| 4666 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4667 | break; |
| 4668 | case 'A': |
| 4669 | // This is a "reference" to a va_list; however, what exactly |
| 4670 | // this means depends on how va_list is defined. There are two |
| 4671 | // different kinds of va_list: ones passed by value, and ones |
| 4672 | // passed by reference. An example of a by-value va_list is |
| 4673 | // x86, where va_list is a char*. An example of by-ref va_list |
| 4674 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 4675 | // we want this argument to be a char*&; for x86-64, we want |
| 4676 | // it to be a __va_list_tag*. |
| 4677 | Type = Context.getBuiltinVaListType(); |
| 4678 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 4679 | if (Type->isArrayType()) { |
| 4680 | Type = Context.getArrayDecayedType(Type); |
| 4681 | } else { |
| 4682 | Type = Context.getLValueReferenceType(Type); |
| 4683 | } |
| 4684 | break; |
| 4685 | case 'V': { |
| 4686 | char *End; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4687 | unsigned NumElements = strtoul(Str, &End, 10); |
| 4688 | assert(End != Str && "Missing vector size"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4689 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4690 | Str = End; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4691 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4692 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 4693 | Type = Context.getVectorType(ElementType, NumElements); |
| 4694 | break; |
| 4695 | } |
Douglas Gregor | d3a23b2 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 4696 | case 'X': { |
| 4697 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 4698 | Type = Context.getComplexType(ElementType); |
| 4699 | break; |
| 4700 | } |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4701 | case 'P': |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 4702 | Type = Context.getFILEType(); |
| 4703 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4704 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4705 | return QualType(); |
| 4706 | } |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4707 | break; |
Chris Lattner | 9a5a7e7 | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 4708 | case 'J': |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4709 | if (Signed) |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4710 | Type = Context.getsigjmp_bufType(); |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4711 | else |
| 4712 | Type = Context.getjmp_bufType(); |
| 4713 | |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4714 | if (Type.isNull()) { |
Mike Stump | f711c41 | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 4715 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | fd612db | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 4716 | return QualType(); |
| 4717 | } |
| 4718 | break; |
Mike Stump | 782fa30 | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 4719 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4720 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4721 | if (!AllowTypeModifiers) |
| 4722 | return Type; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4723 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4724 | Done = false; |
| 4725 | while (!Done) { |
| 4726 | switch (*Str++) { |
| 4727 | default: Done = true; --Str; break; |
| 4728 | case '*': |
| 4729 | Type = Context.getPointerType(Type); |
| 4730 | break; |
| 4731 | case '&': |
| 4732 | Type = Context.getLValueReferenceType(Type); |
| 4733 | break; |
| 4734 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 4735 | case 'C': |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4736 | Type = Type.withConst(); |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4737 | break; |
| 4738 | } |
| 4739 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4740 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4741 | return Type; |
| 4742 | } |
| 4743 | |
| 4744 | /// GetBuiltinType - Return the type for the specified builtin. |
| 4745 | QualType ASTContext::GetBuiltinType(unsigned id, |
| 4746 | GetBuiltinTypeError &Error) { |
| 4747 | const char *TypeStr = BuiltinInfo.GetTypeString(id); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4748 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4749 | llvm::SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4750 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4751 | Error = GE_None; |
| 4752 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4753 | if (Error != GE_None) |
| 4754 | return QualType(); |
| 4755 | while (TypeStr[0] && TypeStr[0] != '.') { |
| 4756 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); |
| 4757 | if (Error != GE_None) |
| 4758 | return QualType(); |
| 4759 | |
| 4760 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 4761 | if (Ty->isArrayType()) |
| 4762 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4763 | |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 4764 | ArgTypes.push_back(Ty); |
| 4765 | } |
| 4766 | |
| 4767 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 4768 | "'.' should only occur at end of builtin type list!"); |
| 4769 | |
| 4770 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 4771 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 4772 | return getFunctionNoProtoType(ResType); |
| 4773 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), |
| 4774 | TypeStr[0] == '.', 0); |
| 4775 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4776 | |
| 4777 | QualType |
| 4778 | ASTContext::UsualArithmeticConversionsType(QualType lhs, QualType rhs) { |
| 4779 | // Perform the usual unary conversions. We do this early so that |
| 4780 | // integral promotions to "int" can allow us to exit early, in the |
| 4781 | // lhs == rhs check. Also, for conversion purposes, we ignore any |
| 4782 | // qualifiers. For example, "const float" and "float" are |
| 4783 | // equivalent. |
| 4784 | if (lhs->isPromotableIntegerType()) |
| 4785 | lhs = getPromotedIntegerType(lhs); |
| 4786 | else |
| 4787 | lhs = lhs.getUnqualifiedType(); |
| 4788 | if (rhs->isPromotableIntegerType()) |
| 4789 | rhs = getPromotedIntegerType(rhs); |
| 4790 | else |
| 4791 | rhs = rhs.getUnqualifiedType(); |
| 4792 | |
| 4793 | // If both types are identical, no conversion is needed. |
| 4794 | if (lhs == rhs) |
| 4795 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4796 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4797 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 4798 | // The caller can deal with this (e.g. pointer + int). |
| 4799 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 4800 | return lhs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4801 | |
| 4802 | // At this point, we have two different arithmetic types. |
| 4803 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4804 | // Handle complex types first (C99 6.3.1.8p1). |
| 4805 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 4806 | // if we have an integer operand, the result is the complex type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4807 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4808 | // convert the rhs to the lhs complex type. |
| 4809 | return lhs; |
| 4810 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4811 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4812 | // convert the lhs to the rhs complex type. |
| 4813 | return rhs; |
| 4814 | } |
| 4815 | // This handles complex/complex, complex/float, or float/complex. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4816 | // When both operands are complex, the shorter operand is converted to the |
| 4817 | // type of the longer, and that is the type of the result. This corresponds |
| 4818 | // to what is done when combining two real floating-point operands. |
| 4819 | // The fun begins when size promotion occur across type domains. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4820 | // 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] | 4821 | // floating-point type, the less precise type is converted, within it's |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4822 | // 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] | 4823 | // when combining a "long double" with a "double _Complex", the |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4824 | // "double _Complex" is promoted to "long double _Complex". |
| 4825 | int result = getFloatingTypeOrder(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4826 | |
| 4827 | if (result > 0) { // The left side is bigger, convert rhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4828 | rhs = getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4829 | } else if (result < 0) { // The right side is bigger, convert lhs. |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4830 | lhs = getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4831 | } |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4832 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 4833 | // domains match. This is a requirement for our implementation, C99 |
| 4834 | // does not require this promotion. |
| 4835 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 4836 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
| 4837 | return rhs; |
| 4838 | } else { // handle "_Complex double, double". |
| 4839 | return lhs; |
| 4840 | } |
| 4841 | } |
| 4842 | return lhs; // The domain/size match exactly. |
| 4843 | } |
| 4844 | // Now handle "real" floating types (i.e. float, double, long double). |
| 4845 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 4846 | // if we have an integer operand, the result is the real floating type. |
| 4847 | if (rhs->isIntegerType()) { |
| 4848 | // convert rhs to the lhs floating point type. |
| 4849 | return lhs; |
| 4850 | } |
| 4851 | if (rhs->isComplexIntegerType()) { |
| 4852 | // convert rhs to the complex floating point type. |
| 4853 | return getComplexType(lhs); |
| 4854 | } |
| 4855 | if (lhs->isIntegerType()) { |
| 4856 | // convert lhs to the rhs floating point type. |
| 4857 | return rhs; |
| 4858 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4859 | if (lhs->isComplexIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4860 | // convert lhs to the complex floating point type. |
| 4861 | return getComplexType(rhs); |
| 4862 | } |
| 4863 | // We have two real floating types, float/complex combos were handled above. |
| 4864 | // Convert the smaller operand to the bigger result. |
| 4865 | int result = getFloatingTypeOrder(lhs, rhs); |
| 4866 | if (result > 0) // convert the rhs |
| 4867 | return lhs; |
| 4868 | assert(result < 0 && "illegal float comparison"); |
| 4869 | return rhs; // convert the lhs |
| 4870 | } |
| 4871 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 4872 | // Handle GCC complex int extension. |
| 4873 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 4874 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 4875 | |
| 4876 | if (lhsComplexInt && rhsComplexInt) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4877 | if (getIntegerTypeOrder(lhsComplexInt->getElementType(), |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4878 | rhsComplexInt->getElementType()) >= 0) |
| 4879 | return lhs; // convert the rhs |
| 4880 | return rhs; |
| 4881 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 4882 | // convert the rhs to the lhs complex type. |
| 4883 | return lhs; |
| 4884 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 4885 | // convert the lhs to the rhs complex type. |
| 4886 | return rhs; |
| 4887 | } |
| 4888 | } |
| 4889 | // Finally, we have two differing integer types. |
| 4890 | // The rules for this case are in C99 6.3.1.8 |
| 4891 | int compare = getIntegerTypeOrder(lhs, rhs); |
| 4892 | bool lhsSigned = lhs->isSignedIntegerType(), |
| 4893 | rhsSigned = rhs->isSignedIntegerType(); |
| 4894 | QualType destType; |
| 4895 | if (lhsSigned == rhsSigned) { |
| 4896 | // Same signedness; use the higher-ranked type |
| 4897 | destType = compare >= 0 ? lhs : rhs; |
| 4898 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 4899 | // The unsigned type has greater than or equal rank to the |
| 4900 | // signed type, so use the unsigned type |
| 4901 | destType = lhsSigned ? rhs : lhs; |
| 4902 | } else if (getIntWidth(lhs) != getIntWidth(rhs)) { |
| 4903 | // The two types are different widths; if we are here, that |
| 4904 | // means the signed type is larger than the unsigned type, so |
| 4905 | // use the signed type. |
| 4906 | destType = lhsSigned ? lhs : rhs; |
| 4907 | } else { |
| 4908 | // The signed type is higher-ranked than the unsigned type, |
| 4909 | // but isn't actually any bigger (like unsigned int and long |
| 4910 | // on most 32-bit systems). Use the unsigned type corresponding |
| 4911 | // to the signed type. |
| 4912 | destType = getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 4913 | } |
| 4914 | return destType; |
| 4915 | } |