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