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