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