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