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" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExternalASTSource.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame^] | 21 | #include "clang/Basic/Builtins.h" |
Chris Lattner | a9376d4 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MemoryBuffer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
| 29 | enum FloatingRank { |
| 30 | FloatRank, DoubleRank, LongDoubleRank |
| 31 | }; |
| 32 | |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 33 | ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM, |
| 34 | TargetInfo &t, |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 35 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame^] | 36 | Builtin::Context &builtins, |
| 37 | bool FreeMem, unsigned size_reserve) : |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 38 | GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0), |
| 39 | ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts), |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 40 | FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels), |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame^] | 41 | BuiltinInfo(builtins), ExternalSource(0) { |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 42 | if (size_reserve > 0) Types.reserve(size_reserve); |
| 43 | InitBuiltinTypes(); |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 44 | TUDecl = TranslationUnitDecl::Create(*this); |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 45 | PrintingPolicy.CPlusPlus = LangOpts.CPlusPlus; |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | ASTContext::~ASTContext() { |
| 49 | // Deallocate all the types. |
| 50 | while (!Types.empty()) { |
Ted Kremenek | 4b05b1d | 2008-05-21 16:38:54 +0000 | [diff] [blame] | 51 | Types.back()->Destroy(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | Types.pop_back(); |
| 53 | } |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 54 | |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 55 | { |
| 56 | llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 57 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); |
| 58 | while (I != E) { |
| 59 | ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); |
| 60 | delete R; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | { |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 65 | llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator |
| 66 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); |
Nuno Lopes | b74668e | 2008-12-17 22:30:25 +0000 | [diff] [blame] | 67 | while (I != E) { |
| 68 | ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second); |
| 69 | delete R; |
| 70 | } |
| 71 | } |
| 72 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 73 | // Destroy nested-name-specifiers. |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 74 | for (llvm::FoldingSet<NestedNameSpecifier>::iterator |
| 75 | NNS = NestedNameSpecifiers.begin(), |
| 76 | NNSEnd = NestedNameSpecifiers.end(); |
Douglas Gregor | e7dcd78 | 2009-03-27 23:25:45 +0000 | [diff] [blame] | 77 | NNS != NNSEnd; |
Douglas Gregor | 1ae0afa | 2009-03-27 23:54:10 +0000 | [diff] [blame] | 78 | /* Increment in loop */) |
| 79 | (*NNS++).Destroy(*this); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 80 | |
| 81 | if (GlobalNestedNameSpecifier) |
| 82 | GlobalNestedNameSpecifier->Destroy(*this); |
| 83 | |
Eli Friedman | b26153c | 2008-05-27 03:08:09 +0000 | [diff] [blame] | 84 | TUDecl->Destroy(*this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 87 | void |
| 88 | ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) { |
| 89 | ExternalSource.reset(Source.take()); |
| 90 | } |
| 91 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 92 | void ASTContext::PrintStats() const { |
| 93 | fprintf(stderr, "*** AST Context Stats:\n"); |
| 94 | fprintf(stderr, " %d types total.\n", (int)Types.size()); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 95 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 96 | unsigned counts[] = { |
| 97 | #define TYPE(Name, Parent) 0, |
| 98 | #define ABSTRACT_TYPE(Name, Parent) |
| 99 | #include "clang/AST/TypeNodes.def" |
| 100 | 0 // Extra |
| 101 | }; |
Douglas Gregor | c2ee10d | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 102 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 104 | Type *T = Types[i]; |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 105 | counts[(unsigned)T->getTypeClass()]++; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Douglas Gregor | dbe833d | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 108 | unsigned Idx = 0; |
| 109 | unsigned TotalBytes = 0; |
| 110 | #define TYPE(Name, Parent) \ |
| 111 | if (counts[Idx]) \ |
| 112 | fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \ |
| 113 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 114 | ++Idx; |
| 115 | #define ABSTRACT_TYPE(Name, Parent) |
| 116 | #include "clang/AST/TypeNodes.def" |
| 117 | |
| 118 | fprintf(stderr, "Total bytes = %d\n", int(TotalBytes)); |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 119 | |
| 120 | if (ExternalSource.get()) { |
| 121 | fprintf(stderr, "\n"); |
| 122 | ExternalSource->PrintStats(); |
| 123 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
| 127 | void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) { |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 128 | Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | void ASTContext::InitBuiltinTypes() { |
| 132 | assert(VoidTy.isNull() && "Context reinitialized?"); |
| 133 | |
| 134 | // C99 6.2.5p19. |
| 135 | InitBuiltinType(VoidTy, BuiltinType::Void); |
| 136 | |
| 137 | // C99 6.2.5p2. |
| 138 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
| 139 | // C99 6.2.5p3. |
Eli Friedman | 15b9176 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 140 | if (LangOpts.CharIsSigned) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 141 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 142 | else |
| 143 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
| 144 | // C99 6.2.5p4. |
| 145 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 146 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 147 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 148 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 149 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
| 150 | |
| 151 | // C99 6.2.5p6. |
| 152 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 153 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 154 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 155 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 156 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
| 157 | |
| 158 | // C99 6.2.5p10. |
| 159 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 160 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 161 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 163 | // GNU extension, 128-bit integers. |
| 164 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 165 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 166 | |
Chris Lattner | 3a25032 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 167 | if (LangOpts.CPlusPlus) // C++ 3.9.1p5 |
| 168 | InitBuiltinType(WCharTy, BuiltinType::WChar); |
| 169 | else // C99 |
| 170 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 171 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 172 | // Placeholder type for functions. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 173 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 174 | |
| 175 | // Placeholder type for type-dependent expressions whose type is |
| 176 | // completely unknown. No code should ever check a type against |
| 177 | // DependentTy and users should never see it; however, it is here to |
| 178 | // help diagnose failures to properly check for type-dependent |
| 179 | // expressions. |
| 180 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 181 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | // C99 6.2.5p11. |
| 183 | FloatComplexTy = getComplexType(FloatTy); |
| 184 | DoubleComplexTy = getComplexType(DoubleTy); |
| 185 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 186 | |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 187 | BuiltinVaListType = QualType(); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 188 | ObjCIdType = QualType(); |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 189 | IdStructType = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 190 | ObjCClassType = QualType(); |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 191 | ClassStructType = 0; |
| 192 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 193 | ObjCConstantStringType = QualType(); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 194 | |
| 195 | // void * type |
| 196 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 197 | |
| 198 | // nullptr type (C++0x 2.14.7) |
| 199 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 202 | //===----------------------------------------------------------------------===// |
| 203 | // Type Sizing and Analysis |
| 204 | //===----------------------------------------------------------------------===// |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 205 | |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 206 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 207 | /// scalar floating point type. |
| 208 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
| 209 | const BuiltinType *BT = T->getAsBuiltinType(); |
| 210 | assert(BT && "Not a floating point type!"); |
| 211 | switch (BT->getKind()) { |
| 212 | default: assert(0 && "Not a floating point type!"); |
| 213 | case BuiltinType::Float: return Target.getFloatFormat(); |
| 214 | case BuiltinType::Double: return Target.getDoubleFormat(); |
| 215 | case BuiltinType::LongDouble: return Target.getLongDoubleFormat(); |
| 216 | } |
| 217 | } |
| 218 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 219 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
| 220 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 221 | /// this method will assert on them. |
Daniel Dunbar | b7d0844 | 2009-02-17 22:16:19 +0000 | [diff] [blame] | 222 | unsigned ASTContext::getDeclAlignInBytes(const Decl *D) { |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 223 | unsigned Align = Target.getCharWidth(); |
| 224 | |
| 225 | if (const AlignedAttr* AA = D->getAttr<AlignedAttr>()) |
| 226 | Align = std::max(Align, AA->getAlignment()); |
| 227 | |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 228 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 229 | QualType T = VD->getType(); |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 230 | if (const ReferenceType* RT = T->getAsReferenceType()) { |
| 231 | unsigned AS = RT->getPointeeType().getAddressSpace(); |
Anders Carlsson | f093023 | 2009-04-10 04:52:36 +0000 | [diff] [blame] | 232 | Align = Target.getPointerAlign(AS); |
Anders Carlsson | 4cc2cfd | 2009-04-10 04:47:03 +0000 | [diff] [blame] | 233 | } else if (!T->isIncompleteType() && !T->isFunctionType()) { |
| 234 | // Incomplete or function types default to 1. |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 235 | while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T)) |
| 236 | T = cast<ArrayType>(T)->getElementType(); |
| 237 | |
| 238 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
| 239 | } |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 240 | } |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 241 | |
| 242 | return Align / Target.getCharWidth(); |
Chris Lattner | af707ab | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 243 | } |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 244 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 245 | /// getTypeSize - Return the size of the specified type, in bits. This method |
| 246 | /// does not work on incomplete types. |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 247 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 248 | ASTContext::getTypeInfo(const Type *T) { |
Mike Stump | 5e30100 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 249 | uint64_t Width=0; |
| 250 | unsigned Align=8; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 251 | switch (T->getTypeClass()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 252 | #define TYPE(Class, Base) |
| 253 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 254 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 255 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 256 | #include "clang/AST/TypeNodes.def" |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 257 | assert(false && "Should not see dependent types"); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 258 | break; |
| 259 | |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 260 | case Type::FunctionNoProto: |
| 261 | case Type::FunctionProto: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 262 | // GCC extension: alignof(function) = 32 bits |
| 263 | Width = 0; |
| 264 | Align = 32; |
| 265 | break; |
| 266 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 267 | case Type::IncompleteArray: |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 268 | case Type::VariableArray: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 269 | Width = 0; |
| 270 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 271 | break; |
| 272 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 273 | case Type::ConstantArray: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 274 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 276 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 277 | Width = EltInfo.first*CAT->getSize().getZExtValue(); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 278 | Align = EltInfo.second; |
| 279 | break; |
Christopher Lamb | 5c09a02 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 280 | } |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 281 | case Type::ExtVector: |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 282 | case Type::Vector: { |
| 283 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 284 | getTypeInfo(cast<VectorType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 285 | Width = EltInfo.first*cast<VectorType>(T)->getNumElements(); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 286 | Align = Width; |
Nate Begeman | 6fe7c8a | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 287 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 288 | // This happens for non-power-of-2 length vectors. |
| 289 | // FIXME: this should probably be a target property. |
| 290 | Align = 1 << llvm::Log2_32_Ceil(Align); |
Chris Lattner | 030d884 | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 291 | break; |
| 292 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 294 | case Type::Builtin: |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 295 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 296 | default: assert(0 && "Unknown builtin type!"); |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 297 | case BuiltinType::Void: |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 298 | // GCC extension: alignof(void) = 8 bits. |
| 299 | Width = 0; |
| 300 | Align = 8; |
| 301 | break; |
| 302 | |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 303 | case BuiltinType::Bool: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 304 | Width = Target.getBoolWidth(); |
| 305 | Align = Target.getBoolAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 306 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 307 | case BuiltinType::Char_S: |
| 308 | case BuiltinType::Char_U: |
| 309 | case BuiltinType::UChar: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 310 | case BuiltinType::SChar: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 311 | Width = Target.getCharWidth(); |
| 312 | Align = Target.getCharAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 313 | break; |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 314 | case BuiltinType::WChar: |
| 315 | Width = Target.getWCharWidth(); |
| 316 | Align = Target.getWCharAlign(); |
| 317 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 318 | case BuiltinType::UShort: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 319 | case BuiltinType::Short: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 320 | Width = Target.getShortWidth(); |
| 321 | Align = Target.getShortAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 322 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 323 | case BuiltinType::UInt: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 324 | case BuiltinType::Int: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 325 | Width = Target.getIntWidth(); |
| 326 | Align = Target.getIntAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 327 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 328 | case BuiltinType::ULong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 329 | case BuiltinType::Long: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 330 | Width = Target.getLongWidth(); |
| 331 | Align = Target.getLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 332 | break; |
Chris Lattner | 692233e | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 333 | case BuiltinType::ULongLong: |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 334 | case BuiltinType::LongLong: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 335 | Width = Target.getLongLongWidth(); |
| 336 | Align = Target.getLongLongAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 337 | break; |
Chris Lattner | ec16cb9 | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 338 | case BuiltinType::Int128: |
| 339 | case BuiltinType::UInt128: |
| 340 | Width = 128; |
| 341 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 342 | break; |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 343 | case BuiltinType::Float: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 344 | Width = Target.getFloatWidth(); |
| 345 | Align = Target.getFloatAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 346 | break; |
| 347 | case BuiltinType::Double: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 348 | Width = Target.getDoubleWidth(); |
| 349 | Align = Target.getDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 350 | break; |
| 351 | case BuiltinType::LongDouble: |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 352 | Width = Target.getLongDoubleWidth(); |
| 353 | Align = Target.getLongDoubleAlign(); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 354 | break; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 355 | case BuiltinType::NullPtr: |
| 356 | Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 357 | Align = Target.getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 358 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 359 | } |
Chris Lattner | bfef6d7 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 360 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 361 | case Type::FixedWidthInt: |
| 362 | // FIXME: This isn't precisely correct; the width/alignment should depend |
| 363 | // on the available types for the target |
| 364 | Width = cast<FixedWidthIntType>(T)->getWidth(); |
Chris Lattner | 736166b | 2009-02-15 21:20:13 +0000 | [diff] [blame] | 365 | Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 366 | Align = Width; |
| 367 | break; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 368 | case Type::ExtQual: |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 369 | // FIXME: Pointers into different addr spaces could have different sizes and |
| 370 | // alignment requirements: getPointerInfo should take an AddrSpace. |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 371 | return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0)); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 372 | case Type::ObjCQualifiedId: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 373 | case Type::ObjCQualifiedInterface: |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 374 | Width = Target.getPointerWidth(0); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 375 | Align = Target.getPointerAlign(0); |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 376 | break; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 377 | case Type::BlockPointer: { |
| 378 | unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace(); |
| 379 | Width = Target.getPointerWidth(AS); |
| 380 | Align = Target.getPointerAlign(AS); |
| 381 | break; |
| 382 | } |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 383 | case Type::Pointer: { |
| 384 | unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace(); |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 385 | Width = Target.getPointerWidth(AS); |
Chris Lattner | f72a443 | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 386 | Align = Target.getPointerAlign(AS); |
| 387 | break; |
| 388 | } |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 389 | case Type::LValueReference: |
| 390 | case Type::RValueReference: |
Chris Lattner | 7ab2ed8 | 2007-07-13 22:16:13 +0000 | [diff] [blame] | 391 | // "When applied to a reference or a reference type, the result is the size |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 392 | // of the referenced type." C++98 5.3.3p2: expr.sizeof. |
Chris Lattner | 6f62c2a | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 393 | // FIXME: This is wrong for struct layout: a reference in a struct has |
| 394 | // pointer size. |
Chris Lattner | bdcd637 | 2008-04-02 17:35:06 +0000 | [diff] [blame] | 395 | return getTypeInfo(cast<ReferenceType>(T)->getPointeeType()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 396 | case Type::MemberPointer: { |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 397 | // FIXME: This is ABI dependent. We use the Itanium C++ ABI. |
| 398 | // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers |
| 399 | // If we ever want to support other ABIs this needs to be abstracted. |
| 400 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 401 | QualType Pointee = cast<MemberPointerType>(T)->getPointeeType(); |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 402 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
| 403 | getTypeInfo(getPointerDiffType()); |
| 404 | Width = PtrDiffInfo.first; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 405 | if (Pointee->isFunctionType()) |
| 406 | Width *= 2; |
Anders Carlsson | 1cca74e | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 407 | Align = PtrDiffInfo.second; |
| 408 | break; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 409 | } |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 410 | case Type::Complex: { |
| 411 | // Complex types have the same alignment as their elements, but twice the |
| 412 | // size. |
| 413 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 414 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 415 | Width = EltInfo.first*2; |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 416 | Align = EltInfo.second; |
| 417 | break; |
| 418 | } |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 419 | case Type::ObjCInterface: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 420 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 421 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 422 | Width = Layout.getSize(); |
| 423 | Align = Layout.getAlignment(); |
| 424 | break; |
| 425 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 426 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 427 | case Type::Enum: { |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 428 | const TagType *TT = cast<TagType>(T); |
| 429 | |
| 430 | if (TT->getDecl()->isInvalidDecl()) { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 431 | Width = 1; |
| 432 | Align = 1; |
| 433 | break; |
| 434 | } |
| 435 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 436 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 437 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 438 | |
Daniel Dunbar | 1d75118 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 439 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 440 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
| 441 | Width = Layout.getSize(); |
| 442 | Align = Layout.getAlignment(); |
Chris Lattner | dc0d73e | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 443 | break; |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 444 | } |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 445 | |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 446 | case Type::Typedef: { |
| 447 | const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
| 448 | if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) { |
| 449 | Align = Aligned->getAlignment(); |
| 450 | Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr()); |
| 451 | } else |
| 452 | return getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 453 | break; |
Chris Lattner | 7176331 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 454 | } |
Douglas Gregor | 1885764 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 455 | |
| 456 | case Type::TypeOfExpr: |
| 457 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 458 | .getTypePtr()); |
| 459 | |
| 460 | case Type::TypeOf: |
| 461 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 462 | |
| 463 | case Type::QualifiedName: |
| 464 | return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr()); |
| 465 | |
| 466 | case Type::TemplateSpecialization: |
| 467 | assert(getCanonicalType(T) != T && |
| 468 | "Cannot request the size of a dependent type"); |
| 469 | // FIXME: this is likely to be wrong once we support template |
| 470 | // aliases, since a template alias could refer to a typedef that |
| 471 | // has an __aligned__ attribute on it. |
| 472 | return getTypeInfo(getCanonicalType(T)); |
| 473 | } |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 474 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 475 | assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2"); |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 476 | return std::make_pair(Width, Align); |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 479 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 480 | /// type for the current target in bits. This can be different than the ABI |
| 481 | /// alignment in cases where it is beneficial for performance to overalign |
| 482 | /// a data type. |
| 483 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) { |
| 484 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 1eed602 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 485 | |
| 486 | // Double and long long should be naturally aligned if possible. |
| 487 | if (const ComplexType* CT = T->getAsComplexType()) |
| 488 | T = CT->getElementType().getTypePtr(); |
| 489 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
| 490 | T->isSpecificBuiltinType(BuiltinType::LongLong)) |
| 491 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 492 | |
Chris Lattner | 34ebde4 | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 493 | return ABIAlign; |
| 494 | } |
| 495 | |
| 496 | |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 497 | /// LayoutField - Field layout. |
| 498 | void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo, |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 499 | bool IsUnion, unsigned StructPacking, |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 500 | ASTContext &Context) { |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 501 | unsigned FieldPacking = StructPacking; |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 502 | uint64_t FieldOffset = IsUnion ? 0 : Size; |
| 503 | uint64_t FieldSize; |
| 504 | unsigned FieldAlign; |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 505 | |
| 506 | // FIXME: Should this override struct packing? Probably we want to |
| 507 | // take the minimum? |
| 508 | if (const PackedAttr *PA = FD->getAttr<PackedAttr>()) |
| 509 | FieldPacking = PA->getAlignment(); |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 510 | |
| 511 | if (const Expr *BitWidthExpr = FD->getBitWidth()) { |
| 512 | // TODO: Need to check this algorithm on other targets! |
| 513 | // (tested on Linux-X86) |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 514 | FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue(); |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 515 | |
| 516 | std::pair<uint64_t, unsigned> FieldInfo = |
| 517 | Context.getTypeInfo(FD->getType()); |
| 518 | uint64_t TypeSize = FieldInfo.first; |
| 519 | |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 520 | // Determine the alignment of this bitfield. The packing |
| 521 | // attributes define a maximum and the alignment attribute defines |
| 522 | // a minimum. |
| 523 | // FIXME: What is the right behavior when the specified alignment |
| 524 | // is smaller than the specified packing? |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 525 | FieldAlign = FieldInfo.second; |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 526 | if (FieldPacking) |
| 527 | FieldAlign = std::min(FieldAlign, FieldPacking); |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 528 | if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>()) |
| 529 | FieldAlign = std::max(FieldAlign, AA->getAlignment()); |
| 530 | |
| 531 | // Check if we need to add padding to give the field the correct |
| 532 | // alignment. |
| 533 | if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize) |
| 534 | FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); |
| 535 | |
| 536 | // Padding members don't affect overall alignment |
| 537 | if (!FD->getIdentifier()) |
| 538 | FieldAlign = 1; |
| 539 | } else { |
Chris Lattner | 8389eab | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 540 | if (FD->getType()->isIncompleteArrayType()) { |
| 541 | // This is a flexible array member; we can't directly |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 542 | // query getTypeInfo about these, so we figure it out here. |
| 543 | // Flexible array members don't have any size, but they |
| 544 | // have to be aligned appropriately for their element type. |
| 545 | FieldSize = 0; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 546 | const ArrayType* ATy = Context.getAsArrayType(FD->getType()); |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 547 | FieldAlign = Context.getTypeAlign(ATy->getElementType()); |
Anders Carlsson | 2f1169f | 2009-04-10 05:31:15 +0000 | [diff] [blame] | 548 | } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) { |
| 549 | unsigned AS = RT->getPointeeType().getAddressSpace(); |
| 550 | FieldSize = Context.Target.getPointerWidth(AS); |
| 551 | FieldAlign = Context.Target.getPointerAlign(AS); |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 552 | } else { |
| 553 | std::pair<uint64_t, unsigned> FieldInfo = |
| 554 | Context.getTypeInfo(FD->getType()); |
| 555 | FieldSize = FieldInfo.first; |
| 556 | FieldAlign = FieldInfo.second; |
| 557 | } |
| 558 | |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 559 | // Determine the alignment of this bitfield. The packing |
| 560 | // attributes define a maximum and the alignment attribute defines |
| 561 | // a minimum. Additionally, the packing alignment must be at least |
| 562 | // a byte for non-bitfields. |
| 563 | // |
| 564 | // FIXME: What is the right behavior when the specified alignment |
| 565 | // is smaller than the specified packing? |
| 566 | if (FieldPacking) |
| 567 | FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking)); |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 568 | if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>()) |
| 569 | FieldAlign = std::max(FieldAlign, AA->getAlignment()); |
| 570 | |
| 571 | // Round up the current record size to the field's alignment boundary. |
| 572 | FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1); |
| 573 | } |
| 574 | |
| 575 | // Place this field at the current location. |
| 576 | FieldOffsets[FieldNo] = FieldOffset; |
| 577 | |
| 578 | // Reserve space for this field. |
| 579 | if (IsUnion) { |
| 580 | Size = std::max(Size, FieldSize); |
| 581 | } else { |
| 582 | Size = FieldOffset + FieldSize; |
| 583 | } |
| 584 | |
Daniel Dunbar | d6884a0 | 2009-05-04 05:16:21 +0000 | [diff] [blame] | 585 | // Remember the next available offset. |
| 586 | NextOffset = Size; |
| 587 | |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 588 | // Remember max struct/class alignment. |
| 589 | Alignment = std::max(Alignment, FieldAlign); |
| 590 | } |
| 591 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 592 | static void CollectLocalObjCIvars(ASTContext *Ctx, |
| 593 | const ObjCInterfaceDecl *OI, |
| 594 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 595 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 596 | E = OI->ivar_end(); I != E; ++I) { |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 597 | ObjCIvarDecl *IVDecl = *I; |
Fariborz Jahanian | a769c00 | 2008-12-17 21:40:49 +0000 | [diff] [blame] | 598 | if (!IVDecl->isInvalidDecl()) |
| 599 | Fields.push_back(cast<FieldDecl>(IVDecl)); |
| 600 | } |
| 601 | } |
| 602 | |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 603 | void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 604 | llvm::SmallVectorImpl<FieldDecl*> &Fields) { |
| 605 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 606 | CollectObjCIvars(SuperClass, Fields); |
| 607 | CollectLocalObjCIvars(this, OI, Fields); |
| 608 | } |
| 609 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 610 | /// ShallowCollectObjCIvars - |
| 611 | /// Collect all ivars, including those synthesized, in the current class. |
| 612 | /// |
| 613 | void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 614 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars, |
| 615 | bool CollectSynthesized) { |
| 616 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 617 | E = OI->ivar_end(); I != E; ++I) { |
| 618 | Ivars.push_back(*I); |
| 619 | } |
| 620 | if (CollectSynthesized) |
| 621 | CollectSynthesizedIvars(OI, Ivars); |
| 622 | } |
| 623 | |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 624 | void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD, |
| 625 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
| 626 | for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(*this), |
| 627 | E = PD->prop_end(*this); I != E; ++I) |
| 628 | if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) |
| 629 | Ivars.push_back(Ivar); |
| 630 | |
| 631 | // Also look into nested protocols. |
| 632 | for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), |
| 633 | E = PD->protocol_end(); P != E; ++P) |
| 634 | CollectProtocolSynthesizedIvars(*P, Ivars); |
| 635 | } |
| 636 | |
| 637 | /// CollectSynthesizedIvars - |
| 638 | /// This routine collect synthesized ivars for the designated class. |
| 639 | /// |
| 640 | void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI, |
| 641 | llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
| 642 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this), |
| 643 | E = OI->prop_end(*this); I != E; ++I) { |
| 644 | if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl()) |
| 645 | Ivars.push_back(Ivar); |
| 646 | } |
| 647 | // Also look into interface's protocol list for properties declared |
| 648 | // in the protocol and whose ivars are synthesized. |
| 649 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 650 | PE = OI->protocol_end(); P != PE; ++P) { |
| 651 | ObjCProtocolDecl *PD = (*P); |
| 652 | CollectProtocolSynthesizedIvars(PD, Ivars); |
| 653 | } |
| 654 | } |
| 655 | |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 656 | unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) { |
| 657 | unsigned count = 0; |
| 658 | for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(*this), |
| 659 | E = PD->prop_end(*this); I != E; ++I) |
| 660 | if ((*I)->getPropertyIvarDecl()) |
| 661 | ++count; |
| 662 | |
| 663 | // Also look into nested protocols. |
| 664 | for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(), |
| 665 | E = PD->protocol_end(); P != E; ++P) |
| 666 | count += CountProtocolSynthesizedIvars(*P); |
| 667 | return count; |
| 668 | } |
| 669 | |
| 670 | unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI) |
| 671 | { |
| 672 | unsigned count = 0; |
| 673 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this), |
| 674 | E = OI->prop_end(*this); I != E; ++I) { |
| 675 | if ((*I)->getPropertyIvarDecl()) |
| 676 | ++count; |
| 677 | } |
| 678 | // Also look into interface's protocol list for properties declared |
| 679 | // in the protocol and whose ivars are synthesized. |
| 680 | for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(), |
| 681 | PE = OI->protocol_end(); P != PE; ++P) { |
| 682 | ObjCProtocolDecl *PD = (*P); |
| 683 | count += CountProtocolSynthesizedIvars(PD); |
| 684 | } |
| 685 | return count; |
| 686 | } |
| 687 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 688 | /// getInterfaceLayoutImpl - Get or compute information about the |
| 689 | /// layout of the given interface. |
| 690 | /// |
| 691 | /// \param Impl - If given, also include the layout of the interface's |
| 692 | /// implementation. This may differ by including synthesized ivars. |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 693 | const ASTRecordLayout & |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 694 | ASTContext::getObjCLayout(const ObjCInterfaceDecl *D, |
| 695 | const ObjCImplementationDecl *Impl) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 696 | assert(!D->isForwardDecl() && "Invalid interface decl!"); |
| 697 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 698 | // Look up this layout, if already laid out, return what we have. |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 699 | ObjCContainerDecl *Key = |
| 700 | Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D; |
| 701 | if (const ASTRecordLayout *Entry = ObjCLayouts[Key]) |
| 702 | return *Entry; |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 703 | |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 704 | unsigned FieldCount = D->ivar_size(); |
| 705 | // Add in synthesized ivar count if laying out an implementation. |
| 706 | if (Impl) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 707 | unsigned SynthCount = CountSynthesizedIvars(D); |
| 708 | FieldCount += SynthCount; |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 709 | // If there aren't any sythesized ivars then reuse the interface |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 710 | // entry. Note we can't cache this because we simply free all |
| 711 | // entries later; however we shouldn't look up implementations |
| 712 | // frequently. |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 713 | if (SynthCount == 0) |
Daniel Dunbar | 453addb | 2009-05-03 11:16:44 +0000 | [diff] [blame] | 714 | return getObjCLayout(D, 0); |
| 715 | } |
| 716 | |
Devang Patel | 6a5a34c | 2008-06-06 02:14:01 +0000 | [diff] [blame] | 717 | ASTRecordLayout *NewEntry = NULL; |
Devang Patel | 6a5a34c | 2008-06-06 02:14:01 +0000 | [diff] [blame] | 718 | if (ObjCInterfaceDecl *SD = D->getSuperClass()) { |
Devang Patel | 6a5a34c | 2008-06-06 02:14:01 +0000 | [diff] [blame] | 719 | const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD); |
| 720 | unsigned Alignment = SL.getAlignment(); |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 721 | |
Daniel Dunbar | 913af35 | 2009-05-07 21:58:26 +0000 | [diff] [blame] | 722 | // We start laying out ivars not at the end of the superclass |
| 723 | // structure, but at the next byte following the last field. |
| 724 | uint64_t Size = llvm::RoundUpToAlignment(SL.NextOffset, 8); |
Daniel Dunbar | d6884a0 | 2009-05-04 05:16:21 +0000 | [diff] [blame] | 725 | |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 726 | ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment); |
Devang Patel | 6a5a34c | 2008-06-06 02:14:01 +0000 | [diff] [blame] | 727 | NewEntry->InitializeLayout(FieldCount); |
Devang Patel | 6a5a34c | 2008-06-06 02:14:01 +0000 | [diff] [blame] | 728 | } else { |
Daniel Dunbar | d8fd6ff | 2009-05-03 11:41:43 +0000 | [diff] [blame] | 729 | ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(); |
Devang Patel | 6a5a34c | 2008-06-06 02:14:01 +0000 | [diff] [blame] | 730 | NewEntry->InitializeLayout(FieldCount); |
| 731 | } |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 732 | |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 733 | unsigned StructPacking = 0; |
| 734 | if (const PackedAttr *PA = D->getAttr<PackedAttr>()) |
| 735 | StructPacking = PA->getAlignment(); |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 736 | |
| 737 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
| 738 | NewEntry->SetAlignment(std::max(NewEntry->getAlignment(), |
| 739 | AA->getAlignment())); |
| 740 | |
| 741 | // Layout each ivar sequentially. |
| 742 | unsigned i = 0; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 743 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
| 744 | ShallowCollectObjCIvars(D, Ivars, Impl); |
| 745 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) |
| 746 | NewEntry->LayoutField(Ivars[k], i++, false, StructPacking, *this); |
| 747 | |
Devang Patel | 44a3dde | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 748 | // Finally, round the size of the total struct up to the alignment of the |
| 749 | // struct itself. |
| 750 | NewEntry->FinalizeLayout(); |
| 751 | return *NewEntry; |
| 752 | } |
| 753 | |
Daniel Dunbar | b2dbbb9 | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 754 | const ASTRecordLayout & |
| 755 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) { |
| 756 | return getObjCLayout(D, 0); |
| 757 | } |
| 758 | |
| 759 | const ASTRecordLayout & |
| 760 | ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) { |
| 761 | return getObjCLayout(D->getClassInterface(), D); |
| 762 | } |
| 763 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 764 | /// getASTRecordLayout - Get or compute information about the layout of the |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 765 | /// specified record (struct/union/class), which indicates its size and field |
| 766 | /// position information. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 767 | const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) { |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 768 | D = D->getDefinition(*this); |
| 769 | assert(D && "Cannot get layout of forward declarations!"); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 770 | |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 771 | // Look up this layout, if already laid out, return what we have. |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 772 | const ASTRecordLayout *&Entry = ASTRecordLayouts[D]; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 773 | if (Entry) return *Entry; |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 774 | |
Devang Patel | 88a981b | 2007-11-01 19:11:01 +0000 | [diff] [blame] | 775 | // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can |
| 776 | // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into. |
| 777 | ASTRecordLayout *NewEntry = new ASTRecordLayout(); |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 778 | Entry = NewEntry; |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 779 | |
Douglas Gregor | e267ff3 | 2008-12-11 20:41:00 +0000 | [diff] [blame] | 780 | // FIXME: Avoid linear walk through the fields, if possible. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 781 | NewEntry->InitializeLayout(std::distance(D->field_begin(*this), |
| 782 | D->field_end(*this))); |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 783 | bool IsUnion = D->isUnion(); |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 784 | |
Daniel Dunbar | 3b0db90 | 2008-10-16 02:34:03 +0000 | [diff] [blame] | 785 | unsigned StructPacking = 0; |
| 786 | if (const PackedAttr *PA = D->getAttr<PackedAttr>()) |
| 787 | StructPacking = PA->getAlignment(); |
| 788 | |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 789 | if (const AlignedAttr *AA = D->getAttr<AlignedAttr>()) |
Devang Patel | 8b27704 | 2008-06-04 21:22:16 +0000 | [diff] [blame] | 790 | NewEntry->SetAlignment(std::max(NewEntry->getAlignment(), |
| 791 | AA->getAlignment())); |
Anders Carlsson | 8af226a | 2008-02-18 07:13:09 +0000 | [diff] [blame] | 792 | |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 793 | // Layout each field, for now, just sequentially, respecting alignment. In |
| 794 | // the future, this will need to be tweakable by targets. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 795 | unsigned FieldIdx = 0; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 796 | for (RecordDecl::field_iterator Field = D->field_begin(*this), |
| 797 | FieldEnd = D->field_end(*this); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 798 | Field != FieldEnd; (void)++Field, ++FieldIdx) |
| 799 | NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this); |
Eli Friedman | 4bd998b | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 800 | |
| 801 | // Finally, round the size of the total struct up to the alignment of the |
| 802 | // struct itself. |
Sebastian Redl | 1590d9c | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 803 | NewEntry->FinalizeLayout(getLangOptions().CPlusPlus); |
Chris Lattner | 5d2a630 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 804 | return *NewEntry; |
Chris Lattner | 464175b | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 807 | //===----------------------------------------------------------------------===// |
| 808 | // Type creation/memoization methods |
| 809 | //===----------------------------------------------------------------------===// |
| 810 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 811 | QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 812 | QualType CanT = getCanonicalType(T); |
| 813 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 814 | return T; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 815 | |
| 816 | // If we are composing extended qualifiers together, merge together into one |
| 817 | // ExtQualType node. |
| 818 | unsigned CVRQuals = T.getCVRQualifiers(); |
| 819 | QualType::GCAttrTypes GCAttr = QualType::GCNone; |
| 820 | Type *TypeNode = T.getTypePtr(); |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 821 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 822 | if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) { |
| 823 | // If this type already has an address space specified, it cannot get |
| 824 | // another one. |
| 825 | assert(EQT->getAddressSpace() == 0 && |
| 826 | "Type cannot be in multiple addr spaces!"); |
| 827 | GCAttr = EQT->getObjCGCAttr(); |
| 828 | TypeNode = EQT->getBaseType(); |
| 829 | } |
Chris Lattner | f46699c | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 830 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 831 | // Check if we've already instantiated this type. |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 832 | llvm::FoldingSetNodeID ID; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 833 | ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 834 | void *InsertPos = 0; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 835 | if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 836 | return QualType(EXTQy, CVRQuals); |
| 837 | |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 838 | // If the base type isn't canonical, this won't be a canonical type either, |
| 839 | // so fill in the canonical type field. |
| 840 | QualType Canonical; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 841 | if (!TypeNode->isCanonical()) { |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 842 | Canonical = getAddrSpaceQualType(CanT, AddressSpace); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 843 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 844 | // Update InsertPos, the previous call could have invalidated it. |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 845 | ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 846 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 847 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 848 | ExtQualType *New = |
| 849 | new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr); |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 850 | ExtQualTypes.InsertNode(New, InsertPos); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 851 | Types.push_back(New); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 852 | return QualType(New, CVRQuals); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 855 | QualType ASTContext::getObjCGCQualType(QualType T, |
| 856 | QualType::GCAttrTypes GCAttr) { |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 857 | QualType CanT = getCanonicalType(T); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 858 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 859 | return T; |
| 860 | |
Fariborz Jahanian | 4027cd1 | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 861 | if (T->isPointerType()) { |
| 862 | QualType Pointee = T->getAsPointerType()->getPointeeType(); |
| 863 | if (Pointee->isPointerType()) { |
| 864 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 865 | return getPointerType(ResultType); |
| 866 | } |
| 867 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 868 | // If we are composing extended qualifiers together, merge together into one |
| 869 | // ExtQualType node. |
| 870 | unsigned CVRQuals = T.getCVRQualifiers(); |
| 871 | Type *TypeNode = T.getTypePtr(); |
| 872 | unsigned AddressSpace = 0; |
| 873 | |
| 874 | if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) { |
| 875 | // If this type already has an address space specified, it cannot get |
| 876 | // another one. |
| 877 | assert(EQT->getObjCGCAttr() == QualType::GCNone && |
| 878 | "Type cannot be in multiple addr spaces!"); |
| 879 | AddressSpace = EQT->getAddressSpace(); |
| 880 | TypeNode = EQT->getBaseType(); |
| 881 | } |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 882 | |
| 883 | // Check if we've already instantiated an gc qual'd type of this type. |
| 884 | llvm::FoldingSetNodeID ID; |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 885 | ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 886 | void *InsertPos = 0; |
| 887 | if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 888 | return QualType(EXTQy, CVRQuals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 889 | |
| 890 | // If the base type isn't canonical, this won't be a canonical type either, |
| 891 | // so fill in the canonical type field. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 892 | // FIXME: Isn't this also not canonical if the base type is a array |
| 893 | // or pointer type? I can't find any documentation for objc_gc, though... |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 894 | QualType Canonical; |
| 895 | if (!T->isCanonical()) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 896 | Canonical = getObjCGCQualType(CanT, GCAttr); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 897 | |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 898 | // Update InsertPos, the previous call could have invalidated it. |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 899 | ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 900 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 901 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 902 | ExtQualType *New = |
| 903 | new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 904 | ExtQualTypes.InsertNode(New, InsertPos); |
| 905 | Types.push_back(New); |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 906 | return QualType(New, CVRQuals); |
Fariborz Jahanian | d33d9c0 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 907 | } |
Chris Lattner | a7674d8 | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 908 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 909 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 910 | /// number with the specified element type. |
| 911 | QualType ASTContext::getComplexType(QualType T) { |
| 912 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 913 | // structure. |
| 914 | llvm::FoldingSetNodeID ID; |
| 915 | ComplexType::Profile(ID, T); |
| 916 | |
| 917 | void *InsertPos = 0; |
| 918 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 919 | return QualType(CT, 0); |
| 920 | |
| 921 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 922 | // so fill in the canonical type field. |
| 923 | QualType Canonical; |
| 924 | if (!T->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 925 | Canonical = getComplexType(getCanonicalType(T)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 926 | |
| 927 | // Get the new insert position for the node we care about. |
| 928 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 929 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 930 | } |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 931 | ComplexType *New = new (*this,8) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 932 | Types.push_back(New); |
| 933 | ComplexTypes.InsertNode(New, InsertPos); |
| 934 | return QualType(New, 0); |
| 935 | } |
| 936 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 937 | QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) { |
| 938 | llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ? |
| 939 | SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes; |
| 940 | FixedWidthIntType *&Entry = Map[Width]; |
| 941 | if (!Entry) |
| 942 | Entry = new FixedWidthIntType(Width, Signed); |
| 943 | return QualType(Entry, 0); |
| 944 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 945 | |
| 946 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 947 | /// the specified type. |
| 948 | QualType ASTContext::getPointerType(QualType T) { |
| 949 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 950 | // structure. |
| 951 | llvm::FoldingSetNodeID ID; |
| 952 | PointerType::Profile(ID, T); |
| 953 | |
| 954 | void *InsertPos = 0; |
| 955 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 956 | return QualType(PT, 0); |
| 957 | |
| 958 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 959 | // so fill in the canonical type field. |
| 960 | QualType Canonical; |
| 961 | if (!T->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 962 | Canonical = getPointerType(getCanonicalType(T)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 963 | |
| 964 | // Get the new insert position for the node we care about. |
| 965 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 966 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 967 | } |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 968 | PointerType *New = new (*this,8) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 969 | Types.push_back(New); |
| 970 | PointerTypes.InsertNode(New, InsertPos); |
| 971 | return QualType(New, 0); |
| 972 | } |
| 973 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 974 | /// getBlockPointerType - Return the uniqued reference to the type for |
| 975 | /// a pointer to the specified block. |
| 976 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 977 | assert(T->isFunctionType() && "block of function types only"); |
| 978 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 979 | // structure. |
| 980 | llvm::FoldingSetNodeID ID; |
| 981 | BlockPointerType::Profile(ID, T); |
| 982 | |
| 983 | void *InsertPos = 0; |
| 984 | if (BlockPointerType *PT = |
| 985 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 986 | return QualType(PT, 0); |
| 987 | |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 988 | // 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] | 989 | // type either so fill in the canonical type field. |
| 990 | QualType Canonical; |
| 991 | if (!T->isCanonical()) { |
| 992 | Canonical = getBlockPointerType(getCanonicalType(T)); |
| 993 | |
| 994 | // Get the new insert position for the node we care about. |
| 995 | BlockPointerType *NewIP = |
| 996 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 997 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 998 | } |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 999 | BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1000 | Types.push_back(New); |
| 1001 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1002 | return QualType(New, 0); |
| 1003 | } |
| 1004 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1005 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1006 | /// lvalue reference to the specified type. |
| 1007 | QualType ASTContext::getLValueReferenceType(QualType T) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1008 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1009 | // structure. |
| 1010 | llvm::FoldingSetNodeID ID; |
| 1011 | ReferenceType::Profile(ID, T); |
| 1012 | |
| 1013 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1014 | if (LValueReferenceType *RT = |
| 1015 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1016 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1017 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1018 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1019 | // either, so fill in the canonical type field. |
| 1020 | QualType Canonical; |
| 1021 | if (!T->isCanonical()) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1022 | Canonical = getLValueReferenceType(getCanonicalType(T)); |
| 1023 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1024 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1025 | LValueReferenceType *NewIP = |
| 1026 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1027 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1030 | LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1031 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1032 | LValueReferenceTypes.InsertNode(New, InsertPos); |
| 1033 | return QualType(New, 0); |
| 1034 | } |
| 1035 | |
| 1036 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1037 | /// rvalue reference to the specified type. |
| 1038 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1039 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1040 | // structure. |
| 1041 | llvm::FoldingSetNodeID ID; |
| 1042 | ReferenceType::Profile(ID, T); |
| 1043 | |
| 1044 | void *InsertPos = 0; |
| 1045 | if (RValueReferenceType *RT = |
| 1046 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1047 | return QualType(RT, 0); |
| 1048 | |
| 1049 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1050 | // either, so fill in the canonical type field. |
| 1051 | QualType Canonical; |
| 1052 | if (!T->isCanonical()) { |
| 1053 | Canonical = getRValueReferenceType(getCanonicalType(T)); |
| 1054 | |
| 1055 | // Get the new insert position for the node we care about. |
| 1056 | RValueReferenceType *NewIP = |
| 1057 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1058 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1059 | } |
| 1060 | |
| 1061 | RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical); |
| 1062 | Types.push_back(New); |
| 1063 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1064 | return QualType(New, 0); |
| 1065 | } |
| 1066 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1067 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1068 | /// member pointer to the specified type, in the specified class. |
| 1069 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) |
| 1070 | { |
| 1071 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1072 | // structure. |
| 1073 | llvm::FoldingSetNodeID ID; |
| 1074 | MemberPointerType::Profile(ID, T, Cls); |
| 1075 | |
| 1076 | void *InsertPos = 0; |
| 1077 | if (MemberPointerType *PT = |
| 1078 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1079 | return QualType(PT, 0); |
| 1080 | |
| 1081 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1082 | // type either, so fill in the canonical type field. |
| 1083 | QualType Canonical; |
| 1084 | if (!T->isCanonical()) { |
| 1085 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1086 | |
| 1087 | // Get the new insert position for the node we care about. |
| 1088 | MemberPointerType *NewIP = |
| 1089 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1090 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1091 | } |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1092 | MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1093 | Types.push_back(New); |
| 1094 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1095 | return QualType(New, 0); |
| 1096 | } |
| 1097 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1098 | /// getConstantArrayType - Return the unique reference to the type for an |
| 1099 | /// array of the specified element type. |
| 1100 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1101 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1102 | ArrayType::ArraySizeModifier ASM, |
| 1103 | unsigned EltTypeQuals) { |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1104 | assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) && |
| 1105 | "Constant array of VLAs is illegal!"); |
| 1106 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1107 | // Convert the array size into a canonical width matching the pointer size for |
| 1108 | // the target. |
| 1109 | llvm::APInt ArySize(ArySizeIn); |
| 1110 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
| 1111 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1112 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1113 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1114 | |
| 1115 | void *InsertPos = 0; |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1116 | if (ConstantArrayType *ATP = |
| 1117 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1118 | return QualType(ATP, 0); |
| 1119 | |
| 1120 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1121 | // so fill in the canonical type field. |
| 1122 | QualType Canonical; |
| 1123 | if (!EltTy->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1124 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1125 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1126 | // Get the new insert position for the node we care about. |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1127 | ConstantArrayType *NewIP = |
| 1128 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1129 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1132 | ConstantArrayType *New = |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1133 | new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1134 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1135 | Types.push_back(New); |
| 1136 | return QualType(New, 0); |
| 1137 | } |
| 1138 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1139 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1140 | /// variable array of the specified element type. |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1141 | QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts, |
| 1142 | ArrayType::ArraySizeModifier ASM, |
| 1143 | unsigned EltTypeQuals) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1144 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1145 | // that have an expression provided for their size. |
| 1146 | |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1147 | VariableArrayType *New = |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1148 | new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1149 | |
| 1150 | VariableArrayTypes.push_back(New); |
| 1151 | Types.push_back(New); |
| 1152 | return QualType(New, 0); |
| 1153 | } |
| 1154 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1155 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1156 | /// the type for a dependently-sized array of the specified element |
| 1157 | /// type. FIXME: We will need these to be uniqued, or at least |
| 1158 | /// comparable, at some point. |
| 1159 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts, |
| 1160 | ArrayType::ArraySizeModifier ASM, |
| 1161 | unsigned EltTypeQuals) { |
| 1162 | assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) && |
| 1163 | "Size must be type- or value-dependent!"); |
| 1164 | |
| 1165 | // Since we don't unique expressions, it isn't possible to unique |
| 1166 | // dependently-sized array types. |
| 1167 | |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1168 | DependentSizedArrayType *New = |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1169 | new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts, |
| 1170 | ASM, EltTypeQuals); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1171 | |
| 1172 | DependentSizedArrayTypes.push_back(New); |
| 1173 | Types.push_back(New); |
| 1174 | return QualType(New, 0); |
| 1175 | } |
| 1176 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1177 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1178 | ArrayType::ArraySizeModifier ASM, |
| 1179 | unsigned EltTypeQuals) { |
| 1180 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1181 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1182 | |
| 1183 | void *InsertPos = 0; |
| 1184 | if (IncompleteArrayType *ATP = |
| 1185 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1186 | return QualType(ATP, 0); |
| 1187 | |
| 1188 | // If the element type isn't canonical, this won't be a canonical type |
| 1189 | // either, so fill in the canonical type field. |
| 1190 | QualType Canonical; |
| 1191 | |
| 1192 | if (!EltTy->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1193 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1194 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1195 | |
| 1196 | // Get the new insert position for the node we care about. |
| 1197 | IncompleteArrayType *NewIP = |
| 1198 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1199 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1200 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1201 | |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1202 | IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical, |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1203 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1204 | |
| 1205 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1206 | Types.push_back(New); |
| 1207 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1210 | /// getVectorType - Return the unique reference to a vector type of |
| 1211 | /// the specified element type and size. VectorType must be a built-in type. |
| 1212 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1213 | BuiltinType *baseType; |
| 1214 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1215 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1216 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1217 | |
| 1218 | // Check if we've already instantiated a vector of this type. |
| 1219 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1220 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1221 | void *InsertPos = 0; |
| 1222 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1223 | return QualType(VTP, 0); |
| 1224 | |
| 1225 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1226 | // so fill in the canonical type field. |
| 1227 | QualType Canonical; |
| 1228 | if (!vecType->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1229 | Canonical = getVectorType(getCanonicalType(vecType), NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1230 | |
| 1231 | // Get the new insert position for the node we care about. |
| 1232 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1233 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1234 | } |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1235 | VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1236 | VectorTypes.InsertNode(New, InsertPos); |
| 1237 | Types.push_back(New); |
| 1238 | return QualType(New, 0); |
| 1239 | } |
| 1240 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1241 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1242 | /// 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] | 1243 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1244 | BuiltinType *baseType; |
| 1245 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1246 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1247 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1248 | |
| 1249 | // Check if we've already instantiated a vector of this type. |
| 1250 | llvm::FoldingSetNodeID ID; |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1251 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1252 | void *InsertPos = 0; |
| 1253 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1254 | return QualType(VTP, 0); |
| 1255 | |
| 1256 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1257 | // so fill in the canonical type field. |
| 1258 | QualType Canonical; |
| 1259 | if (!vecType->isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1260 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1261 | |
| 1262 | // Get the new insert position for the node we care about. |
| 1263 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1264 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1265 | } |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1266 | ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1267 | VectorTypes.InsertNode(New, InsertPos); |
| 1268 | Types.push_back(New); |
| 1269 | return QualType(New, 0); |
| 1270 | } |
| 1271 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1272 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1273 | /// |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1274 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1275 | // Unique functions, to guarantee there is only one function of a particular |
| 1276 | // structure. |
| 1277 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1278 | FunctionNoProtoType::Profile(ID, ResultTy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1279 | |
| 1280 | void *InsertPos = 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1281 | if (FunctionNoProtoType *FT = |
| 1282 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1283 | return QualType(FT, 0); |
| 1284 | |
| 1285 | QualType Canonical; |
| 1286 | if (!ResultTy->isCanonical()) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1287 | Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1288 | |
| 1289 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1290 | FunctionNoProtoType *NewIP = |
| 1291 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1292 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1295 | FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1296 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1297 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1298 | return QualType(New, 0); |
| 1299 | } |
| 1300 | |
| 1301 | /// getFunctionType - Return a normal function type with a typed argument |
| 1302 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1303 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1304 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1305 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1306 | bool hasAnyExceptionSpec, unsigned NumExs, |
| 1307 | const QualType *ExArray) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1308 | // Unique functions, to guarantee there is only one function of a particular |
| 1309 | // structure. |
| 1310 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1311 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1312 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
| 1313 | NumExs, ExArray); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1314 | |
| 1315 | void *InsertPos = 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1316 | if (FunctionProtoType *FTP = |
| 1317 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1318 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1319 | |
| 1320 | // Determine whether the type being created is already canonical or not. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1321 | bool isCanonical = ResultTy->isCanonical(); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1322 | if (hasExceptionSpec) |
| 1323 | isCanonical = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1324 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
| 1325 | if (!ArgArray[i]->isCanonical()) |
| 1326 | isCanonical = false; |
| 1327 | |
| 1328 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1329 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1330 | QualType Canonical; |
| 1331 | if (!isCanonical) { |
| 1332 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1333 | CanonicalArgs.reserve(NumArgs); |
| 1334 | for (unsigned i = 0; i != NumArgs; ++i) |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1335 | CanonicalArgs.push_back(getCanonicalType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1336 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1337 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1338 | CanonicalArgs.data(), NumArgs, |
Sebastian Redl | bfa2fcb | 2009-05-06 23:27:55 +0000 | [diff] [blame] | 1339 | isVariadic, TypeQuals); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1340 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1341 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1342 | FunctionProtoType *NewIP = |
| 1343 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1344 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1345 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1346 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1347 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1348 | // for two variable size arrays (for parameter and exception types) at the |
| 1349 | // end of them. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1350 | FunctionProtoType *FTP = |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1351 | (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + |
| 1352 | NumArgs*sizeof(QualType) + |
| 1353 | NumExs*sizeof(QualType), 8); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1354 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1355 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
| 1356 | ExArray, NumExs, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1357 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1358 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1359 | return QualType(FTP, 0); |
| 1360 | } |
| 1361 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1362 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1363 | /// specified type declaration. |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1364 | QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1365 | assert(Decl && "Passed null for Decl param"); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1366 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1367 | |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1368 | if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1369 | return getTypedefType(Typedef); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1370 | else if (isa<TemplateTypeParmDecl>(Decl)) { |
| 1371 | assert(false && "Template type parameter types are always available."); |
| 1372 | } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1373 | return getObjCInterfaceType(ObjCInterface); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1374 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1375 | if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1376 | if (PrevDecl) |
| 1377 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1378 | else |
| 1379 | Decl->TypeForDecl = new (*this,8) RecordType(Record); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1380 | } |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1381 | else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
| 1382 | if (PrevDecl) |
| 1383 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1384 | else |
| 1385 | Decl->TypeForDecl = new (*this,8) EnumType(Enum); |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1386 | } |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1387 | else |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1388 | assert(false && "TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1389 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1390 | if (!PrevDecl) Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1391 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1394 | /// getTypedefType - Return the unique reference to the type for the |
| 1395 | /// specified typename decl. |
| 1396 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 1397 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1398 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1399 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1400 | Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1401 | Types.push_back(Decl->TypeForDecl); |
| 1402 | return QualType(Decl->TypeForDecl, 0); |
| 1403 | } |
| 1404 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1405 | /// getObjCInterfaceType - Return the unique reference to the type for the |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1406 | /// specified ObjC interface decl. |
Daniel Dunbar | 3b3a458 | 2009-04-22 04:34:53 +0000 | [diff] [blame] | 1407 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) { |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1408 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1409 | |
Daniel Dunbar | 3b3a458 | 2009-04-22 04:34:53 +0000 | [diff] [blame] | 1410 | ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl); |
| 1411 | Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID); |
Steve Naroff | 3536b44 | 2007-09-06 21:24:23 +0000 | [diff] [blame] | 1412 | Types.push_back(Decl->TypeForDecl); |
| 1413 | return QualType(Decl->TypeForDecl, 0); |
| 1414 | } |
| 1415 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1416 | /// \brief Retrieve the template type parameter type for a template |
| 1417 | /// parameter with the given depth, index, and (optionally) name. |
| 1418 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
| 1419 | IdentifierInfo *Name) { |
| 1420 | llvm::FoldingSetNodeID ID; |
| 1421 | TemplateTypeParmType::Profile(ID, Depth, Index, Name); |
| 1422 | void *InsertPos = 0; |
| 1423 | TemplateTypeParmType *TypeParm |
| 1424 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1425 | |
| 1426 | if (TypeParm) |
| 1427 | return QualType(TypeParm, 0); |
| 1428 | |
| 1429 | if (Name) |
| 1430 | TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name, |
| 1431 | getTemplateTypeParmType(Depth, Index)); |
| 1432 | else |
| 1433 | TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index); |
| 1434 | |
| 1435 | Types.push_back(TypeParm); |
| 1436 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1437 | |
| 1438 | return QualType(TypeParm, 0); |
| 1439 | } |
| 1440 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1441 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1442 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
| 1443 | const TemplateArgument *Args, |
| 1444 | unsigned NumArgs, |
| 1445 | QualType Canon) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1446 | if (!Canon.isNull()) |
| 1447 | Canon = getCanonicalType(Canon); |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 1448 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1449 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1450 | TemplateSpecializationType::Profile(ID, Template, Args, NumArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1451 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1452 | void *InsertPos = 0; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1453 | TemplateSpecializationType *Spec |
| 1454 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1455 | |
| 1456 | if (Spec) |
| 1457 | return QualType(Spec, 0); |
| 1458 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1459 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1460 | sizeof(TemplateArgument) * NumArgs), |
| 1461 | 8); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1462 | Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1463 | Types.push_back(Spec); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1464 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1465 | |
| 1466 | return QualType(Spec, 0); |
| 1467 | } |
| 1468 | |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1469 | QualType |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1470 | ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS, |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1471 | QualType NamedType) { |
| 1472 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1473 | QualifiedNameType::Profile(ID, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1474 | |
| 1475 | void *InsertPos = 0; |
| 1476 | QualifiedNameType *T |
| 1477 | = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1478 | if (T) |
| 1479 | return QualType(T, 0); |
| 1480 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1481 | T = new (*this) QualifiedNameType(NNS, NamedType, |
| 1482 | getCanonicalType(NamedType)); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1483 | Types.push_back(T); |
| 1484 | QualifiedNameTypes.InsertNode(T, InsertPos); |
| 1485 | return QualType(T, 0); |
| 1486 | } |
| 1487 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1488 | QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
| 1489 | const IdentifierInfo *Name, |
| 1490 | QualType Canon) { |
| 1491 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1492 | |
| 1493 | if (Canon.isNull()) { |
| 1494 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 1495 | if (CanonNNS != NNS) |
| 1496 | Canon = getTypenameType(CanonNNS, Name); |
| 1497 | } |
| 1498 | |
| 1499 | llvm::FoldingSetNodeID ID; |
| 1500 | TypenameType::Profile(ID, NNS, Name); |
| 1501 | |
| 1502 | void *InsertPos = 0; |
| 1503 | TypenameType *T |
| 1504 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1505 | if (T) |
| 1506 | return QualType(T, 0); |
| 1507 | |
| 1508 | T = new (*this) TypenameType(NNS, Name, Canon); |
| 1509 | Types.push_back(T); |
| 1510 | TypenameTypes.InsertNode(T, InsertPos); |
| 1511 | return QualType(T, 0); |
| 1512 | } |
| 1513 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1514 | QualType |
| 1515 | ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
| 1516 | const TemplateSpecializationType *TemplateId, |
| 1517 | QualType Canon) { |
| 1518 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1519 | |
| 1520 | if (Canon.isNull()) { |
| 1521 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 1522 | QualType CanonType = getCanonicalType(QualType(TemplateId, 0)); |
| 1523 | if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) { |
| 1524 | const TemplateSpecializationType *CanonTemplateId |
| 1525 | = CanonType->getAsTemplateSpecializationType(); |
| 1526 | assert(CanonTemplateId && |
| 1527 | "Canonical type must also be a template specialization type"); |
| 1528 | Canon = getTypenameType(CanonNNS, CanonTemplateId); |
| 1529 | } |
| 1530 | } |
| 1531 | |
| 1532 | llvm::FoldingSetNodeID ID; |
| 1533 | TypenameType::Profile(ID, NNS, TemplateId); |
| 1534 | |
| 1535 | void *InsertPos = 0; |
| 1536 | TypenameType *T |
| 1537 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1538 | if (T) |
| 1539 | return QualType(T, 0); |
| 1540 | |
| 1541 | T = new (*this) TypenameType(NNS, TemplateId, Canon); |
| 1542 | Types.push_back(T); |
| 1543 | TypenameTypes.InsertNode(T, InsertPos); |
| 1544 | return QualType(T, 0); |
| 1545 | } |
| 1546 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1547 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 1548 | /// alphabetically. |
| 1549 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 1550 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1551 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols, |
| 1555 | unsigned &NumProtocols) { |
| 1556 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
| 1557 | |
| 1558 | // Sort protocols, keyed by name. |
| 1559 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 1560 | |
| 1561 | // Remove duplicates. |
| 1562 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 1563 | NumProtocols = ProtocolsEnd-Protocols; |
| 1564 | } |
| 1565 | |
| 1566 | |
Chris Lattner | 065f0d7 | 2008-04-07 04:44:08 +0000 | [diff] [blame] | 1567 | /// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for |
| 1568 | /// the given interface decl and the conforming protocol list. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1569 | QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl, |
| 1570 | ObjCProtocolDecl **Protocols, unsigned NumProtocols) { |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1571 | // Sort the protocol list alphabetically to canonicalize it. |
| 1572 | SortAndUniqueProtocols(Protocols, NumProtocols); |
| 1573 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1574 | llvm::FoldingSetNodeID ID; |
Chris Lattner | b048981 | 2008-04-07 06:38:24 +0000 | [diff] [blame] | 1575 | ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1576 | |
| 1577 | void *InsertPos = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1578 | if (ObjCQualifiedInterfaceType *QT = |
| 1579 | ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1580 | return QualType(QT, 0); |
| 1581 | |
| 1582 | // No Match; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1583 | ObjCQualifiedInterfaceType *QType = |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1584 | new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols); |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1585 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1586 | Types.push_back(QType); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1587 | ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1588 | return QualType(QType, 0); |
| 1589 | } |
| 1590 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1591 | /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl |
| 1592 | /// and the conforming protocol list. |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 1593 | QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols, |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1594 | unsigned NumProtocols) { |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1595 | // Sort the protocol list alphabetically to canonicalize it. |
| 1596 | SortAndUniqueProtocols(Protocols, NumProtocols); |
| 1597 | |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1598 | llvm::FoldingSetNodeID ID; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1599 | ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols); |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1600 | |
| 1601 | void *InsertPos = 0; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1602 | if (ObjCQualifiedIdType *QT = |
Chris Lattner | 62f5f7f | 2008-07-26 00:46:50 +0000 | [diff] [blame] | 1603 | ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1604 | return QualType(QT, 0); |
| 1605 | |
| 1606 | // No Match; |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1607 | ObjCQualifiedIdType *QType = |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1608 | new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols); |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1609 | Types.push_back(QType); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1610 | ObjCQualifiedIdTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 1611 | return QualType(QType, 0); |
| 1612 | } |
| 1613 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1614 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 1615 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1616 | /// multiple declarations that refer to "typeof(x)" all contain different |
| 1617 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
| 1618 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1619 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1620 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1621 | TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1622 | Types.push_back(toe); |
| 1623 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1626 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 1627 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 1628 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
| 1629 | /// an issue. This doesn't effect the type checker, since it operates |
| 1630 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1631 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1632 | QualType Canonical = getCanonicalType(tofType); |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1633 | TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1634 | Types.push_back(tot); |
| 1635 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1638 | /// getTagDeclType - Return the unique reference to the type for the |
| 1639 | /// specified TagDecl (struct/union/class/enum) decl. |
| 1640 | QualType ASTContext::getTagDeclType(TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 1641 | assert (Decl); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1642 | return getTypeDeclType(Decl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 1646 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 1647 | /// needs to agree with the definition in <stddef.h>. |
| 1648 | QualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 1649 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1652 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 1653 | /// Used when in C++, as a GCC extension. |
| 1654 | QualType ASTContext::getSignedWCharType() const { |
| 1655 | // FIXME: derive from "Target" ? |
| 1656 | return WCharTy; |
| 1657 | } |
| 1658 | |
| 1659 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 1660 | /// Used when in C++, as a GCC extension. |
| 1661 | QualType ASTContext::getUnsignedWCharType() const { |
| 1662 | // FIXME: derive from "Target" ? |
| 1663 | return UnsignedIntTy; |
| 1664 | } |
| 1665 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 1666 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 1667 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 1668 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 1669 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1672 | //===----------------------------------------------------------------------===// |
| 1673 | // Type Operators |
| 1674 | //===----------------------------------------------------------------------===// |
| 1675 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 1676 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 1677 | /// the specified potentially non-canonical type. The non-canonical version |
| 1678 | /// of a type may have many "decorated" versions of types. Decorators can |
| 1679 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 1680 | /// to be free of any of these, allowing two canonical types to be compared |
| 1681 | /// for exact equality with a simple pointer comparison. |
| 1682 | QualType ASTContext::getCanonicalType(QualType T) { |
| 1683 | QualType CanType = T.getTypePtr()->getCanonicalTypeInternal(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1684 | |
| 1685 | // If the result has type qualifiers, make sure to canonicalize them as well. |
| 1686 | unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers(); |
| 1687 | if (TypeQuals == 0) return CanType; |
| 1688 | |
| 1689 | // If the type qualifiers are on an array type, get the canonical type of the |
| 1690 | // array with the qualifiers applied to the element type. |
| 1691 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 1692 | if (!AT) |
| 1693 | return CanType.getQualifiedType(TypeQuals); |
| 1694 | |
| 1695 | // Get the canonical version of the element with the extra qualifiers on it. |
| 1696 | // This can recursively sink qualifiers through multiple levels of arrays. |
| 1697 | QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals); |
| 1698 | NewEltTy = getCanonicalType(NewEltTy); |
| 1699 | |
| 1700 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 1701 | return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(), |
| 1702 | CAT->getIndexTypeQualifier()); |
| 1703 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
| 1704 | return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
| 1705 | IAT->getIndexTypeQualifier()); |
| 1706 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1707 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
| 1708 | return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(), |
| 1709 | DSAT->getSizeModifier(), |
| 1710 | DSAT->getIndexTypeQualifier()); |
| 1711 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1712 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
| 1713 | return getVariableArrayType(NewEltTy, VAT->getSizeExpr(), |
| 1714 | VAT->getSizeModifier(), |
| 1715 | VAT->getIndexTypeQualifier()); |
| 1716 | } |
| 1717 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1718 | Decl *ASTContext::getCanonicalDecl(Decl *D) { |
Douglas Gregor | c4ccf01 | 2009-05-10 22:59:12 +0000 | [diff] [blame] | 1719 | if (!D) |
| 1720 | return 0; |
| 1721 | |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1722 | if (TagDecl *Tag = dyn_cast<TagDecl>(D)) { |
| 1723 | QualType T = getTagDeclType(Tag); |
| 1724 | return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType) |
| 1725 | ->getDecl()); |
| 1726 | } |
| 1727 | |
| 1728 | if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(D)) { |
| 1729 | while (Template->getPreviousDeclaration()) |
| 1730 | Template = Template->getPreviousDeclaration(); |
| 1731 | return Template; |
| 1732 | } |
| 1733 | |
| 1734 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 1735 | while (Function->getPreviousDeclaration()) |
| 1736 | Function = Function->getPreviousDeclaration(); |
| 1737 | return const_cast<FunctionDecl *>(Function); |
| 1738 | } |
| 1739 | |
| 1740 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 1741 | while (Var->getPreviousDeclaration()) |
| 1742 | Var = Var->getPreviousDeclaration(); |
| 1743 | return const_cast<VarDecl *>(Var); |
| 1744 | } |
| 1745 | |
| 1746 | return D; |
| 1747 | } |
| 1748 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1749 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
| 1750 | // If this template name refers to a template, the canonical |
| 1751 | // template name merely stores the template itself. |
| 1752 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) |
Douglas Gregor | 7da97d0 | 2009-05-10 22:57:19 +0000 | [diff] [blame] | 1753 | return TemplateName(cast<TemplateDecl>(getCanonicalDecl(Template))); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1754 | |
| 1755 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 1756 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 1757 | return DTN->CanonicalTemplateName; |
| 1758 | } |
| 1759 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1760 | NestedNameSpecifier * |
| 1761 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
| 1762 | if (!NNS) |
| 1763 | return 0; |
| 1764 | |
| 1765 | switch (NNS->getKind()) { |
| 1766 | case NestedNameSpecifier::Identifier: |
| 1767 | // Canonicalize the prefix but keep the identifier the same. |
| 1768 | return NestedNameSpecifier::Create(*this, |
| 1769 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 1770 | NNS->getAsIdentifier()); |
| 1771 | |
| 1772 | case NestedNameSpecifier::Namespace: |
| 1773 | // A namespace is canonical; build a nested-name-specifier with |
| 1774 | // this namespace and no prefix. |
| 1775 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 1776 | |
| 1777 | case NestedNameSpecifier::TypeSpec: |
| 1778 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 1779 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
| 1780 | NestedNameSpecifier *Prefix = 0; |
| 1781 | |
| 1782 | // FIXME: This isn't the right check! |
| 1783 | if (T->isDependentType()) |
| 1784 | Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix()); |
| 1785 | |
| 1786 | return NestedNameSpecifier::Create(*this, Prefix, |
| 1787 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
| 1788 | T.getTypePtr()); |
| 1789 | } |
| 1790 | |
| 1791 | case NestedNameSpecifier::Global: |
| 1792 | // The global specifier is canonical and unique. |
| 1793 | return NNS; |
| 1794 | } |
| 1795 | |
| 1796 | // Required to silence a GCC warning |
| 1797 | return 0; |
| 1798 | } |
| 1799 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1800 | |
| 1801 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 1802 | // Handle the non-qualified case efficiently. |
| 1803 | if (T.getCVRQualifiers() == 0) { |
| 1804 | // Handle the common positive case fast. |
| 1805 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 1806 | return AT; |
| 1807 | } |
| 1808 | |
| 1809 | // Handle the common negative case fast, ignoring CVR qualifiers. |
| 1810 | QualType CType = T->getCanonicalTypeInternal(); |
| 1811 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1812 | // Make sure to look through type qualifiers (like ExtQuals) for the negative |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1813 | // test. |
| 1814 | if (!isa<ArrayType>(CType) && |
| 1815 | !isa<ArrayType>(CType.getUnqualifiedType())) |
| 1816 | return 0; |
| 1817 | |
| 1818 | // Apply any CVR qualifiers from the array type to the element type. This |
| 1819 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 1820 | // any type qualifiers, the element type is so qualified, not the array type." |
| 1821 | |
| 1822 | // If we get here, we either have type qualifiers on the type, or we have |
| 1823 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
| 1824 | // we must propagate them down into the elemeng type. |
| 1825 | unsigned CVRQuals = T.getCVRQualifiers(); |
| 1826 | unsigned AddrSpace = 0; |
| 1827 | Type *Ty = T.getTypePtr(); |
| 1828 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1829 | // Rip through ExtQualType's and typedefs to get to a concrete type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1830 | while (1) { |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1831 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) { |
| 1832 | AddrSpace = EXTQT->getAddressSpace(); |
| 1833 | Ty = EXTQT->getBaseType(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1834 | } else { |
| 1835 | T = Ty->getDesugaredType(); |
| 1836 | if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0) |
| 1837 | break; |
| 1838 | CVRQuals |= T.getCVRQualifiers(); |
| 1839 | Ty = T.getTypePtr(); |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | // If we have a simple case, just return now. |
| 1844 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
| 1845 | if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0)) |
| 1846 | return ATy; |
| 1847 | |
| 1848 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 1849 | // qualifiers into the array element type and return a new array type. |
| 1850 | // Get the canonical version of the element with the extra qualifiers on it. |
| 1851 | // This can recursively sink qualifiers through multiple levels of arrays. |
| 1852 | QualType NewEltTy = ATy->getElementType(); |
| 1853 | if (AddrSpace) |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 1854 | NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1855 | NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals); |
| 1856 | |
| 1857 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 1858 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 1859 | CAT->getSizeModifier(), |
| 1860 | CAT->getIndexTypeQualifier())); |
| 1861 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 1862 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 1863 | IAT->getSizeModifier(), |
| 1864 | IAT->getIndexTypeQualifier())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1865 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1866 | if (const DependentSizedArrayType *DSAT |
| 1867 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 1868 | return cast<ArrayType>( |
| 1869 | getDependentSizedArrayType(NewEltTy, |
| 1870 | DSAT->getSizeExpr(), |
| 1871 | DSAT->getSizeModifier(), |
| 1872 | DSAT->getIndexTypeQualifier())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1873 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1874 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
| 1875 | return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(), |
| 1876 | VAT->getSizeModifier(), |
| 1877 | VAT->getIndexTypeQualifier())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1881 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 1882 | /// specified array type to a pointer. This operation is non-trivial when |
| 1883 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 1884 | /// this returns a pointer to a properly qualified element of the array. |
| 1885 | /// |
| 1886 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 1887 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1888 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 1889 | // typedefs in the element type of the array. This also handles propagation |
| 1890 | // of type qualifiers from the array type into the element type if present |
| 1891 | // (C99 6.7.3p8). |
| 1892 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 1893 | assert(PrettyArrayType && "Not an array type!"); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1894 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1895 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1896 | |
| 1897 | // int x[restrict 4] -> int *restrict |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1898 | return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Daniel Dunbar | d786f6a | 2009-01-05 22:14:37 +0000 | [diff] [blame] | 1901 | QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) { |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 1902 | QualType ElemTy = VAT->getElementType(); |
| 1903 | |
| 1904 | if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy)) |
| 1905 | return getBaseElementType(VAT); |
| 1906 | |
| 1907 | return ElemTy; |
| 1908 | } |
| 1909 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1910 | /// getFloatingRank - Return a relative rank for floating point types. |
| 1911 | /// 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] | 1912 | static FloatingRank getFloatingRank(QualType T) { |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1913 | if (const ComplexType *CT = T->getAsComplexType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1914 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1915 | |
Daniel Dunbar | d786f6a | 2009-01-05 22:14:37 +0000 | [diff] [blame] | 1916 | assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type"); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1917 | switch (T->getAsBuiltinType()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1918 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1919 | case BuiltinType::Float: return FloatRank; |
| 1920 | case BuiltinType::Double: return DoubleRank; |
| 1921 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 1922 | } |
| 1923 | } |
| 1924 | |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 1925 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 1926 | /// point or a complex type (based on typeDomain/typeSize). |
| 1927 | /// 'typeDomain' is a real floating point or complex type. |
| 1928 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 1929 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 1930 | QualType Domain) const { |
| 1931 | FloatingRank EltRank = getFloatingRank(Size); |
| 1932 | if (Domain->isComplexType()) { |
| 1933 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 1934 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 1935 | case FloatRank: return FloatComplexTy; |
| 1936 | case DoubleRank: return DoubleComplexTy; |
| 1937 | case LongDoubleRank: return LongDoubleComplexTy; |
| 1938 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1939 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 1940 | |
| 1941 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 1942 | switch (EltRank) { |
| 1943 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 1944 | case FloatRank: return FloatTy; |
| 1945 | case DoubleRank: return DoubleTy; |
| 1946 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 1947 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1950 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 1951 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 1952 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
| 1953 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1954 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 1955 | FloatingRank LHSR = getFloatingRank(LHS); |
| 1956 | FloatingRank RHSR = getFloatingRank(RHS); |
| 1957 | |
| 1958 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 1959 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 1960 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 1961 | return 1; |
| 1962 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1965 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 1966 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 1967 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1968 | unsigned ASTContext::getIntegerRank(Type *T) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1969 | assert(T->isCanonical() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1970 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
| 1971 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
| 1972 | |
| 1973 | // There are two things which impact the integer rank: the width, and |
| 1974 | // the ordering of builtins. The builtin ordering is encoded in the |
| 1975 | // bottom three bits; the width is encoded in the bits above that. |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame^] | 1976 | if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1977 | return FWIT->getWidth() << 3; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1978 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1979 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1980 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 1981 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1982 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1983 | case BuiltinType::Char_S: |
| 1984 | case BuiltinType::Char_U: |
| 1985 | case BuiltinType::SChar: |
| 1986 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1987 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1988 | case BuiltinType::Short: |
| 1989 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1990 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1991 | case BuiltinType::Int: |
| 1992 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1993 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1994 | case BuiltinType::Long: |
| 1995 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1996 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 1997 | case BuiltinType::LongLong: |
| 1998 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1999 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2000 | case BuiltinType::Int128: |
| 2001 | case BuiltinType::UInt128: |
| 2002 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2003 | } |
| 2004 | } |
| 2005 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2006 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
| 2007 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
| 2008 | /// LHS < RHS, return -1. |
| 2009 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2010 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 2011 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2012 | if (LHSC == RHSC) return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2013 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2014 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 2015 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2016 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2017 | unsigned LHSRank = getIntegerRank(LHSC); |
| 2018 | unsigned RHSRank = getIntegerRank(RHSC); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2019 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2020 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 2021 | if (LHSRank == RHSRank) return 0; |
| 2022 | return LHSRank > RHSRank ? 1 : -1; |
| 2023 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2024 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2025 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 2026 | if (LHSUnsigned) { |
| 2027 | // If the unsigned [LHS] type is larger, return it. |
| 2028 | if (LHSRank >= RHSRank) |
| 2029 | return 1; |
| 2030 | |
| 2031 | // If the signed type can represent all values of the unsigned type, it |
| 2032 | // wins. Because we are dealing with 2's complement and types that are |
| 2033 | // powers of two larger than each other, this is always safe. |
| 2034 | return -1; |
| 2035 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2036 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2037 | // If the unsigned [RHS] type is larger, return it. |
| 2038 | if (RHSRank >= LHSRank) |
| 2039 | return -1; |
| 2040 | |
| 2041 | // If the signed type can represent all values of the unsigned type, it |
| 2042 | // wins. Because we are dealing with 2's complement and types that are |
| 2043 | // powers of two larger than each other, this is always safe. |
| 2044 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2045 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2046 | |
| 2047 | // getCFConstantStringType - Return the type used for constant CFStrings. |
| 2048 | QualType ASTContext::getCFConstantStringType() { |
| 2049 | if (!CFConstantStringTypeDecl) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 2050 | CFConstantStringTypeDecl = |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2051 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2052 | &Idents.get("NSConstantString")); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2053 | QualType FieldTypes[4]; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2054 | |
| 2055 | // const int *isa; |
| 2056 | FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2057 | // int flags; |
| 2058 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2059 | // const char *str; |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2060 | FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2061 | // long length; |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2062 | FieldTypes[3] = LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2063 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2064 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2065 | for (unsigned i = 0; i < 4; ++i) { |
| 2066 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
| 2067 | SourceLocation(), 0, |
| 2068 | FieldTypes[i], /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2069 | /*Mutable=*/false); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2070 | CFConstantStringTypeDecl->addDecl(*this, Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | CFConstantStringTypeDecl->completeDefinition(*this); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 2077 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2078 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2079 | void ASTContext::setCFConstantStringType(QualType T) { |
| 2080 | const RecordType *Rec = T->getAsRecordType(); |
| 2081 | assert(Rec && "Invalid CFConstantStringType"); |
| 2082 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 2083 | } |
| 2084 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2085 | QualType ASTContext::getObjCFastEnumerationStateType() |
| 2086 | { |
| 2087 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2088 | ObjCFastEnumerationStateTypeDecl = |
| 2089 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2090 | &Idents.get("__objcFastEnumerationState")); |
| 2091 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2092 | QualType FieldTypes[] = { |
| 2093 | UnsignedLongTy, |
| 2094 | getPointerType(ObjCIdType), |
| 2095 | getPointerType(UnsignedLongTy), |
| 2096 | getConstantArrayType(UnsignedLongTy, |
| 2097 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 2098 | }; |
| 2099 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2100 | for (size_t i = 0; i < 4; ++i) { |
| 2101 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2102 | ObjCFastEnumerationStateTypeDecl, |
| 2103 | SourceLocation(), 0, |
| 2104 | FieldTypes[i], /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2105 | /*Mutable=*/false); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2106 | ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2107 | } |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2108 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2109 | ObjCFastEnumerationStateTypeDecl->completeDefinition(*this); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 2113 | } |
| 2114 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2115 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
| 2116 | const RecordType *Rec = T->getAsRecordType(); |
| 2117 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 2118 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 2119 | } |
| 2120 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 2121 | // This returns true if a type has been typedefed to BOOL: |
| 2122 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 2123 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 2124 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 2125 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 2126 | return II->isStr("BOOL"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2127 | |
| 2128 | return false; |
| 2129 | } |
| 2130 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2131 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2132 | /// purpose. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2133 | int ASTContext::getObjCEncodingTypeSize(QualType type) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2134 | uint64_t sz = getTypeSize(type); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2135 | |
| 2136 | // Make all integer and enum types at least as large as an int |
| 2137 | if (sz > 0 && type->isIntegralType()) |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2138 | sz = std::max(sz, getTypeSize(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2139 | // Treat arrays as pointers, since that's how they're passed in. |
| 2140 | else if (type->isArrayType()) |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2141 | sz = getTypeSize(VoidPtrTy); |
| 2142 | return sz / getTypeSize(CharTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2145 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2146 | /// declaration. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2147 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 2148 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2149 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 2150 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2151 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2152 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2153 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2154 | // Compute size of all parameters. |
| 2155 | // Start with computing size of a pointer in number of bytes. |
| 2156 | // FIXME: There might(should) be a better way of doing this computation! |
| 2157 | SourceLocation Loc; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2158 | int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2159 | // The first two arguments (self and _cmd) are pointers; account for |
| 2160 | // their size. |
| 2161 | int ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2162 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 2163 | E = Decl->param_end(); PI != E; ++PI) { |
| 2164 | QualType PType = (*PI)->getType(); |
| 2165 | int sz = getObjCEncodingTypeSize(PType); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2166 | assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2167 | ParmOffset += sz; |
| 2168 | } |
| 2169 | S += llvm::utostr(ParmOffset); |
| 2170 | S += "@0:"; |
| 2171 | S += llvm::utostr(PtrSize); |
| 2172 | |
| 2173 | // Argument types. |
| 2174 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2175 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 2176 | E = Decl->param_end(); PI != E; ++PI) { |
| 2177 | ParmVarDecl *PVDecl = *PI; |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 2178 | QualType PType = PVDecl->getOriginalType(); |
| 2179 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 2180 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 2181 | // Use array's original type only if it has known number of |
| 2182 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 2183 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 2184 | PType = PVDecl->getType(); |
| 2185 | } else if (PType->isFunctionType()) |
| 2186 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 2187 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2188 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 2189 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2190 | getObjCEncodingForType(PType, S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2191 | S += llvm::utostr(ParmOffset); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2192 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2193 | } |
| 2194 | } |
| 2195 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2196 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 2197 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2198 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 2199 | /// NULL when getting encodings for protocol properties. |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 2200 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 2201 | /// attributes readonly and bycopy are encoded as single characters. The |
| 2202 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 2203 | /// encoded as single characters, followed by an identifier. Property types |
| 2204 | /// are also encoded as a parametrized attribute. The characters used to encode |
| 2205 | /// these attributes are defined by the following enumeration: |
| 2206 | /// @code |
| 2207 | /// enum PropertyAttributes { |
| 2208 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 2209 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 2210 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 2211 | /// kPropertyDynamic = 'D', // property is dynamic |
| 2212 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 2213 | /// kPropertySetter = 'S', // followed by setter selector name |
| 2214 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 2215 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 2216 | /// kPropertyWeak = 'W' // 'weak' property |
| 2217 | /// kPropertyStrong = 'P' // property GC'able |
| 2218 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 2219 | /// }; |
| 2220 | /// @endcode |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2221 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
| 2222 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 2223 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2224 | // Collect information from the property implementation decl(s). |
| 2225 | bool Dynamic = false; |
| 2226 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 2227 | |
| 2228 | // FIXME: Duplicated code due to poor abstraction. |
| 2229 | if (Container) { |
| 2230 | if (const ObjCCategoryImplDecl *CID = |
| 2231 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 2232 | for (ObjCCategoryImplDecl::propimpl_iterator |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2233 | i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this); |
| 2234 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2235 | ObjCPropertyImplDecl *PID = *i; |
| 2236 | if (PID->getPropertyDecl() == PD) { |
| 2237 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 2238 | Dynamic = true; |
| 2239 | } else { |
| 2240 | SynthesizePID = PID; |
| 2241 | } |
| 2242 | } |
| 2243 | } |
| 2244 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2245 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2246 | for (ObjCCategoryImplDecl::propimpl_iterator |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2247 | i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this); |
| 2248 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2249 | ObjCPropertyImplDecl *PID = *i; |
| 2250 | if (PID->getPropertyDecl() == PD) { |
| 2251 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 2252 | Dynamic = true; |
| 2253 | } else { |
| 2254 | SynthesizePID = PID; |
| 2255 | } |
| 2256 | } |
| 2257 | } |
| 2258 | } |
| 2259 | } |
| 2260 | |
| 2261 | // FIXME: This is not very efficient. |
| 2262 | S = "T"; |
| 2263 | |
| 2264 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2265 | // GCC has some special rules regarding encoding of properties which |
| 2266 | // closely resembles encoding of ivars. |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 2267 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2268 | true /* outermost type */, |
| 2269 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2270 | |
| 2271 | if (PD->isReadOnly()) { |
| 2272 | S += ",R"; |
| 2273 | } else { |
| 2274 | switch (PD->getSetterKind()) { |
| 2275 | case ObjCPropertyDecl::Assign: break; |
| 2276 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
| 2277 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
| 2278 | } |
| 2279 | } |
| 2280 | |
| 2281 | // It really isn't clear at all what this means, since properties |
| 2282 | // are "dynamic by default". |
| 2283 | if (Dynamic) |
| 2284 | S += ",D"; |
| 2285 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2286 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 2287 | S += ",N"; |
| 2288 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2289 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 2290 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2291 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 2295 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2296 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | if (SynthesizePID) { |
| 2300 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 2301 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 2302 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | // FIXME: OBJCGC: weak & strong |
| 2306 | } |
| 2307 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2308 | /// getLegacyIntegralTypeEncoding - |
| 2309 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 2310 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2311 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 2312 | /// |
| 2313 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
| 2314 | if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) { |
| 2315 | if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 2316 | if (BT->getKind() == BuiltinType::ULong && |
| 2317 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2318 | PointeeTy = UnsignedIntTy; |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 2319 | else |
| 2320 | if (BT->getKind() == BuiltinType::Long && |
| 2321 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2322 | PointeeTy = IntTy; |
| 2323 | } |
| 2324 | } |
| 2325 | } |
| 2326 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2327 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 2328 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 2329 | // We follow the behavior of gcc, expanding structures which are |
| 2330 | // directly pointed to, and expanding embedded structures. Note that |
| 2331 | // these rules are sufficient to prevent recursive encoding of the |
| 2332 | // same type. |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 2333 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
| 2334 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 2337 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 2338 | const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 2339 | const Expr *E = FD->getBitWidth(); |
| 2340 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 2341 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 2342 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 2343 | S += 'b'; |
| 2344 | S += llvm::utostr(N); |
| 2345 | } |
| 2346 | |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 2347 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 2348 | bool ExpandPointedToStructures, |
| 2349 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 2350 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2351 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2352 | bool EncodingProperty) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 2353 | if (const BuiltinType *BT = T->getAsBuiltinType()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2354 | if (FD && FD->isBitField()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 2355 | EncodeBitField(this, S, FD); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2356 | } |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2357 | else { |
| 2358 | char encoding; |
| 2359 | switch (BT->getKind()) { |
| 2360 | default: assert(0 && "Unhandled builtin type kind"); |
| 2361 | case BuiltinType::Void: encoding = 'v'; break; |
| 2362 | case BuiltinType::Bool: encoding = 'B'; break; |
| 2363 | case BuiltinType::Char_U: |
| 2364 | case BuiltinType::UChar: encoding = 'C'; break; |
| 2365 | case BuiltinType::UShort: encoding = 'S'; break; |
| 2366 | case BuiltinType::UInt: encoding = 'I'; break; |
Fariborz Jahanian | 72696e1 | 2009-02-11 22:31:45 +0000 | [diff] [blame] | 2367 | case BuiltinType::ULong: |
| 2368 | encoding = |
| 2369 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
| 2370 | break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2371 | case BuiltinType::UInt128: encoding = 'T'; break; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2372 | case BuiltinType::ULongLong: encoding = 'Q'; break; |
| 2373 | case BuiltinType::Char_S: |
| 2374 | case BuiltinType::SChar: encoding = 'c'; break; |
| 2375 | case BuiltinType::Short: encoding = 's'; break; |
| 2376 | case BuiltinType::Int: encoding = 'i'; break; |
Fariborz Jahanian | 72696e1 | 2009-02-11 22:31:45 +0000 | [diff] [blame] | 2377 | case BuiltinType::Long: |
| 2378 | encoding = |
| 2379 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; |
| 2380 | break; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2381 | case BuiltinType::LongLong: encoding = 'q'; break; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2382 | case BuiltinType::Int128: encoding = 't'; break; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2383 | case BuiltinType::Float: encoding = 'f'; break; |
| 2384 | case BuiltinType::Double: encoding = 'd'; break; |
| 2385 | case BuiltinType::LongDouble: encoding = 'd'; break; |
| 2386 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2387 | |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2388 | S += encoding; |
| 2389 | } |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 2390 | } else if (const ComplexType *CT = T->getAsComplexType()) { |
| 2391 | S += 'j'; |
| 2392 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
| 2393 | false); |
| 2394 | } else if (T->isObjCQualifiedIdType()) { |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2395 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
| 2396 | ExpandPointedToStructures, |
| 2397 | ExpandStructures, FD); |
| 2398 | if (FD || EncodingProperty) { |
| 2399 | // Note that we do extended encoding of protocol qualifer list |
| 2400 | // Only when doing ivar or property encoding. |
| 2401 | const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType(); |
| 2402 | S += '"'; |
Steve Naroff | 446ee4e | 2009-05-27 16:21:00 +0000 | [diff] [blame] | 2403 | for (ObjCQualifiedIdType::qual_iterator I = QIDT->qual_begin(), |
| 2404 | E = QIDT->qual_end(); I != E; ++I) { |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2405 | S += '<'; |
Steve Naroff | 446ee4e | 2009-05-27 16:21:00 +0000 | [diff] [blame] | 2406 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2407 | S += '>'; |
| 2408 | } |
| 2409 | S += '"'; |
| 2410 | } |
| 2411 | return; |
Fariborz Jahanian | c569249 | 2007-12-17 21:03:50 +0000 | [diff] [blame] | 2412 | } |
| 2413 | else if (const PointerType *PT = T->getAsPointerType()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2414 | QualType PointeeTy = PT->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2415 | bool isReadOnly = false; |
| 2416 | // For historical/compatibility reasons, the read-only qualifier of the |
| 2417 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 2418 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
| 2419 | // Also, do not emit the 'r' for anything but the outermost type! |
| 2420 | if (dyn_cast<TypedefType>(T.getTypePtr())) { |
| 2421 | if (OutermostType && T.isConstQualified()) { |
| 2422 | isReadOnly = true; |
| 2423 | S += 'r'; |
| 2424 | } |
| 2425 | } |
| 2426 | else if (OutermostType) { |
| 2427 | QualType P = PointeeTy; |
| 2428 | while (P->getAsPointerType()) |
| 2429 | P = P->getAsPointerType()->getPointeeType(); |
| 2430 | if (P.isConstQualified()) { |
| 2431 | isReadOnly = true; |
| 2432 | S += 'r'; |
| 2433 | } |
| 2434 | } |
| 2435 | if (isReadOnly) { |
| 2436 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 2437 | // combinations need to be rearranged. |
| 2438 | // Rewrite "in const" from "nr" to "rn" |
| 2439 | const char * s = S.c_str(); |
| 2440 | int len = S.length(); |
| 2441 | if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') { |
| 2442 | std::string replace = "rn"; |
| 2443 | S.replace(S.end()-2, S.end(), replace); |
| 2444 | } |
| 2445 | } |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2446 | if (isObjCIdStructType(PointeeTy)) { |
Fariborz Jahanian | c2939bc | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 2447 | S += '@'; |
| 2448 | return; |
Fariborz Jahanian | c166d73 | 2008-12-19 00:14:49 +0000 | [diff] [blame] | 2449 | } |
| 2450 | else if (PointeeTy->isObjCInterfaceType()) { |
Fariborz Jahanian | bb99bde | 2009-02-16 21:41:04 +0000 | [diff] [blame] | 2451 | if (!EncodingProperty && |
Fariborz Jahanian | 225dfd7 | 2009-02-16 22:09:26 +0000 | [diff] [blame] | 2452 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
Fariborz Jahanian | 3e1b16c | 2008-12-23 21:30:15 +0000 | [diff] [blame] | 2453 | // Another historical/compatibility reason. |
| 2454 | // We encode the underlying type which comes out as |
| 2455 | // {...}; |
| 2456 | S += '^'; |
| 2457 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 2458 | false, ExpandPointedToStructures, |
| 2459 | NULL); |
| 2460 | return; |
| 2461 | } |
Fariborz Jahanian | c166d73 | 2008-12-19 00:14:49 +0000 | [diff] [blame] | 2462 | S += '@'; |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2463 | if (FD || EncodingProperty) { |
Fariborz Jahanian | 86f938b | 2009-02-21 18:23:24 +0000 | [diff] [blame] | 2464 | const ObjCInterfaceType *OIT = |
| 2465 | PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType(); |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2466 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | adcaf54 | 2008-12-20 19:17:01 +0000 | [diff] [blame] | 2467 | S += '"'; |
| 2468 | S += OI->getNameAsCString(); |
Steve Naroff | 446ee4e | 2009-05-27 16:21:00 +0000 | [diff] [blame] | 2469 | for (ObjCInterfaceType::qual_iterator I = OIT->qual_begin(), |
| 2470 | E = OIT->qual_end(); I != E; ++I) { |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2471 | S += '<'; |
Steve Naroff | 446ee4e | 2009-05-27 16:21:00 +0000 | [diff] [blame] | 2472 | S += (*I)->getNameAsString(); |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2473 | S += '>'; |
| 2474 | } |
Fariborz Jahanian | adcaf54 | 2008-12-20 19:17:01 +0000 | [diff] [blame] | 2475 | S += '"'; |
| 2476 | } |
Fariborz Jahanian | c166d73 | 2008-12-19 00:14:49 +0000 | [diff] [blame] | 2477 | return; |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2478 | } else if (isObjCClassStructType(PointeeTy)) { |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 2479 | S += '#'; |
| 2480 | return; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2481 | } else if (isObjCSelType(PointeeTy)) { |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 2482 | S += ':'; |
| 2483 | return; |
Fariborz Jahanian | c2939bc | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 2484 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2485 | |
| 2486 | if (PointeeTy->isCharType()) { |
| 2487 | // char pointer types should be encoded as '*' unless it is a |
| 2488 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 2489 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2490 | S += '*'; |
| 2491 | return; |
| 2492 | } |
| 2493 | } |
| 2494 | |
| 2495 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2496 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 2497 | |
| 2498 | getObjCEncodingForTypeImpl(PointeeTy, S, |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2499 | false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2500 | NULL); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2501 | } else if (const ArrayType *AT = |
| 2502 | // Ignore type qualifiers etc. |
| 2503 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 2504 | if (isa<IncompleteArrayType>(AT)) { |
| 2505 | // Incomplete arrays are encoded as a pointer to the array element. |
| 2506 | S += '^'; |
| 2507 | |
| 2508 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
| 2509 | false, ExpandStructures, FD); |
| 2510 | } else { |
| 2511 | S += '['; |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2512 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 2513 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 2514 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 2515 | else { |
| 2516 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 2517 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 2518 | S += '0'; |
| 2519 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2520 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 2521 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
| 2522 | false, ExpandStructures, FD); |
| 2523 | S += ']'; |
| 2524 | } |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 2525 | } else if (T->getAsFunctionType()) { |
| 2526 | S += '?'; |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 2527 | } else if (const RecordType *RTy = T->getAsRecordType()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 2528 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2529 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 2530 | // Anonymous structures print as '?' |
| 2531 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 2532 | S += II->getName(); |
| 2533 | } else { |
| 2534 | S += '?'; |
| 2535 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2536 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2537 | S += '='; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2538 | for (RecordDecl::field_iterator Field = RDecl->field_begin(*this), |
| 2539 | FieldEnd = RDecl->field_end(*this); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2540 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2541 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2542 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2543 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2544 | S += '"'; |
| 2545 | } |
| 2546 | |
| 2547 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2548 | if (Field->isBitField()) { |
| 2549 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
| 2550 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2551 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2552 | QualType qt = Field->getType(); |
| 2553 | getLegacyIntegralTypeEncoding(qt); |
| 2554 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2555 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2556 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2557 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 2558 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2559 | S += RDecl->isUnion() ? ')' : '}'; |
Steve Naroff | 5e71124 | 2007-12-12 22:30:11 +0000 | [diff] [blame] | 2560 | } else if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 2561 | if (FD && FD->isBitField()) |
| 2562 | EncodeBitField(this, S, FD); |
| 2563 | else |
| 2564 | S += 'i'; |
Steve Naroff | 485eeff | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 2565 | } else if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 2566 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2567 | } else if (T->isObjCInterfaceType()) { |
| 2568 | // @encode(class_name) |
| 2569 | ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl(); |
| 2570 | S += '{'; |
| 2571 | const IdentifierInfo *II = OI->getIdentifier(); |
| 2572 | S += II->getName(); |
| 2573 | S += '='; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2574 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2575 | CollectObjCIvars(OI, RecFields); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2576 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2577 | if (RecFields[i]->isBitField()) |
| 2578 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
| 2579 | RecFields[i]); |
| 2580 | else |
| 2581 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
| 2582 | FD); |
| 2583 | } |
| 2584 | S += '}'; |
| 2585 | } |
| 2586 | else |
Steve Naroff | f69cc5d | 2008-01-30 19:17:43 +0000 | [diff] [blame] | 2587 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2590 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 2591 | std::string& S) const { |
| 2592 | if (QT & Decl::OBJC_TQ_In) |
| 2593 | S += 'n'; |
| 2594 | if (QT & Decl::OBJC_TQ_Inout) |
| 2595 | S += 'N'; |
| 2596 | if (QT & Decl::OBJC_TQ_Out) |
| 2597 | S += 'o'; |
| 2598 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 2599 | S += 'O'; |
| 2600 | if (QT & Decl::OBJC_TQ_Byref) |
| 2601 | S += 'R'; |
| 2602 | if (QT & Decl::OBJC_TQ_Oneway) |
| 2603 | S += 'V'; |
| 2604 | } |
| 2605 | |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2606 | void ASTContext::setBuiltinVaListType(QualType T) |
| 2607 | { |
| 2608 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
| 2609 | |
| 2610 | BuiltinVaListType = T; |
| 2611 | } |
| 2612 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2613 | void ASTContext::setObjCIdType(QualType T) |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 2614 | { |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2615 | ObjCIdType = T; |
| 2616 | |
| 2617 | const TypedefType *TT = T->getAsTypedefType(); |
| 2618 | if (!TT) |
| 2619 | return; |
| 2620 | |
| 2621 | TypedefDecl *TD = TT->getDecl(); |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 2622 | |
| 2623 | // typedef struct objc_object *id; |
| 2624 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 2625 | // User error - caller will issue diagnostics. |
| 2626 | if (!ptr) |
| 2627 | return; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 2628 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 2629 | // User error - caller will issue diagnostics. |
| 2630 | if (!rec) |
| 2631 | return; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 2632 | IdStructType = rec; |
| 2633 | } |
| 2634 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2635 | void ASTContext::setObjCSelType(QualType T) |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2636 | { |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2637 | ObjCSelType = T; |
| 2638 | |
| 2639 | const TypedefType *TT = T->getAsTypedefType(); |
| 2640 | if (!TT) |
| 2641 | return; |
| 2642 | TypedefDecl *TD = TT->getDecl(); |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2643 | |
| 2644 | // typedef struct objc_selector *SEL; |
| 2645 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 2646 | if (!ptr) |
| 2647 | return; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2648 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 2649 | if (!rec) |
| 2650 | return; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2651 | SelStructType = rec; |
| 2652 | } |
| 2653 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2654 | void ASTContext::setObjCProtoType(QualType QT) |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 2655 | { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2656 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2659 | void ASTContext::setObjCClassType(QualType T) |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 2660 | { |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2661 | ObjCClassType = T; |
| 2662 | |
| 2663 | const TypedefType *TT = T->getAsTypedefType(); |
| 2664 | if (!TT) |
| 2665 | return; |
| 2666 | TypedefDecl *TD = TT->getDecl(); |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 2667 | |
| 2668 | // typedef struct objc_class *Class; |
| 2669 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
| 2670 | assert(ptr && "'Class' incorrectly typed"); |
| 2671 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
| 2672 | assert(rec && "'Class' incorrectly typed"); |
| 2673 | ClassStructType = rec; |
| 2674 | } |
| 2675 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2676 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
| 2677 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 2678 | "'NSConstantString' type already set!"); |
| 2679 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2680 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2683 | /// \brief Retrieve the template name that represents a qualified |
| 2684 | /// template name such as \c std::vector. |
| 2685 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 2686 | bool TemplateKeyword, |
| 2687 | TemplateDecl *Template) { |
| 2688 | llvm::FoldingSetNodeID ID; |
| 2689 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 2690 | |
| 2691 | void *InsertPos = 0; |
| 2692 | QualifiedTemplateName *QTN = |
| 2693 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 2694 | if (!QTN) { |
| 2695 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 2696 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 2697 | } |
| 2698 | |
| 2699 | return TemplateName(QTN); |
| 2700 | } |
| 2701 | |
| 2702 | /// \brief Retrieve the template name that represents a dependent |
| 2703 | /// template name such as \c MetaFun::template apply. |
| 2704 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 2705 | const IdentifierInfo *Name) { |
| 2706 | assert(NNS->isDependent() && "Nested name specifier must be dependent"); |
| 2707 | |
| 2708 | llvm::FoldingSetNodeID ID; |
| 2709 | DependentTemplateName::Profile(ID, NNS, Name); |
| 2710 | |
| 2711 | void *InsertPos = 0; |
| 2712 | DependentTemplateName *QTN = |
| 2713 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 2714 | |
| 2715 | if (QTN) |
| 2716 | return TemplateName(QTN); |
| 2717 | |
| 2718 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 2719 | if (CanonNNS == NNS) { |
| 2720 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 2721 | } else { |
| 2722 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 2723 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
| 2724 | } |
| 2725 | |
| 2726 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 2727 | return TemplateName(QTN); |
| 2728 | } |
| 2729 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2730 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 2731 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 2732 | /// is actually a value of type @c TargetInfo::IntType. |
| 2733 | QualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2734 | switch (Type) { |
| 2735 | case TargetInfo::NoInt: return QualType(); |
| 2736 | case TargetInfo::SignedShort: return ShortTy; |
| 2737 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 2738 | case TargetInfo::SignedInt: return IntTy; |
| 2739 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 2740 | case TargetInfo::SignedLong: return LongTy; |
| 2741 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 2742 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 2743 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 2744 | } |
| 2745 | |
| 2746 | assert(false && "Unhandled TargetInfo::IntType value"); |
Daniel Dunbar | b3ac543 | 2008-11-11 01:16:00 +0000 | [diff] [blame] | 2747 | return QualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 2748 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 2749 | |
| 2750 | //===----------------------------------------------------------------------===// |
| 2751 | // Type Predicates. |
| 2752 | //===----------------------------------------------------------------------===// |
| 2753 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2754 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 2755 | /// NSObject attribute on a c-style pointer type. |
| 2756 | /// FIXME - Make it work directly on types. |
| 2757 | /// |
| 2758 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 2759 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 2760 | if (TypedefDecl *TD = TDT->getDecl()) |
| 2761 | if (TD->getAttr<ObjCNSObjectAttr>()) |
| 2762 | return true; |
| 2763 | } |
| 2764 | return false; |
| 2765 | } |
| 2766 | |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 2767 | /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer |
| 2768 | /// to an object type. This includes "id" and "Class" (two 'special' pointers |
| 2769 | /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified |
| 2770 | /// ID type). |
| 2771 | bool ASTContext::isObjCObjectPointerType(QualType Ty) const { |
Steve Naroff | d461777 | 2009-02-23 18:36:16 +0000 | [diff] [blame] | 2772 | if (Ty->isObjCQualifiedIdType()) |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 2773 | return true; |
| 2774 | |
Steve Naroff | 6ae9850 | 2008-10-21 18:24:04 +0000 | [diff] [blame] | 2775 | // Blocks are objects. |
| 2776 | if (Ty->isBlockPointerType()) |
| 2777 | return true; |
| 2778 | |
| 2779 | // All other object types are pointers. |
Chris Lattner | 16ede0e | 2009-04-12 23:51:02 +0000 | [diff] [blame] | 2780 | const PointerType *PT = Ty->getAsPointerType(); |
| 2781 | if (PT == 0) |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 2782 | return false; |
| 2783 | |
Chris Lattner | 16ede0e | 2009-04-12 23:51:02 +0000 | [diff] [blame] | 2784 | // If this a pointer to an interface (e.g. NSString*), it is ok. |
| 2785 | if (PT->getPointeeType()->isObjCInterfaceType() || |
| 2786 | // If is has NSObject attribute, OK as well. |
| 2787 | isObjCNSObjectType(Ty)) |
| 2788 | return true; |
| 2789 | |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 2790 | // Check to see if this is 'id' or 'Class', both of which are typedefs for |
| 2791 | // pointer types. This looks for the typedef specifically, not for the |
Chris Lattner | 16ede0e | 2009-04-12 23:51:02 +0000 | [diff] [blame] | 2792 | // underlying type. Iteratively strip off typedefs so that we can handle |
| 2793 | // typedefs of typedefs. |
| 2794 | while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 2795 | if (Ty.getUnqualifiedType() == getObjCIdType() || |
| 2796 | Ty.getUnqualifiedType() == getObjCClassType()) |
| 2797 | return true; |
| 2798 | |
| 2799 | Ty = TDT->getDecl()->getUnderlyingType(); |
| 2800 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 2801 | |
Chris Lattner | 16ede0e | 2009-04-12 23:51:02 +0000 | [diff] [blame] | 2802 | return false; |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 2805 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 2806 | /// garbage collection attribute. |
| 2807 | /// |
| 2808 | QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2809 | QualType::GCAttrTypes GCAttrs = QualType::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 2810 | if (getLangOptions().ObjC1 && |
| 2811 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2812 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 2813 | // Default behavious under objective-c's gc is for objective-c pointers |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 2814 | // (or pointers to them) be treated as though they were declared |
| 2815 | // as __strong. |
| 2816 | if (GCAttrs == QualType::GCNone) { |
| 2817 | if (isObjCObjectPointerType(Ty)) |
| 2818 | GCAttrs = QualType::Strong; |
| 2819 | else if (Ty->isPointerType()) |
| 2820 | return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType()); |
| 2821 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 2822 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 2823 | // set on them. |
Fariborz Jahanian | 8df7a28 | 2009-06-02 18:32:00 +0000 | [diff] [blame] | 2824 | else if (!Ty->isPointerType() && !isObjCObjectPointerType(Ty)) |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 2825 | return QualType::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 2826 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2827 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 2830 | //===----------------------------------------------------------------------===// |
| 2831 | // Type Compatibility Testing |
| 2832 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 2833 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 2834 | /// areCompatVectorTypes - Return true if the two specified vector types are |
| 2835 | /// compatible. |
| 2836 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 2837 | const VectorType *RHS) { |
| 2838 | assert(LHS->isCanonical() && RHS->isCanonical()); |
| 2839 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2840 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2843 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 2844 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 2845 | /// protocol qualifiers on the LHS or RHS. |
| 2846 | /// |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2847 | bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, |
| 2848 | const ObjCInterfaceType *RHS) { |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 2849 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 2850 | // the LHS. |
| 2851 | if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 2852 | return false; |
| 2853 | |
| 2854 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 2855 | // protocol qualified at all, then we are good. |
| 2856 | if (!isa<ObjCQualifiedInterfaceType>(LHS)) |
| 2857 | return true; |
| 2858 | |
| 2859 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 2860 | // isn't a superset. |
| 2861 | if (!isa<ObjCQualifiedInterfaceType>(RHS)) |
| 2862 | return true; // FIXME: should return false! |
| 2863 | |
| 2864 | // Finally, we must have two protocol-qualified interfaces. |
| 2865 | const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS); |
| 2866 | const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 2867 | |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 2868 | // All LHS protocols must have a presence on the RHS. |
| 2869 | assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?"); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 2870 | |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 2871 | for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(), |
| 2872 | LHSPE = LHSP->qual_end(); |
| 2873 | LHSPI != LHSPE; LHSPI++) { |
| 2874 | bool RHSImplementsProtocol = false; |
| 2875 | |
| 2876 | // If the RHS doesn't implement the protocol on the left, the types |
| 2877 | // are incompatible. |
| 2878 | for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(), |
| 2879 | RHSPE = RHSP->qual_end(); |
| 2880 | !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) { |
| 2881 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) |
| 2882 | RHSImplementsProtocol = true; |
| 2883 | } |
| 2884 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 2885 | if (!RHSImplementsProtocol) |
| 2886 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 2887 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 2888 | // The RHS implements all protocols listed on the LHS. |
| 2889 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2892 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 2893 | // get the "pointed to" types |
| 2894 | const PointerType *LHSPT = LHS->getAsPointerType(); |
| 2895 | const PointerType *RHSPT = RHS->getAsPointerType(); |
| 2896 | |
| 2897 | if (!LHSPT || !RHSPT) |
| 2898 | return false; |
| 2899 | |
| 2900 | QualType lhptee = LHSPT->getPointeeType(); |
| 2901 | QualType rhptee = RHSPT->getPointeeType(); |
| 2902 | const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); |
| 2903 | const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); |
| 2904 | // ID acts sort of like void* for ObjC interfaces |
| 2905 | if (LHSIface && isObjCIdStructType(rhptee)) |
| 2906 | return true; |
| 2907 | if (RHSIface && isObjCIdStructType(lhptee)) |
| 2908 | return true; |
| 2909 | if (!LHSIface || !RHSIface) |
| 2910 | return false; |
| 2911 | return canAssignObjCInterfaces(LHSIface, RHSIface) || |
| 2912 | canAssignObjCInterfaces(RHSIface, LHSIface); |
| 2913 | } |
| 2914 | |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 2915 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
| 2916 | /// both shall have the identically qualified version of a compatible type. |
| 2917 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
| 2918 | /// same. See 6.7.[2,3,5] for additional rules. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2919 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { |
| 2920 | return !mergeTypes(LHS, RHS).isNull(); |
| 2921 | } |
| 2922 | |
| 2923 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { |
| 2924 | const FunctionType *lbase = lhs->getAsFunctionType(); |
| 2925 | const FunctionType *rbase = rhs->getAsFunctionType(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2926 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 2927 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2928 | bool allLTypes = true; |
| 2929 | bool allRTypes = true; |
| 2930 | |
| 2931 | // Check return type |
| 2932 | QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); |
| 2933 | if (retType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2934 | if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) |
| 2935 | allLTypes = false; |
| 2936 | if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) |
| 2937 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2938 | |
| 2939 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2940 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 2941 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2942 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 2943 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 2944 | |
| 2945 | // Compatible functions must have the same number of arguments |
| 2946 | if (lproto_nargs != rproto_nargs) |
| 2947 | return QualType(); |
| 2948 | |
| 2949 | // Variadic and non-variadic functions aren't compatible |
| 2950 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 2951 | return QualType(); |
| 2952 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2953 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 2954 | return QualType(); |
| 2955 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2956 | // Check argument compatibility |
| 2957 | llvm::SmallVector<QualType, 10> types; |
| 2958 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 2959 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 2960 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
| 2961 | QualType argtype = mergeTypes(largtype, rargtype); |
| 2962 | if (argtype.isNull()) return QualType(); |
| 2963 | types.push_back(argtype); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2964 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 2965 | allLTypes = false; |
| 2966 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 2967 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2968 | } |
| 2969 | if (allLTypes) return lhs; |
| 2970 | if (allRTypes) return rhs; |
| 2971 | return getFunctionType(retType, types.begin(), types.size(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2972 | lproto->isVariadic(), lproto->getTypeQuals()); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
| 2975 | if (lproto) allRTypes = false; |
| 2976 | if (rproto) allLTypes = false; |
| 2977 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2978 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2979 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2980 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 2981 | if (proto->isVariadic()) return QualType(); |
| 2982 | // Check that the types are compatible with the types that |
| 2983 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 2984 | // The only types actually affected are promotable integer |
| 2985 | // types and floats, which would be passed as a different |
| 2986 | // type depending on whether the prototype is visible. |
| 2987 | unsigned proto_nargs = proto->getNumArgs(); |
| 2988 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 2989 | QualType argTy = proto->getArgType(i); |
| 2990 | if (argTy->isPromotableIntegerType() || |
| 2991 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 2992 | return QualType(); |
| 2993 | } |
| 2994 | |
| 2995 | if (allLTypes) return lhs; |
| 2996 | if (allRTypes) return rhs; |
| 2997 | return getFunctionType(retType, proto->arg_type_begin(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 2998 | proto->getNumArgs(), lproto->isVariadic(), |
| 2999 | lproto->getTypeQuals()); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | if (allLTypes) return lhs; |
| 3003 | if (allRTypes) return rhs; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3004 | return getFunctionNoProtoType(retType); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
| 3007 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 3008 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 3009 | // type is adjusted to "T" prior to any further analysis, the expression |
| 3010 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3011 | // expression is an lvalue unless the reference is an rvalue reference and |
| 3012 | // the expression is a function call (possibly inside parentheses). |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3013 | // FIXME: C++ shouldn't be going through here! The rules are different |
| 3014 | // enough that they should be handled separately. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3015 | // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really* |
| 3016 | // shouldn't be going through here! |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3017 | if (const ReferenceType *RT = LHS->getAsReferenceType()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 3018 | LHS = RT->getPointeeType(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3019 | if (const ReferenceType *RT = RHS->getAsReferenceType()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 3020 | RHS = RT->getPointeeType(); |
Chris Lattner | f3692dc | 2008-04-07 05:37:56 +0000 | [diff] [blame] | 3021 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3022 | QualType LHSCan = getCanonicalType(LHS), |
| 3023 | RHSCan = getCanonicalType(RHS); |
| 3024 | |
| 3025 | // If two types are identical, they are compatible. |
| 3026 | if (LHSCan == RHSCan) |
| 3027 | return LHS; |
| 3028 | |
| 3029 | // If the qualifiers are different, the types aren't compatible |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 3030 | // Note that we handle extended qualifiers later, in the |
| 3031 | // case for ExtQualType. |
| 3032 | if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers()) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3033 | return QualType(); |
| 3034 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 3035 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 3036 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3037 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3038 | // We want to consider the two function types to be the same for these |
| 3039 | // comparisons, just force one to the other. |
| 3040 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 3041 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 3042 | |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3043 | // Strip off objc_gc attributes off the top level so they can be merged. |
| 3044 | // This is a complete mess, but the attribute itself doesn't make much sense. |
Fariborz Jahanian | 585f7b2 | 2009-06-02 01:40:22 +0000 | [diff] [blame] | 3045 | if (RHSClass == Type::ExtQual) { |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3046 | QualType::GCAttrTypes GCAttr = RHSCan.getObjCGCAttr(); |
| 3047 | if (GCAttr != QualType::GCNone) { |
Fariborz Jahanian | 86f4385 | 2009-06-02 20:58:58 +0000 | [diff] [blame] | 3048 | QualType::GCAttrTypes GCLHSAttr = LHSCan.getObjCGCAttr(); |
Fariborz Jahanian | 8df7a28 | 2009-06-02 18:32:00 +0000 | [diff] [blame] | 3049 | // __weak attribute must appear on both declarations. |
Fariborz Jahanian | 86f4385 | 2009-06-02 20:58:58 +0000 | [diff] [blame] | 3050 | // __strong attribue is redundant if other decl is an objective-c |
| 3051 | // object pointer (or decorated with __strong attribute); otherwise |
| 3052 | // issue error. |
| 3053 | if ((GCAttr == QualType::Weak && GCLHSAttr != GCAttr) || |
| 3054 | (GCAttr == QualType::Strong && GCLHSAttr != GCAttr && |
| 3055 | LHSCan->isPointerType() && !isObjCObjectPointerType(LHSCan) && |
| 3056 | !isObjCIdStructType(LHSCan->getAsPointerType()->getPointeeType()))) |
Fariborz Jahanian | 8df7a28 | 2009-06-02 18:32:00 +0000 | [diff] [blame] | 3057 | return QualType(); |
| 3058 | |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3059 | RHS = QualType(cast<ExtQualType>(RHS.getDesugaredType())->getBaseType(), |
| 3060 | RHS.getCVRQualifiers()); |
| 3061 | QualType Result = mergeTypes(LHS, RHS); |
Fariborz Jahanian | 8df7a28 | 2009-06-02 18:32:00 +0000 | [diff] [blame] | 3062 | if (!Result.isNull()) { |
| 3063 | if (Result.getObjCGCAttr() == QualType::GCNone) |
| 3064 | Result = getObjCGCQualType(Result, GCAttr); |
| 3065 | else if (Result.getObjCGCAttr() != GCAttr) |
| 3066 | Result = QualType(); |
| 3067 | } |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3068 | return Result; |
| 3069 | } |
Fariborz Jahanian | 585f7b2 | 2009-06-02 01:40:22 +0000 | [diff] [blame] | 3070 | } |
Fariborz Jahanian | 585f7b2 | 2009-06-02 01:40:22 +0000 | [diff] [blame] | 3071 | if (LHSClass == Type::ExtQual) { |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3072 | QualType::GCAttrTypes GCAttr = LHSCan.getObjCGCAttr(); |
| 3073 | if (GCAttr != QualType::GCNone) { |
Fariborz Jahanian | 8df7a28 | 2009-06-02 18:32:00 +0000 | [diff] [blame] | 3074 | QualType::GCAttrTypes GCRHSAttr = RHSCan.getObjCGCAttr(); |
| 3075 | // __weak attribute must appear on both declarations. __strong |
Fariborz Jahanian | 86f4385 | 2009-06-02 20:58:58 +0000 | [diff] [blame] | 3076 | // __strong attribue is redundant if other decl is an objective-c |
| 3077 | // object pointer (or decorated with __strong attribute); otherwise |
| 3078 | // issue error. |
| 3079 | if ((GCAttr == QualType::Weak && GCRHSAttr != GCAttr) || |
| 3080 | (GCAttr == QualType::Strong && GCRHSAttr != GCAttr && |
| 3081 | RHSCan->isPointerType() && !isObjCObjectPointerType(RHSCan) && |
| 3082 | !isObjCIdStructType(RHSCan->getAsPointerType()->getPointeeType()))) |
Fariborz Jahanian | 8df7a28 | 2009-06-02 18:32:00 +0000 | [diff] [blame] | 3083 | return QualType(); |
Fariborz Jahanian | 86f4385 | 2009-06-02 20:58:58 +0000 | [diff] [blame] | 3084 | |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3085 | LHS = QualType(cast<ExtQualType>(LHS.getDesugaredType())->getBaseType(), |
| 3086 | LHS.getCVRQualifiers()); |
| 3087 | QualType Result = mergeTypes(LHS, RHS); |
Fariborz Jahanian | 8df7a28 | 2009-06-02 18:32:00 +0000 | [diff] [blame] | 3088 | if (!Result.isNull()) { |
| 3089 | if (Result.getObjCGCAttr() == QualType::GCNone) |
| 3090 | Result = getObjCGCQualType(Result, GCAttr); |
| 3091 | else if (Result.getObjCGCAttr() != GCAttr) |
| 3092 | Result = QualType(); |
| 3093 | } |
Eli Friedman | 354e53d | 2009-06-02 07:45:37 +0000 | [diff] [blame] | 3094 | return Result; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3095 | } |
Fariborz Jahanian | 585f7b2 | 2009-06-02 01:40:22 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 3098 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 3099 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 3100 | LHSClass = Type::ConstantArray; |
| 3101 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 3102 | RHSClass = Type::ConstantArray; |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 3103 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3104 | // Canonicalize ExtVector -> Vector. |
| 3105 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 3106 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 3107 | |
Chris Lattner | b048981 | 2008-04-07 06:38:24 +0000 | [diff] [blame] | 3108 | // Consider qualified interfaces and interfaces the same. |
| 3109 | if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface; |
| 3110 | if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3111 | |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 3112 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3113 | if (LHSClass != RHSClass) { |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 3114 | const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType(); |
| 3115 | const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType(); |
Fariborz Jahanian | c8d2e77 | 2009-04-15 21:54:48 +0000 | [diff] [blame] | 3116 | |
Steve Naroff | d824c9c | 2009-04-14 15:11:46 +0000 | [diff] [blame] | 3117 | // 'id' and 'Class' act sort of like void* for ObjC interfaces |
| 3118 | if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS))) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 3119 | return LHS; |
Steve Naroff | d824c9c | 2009-04-14 15:11:46 +0000 | [diff] [blame] | 3120 | if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS))) |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 3121 | return RHS; |
| 3122 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 3123 | // ID is compatible with all qualified id types. |
| 3124 | if (LHS->isObjCQualifiedIdType()) { |
| 3125 | if (const PointerType *PT = RHS->getAsPointerType()) { |
| 3126 | QualType pType = PT->getPointeeType(); |
Steve Naroff | d824c9c | 2009-04-14 15:11:46 +0000 | [diff] [blame] | 3127 | if (isObjCIdStructType(pType) || isObjCClassStructType(pType)) |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 3128 | return LHS; |
| 3129 | // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true). |
| 3130 | // Unfortunately, this API is part of Sema (which we don't have access |
| 3131 | // to. Need to refactor. The following check is insufficient, since we |
| 3132 | // need to make sure the class implements the protocol. |
| 3133 | if (pType->isObjCInterfaceType()) |
| 3134 | return LHS; |
| 3135 | } |
| 3136 | } |
| 3137 | if (RHS->isObjCQualifiedIdType()) { |
| 3138 | if (const PointerType *PT = LHS->getAsPointerType()) { |
| 3139 | QualType pType = PT->getPointeeType(); |
Steve Naroff | d824c9c | 2009-04-14 15:11:46 +0000 | [diff] [blame] | 3140 | if (isObjCIdStructType(pType) || isObjCClassStructType(pType)) |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 3141 | return RHS; |
| 3142 | // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true). |
| 3143 | // Unfortunately, this API is part of Sema (which we don't have access |
| 3144 | // to. Need to refactor. The following check is insufficient, since we |
| 3145 | // need to make sure the class implements the protocol. |
| 3146 | if (pType->isObjCInterfaceType()) |
| 3147 | return RHS; |
| 3148 | } |
| 3149 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3150 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 3151 | // a signed integer type, or an unsigned integer type. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3152 | if (const EnumType* ETy = LHS->getAsEnumType()) { |
| 3153 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 3154 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 3155 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3156 | if (const EnumType* ETy = RHS->getAsEnumType()) { |
| 3157 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 3158 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 3159 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3160 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3161 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 3162 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3163 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 3164 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3165 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3166 | #define TYPE(Class, Base) |
| 3167 | #define ABSTRACT_TYPE(Class, Base) |
| 3168 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 3169 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 3170 | #include "clang/AST/TypeNodes.def" |
| 3171 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 3172 | return QualType(); |
| 3173 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3174 | case Type::LValueReference: |
| 3175 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3176 | case Type::MemberPointer: |
| 3177 | assert(false && "C++ should never be in mergeTypes"); |
| 3178 | return QualType(); |
| 3179 | |
| 3180 | case Type::IncompleteArray: |
| 3181 | case Type::VariableArray: |
| 3182 | case Type::FunctionProto: |
| 3183 | case Type::ExtVector: |
| 3184 | case Type::ObjCQualifiedInterface: |
| 3185 | assert(false && "Types are eliminated above"); |
| 3186 | return QualType(); |
| 3187 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3188 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3189 | { |
| 3190 | // Merge two pointer types, while trying to preserve typedef info |
| 3191 | QualType LHSPointee = LHS->getAsPointerType()->getPointeeType(); |
| 3192 | QualType RHSPointee = RHS->getAsPointerType()->getPointeeType(); |
| 3193 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 3194 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3195 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3196 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3197 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3198 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3199 | return getPointerType(ResultType); |
| 3200 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 3201 | case Type::BlockPointer: |
| 3202 | { |
| 3203 | // Merge two block pointer types, while trying to preserve typedef info |
| 3204 | QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType(); |
| 3205 | QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType(); |
| 3206 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 3207 | if (ResultType.isNull()) return QualType(); |
| 3208 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 3209 | return LHS; |
| 3210 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 3211 | return RHS; |
| 3212 | return getBlockPointerType(ResultType); |
| 3213 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3214 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3215 | { |
| 3216 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 3217 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 3218 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 3219 | return QualType(); |
| 3220 | |
| 3221 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 3222 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
| 3223 | QualType ResultType = mergeTypes(LHSElem, RHSElem); |
| 3224 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3225 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 3226 | return LHS; |
| 3227 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 3228 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 3229 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 3230 | ArrayType::ArraySizeModifier(), 0); |
| 3231 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 3232 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3233 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 3234 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3235 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 3236 | return LHS; |
| 3237 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 3238 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3239 | if (LVAT) { |
| 3240 | // FIXME: This isn't correct! But tricky to implement because |
| 3241 | // the array's size has to be the size of LHS, but the type |
| 3242 | // has to be different. |
| 3243 | return LHS; |
| 3244 | } |
| 3245 | if (RVAT) { |
| 3246 | // FIXME: This isn't correct! But tricky to implement because |
| 3247 | // the array's size has to be the size of RHS, but the type |
| 3248 | // has to be different. |
| 3249 | return RHS; |
| 3250 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 3251 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 3252 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3253 | return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3254 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3255 | case Type::FunctionNoProto: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3256 | return mergeFunctionTypes(LHS, RHS); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3257 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3258 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3259 | // FIXME: Why are these compatible? |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3260 | if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS; |
| 3261 | if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3262 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3263 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 3264 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3265 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 3266 | case Type::Complex: |
| 3267 | // Distinct complex types are incompatible. |
| 3268 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 3269 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 3270 | // FIXME: The merged type should be an ExtVector! |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3271 | if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType())) |
| 3272 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3273 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 3274 | case Type::ObjCInterface: { |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 3275 | // Check if the interfaces are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 3276 | // FIXME: This should be type compatibility, e.g. whether |
| 3277 | // "LHS x; RHS x;" at global scope is legal. |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 3278 | const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType(); |
| 3279 | const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType(); |
| 3280 | if (LHSIface && RHSIface && |
| 3281 | canAssignObjCInterfaces(LHSIface, RHSIface)) |
| 3282 | return LHS; |
| 3283 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3284 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 3285 | } |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 3286 | case Type::ObjCQualifiedId: |
| 3287 | // Distinct qualified id's are not compatible. |
| 3288 | return QualType(); |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 3289 | case Type::FixedWidthInt: |
| 3290 | // Distinct fixed-width integers are not compatible. |
| 3291 | return QualType(); |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 3292 | case Type::ExtQual: |
| 3293 | // FIXME: ExtQual types can be compatible even if they're not |
| 3294 | // identical! |
| 3295 | return QualType(); |
| 3296 | // First attempt at an implementation, but I'm not really sure it's |
| 3297 | // right... |
| 3298 | #if 0 |
| 3299 | ExtQualType* LQual = cast<ExtQualType>(LHSCan); |
| 3300 | ExtQualType* RQual = cast<ExtQualType>(RHSCan); |
| 3301 | if (LQual->getAddressSpace() != RQual->getAddressSpace() || |
| 3302 | LQual->getObjCGCAttr() != RQual->getObjCGCAttr()) |
| 3303 | return QualType(); |
| 3304 | QualType LHSBase, RHSBase, ResultType, ResCanUnqual; |
| 3305 | LHSBase = QualType(LQual->getBaseType(), 0); |
| 3306 | RHSBase = QualType(RQual->getBaseType(), 0); |
| 3307 | ResultType = mergeTypes(LHSBase, RHSBase); |
| 3308 | if (ResultType.isNull()) return QualType(); |
| 3309 | ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType(); |
| 3310 | if (LHSCan.getUnqualifiedType() == ResCanUnqual) |
| 3311 | return LHS; |
| 3312 | if (RHSCan.getUnqualifiedType() == ResCanUnqual) |
| 3313 | return RHS; |
| 3314 | ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace()); |
| 3315 | ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr()); |
| 3316 | ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers()); |
| 3317 | return ResultType; |
| 3318 | #endif |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3319 | |
| 3320 | case Type::TemplateSpecialization: |
| 3321 | assert(false && "Dependent types have no size"); |
| 3322 | break; |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 3323 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3324 | |
| 3325 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 3326 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 3327 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 3328 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 3329 | // Integer Predicates |
| 3330 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 3331 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 3332 | unsigned ASTContext::getIntWidth(QualType T) { |
| 3333 | if (T == BoolTy) |
| 3334 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3335 | if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) { |
| 3336 | return FWIT->getWidth(); |
| 3337 | } |
| 3338 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 3339 | return (unsigned)getTypeSize(T); |
| 3340 | } |
| 3341 | |
| 3342 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
| 3343 | assert(T->isSignedIntegerType() && "Unexpected type"); |
| 3344 | if (const EnumType* ETy = T->getAsEnumType()) |
| 3345 | T = ETy->getDecl()->getIntegerType(); |
| 3346 | const BuiltinType* BTy = T->getAsBuiltinType(); |
| 3347 | assert (BTy && "Unexpected signed integer type"); |
| 3348 | switch (BTy->getKind()) { |
| 3349 | case BuiltinType::Char_S: |
| 3350 | case BuiltinType::SChar: |
| 3351 | return UnsignedCharTy; |
| 3352 | case BuiltinType::Short: |
| 3353 | return UnsignedShortTy; |
| 3354 | case BuiltinType::Int: |
| 3355 | return UnsignedIntTy; |
| 3356 | case BuiltinType::Long: |
| 3357 | return UnsignedLongTy; |
| 3358 | case BuiltinType::LongLong: |
| 3359 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3360 | case BuiltinType::Int128: |
| 3361 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 3362 | default: |
| 3363 | assert(0 && "Unexpected signed integer type"); |
| 3364 | return QualType(); |
| 3365 | } |
| 3366 | } |
| 3367 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3368 | ExternalASTSource::~ExternalASTSource() { } |
| 3369 | |
| 3370 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 3371 | |
| 3372 | |
| 3373 | //===----------------------------------------------------------------------===// |
| 3374 | // Builtin Type Computation |
| 3375 | //===----------------------------------------------------------------------===// |
| 3376 | |
| 3377 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
| 3378 | /// pointer over the consumed characters. This returns the resultant type. |
| 3379 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
| 3380 | ASTContext::GetBuiltinTypeError &Error, |
| 3381 | bool AllowTypeModifiers = true) { |
| 3382 | // Modifiers. |
| 3383 | int HowLong = 0; |
| 3384 | bool Signed = false, Unsigned = false; |
| 3385 | |
| 3386 | // Read the modifiers first. |
| 3387 | bool Done = false; |
| 3388 | while (!Done) { |
| 3389 | switch (*Str++) { |
| 3390 | default: Done = true; --Str; break; |
| 3391 | case 'S': |
| 3392 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 3393 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 3394 | Signed = true; |
| 3395 | break; |
| 3396 | case 'U': |
| 3397 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 3398 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 3399 | Unsigned = true; |
| 3400 | break; |
| 3401 | case 'L': |
| 3402 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 3403 | ++HowLong; |
| 3404 | break; |
| 3405 | } |
| 3406 | } |
| 3407 | |
| 3408 | QualType Type; |
| 3409 | |
| 3410 | // Read the base type. |
| 3411 | switch (*Str++) { |
| 3412 | default: assert(0 && "Unknown builtin type letter!"); |
| 3413 | case 'v': |
| 3414 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 3415 | "Bad modifiers used with 'v'!"); |
| 3416 | Type = Context.VoidTy; |
| 3417 | break; |
| 3418 | case 'f': |
| 3419 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 3420 | "Bad modifiers used with 'f'!"); |
| 3421 | Type = Context.FloatTy; |
| 3422 | break; |
| 3423 | case 'd': |
| 3424 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 3425 | "Bad modifiers used with 'd'!"); |
| 3426 | if (HowLong) |
| 3427 | Type = Context.LongDoubleTy; |
| 3428 | else |
| 3429 | Type = Context.DoubleTy; |
| 3430 | break; |
| 3431 | case 's': |
| 3432 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 3433 | if (Unsigned) |
| 3434 | Type = Context.UnsignedShortTy; |
| 3435 | else |
| 3436 | Type = Context.ShortTy; |
| 3437 | break; |
| 3438 | case 'i': |
| 3439 | if (HowLong == 3) |
| 3440 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 3441 | else if (HowLong == 2) |
| 3442 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 3443 | else if (HowLong == 1) |
| 3444 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 3445 | else |
| 3446 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 3447 | break; |
| 3448 | case 'c': |
| 3449 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 3450 | if (Signed) |
| 3451 | Type = Context.SignedCharTy; |
| 3452 | else if (Unsigned) |
| 3453 | Type = Context.UnsignedCharTy; |
| 3454 | else |
| 3455 | Type = Context.CharTy; |
| 3456 | break; |
| 3457 | case 'b': // boolean |
| 3458 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 3459 | Type = Context.BoolTy; |
| 3460 | break; |
| 3461 | case 'z': // size_t. |
| 3462 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 3463 | Type = Context.getSizeType(); |
| 3464 | break; |
| 3465 | case 'F': |
| 3466 | Type = Context.getCFConstantStringType(); |
| 3467 | break; |
| 3468 | case 'a': |
| 3469 | Type = Context.getBuiltinVaListType(); |
| 3470 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 3471 | break; |
| 3472 | case 'A': |
| 3473 | // This is a "reference" to a va_list; however, what exactly |
| 3474 | // this means depends on how va_list is defined. There are two |
| 3475 | // different kinds of va_list: ones passed by value, and ones |
| 3476 | // passed by reference. An example of a by-value va_list is |
| 3477 | // x86, where va_list is a char*. An example of by-ref va_list |
| 3478 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 3479 | // we want this argument to be a char*&; for x86-64, we want |
| 3480 | // it to be a __va_list_tag*. |
| 3481 | Type = Context.getBuiltinVaListType(); |
| 3482 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 3483 | if (Type->isArrayType()) { |
| 3484 | Type = Context.getArrayDecayedType(Type); |
| 3485 | } else { |
| 3486 | Type = Context.getLValueReferenceType(Type); |
| 3487 | } |
| 3488 | break; |
| 3489 | case 'V': { |
| 3490 | char *End; |
| 3491 | |
| 3492 | unsigned NumElements = strtoul(Str, &End, 10); |
| 3493 | assert(End != Str && "Missing vector size"); |
| 3494 | |
| 3495 | Str = End; |
| 3496 | |
| 3497 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 3498 | Type = Context.getVectorType(ElementType, NumElements); |
| 3499 | break; |
| 3500 | } |
| 3501 | case 'P': { |
| 3502 | IdentifierInfo *II = &Context.Idents.get("FILE"); |
| 3503 | DeclContext::lookup_result Lookup |
| 3504 | = Context.getTranslationUnitDecl()->lookup(Context, II); |
| 3505 | if (Lookup.first != Lookup.second && isa<TypeDecl>(*Lookup.first)) { |
| 3506 | Type = Context.getTypeDeclType(cast<TypeDecl>(*Lookup.first)); |
| 3507 | break; |
| 3508 | } |
| 3509 | else { |
| 3510 | Error = ASTContext::GE_Missing_FILE; |
| 3511 | return QualType(); |
| 3512 | } |
| 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | if (!AllowTypeModifiers) |
| 3517 | return Type; |
| 3518 | |
| 3519 | Done = false; |
| 3520 | while (!Done) { |
| 3521 | switch (*Str++) { |
| 3522 | default: Done = true; --Str; break; |
| 3523 | case '*': |
| 3524 | Type = Context.getPointerType(Type); |
| 3525 | break; |
| 3526 | case '&': |
| 3527 | Type = Context.getLValueReferenceType(Type); |
| 3528 | break; |
| 3529 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 3530 | case 'C': |
| 3531 | Type = Type.getQualifiedType(QualType::Const); |
| 3532 | break; |
| 3533 | } |
| 3534 | } |
| 3535 | |
| 3536 | return Type; |
| 3537 | } |
| 3538 | |
| 3539 | /// GetBuiltinType - Return the type for the specified builtin. |
| 3540 | QualType ASTContext::GetBuiltinType(unsigned id, |
| 3541 | GetBuiltinTypeError &Error) { |
| 3542 | const char *TypeStr = BuiltinInfo.GetTypeString(id); |
| 3543 | |
| 3544 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 3545 | |
| 3546 | Error = GE_None; |
| 3547 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); |
| 3548 | if (Error != GE_None) |
| 3549 | return QualType(); |
| 3550 | while (TypeStr[0] && TypeStr[0] != '.') { |
| 3551 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); |
| 3552 | if (Error != GE_None) |
| 3553 | return QualType(); |
| 3554 | |
| 3555 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 3556 | if (Ty->isArrayType()) |
| 3557 | Ty = getArrayDecayedType(Ty); |
| 3558 | |
| 3559 | ArgTypes.push_back(Ty); |
| 3560 | } |
| 3561 | |
| 3562 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 3563 | "'.' should only occur at end of builtin type list!"); |
| 3564 | |
| 3565 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 3566 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 3567 | return getFunctionNoProtoType(ResType); |
| 3568 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), |
| 3569 | TypeStr[0] == '.', 0); |
| 3570 | } |