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), |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 41 | ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), |
| 42 | 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 | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 471 | if (const ReferenceType* RT = T->getAsReferenceType()) { |
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 | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 997 | QualType Pointee = T->getAsPointerType()->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) { |
| 1045 | if (T->isPointerType()) { |
| 1046 | QualType Pointee = T->getAsPointerType()->getPointeeType(); |
| 1047 | QualType ResultType = getNoReturnType(Pointee); |
| 1048 | return getPointerType(ResultType); |
| 1049 | } |
| 1050 | if (T->isBlockPointerType()) { |
| 1051 | QualType Pointee = T->getAsBlockPointerType()->getPointeeType(); |
| 1052 | QualType ResultType = getNoReturnType(Pointee); |
| 1053 | return getBlockPointerType(ResultType); |
| 1054 | } |
| 1055 | if (!T->isFunctionType()) |
| 1056 | assert(0 && "can't noreturn qualify non-pointer to function or block type"); |
| 1057 | |
| 1058 | if (const FunctionNoProtoType *F = dyn_cast<FunctionNoProtoType>(T)) { |
| 1059 | return getFunctionNoProtoType(F->getResultType(), true); |
| 1060 | } |
| 1061 | const FunctionProtoType *F = cast<FunctionProtoType>(T); |
| 1062 | return getFunctionType(F->getResultType(), F->arg_type_begin(), |
| 1063 | F->getNumArgs(), F->isVariadic(), F->getTypeQuals(), |
| 1064 | F->hasExceptionSpec(), F->hasAnyExceptionSpec(), |
| 1065 | F->getNumExceptions(), F->exception_begin(), true); |
| 1066 | } |
| 1067 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1068 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1069 | /// number with the specified element type. |
| 1070 | QualType ASTContext::getComplexType(QualType T) { |
| 1071 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1072 | // structure. |
| 1073 | llvm::FoldingSetNodeID ID; |
| 1074 | ComplexType::Profile(ID, T); |
| 1075 | |
| 1076 | void *InsertPos = 0; |
| 1077 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1078 | return QualType(CT, 0); |
| 1079 | |
| 1080 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1081 | // so fill in the canonical type field. |
| 1082 | QualType Canonical; |
| 1083 | if (!T->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1084 | Canonical = getComplexType(getCanonicalType(T)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1085 | |
| 1086 | // Get the new insert position for the node we care about. |
| 1087 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1088 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1089 | } |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1090 | ComplexType *New = new (*this,8) ComplexType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1091 | Types.push_back(New); |
| 1092 | ComplexTypes.InsertNode(New, InsertPos); |
| 1093 | return QualType(New, 0); |
| 1094 | } |
| 1095 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1096 | QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) { |
| 1097 | llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ? |
| 1098 | SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes; |
| 1099 | FixedWidthIntType *&Entry = Map[Width]; |
| 1100 | if (!Entry) |
| 1101 | Entry = new FixedWidthIntType(Width, Signed); |
| 1102 | return QualType(Entry, 0); |
| 1103 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1104 | |
| 1105 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1106 | /// the specified type. |
| 1107 | QualType ASTContext::getPointerType(QualType T) { |
| 1108 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1109 | // structure. |
| 1110 | llvm::FoldingSetNodeID ID; |
| 1111 | PointerType::Profile(ID, T); |
| 1112 | |
| 1113 | void *InsertPos = 0; |
| 1114 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1115 | return QualType(PT, 0); |
| 1116 | |
| 1117 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1118 | // so fill in the canonical type field. |
| 1119 | QualType Canonical; |
| 1120 | if (!T->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1121 | Canonical = getPointerType(getCanonicalType(T)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1122 | |
| 1123 | // Get the new insert position for the node we care about. |
| 1124 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1125 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1126 | } |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1127 | PointerType *New = new (*this,8) PointerType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1128 | Types.push_back(New); |
| 1129 | PointerTypes.InsertNode(New, InsertPos); |
| 1130 | return QualType(New, 0); |
| 1131 | } |
| 1132 | |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1133 | /// getBlockPointerType - Return the uniqued reference to the type for |
| 1134 | /// a pointer to the specified block. |
| 1135 | QualType ASTContext::getBlockPointerType(QualType T) { |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1136 | assert(T->isFunctionType() && "block of function types only"); |
| 1137 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1138 | // structure. |
| 1139 | llvm::FoldingSetNodeID ID; |
| 1140 | BlockPointerType::Profile(ID, T); |
| 1141 | |
| 1142 | void *InsertPos = 0; |
| 1143 | if (BlockPointerType *PT = |
| 1144 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1145 | return QualType(PT, 0); |
| 1146 | |
Steve Naroff | 296e8d5 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1147 | // 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] | 1148 | // type either so fill in the canonical type field. |
| 1149 | QualType Canonical; |
| 1150 | if (!T->isCanonical()) { |
| 1151 | Canonical = getBlockPointerType(getCanonicalType(T)); |
| 1152 | |
| 1153 | // Get the new insert position for the node we care about. |
| 1154 | BlockPointerType *NewIP = |
| 1155 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1156 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1157 | } |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1158 | BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1159 | Types.push_back(New); |
| 1160 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1161 | return QualType(New, 0); |
| 1162 | } |
| 1163 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1164 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1165 | /// lvalue reference to the specified type. |
| 1166 | QualType ASTContext::getLValueReferenceType(QualType T) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1167 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1168 | // structure. |
| 1169 | llvm::FoldingSetNodeID ID; |
| 1170 | ReferenceType::Profile(ID, T); |
| 1171 | |
| 1172 | void *InsertPos = 0; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1173 | if (LValueReferenceType *RT = |
| 1174 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1175 | return QualType(RT, 0); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1176 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1177 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1178 | // either, so fill in the canonical type field. |
| 1179 | QualType Canonical; |
| 1180 | if (!T->isCanonical()) { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1181 | Canonical = getLValueReferenceType(getCanonicalType(T)); |
| 1182 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1183 | // Get the new insert position for the node we care about. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1184 | LValueReferenceType *NewIP = |
| 1185 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1186 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1189 | LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1190 | Types.push_back(New); |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1191 | LValueReferenceTypes.InsertNode(New, InsertPos); |
| 1192 | return QualType(New, 0); |
| 1193 | } |
| 1194 | |
| 1195 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1196 | /// rvalue reference to the specified type. |
| 1197 | QualType ASTContext::getRValueReferenceType(QualType T) { |
| 1198 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1199 | // structure. |
| 1200 | llvm::FoldingSetNodeID ID; |
| 1201 | ReferenceType::Profile(ID, T); |
| 1202 | |
| 1203 | void *InsertPos = 0; |
| 1204 | if (RValueReferenceType *RT = |
| 1205 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1206 | return QualType(RT, 0); |
| 1207 | |
| 1208 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1209 | // either, so fill in the canonical type field. |
| 1210 | QualType Canonical; |
| 1211 | if (!T->isCanonical()) { |
| 1212 | Canonical = getRValueReferenceType(getCanonicalType(T)); |
| 1213 | |
| 1214 | // Get the new insert position for the node we care about. |
| 1215 | RValueReferenceType *NewIP = |
| 1216 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1217 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1218 | } |
| 1219 | |
| 1220 | RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical); |
| 1221 | Types.push_back(New); |
| 1222 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1223 | return QualType(New, 0); |
| 1224 | } |
| 1225 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1226 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1227 | /// member pointer to the specified type, in the specified class. |
| 1228 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) |
| 1229 | { |
| 1230 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1231 | // structure. |
| 1232 | llvm::FoldingSetNodeID ID; |
| 1233 | MemberPointerType::Profile(ID, T, Cls); |
| 1234 | |
| 1235 | void *InsertPos = 0; |
| 1236 | if (MemberPointerType *PT = |
| 1237 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1238 | return QualType(PT, 0); |
| 1239 | |
| 1240 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1241 | // type either, so fill in the canonical type field. |
| 1242 | QualType Canonical; |
| 1243 | if (!T->isCanonical()) { |
| 1244 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1245 | |
| 1246 | // Get the new insert position for the node we care about. |
| 1247 | MemberPointerType *NewIP = |
| 1248 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1249 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
| 1250 | } |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1251 | MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1252 | Types.push_back(New); |
| 1253 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1254 | return QualType(New, 0); |
| 1255 | } |
| 1256 | |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1257 | /// getConstantArrayType - Return the unique reference to the type for an |
| 1258 | /// array of the specified element type. |
| 1259 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1260 | const llvm::APInt &ArySizeIn, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1261 | ArrayType::ArraySizeModifier ASM, |
| 1262 | unsigned EltTypeQuals) { |
Eli Friedman | 587cbdf | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1263 | assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) && |
| 1264 | "Constant array of VLAs is illegal!"); |
| 1265 | |
Chris Lattner | 38aeec7 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1266 | // Convert the array size into a canonical width matching the pointer size for |
| 1267 | // the target. |
| 1268 | llvm::APInt ArySize(ArySizeIn); |
| 1269 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
| 1270 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1271 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1272 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1273 | |
| 1274 | void *InsertPos = 0; |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1275 | if (ConstantArrayType *ATP = |
| 1276 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1277 | return QualType(ATP, 0); |
| 1278 | |
| 1279 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1280 | // so fill in the canonical type field. |
| 1281 | QualType Canonical; |
| 1282 | if (!EltTy->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1283 | Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1284 | ASM, EltTypeQuals); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1285 | // Get the new insert position for the node we care about. |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1286 | ConstantArrayType *NewIP = |
| 1287 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1288 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1291 | ConstantArrayType *New = |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1292 | new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals); |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1293 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1294 | Types.push_back(New); |
| 1295 | return QualType(New, 0); |
| 1296 | } |
| 1297 | |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1298 | /// getConstantArrayWithExprType - Return a reference to the type for |
| 1299 | /// an array of the specified element type. |
| 1300 | QualType |
| 1301 | ASTContext::getConstantArrayWithExprType(QualType EltTy, |
| 1302 | const llvm::APInt &ArySizeIn, |
| 1303 | Expr *ArySizeExpr, |
| 1304 | ArrayType::ArraySizeModifier ASM, |
| 1305 | unsigned EltTypeQuals, |
| 1306 | SourceRange Brackets) { |
| 1307 | // Convert the array size into a canonical width matching the pointer |
| 1308 | // size for the target. |
| 1309 | llvm::APInt ArySize(ArySizeIn); |
| 1310 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
| 1311 | |
| 1312 | // Compute the canonical ConstantArrayType. |
| 1313 | QualType Canonical = getConstantArrayType(getCanonicalType(EltTy), |
| 1314 | ArySize, ASM, EltTypeQuals); |
| 1315 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1316 | // that have an expression provided for their size. |
| 1317 | ConstantArrayWithExprType *New = |
| 1318 | new(*this,8)ConstantArrayWithExprType(EltTy, Canonical, |
| 1319 | ArySize, ArySizeExpr, |
| 1320 | ASM, EltTypeQuals, Brackets); |
| 1321 | Types.push_back(New); |
| 1322 | return QualType(New, 0); |
| 1323 | } |
| 1324 | |
| 1325 | /// getConstantArrayWithoutExprType - Return a reference to the type for |
| 1326 | /// an array of the specified element type. |
| 1327 | QualType |
| 1328 | ASTContext::getConstantArrayWithoutExprType(QualType EltTy, |
| 1329 | const llvm::APInt &ArySizeIn, |
| 1330 | ArrayType::ArraySizeModifier ASM, |
| 1331 | unsigned EltTypeQuals) { |
| 1332 | // Convert the array size into a canonical width matching the pointer |
| 1333 | // size for the target. |
| 1334 | llvm::APInt ArySize(ArySizeIn); |
| 1335 | ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace())); |
| 1336 | |
| 1337 | // Compute the canonical ConstantArrayType. |
| 1338 | QualType Canonical = getConstantArrayType(getCanonicalType(EltTy), |
| 1339 | ArySize, ASM, EltTypeQuals); |
| 1340 | ConstantArrayWithoutExprType *New = |
| 1341 | new(*this,8)ConstantArrayWithoutExprType(EltTy, Canonical, |
| 1342 | ArySize, ASM, EltTypeQuals); |
| 1343 | Types.push_back(New); |
| 1344 | return QualType(New, 0); |
| 1345 | } |
| 1346 | |
Steve Naroff | bdbf7b0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1347 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1348 | /// variable array of the specified element type. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1349 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1350 | Expr *NumElts, |
Steve Naroff | c940612 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1351 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1352 | unsigned EltTypeQuals, |
| 1353 | SourceRange Brackets) { |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1354 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 1355 | // that have an expression provided for their size. |
| 1356 | |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1357 | VariableArrayType *New = |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1358 | new(*this,8)VariableArrayType(EltTy, QualType(), |
| 1359 | NumElts, ASM, EltTypeQuals, Brackets); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1360 | |
| 1361 | VariableArrayTypes.push_back(New); |
| 1362 | Types.push_back(New); |
| 1363 | return QualType(New, 0); |
| 1364 | } |
| 1365 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1366 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 1367 | /// the type for a dependently-sized array of the specified element |
| 1368 | /// type. FIXME: We will need these to be uniqued, or at least |
| 1369 | /// comparable, at some point. |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1370 | QualType ASTContext::getDependentSizedArrayType(QualType EltTy, |
| 1371 | Expr *NumElts, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1372 | ArrayType::ArraySizeModifier ASM, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1373 | unsigned EltTypeQuals, |
| 1374 | SourceRange Brackets) { |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1375 | assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) && |
| 1376 | "Size must be type- or value-dependent!"); |
| 1377 | |
| 1378 | // Since we don't unique expressions, it isn't possible to unique |
| 1379 | // dependently-sized array types. |
| 1380 | |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1381 | DependentSizedArrayType *New = |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1382 | new (*this,8) DependentSizedArrayType(EltTy, QualType(), |
| 1383 | NumElts, ASM, EltTypeQuals, |
| 1384 | Brackets); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1385 | |
| 1386 | DependentSizedArrayTypes.push_back(New); |
| 1387 | Types.push_back(New); |
| 1388 | return QualType(New, 0); |
| 1389 | } |
| 1390 | |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1391 | QualType ASTContext::getIncompleteArrayType(QualType EltTy, |
| 1392 | ArrayType::ArraySizeModifier ASM, |
| 1393 | unsigned EltTypeQuals) { |
| 1394 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 0be2ef2 | 2009-02-19 17:31:02 +0000 | [diff] [blame] | 1395 | IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1396 | |
| 1397 | void *InsertPos = 0; |
| 1398 | if (IncompleteArrayType *ATP = |
| 1399 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1400 | return QualType(ATP, 0); |
| 1401 | |
| 1402 | // If the element type isn't canonical, this won't be a canonical type |
| 1403 | // either, so fill in the canonical type field. |
| 1404 | QualType Canonical; |
| 1405 | |
| 1406 | if (!EltTy->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1407 | Canonical = getIncompleteArrayType(getCanonicalType(EltTy), |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1408 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1409 | |
| 1410 | // Get the new insert position for the node we care about. |
| 1411 | IncompleteArrayType *NewIP = |
| 1412 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1413 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Ted Kremenek | 2bd24ba | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 1414 | } |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1415 | |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1416 | IncompleteArrayType *New |
| 1417 | = new (*this,8) IncompleteArrayType(EltTy, Canonical, |
| 1418 | ASM, EltTypeQuals); |
Eli Friedman | c5773c4 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 1419 | |
| 1420 | IncompleteArrayTypes.InsertNode(New, InsertPos); |
| 1421 | Types.push_back(New); |
| 1422 | return QualType(New, 0); |
Steve Naroff | fb22d96 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1425 | /// getVectorType - Return the unique reference to a vector type of |
| 1426 | /// the specified element type and size. VectorType must be a built-in type. |
| 1427 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1428 | BuiltinType *baseType; |
| 1429 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1430 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1431 | assert(baseType != 0 && "getVectorType(): Expecting a built-in type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1432 | |
| 1433 | // Check if we've already instantiated a vector of this type. |
| 1434 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1435 | VectorType::Profile(ID, vecType, NumElts, Type::Vector); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1436 | void *InsertPos = 0; |
| 1437 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1438 | return QualType(VTP, 0); |
| 1439 | |
| 1440 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1441 | // so fill in the canonical type field. |
| 1442 | QualType Canonical; |
| 1443 | if (!vecType->isCanonical()) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1444 | Canonical = getVectorType(getCanonicalType(vecType), NumElts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1445 | |
| 1446 | // Get the new insert position for the node we care about. |
| 1447 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1448 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1449 | } |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1450 | VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1451 | VectorTypes.InsertNode(New, InsertPos); |
| 1452 | Types.push_back(New); |
| 1453 | return QualType(New, 0); |
| 1454 | } |
| 1455 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1456 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1457 | /// 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] | 1458 | QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) { |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1459 | BuiltinType *baseType; |
| 1460 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1461 | baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr()); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1462 | assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type"); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1463 | |
| 1464 | // Check if we've already instantiated a vector of this type. |
| 1465 | llvm::FoldingSetNodeID ID; |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1466 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1467 | void *InsertPos = 0; |
| 1468 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1469 | return QualType(VTP, 0); |
| 1470 | |
| 1471 | // If the element type isn't canonical, this won't be a canonical type either, |
| 1472 | // so fill in the canonical type field. |
| 1473 | QualType Canonical; |
| 1474 | if (!vecType->isCanonical()) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1475 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1476 | |
| 1477 | // Get the new insert position for the node we care about. |
| 1478 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1479 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1480 | } |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1481 | ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 7332292 | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 1482 | VectorTypes.InsertNode(New, InsertPos); |
| 1483 | Types.push_back(New); |
| 1484 | return QualType(New, 0); |
| 1485 | } |
| 1486 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1487 | QualType ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 1488 | Expr *SizeExpr, |
| 1489 | SourceLocation AttrLoc) { |
| 1490 | DependentSizedExtVectorType *New = |
| 1491 | new (*this,8) DependentSizedExtVectorType(vecType, QualType(), |
| 1492 | SizeExpr, AttrLoc); |
| 1493 | |
| 1494 | DependentSizedExtVectorTypes.push_back(New); |
| 1495 | Types.push_back(New); |
| 1496 | return QualType(New, 0); |
| 1497 | } |
| 1498 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1499 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1500 | /// |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame^] | 1501 | QualType ASTContext::getFunctionNoProtoType(QualType ResultTy, bool NoReturn) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1502 | // Unique functions, to guarantee there is only one function of a particular |
| 1503 | // structure. |
| 1504 | llvm::FoldingSetNodeID ID; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame^] | 1505 | FunctionNoProtoType::Profile(ID, ResultTy, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1506 | |
| 1507 | void *InsertPos = 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1508 | if (FunctionNoProtoType *FT = |
| 1509 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1510 | return QualType(FT, 0); |
| 1511 | |
| 1512 | QualType Canonical; |
| 1513 | if (!ResultTy->isCanonical()) { |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame^] | 1514 | Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1515 | |
| 1516 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1517 | FunctionNoProtoType *NewIP = |
| 1518 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1519 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame^] | 1522 | FunctionNoProtoType *New |
| 1523 | = new (*this,8) FunctionNoProtoType(ResultTy, Canonical, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1524 | Types.push_back(New); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1525 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1526 | return QualType(New, 0); |
| 1527 | } |
| 1528 | |
| 1529 | /// getFunctionType - Return a normal function type with a typed argument |
| 1530 | /// list. isVariadic indicates whether the argument list includes '...'. |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 1531 | QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray, |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 1532 | unsigned NumArgs, bool isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1533 | unsigned TypeQuals, bool hasExceptionSpec, |
| 1534 | bool hasAnyExceptionSpec, unsigned NumExs, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame^] | 1535 | const QualType *ExArray, bool NoReturn) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1536 | // Unique functions, to guarantee there is only one function of a particular |
| 1537 | // structure. |
| 1538 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1539 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1540 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame^] | 1541 | NumExs, ExArray, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1542 | |
| 1543 | void *InsertPos = 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1544 | if (FunctionProtoType *FTP = |
| 1545 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1546 | return QualType(FTP, 0); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1547 | |
| 1548 | // Determine whether the type being created is already canonical or not. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1549 | bool isCanonical = ResultTy->isCanonical(); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1550 | if (hasExceptionSpec) |
| 1551 | isCanonical = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1552 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
| 1553 | if (!ArgArray[i]->isCanonical()) |
| 1554 | isCanonical = false; |
| 1555 | |
| 1556 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1557 | // The exception spec is not part of the canonical type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1558 | QualType Canonical; |
| 1559 | if (!isCanonical) { |
| 1560 | llvm::SmallVector<QualType, 16> CanonicalArgs; |
| 1561 | CanonicalArgs.reserve(NumArgs); |
| 1562 | for (unsigned i = 0; i != NumArgs; ++i) |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1563 | CanonicalArgs.push_back(getCanonicalType(ArgArray[i])); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1564 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1565 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1566 | CanonicalArgs.data(), NumArgs, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame^] | 1567 | isVariadic, TypeQuals, NoReturn); |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1568 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1569 | // Get the new insert position for the node we care about. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1570 | FunctionProtoType *NewIP = |
| 1571 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Chris Lattner | f6e764f | 2008-10-12 00:26:57 +0000 | [diff] [blame] | 1572 | assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1573 | } |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1574 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1575 | // FunctionProtoType objects are allocated with extra bytes after them |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1576 | // for two variable size arrays (for parameter and exception types) at the |
| 1577 | // end of them. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1578 | FunctionProtoType *FTP = |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1579 | (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) + |
| 1580 | NumArgs*sizeof(QualType) + |
| 1581 | NumExs*sizeof(QualType), 8); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1582 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic, |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 1583 | TypeQuals, hasExceptionSpec, hasAnyExceptionSpec, |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame^] | 1584 | ExArray, NumExs, Canonical, NoReturn); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1585 | Types.push_back(FTP); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1586 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1587 | return QualType(FTP, 0); |
| 1588 | } |
| 1589 | |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1590 | /// getTypeDeclType - Return the unique reference to the type for the |
| 1591 | /// specified type declaration. |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1592 | QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) { |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1593 | assert(Decl && "Passed null for Decl param"); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1594 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1595 | |
Argyrios Kyrtzidis | 1e6759e | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 1596 | if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1597 | return getTypedefType(Typedef); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1598 | else if (isa<TemplateTypeParmDecl>(Decl)) { |
| 1599 | assert(false && "Template type parameter types are always available."); |
| 1600 | } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl)) |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1601 | return getObjCInterfaceType(ObjCInterface); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1602 | |
Douglas Gregor | c1efaec | 2009-02-28 01:32:25 +0000 | [diff] [blame] | 1603 | if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1604 | if (PrevDecl) |
| 1605 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1606 | else |
| 1607 | Decl->TypeForDecl = new (*this,8) RecordType(Record); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1608 | } |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1609 | else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
| 1610 | if (PrevDecl) |
| 1611 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1612 | else |
| 1613 | Decl->TypeForDecl = new (*this,8) EnumType(Enum); |
Ted Kremenek | 566c2ba | 2009-01-19 21:31:22 +0000 | [diff] [blame] | 1614 | } |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1615 | else |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1616 | assert(false && "TypeDecl without a type?"); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1617 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1618 | if (!PrevDecl) Types.push_back(Decl->TypeForDecl); |
Argyrios Kyrtzidis | 49aa7ff | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 1619 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1622 | /// getTypedefType - Return the unique reference to the type for the |
| 1623 | /// specified typename decl. |
| 1624 | QualType ASTContext::getTypedefType(TypedefDecl *Decl) { |
| 1625 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 1626 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1627 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1628 | Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1629 | Types.push_back(Decl->TypeForDecl); |
| 1630 | return QualType(Decl->TypeForDecl, 0); |
| 1631 | } |
| 1632 | |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1633 | /// \brief Retrieve the template type parameter type for a template |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1634 | /// parameter or parameter pack with the given depth, index, and (optionally) |
| 1635 | /// name. |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1636 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1637 | bool ParameterPack, |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1638 | IdentifierInfo *Name) { |
| 1639 | llvm::FoldingSetNodeID ID; |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1640 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1641 | void *InsertPos = 0; |
| 1642 | TemplateTypeParmType *TypeParm |
| 1643 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1644 | |
| 1645 | if (TypeParm) |
| 1646 | return QualType(TypeParm, 0); |
| 1647 | |
Anders Carlsson | 76e4ce4 | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 1648 | if (Name) { |
| 1649 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
| 1650 | TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack, |
| 1651 | Name, Canon); |
| 1652 | } else |
| 1653 | TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | fab9d67 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 1654 | |
| 1655 | Types.push_back(TypeParm); |
| 1656 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 1657 | |
| 1658 | return QualType(TypeParm, 0); |
| 1659 | } |
| 1660 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1661 | QualType |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1662 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
| 1663 | const TemplateArgument *Args, |
| 1664 | unsigned NumArgs, |
| 1665 | QualType Canon) { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1666 | if (!Canon.isNull()) |
| 1667 | Canon = getCanonicalType(Canon); |
Douglas Gregor | fc705b8 | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 1668 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1669 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1670 | TemplateSpecializationType::Profile(ID, Template, Args, NumArgs); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1671 | |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1672 | void *InsertPos = 0; |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1673 | TemplateSpecializationType *Spec |
| 1674 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1675 | |
| 1676 | if (Spec) |
| 1677 | return QualType(Spec, 0); |
| 1678 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1679 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 1680 | sizeof(TemplateArgument) * NumArgs), |
| 1681 | 8); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1682 | Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1683 | Types.push_back(Spec); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1684 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 1685 | |
| 1686 | return QualType(Spec, 0); |
| 1687 | } |
| 1688 | |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1689 | QualType |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1690 | ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS, |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1691 | QualType NamedType) { |
| 1692 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1693 | QualifiedNameType::Profile(ID, NNS, NamedType); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1694 | |
| 1695 | void *InsertPos = 0; |
| 1696 | QualifiedNameType *T |
| 1697 | = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1698 | if (T) |
| 1699 | return QualType(T, 0); |
| 1700 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1701 | T = new (*this) QualifiedNameType(NNS, NamedType, |
| 1702 | getCanonicalType(NamedType)); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1703 | Types.push_back(T); |
| 1704 | QualifiedNameTypes.InsertNode(T, InsertPos); |
| 1705 | return QualType(T, 0); |
| 1706 | } |
| 1707 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1708 | QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
| 1709 | const IdentifierInfo *Name, |
| 1710 | QualType Canon) { |
| 1711 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1712 | |
| 1713 | if (Canon.isNull()) { |
| 1714 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 1715 | if (CanonNNS != NNS) |
| 1716 | Canon = getTypenameType(CanonNNS, Name); |
| 1717 | } |
| 1718 | |
| 1719 | llvm::FoldingSetNodeID ID; |
| 1720 | TypenameType::Profile(ID, NNS, Name); |
| 1721 | |
| 1722 | void *InsertPos = 0; |
| 1723 | TypenameType *T |
| 1724 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1725 | if (T) |
| 1726 | return QualType(T, 0); |
| 1727 | |
| 1728 | T = new (*this) TypenameType(NNS, Name, Canon); |
| 1729 | Types.push_back(T); |
| 1730 | TypenameTypes.InsertNode(T, InsertPos); |
| 1731 | return QualType(T, 0); |
| 1732 | } |
| 1733 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 1734 | QualType |
| 1735 | ASTContext::getTypenameType(NestedNameSpecifier *NNS, |
| 1736 | const TemplateSpecializationType *TemplateId, |
| 1737 | QualType Canon) { |
| 1738 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 1739 | |
| 1740 | if (Canon.isNull()) { |
| 1741 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 1742 | QualType CanonType = getCanonicalType(QualType(TemplateId, 0)); |
| 1743 | if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) { |
| 1744 | const TemplateSpecializationType *CanonTemplateId |
| 1745 | = CanonType->getAsTemplateSpecializationType(); |
| 1746 | assert(CanonTemplateId && |
| 1747 | "Canonical type must also be a template specialization type"); |
| 1748 | Canon = getTypenameType(CanonNNS, CanonTemplateId); |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | llvm::FoldingSetNodeID ID; |
| 1753 | TypenameType::Profile(ID, NNS, TemplateId); |
| 1754 | |
| 1755 | void *InsertPos = 0; |
| 1756 | TypenameType *T |
| 1757 | = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 1758 | if (T) |
| 1759 | return QualType(T, 0); |
| 1760 | |
| 1761 | T = new (*this) TypenameType(NNS, TemplateId, Canon); |
| 1762 | Types.push_back(T); |
| 1763 | TypenameTypes.InsertNode(T, InsertPos); |
| 1764 | return QualType(T, 0); |
| 1765 | } |
| 1766 | |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1767 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 1768 | /// alphabetically. |
| 1769 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 1770 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1771 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
| 1774 | static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols, |
| 1775 | unsigned &NumProtocols) { |
| 1776 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
| 1777 | |
| 1778 | // Sort protocols, keyed by name. |
| 1779 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 1780 | |
| 1781 | // Remove duplicates. |
| 1782 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 1783 | NumProtocols = ProtocolsEnd-Protocols; |
| 1784 | } |
| 1785 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1786 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 1787 | /// the given interface decl and the conforming protocol list. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1788 | QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT, |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1789 | ObjCProtocolDecl **Protocols, |
| 1790 | unsigned NumProtocols) { |
| 1791 | // Sort the protocol list alphabetically to canonicalize it. |
| 1792 | if (NumProtocols) |
| 1793 | SortAndUniqueProtocols(Protocols, NumProtocols); |
| 1794 | |
| 1795 | llvm::FoldingSetNodeID ID; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1796 | ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1797 | |
| 1798 | void *InsertPos = 0; |
| 1799 | if (ObjCObjectPointerType *QT = |
| 1800 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1801 | return QualType(QT, 0); |
| 1802 | |
| 1803 | // No Match; |
| 1804 | ObjCObjectPointerType *QType = |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1805 | new (*this,8) ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols); |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1806 | |
| 1807 | Types.push_back(QType); |
| 1808 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
| 1809 | return QualType(QType, 0); |
| 1810 | } |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1811 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1812 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 1813 | /// specified ObjC interface decl. The list of protocols is optional. |
| 1814 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1815 | ObjCProtocolDecl **Protocols, unsigned NumProtocols) { |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1816 | if (NumProtocols) |
| 1817 | // Sort the protocol list alphabetically to canonicalize it. |
| 1818 | SortAndUniqueProtocols(Protocols, NumProtocols); |
Chris Lattner | 88cb27a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 1819 | |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1820 | llvm::FoldingSetNodeID ID; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1821 | ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1822 | |
| 1823 | void *InsertPos = 0; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1824 | if (ObjCInterfaceType *QT = |
| 1825 | ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1826 | return QualType(QT, 0); |
| 1827 | |
| 1828 | // No Match; |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1829 | ObjCInterfaceType *QType = |
| 1830 | new (*this,8) ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl), |
| 1831 | Protocols, NumProtocols); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1832 | Types.push_back(QType); |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 1833 | ObjCInterfaceTypes.InsertNode(QType, InsertPos); |
Fariborz Jahanian | 4b6c905 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 1834 | return QualType(QType, 0); |
| 1835 | } |
| 1836 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1837 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 1838 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1839 | /// multiple declarations that refer to "typeof(x)" all contain different |
| 1840 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
| 1841 | /// on canonical type's (which are always unique). |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1842 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 1843 | TypeOfExprType *toe; |
| 1844 | if (tofExpr->isTypeDependent()) |
| 1845 | toe = new (*this, 8) TypeOfExprType(tofExpr); |
| 1846 | else { |
| 1847 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
| 1848 | toe = new (*this,8) TypeOfExprType(tofExpr, Canonical); |
| 1849 | } |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1850 | Types.push_back(toe); |
| 1851 | return QualType(toe, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1854 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 1855 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 1856 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
| 1857 | /// an issue. This doesn't effect the type checker, since it operates |
| 1858 | /// on canonical type's (which are always unique). |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1859 | QualType ASTContext::getTypeOfType(QualType tofType) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1860 | QualType Canonical = getCanonicalType(tofType); |
Steve Naroff | f83820b | 2009-01-27 22:08:43 +0000 | [diff] [blame] | 1861 | TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical); |
Steve Naroff | 9752f25 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 1862 | Types.push_back(tot); |
| 1863 | return QualType(tot, 0); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 1866 | /// getDecltypeForExpr - Given an expr, will return the decltype for that |
| 1867 | /// expression, according to the rules in C++0x [dcl.type.simple]p4 |
| 1868 | static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) { |
Anders Carlsson | a07c33e | 2009-06-25 15:00:34 +0000 | [diff] [blame] | 1869 | if (e->isTypeDependent()) |
| 1870 | return Context.DependentTy; |
| 1871 | |
Anders Carlsson | 60a9a2a | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 1872 | // If e is an id expression or a class member access, decltype(e) is defined |
| 1873 | // as the type of the entity named by e. |
| 1874 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) { |
| 1875 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) |
| 1876 | return VD->getType(); |
| 1877 | } |
| 1878 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) { |
| 1879 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) |
| 1880 | return FD->getType(); |
| 1881 | } |
| 1882 | // If e is a function call or an invocation of an overloaded operator, |
| 1883 | // (parentheses around e are ignored), decltype(e) is defined as the |
| 1884 | // return type of that function. |
| 1885 | if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens())) |
| 1886 | return CE->getCallReturnType(); |
| 1887 | |
| 1888 | QualType T = e->getType(); |
| 1889 | |
| 1890 | // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is |
| 1891 | // defined as T&, otherwise decltype(e) is defined as T. |
| 1892 | if (e->isLvalue(Context) == Expr::LV_Valid) |
| 1893 | T = Context.getLValueReferenceType(T); |
| 1894 | |
| 1895 | return T; |
| 1896 | } |
| 1897 | |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1898 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 1899 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 1900 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
| 1901 | /// an issue. This doesn't effect the type checker, since it operates |
| 1902 | /// on canonical type's (which are always unique). |
| 1903 | QualType ASTContext::getDecltypeType(Expr *e) { |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 1904 | DecltypeType *dt; |
| 1905 | if (e->isTypeDependent()) // FIXME: canonicalize the expression |
Anders Carlsson | 563a03b | 2009-07-10 19:20:26 +0000 | [diff] [blame] | 1906 | dt = new (*this, 8) DecltypeType(e, DependentTy); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 1907 | else { |
| 1908 | QualType T = getDecltypeForExpr(e, *this); |
Anders Carlsson | 563a03b | 2009-07-10 19:20:26 +0000 | [diff] [blame] | 1909 | dt = new (*this, 8) DecltypeType(e, T, getCanonicalType(T)); |
Douglas Gregor | dd0257c | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 1910 | } |
Anders Carlsson | 395b475 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1911 | Types.push_back(dt); |
| 1912 | return QualType(dt, 0); |
| 1913 | } |
| 1914 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1915 | /// getTagDeclType - Return the unique reference to the type for the |
| 1916 | /// specified TagDecl (struct/union/class/enum) decl. |
| 1917 | QualType ASTContext::getTagDeclType(TagDecl *Decl) { |
Ted Kremenek | d778f88 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 1918 | assert (Decl); |
Douglas Gregor | 2ce52f3 | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 1919 | return getTypeDeclType(Decl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 1923 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 1924 | /// needs to agree with the definition in <stddef.h>. |
| 1925 | QualType ASTContext::getSizeType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 1926 | return getFromTargetType(Target.getSizeType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1929 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 1930 | /// Used when in C++, as a GCC extension. |
| 1931 | QualType ASTContext::getSignedWCharType() const { |
| 1932 | // FIXME: derive from "Target" ? |
| 1933 | return WCharTy; |
| 1934 | } |
| 1935 | |
| 1936 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 1937 | /// Used when in C++, as a GCC extension. |
| 1938 | QualType ASTContext::getUnsignedWCharType() const { |
| 1939 | // FIXME: derive from "Target" ? |
| 1940 | return UnsignedIntTy; |
| 1941 | } |
| 1942 | |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 1943 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) |
| 1944 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 1945 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 1946 | return getFromTargetType(Target.getPtrDiffType(0)); |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 1949 | //===----------------------------------------------------------------------===// |
| 1950 | // Type Operators |
| 1951 | //===----------------------------------------------------------------------===// |
| 1952 | |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 1953 | /// getCanonicalType - Return the canonical (structural) type corresponding to |
| 1954 | /// the specified potentially non-canonical type. The non-canonical version |
| 1955 | /// of a type may have many "decorated" versions of types. Decorators can |
| 1956 | /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed |
| 1957 | /// to be free of any of these, allowing two canonical types to be compared |
| 1958 | /// for exact equality with a simple pointer comparison. |
| 1959 | QualType ASTContext::getCanonicalType(QualType T) { |
| 1960 | QualType CanType = T.getTypePtr()->getCanonicalTypeInternal(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1961 | |
| 1962 | // If the result has type qualifiers, make sure to canonicalize them as well. |
| 1963 | unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers(); |
| 1964 | if (TypeQuals == 0) return CanType; |
| 1965 | |
| 1966 | // If the type qualifiers are on an array type, get the canonical type of the |
| 1967 | // array with the qualifiers applied to the element type. |
| 1968 | ArrayType *AT = dyn_cast<ArrayType>(CanType); |
| 1969 | if (!AT) |
| 1970 | return CanType.getQualifiedType(TypeQuals); |
| 1971 | |
| 1972 | // Get the canonical version of the element with the extra qualifiers on it. |
| 1973 | // This can recursively sink qualifiers through multiple levels of arrays. |
| 1974 | QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals); |
| 1975 | NewEltTy = getCanonicalType(NewEltTy); |
| 1976 | |
| 1977 | if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 1978 | return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(), |
| 1979 | CAT->getIndexTypeQualifier()); |
| 1980 | if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) |
| 1981 | return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(), |
| 1982 | IAT->getIndexTypeQualifier()); |
| 1983 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1984 | if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT)) |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1985 | return getDependentSizedArrayType(NewEltTy, |
| 1986 | DSAT->getSizeExpr(), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1987 | DSAT->getSizeModifier(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1988 | DSAT->getIndexTypeQualifier(), |
| 1989 | DSAT->getBracketsRange()); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1990 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1991 | VariableArrayType *VAT = cast<VariableArrayType>(AT); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1992 | return getVariableArrayType(NewEltTy, |
| 1993 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1994 | VAT->getSizeModifier(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1995 | VAT->getIndexTypeQualifier(), |
| 1996 | VAT->getBracketsRange()); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 1999 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { |
| 2000 | // If this template name refers to a template, the canonical |
| 2001 | // template name merely stores the template itself. |
| 2002 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 2003 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 25a3ef7 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 2004 | |
| 2005 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 2006 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 2007 | return DTN->CanonicalTemplateName; |
| 2008 | } |
| 2009 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2010 | NestedNameSpecifier * |
| 2011 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) { |
| 2012 | if (!NNS) |
| 2013 | return 0; |
| 2014 | |
| 2015 | switch (NNS->getKind()) { |
| 2016 | case NestedNameSpecifier::Identifier: |
| 2017 | // Canonicalize the prefix but keep the identifier the same. |
| 2018 | return NestedNameSpecifier::Create(*this, |
| 2019 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 2020 | NNS->getAsIdentifier()); |
| 2021 | |
| 2022 | case NestedNameSpecifier::Namespace: |
| 2023 | // A namespace is canonical; build a nested-name-specifier with |
| 2024 | // this namespace and no prefix. |
| 2025 | return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace()); |
| 2026 | |
| 2027 | case NestedNameSpecifier::TypeSpec: |
| 2028 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 2029 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
| 2030 | NestedNameSpecifier *Prefix = 0; |
| 2031 | |
| 2032 | // FIXME: This isn't the right check! |
| 2033 | if (T->isDependentType()) |
| 2034 | Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix()); |
| 2035 | |
| 2036 | return NestedNameSpecifier::Create(*this, Prefix, |
| 2037 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
| 2038 | T.getTypePtr()); |
| 2039 | } |
| 2040 | |
| 2041 | case NestedNameSpecifier::Global: |
| 2042 | // The global specifier is canonical and unique. |
| 2043 | return NNS; |
| 2044 | } |
| 2045 | |
| 2046 | // Required to silence a GCC warning |
| 2047 | return 0; |
| 2048 | } |
| 2049 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2050 | |
| 2051 | const ArrayType *ASTContext::getAsArrayType(QualType T) { |
| 2052 | // Handle the non-qualified case efficiently. |
| 2053 | if (T.getCVRQualifiers() == 0) { |
| 2054 | // Handle the common positive case fast. |
| 2055 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 2056 | return AT; |
| 2057 | } |
| 2058 | |
| 2059 | // Handle the common negative case fast, ignoring CVR qualifiers. |
| 2060 | QualType CType = T->getCanonicalTypeInternal(); |
| 2061 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 2062 | // Make sure to look through type qualifiers (like ExtQuals) for the negative |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2063 | // test. |
| 2064 | if (!isa<ArrayType>(CType) && |
| 2065 | !isa<ArrayType>(CType.getUnqualifiedType())) |
| 2066 | return 0; |
| 2067 | |
| 2068 | // Apply any CVR qualifiers from the array type to the element type. This |
| 2069 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 2070 | // any type qualifiers, the element type is so qualified, not the array type." |
| 2071 | |
| 2072 | // If we get here, we either have type qualifiers on the type, or we have |
| 2073 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
| 2074 | // we must propagate them down into the elemeng type. |
| 2075 | unsigned CVRQuals = T.getCVRQualifiers(); |
| 2076 | unsigned AddrSpace = 0; |
| 2077 | Type *Ty = T.getTypePtr(); |
| 2078 | |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 2079 | // Rip through ExtQualType's and typedefs to get to a concrete type. |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2080 | while (1) { |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 2081 | if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) { |
| 2082 | AddrSpace = EXTQT->getAddressSpace(); |
| 2083 | Ty = EXTQT->getBaseType(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2084 | } else { |
| 2085 | T = Ty->getDesugaredType(); |
| 2086 | if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0) |
| 2087 | break; |
| 2088 | CVRQuals |= T.getCVRQualifiers(); |
| 2089 | Ty = T.getTypePtr(); |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | // If we have a simple case, just return now. |
| 2094 | const ArrayType *ATy = dyn_cast<ArrayType>(Ty); |
| 2095 | if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0)) |
| 2096 | return ATy; |
| 2097 | |
| 2098 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 2099 | // qualifiers into the array element type and return a new array type. |
| 2100 | // Get the canonical version of the element with the extra qualifiers on it. |
| 2101 | // This can recursively sink qualifiers through multiple levels of arrays. |
| 2102 | QualType NewEltTy = ATy->getElementType(); |
| 2103 | if (AddrSpace) |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 2104 | NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2105 | NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals); |
| 2106 | |
| 2107 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 2108 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 2109 | CAT->getSizeModifier(), |
| 2110 | CAT->getIndexTypeQualifier())); |
| 2111 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 2112 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 2113 | IAT->getSizeModifier(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2114 | IAT->getIndexTypeQualifier())); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2115 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2116 | if (const DependentSizedArrayType *DSAT |
| 2117 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 2118 | return cast<ArrayType>( |
| 2119 | getDependentSizedArrayType(NewEltTy, |
| 2120 | DSAT->getSizeExpr(), |
| 2121 | DSAT->getSizeModifier(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2122 | DSAT->getIndexTypeQualifier(), |
| 2123 | DSAT->getBracketsRange())); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2124 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2125 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2126 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
| 2127 | VAT->getSizeExpr(), |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2128 | VAT->getSizeModifier(), |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2129 | VAT->getIndexTypeQualifier(), |
| 2130 | VAT->getBracketsRange())); |
Chris Lattner | 77c9647 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2134 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 2135 | /// specified array type to a pointer. This operation is non-trivial when |
| 2136 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 2137 | /// this returns a pointer to a properly qualified element of the array. |
| 2138 | /// |
| 2139 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
| 2140 | QualType ASTContext::getArrayDecayedType(QualType Ty) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2141 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 2142 | // typedefs in the element type of the array. This also handles propagation |
| 2143 | // of type qualifiers from the array type into the element type if present |
| 2144 | // (C99 6.7.3p8). |
| 2145 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 2146 | assert(PrettyArrayType && "Not an array type!"); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2147 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2148 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2149 | |
| 2150 | // int x[restrict 4] -> int *restrict |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2151 | return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier()); |
Chris Lattner | e632774 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 2154 | QualType ASTContext::getBaseElementType(QualType QT) { |
| 2155 | QualifierSet qualifiers; |
| 2156 | while (true) { |
| 2157 | const Type *UT = qualifiers.strip(QT); |
| 2158 | if (const ArrayType *AT = getAsArrayType(QualType(UT,0))) { |
| 2159 | QT = AT->getElementType(); |
| 2160 | }else { |
| 2161 | return qualifiers.apply(QT, *this); |
| 2162 | } |
| 2163 | } |
| 2164 | } |
| 2165 | |
Daniel Dunbar | d786f6a | 2009-01-05 22:14:37 +0000 | [diff] [blame] | 2166 | QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) { |
Anders Carlsson | 6183a99 | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 2167 | QualType ElemTy = VAT->getElementType(); |
| 2168 | |
| 2169 | if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy)) |
| 2170 | return getBaseElementType(VAT); |
| 2171 | |
| 2172 | return ElemTy; |
| 2173 | } |
| 2174 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2175 | /// getFloatingRank - Return a relative rank for floating point types. |
| 2176 | /// 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] | 2177 | static FloatingRank getFloatingRank(QualType T) { |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 2178 | if (const ComplexType *CT = T->getAsComplexType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2179 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2180 | |
Daniel Dunbar | d786f6a | 2009-01-05 22:14:37 +0000 | [diff] [blame] | 2181 | assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type"); |
Christopher Lamb | ebb97e9 | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 2182 | switch (T->getAsBuiltinType()->getKind()) { |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2183 | default: assert(0 && "getFloatingRank(): not a floating type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2184 | case BuiltinType::Float: return FloatRank; |
| 2185 | case BuiltinType::Double: return DoubleRank; |
| 2186 | case BuiltinType::LongDouble: return LongDoubleRank; |
| 2187 | } |
| 2188 | } |
| 2189 | |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2190 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 2191 | /// point or a complex type (based on typeDomain/typeSize). |
| 2192 | /// 'typeDomain' is a real floating point or complex type. |
| 2193 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2194 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 2195 | QualType Domain) const { |
| 2196 | FloatingRank EltRank = getFloatingRank(Size); |
| 2197 | if (Domain->isComplexType()) { |
| 2198 | switch (EltRank) { |
Steve Naroff | 716c730 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 2199 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2200 | case FloatRank: return FloatComplexTy; |
| 2201 | case DoubleRank: return DoubleComplexTy; |
| 2202 | case LongDoubleRank: return LongDoubleComplexTy; |
| 2203 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2204 | } |
Chris Lattner | 1361b11 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 2205 | |
| 2206 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 2207 | switch (EltRank) { |
| 2208 | default: assert(0 && "getFloatingRank(): illegal value for rank"); |
| 2209 | case FloatRank: return FloatTy; |
| 2210 | case DoubleRank: return DoubleTy; |
| 2211 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 2212 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2215 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 2216 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 2217 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
| 2218 | /// LHS < RHS, return -1. |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2219 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) { |
| 2220 | FloatingRank LHSR = getFloatingRank(LHS); |
| 2221 | FloatingRank RHSR = getFloatingRank(RHS); |
| 2222 | |
| 2223 | if (LHSR == RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2224 | return 0; |
Chris Lattner | a75cea3 | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 2225 | if (LHSR > RHSR) |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 2226 | return 1; |
| 2227 | return -1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2230 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 2231 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 2232 | /// or if it is not canonicalized. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2233 | unsigned ASTContext::getIntegerRank(Type *T) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2234 | assert(T->isCanonical() && "T should be canonicalized"); |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2235 | if (EnumType* ET = dyn_cast<EnumType>(T)) |
| 2236 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
| 2237 | |
Eli Friedman | a342675 | 2009-07-05 23:44:27 +0000 | [diff] [blame] | 2238 | if (T->isSpecificBuiltinType(BuiltinType::WChar)) |
| 2239 | T = getFromTargetType(Target.getWCharType()).getTypePtr(); |
| 2240 | |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2241 | if (T->isSpecificBuiltinType(BuiltinType::Char16)) |
| 2242 | T = getFromTargetType(Target.getChar16Type()).getTypePtr(); |
| 2243 | |
| 2244 | if (T->isSpecificBuiltinType(BuiltinType::Char32)) |
| 2245 | T = getFromTargetType(Target.getChar32Type()).getTypePtr(); |
| 2246 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2247 | // There are two things which impact the integer rank: the width, and |
| 2248 | // the ordering of builtins. The builtin ordering is encoded in the |
| 2249 | // bottom three bits; the width is encoded in the bits above that. |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 2250 | if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2251 | return FWIT->getWidth() << 3; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2252 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2253 | switch (cast<BuiltinType>(T)->getKind()) { |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2254 | default: assert(0 && "getIntegerRank(): not a built-in integer"); |
| 2255 | case BuiltinType::Bool: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2256 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2257 | case BuiltinType::Char_S: |
| 2258 | case BuiltinType::Char_U: |
| 2259 | case BuiltinType::SChar: |
| 2260 | case BuiltinType::UChar: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2261 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2262 | case BuiltinType::Short: |
| 2263 | case BuiltinType::UShort: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2264 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2265 | case BuiltinType::Int: |
| 2266 | case BuiltinType::UInt: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2267 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2268 | case BuiltinType::Long: |
| 2269 | case BuiltinType::ULong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2270 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2271 | case BuiltinType::LongLong: |
| 2272 | case BuiltinType::ULongLong: |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2273 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 2274 | case BuiltinType::Int128: |
| 2275 | case BuiltinType::UInt128: |
| 2276 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2277 | } |
| 2278 | } |
| 2279 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2280 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
| 2281 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
| 2282 | /// LHS < RHS, return -1. |
| 2283 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) { |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2284 | Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 2285 | Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2286 | if (LHSC == RHSC) return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2287 | |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2288 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 2289 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2290 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2291 | unsigned LHSRank = getIntegerRank(LHSC); |
| 2292 | unsigned RHSRank = getIntegerRank(RHSC); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2293 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2294 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 2295 | if (LHSRank == RHSRank) return 0; |
| 2296 | return LHSRank > RHSRank ? 1 : -1; |
| 2297 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2298 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2299 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 2300 | if (LHSUnsigned) { |
| 2301 | // If the unsigned [LHS] type is larger, return it. |
| 2302 | if (LHSRank >= RHSRank) |
| 2303 | return 1; |
| 2304 | |
| 2305 | // If the signed type can represent all values of the unsigned type, it |
| 2306 | // wins. Because we are dealing with 2's complement and types that are |
| 2307 | // powers of two larger than each other, this is always safe. |
| 2308 | return -1; |
| 2309 | } |
Chris Lattner | f52ab25 | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2310 | |
Chris Lattner | 7cfeb08 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 2311 | // If the unsigned [RHS] type is larger, return it. |
| 2312 | if (RHSRank >= LHSRank) |
| 2313 | return -1; |
| 2314 | |
| 2315 | // If the signed type can represent all values of the unsigned type, it |
| 2316 | // wins. Because we are dealing with 2's complement and types that are |
| 2317 | // powers of two larger than each other, this is always safe. |
| 2318 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2319 | } |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2320 | |
| 2321 | // getCFConstantStringType - Return the type used for constant CFStrings. |
| 2322 | QualType ASTContext::getCFConstantStringType() { |
| 2323 | if (!CFConstantStringTypeDecl) { |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 2324 | CFConstantStringTypeDecl = |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 2325 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2326 | &Idents.get("NSConstantString")); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2327 | QualType FieldTypes[4]; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2328 | |
| 2329 | // const int *isa; |
| 2330 | FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2331 | // int flags; |
| 2332 | FieldTypes[1] = IntTy; |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2333 | // const char *str; |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2334 | FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const)); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2335 | // long length; |
Anders Carlsson | f06273f | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 2336 | FieldTypes[3] = LongTy; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2337 | |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2338 | // Create fields |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2339 | for (unsigned i = 0; i < 4; ++i) { |
| 2340 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
| 2341 | SourceLocation(), 0, |
| 2342 | FieldTypes[i], /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2343 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2344 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | CFConstantStringTypeDecl->completeDefinition(*this); |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 8467583 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 2351 | } |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2352 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2353 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 2354 | const RecordType *Rec = T->getAsRecordType(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2355 | assert(Rec && "Invalid CFConstantStringType"); |
| 2356 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 2357 | } |
| 2358 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2359 | QualType ASTContext::getObjCFastEnumerationStateType() |
| 2360 | { |
| 2361 | if (!ObjCFastEnumerationStateTypeDecl) { |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2362 | ObjCFastEnumerationStateTypeDecl = |
| 2363 | RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(), |
| 2364 | &Idents.get("__objcFastEnumerationState")); |
| 2365 | |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2366 | QualType FieldTypes[] = { |
| 2367 | UnsignedLongTy, |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2368 | getPointerType(ObjCIdTypedefType), |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2369 | getPointerType(UnsignedLongTy), |
| 2370 | getConstantArrayType(UnsignedLongTy, |
| 2371 | llvm::APInt(32, 5), ArrayType::Normal, 0) |
| 2372 | }; |
| 2373 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2374 | for (size_t i = 0; i < 4; ++i) { |
| 2375 | FieldDecl *Field = FieldDecl::Create(*this, |
| 2376 | ObjCFastEnumerationStateTypeDecl, |
| 2377 | SourceLocation(), 0, |
| 2378 | FieldTypes[i], /*BitWidth=*/0, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2379 | /*Mutable=*/false); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2380 | ObjCFastEnumerationStateTypeDecl->addDecl(Field); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2381 | } |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2382 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2383 | ObjCFastEnumerationStateTypeDecl->completeDefinition(*this); |
Anders Carlsson | bd4c1ad | 2008-08-30 19:34:46 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | return getTagDeclType(ObjCFastEnumerationStateTypeDecl); |
| 2387 | } |
| 2388 | |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2389 | void ASTContext::setObjCFastEnumerationStateType(QualType T) { |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 2390 | const RecordType *Rec = T->getAsRecordType(); |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2391 | assert(Rec && "Invalid ObjCFAstEnumerationStateType"); |
| 2392 | ObjCFastEnumerationStateTypeDecl = Rec->getDecl(); |
| 2393 | } |
| 2394 | |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 2395 | // This returns true if a type has been typedefed to BOOL: |
| 2396 | // typedef <type> BOOL; |
Chris Lattner | 2d99833 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 2397 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 2398 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | bb49c3e | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 2399 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 2400 | return II->isStr("BOOL"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2401 | |
| 2402 | return false; |
| 2403 | } |
| 2404 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2405 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2406 | /// purpose. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2407 | int ASTContext::getObjCEncodingTypeSize(QualType type) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2408 | uint64_t sz = getTypeSize(type); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2409 | |
| 2410 | // Make all integer and enum types at least as large as an int |
| 2411 | if (sz > 0 && type->isIntegralType()) |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2412 | sz = std::max(sz, getTypeSize(IntTy)); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2413 | // Treat arrays as pointers, since that's how they're passed in. |
| 2414 | else if (type->isArrayType()) |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2415 | sz = getTypeSize(VoidPtrTy); |
| 2416 | return sz / getTypeSize(CharTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2419 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2420 | /// declaration. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2421 | void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 2422 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2423 | // FIXME: This is not very efficient. |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 2424 | // Encode type qualifer, 'in', 'inout', etc. for the return type. |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2425 | getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2426 | // Encode result type. |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2427 | getObjCEncodingForType(Decl->getResultType(), S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2428 | // Compute size of all parameters. |
| 2429 | // Start with computing size of a pointer in number of bytes. |
| 2430 | // FIXME: There might(should) be a better way of doing this computation! |
| 2431 | SourceLocation Loc; |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2432 | int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2433 | // The first two arguments (self and _cmd) are pointers; account for |
| 2434 | // their size. |
| 2435 | int ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2436 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 2437 | E = Decl->param_end(); PI != E; ++PI) { |
| 2438 | QualType PType = (*PI)->getType(); |
| 2439 | int sz = getObjCEncodingTypeSize(PType); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2440 | assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2441 | ParmOffset += sz; |
| 2442 | } |
| 2443 | S += llvm::utostr(ParmOffset); |
| 2444 | S += "@0:"; |
| 2445 | S += llvm::utostr(PtrSize); |
| 2446 | |
| 2447 | // Argument types. |
| 2448 | ParmOffset = 2 * PtrSize; |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 2449 | for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(), |
| 2450 | E = Decl->param_end(); PI != E; ++PI) { |
| 2451 | ParmVarDecl *PVDecl = *PI; |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 2452 | QualType PType = PVDecl->getOriginalType(); |
| 2453 | if (const ArrayType *AT = |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 2454 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 2455 | // Use array's original type only if it has known number of |
| 2456 | // elements. |
Steve Naroff | bb3fde3 | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 2457 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | ab76d45 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 2458 | PType = PVDecl->getType(); |
| 2459 | } else if (PType->isFunctionType()) |
| 2460 | PType = PVDecl->getType(); |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 2461 | // Process argument qualifiers for user supplied arguments; such as, |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2462 | // 'in', 'inout', etc. |
Fariborz Jahanian | 4306d3c | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 2463 | getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2464 | getObjCEncodingForType(PType, S); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2465 | S += llvm::utostr(ParmOffset); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2466 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 33e1d64 | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 2467 | } |
| 2468 | } |
| 2469 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2470 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 2471 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2472 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 2473 | /// NULL when getting encodings for protocol properties. |
Fariborz Jahanian | 83bccb8 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 2474 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 2475 | /// attributes readonly and bycopy are encoded as single characters. The |
| 2476 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 2477 | /// encoded as single characters, followed by an identifier. Property types |
| 2478 | /// are also encoded as a parametrized attribute. The characters used to encode |
| 2479 | /// these attributes are defined by the following enumeration: |
| 2480 | /// @code |
| 2481 | /// enum PropertyAttributes { |
| 2482 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 2483 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 2484 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 2485 | /// kPropertyDynamic = 'D', // property is dynamic |
| 2486 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 2487 | /// kPropertySetter = 'S', // followed by setter selector name |
| 2488 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
| 2489 | /// kPropertyType = 't' // followed by old-style type encoding. |
| 2490 | /// kPropertyWeak = 'W' // 'weak' property |
| 2491 | /// kPropertyStrong = 'P' // property GC'able |
| 2492 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 2493 | /// }; |
| 2494 | /// @endcode |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2495 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
| 2496 | const Decl *Container, |
Chris Lattner | e6db3b0 | 2008-11-19 07:24:05 +0000 | [diff] [blame] | 2497 | std::string& S) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2498 | // Collect information from the property implementation decl(s). |
| 2499 | bool Dynamic = false; |
| 2500 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 2501 | |
| 2502 | // FIXME: Duplicated code due to poor abstraction. |
| 2503 | if (Container) { |
| 2504 | if (const ObjCCategoryImplDecl *CID = |
| 2505 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 2506 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2507 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2508 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2509 | ObjCPropertyImplDecl *PID = *i; |
| 2510 | if (PID->getPropertyDecl() == PD) { |
| 2511 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 2512 | Dynamic = true; |
| 2513 | } else { |
| 2514 | SynthesizePID = PID; |
| 2515 | } |
| 2516 | } |
| 2517 | } |
| 2518 | } else { |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 2519 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2520 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2521 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2522 | i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2523 | ObjCPropertyImplDecl *PID = *i; |
| 2524 | if (PID->getPropertyDecl() == PD) { |
| 2525 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 2526 | Dynamic = true; |
| 2527 | } else { |
| 2528 | SynthesizePID = PID; |
| 2529 | } |
| 2530 | } |
| 2531 | } |
| 2532 | } |
| 2533 | } |
| 2534 | |
| 2535 | // FIXME: This is not very efficient. |
| 2536 | S = "T"; |
| 2537 | |
| 2538 | // Encode result type. |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2539 | // GCC has some special rules regarding encoding of properties which |
| 2540 | // closely resembles encoding of ivars. |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 2541 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2542 | true /* outermost type */, |
| 2543 | true /* encoding for property */); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2544 | |
| 2545 | if (PD->isReadOnly()) { |
| 2546 | S += ",R"; |
| 2547 | } else { |
| 2548 | switch (PD->getSetterKind()) { |
| 2549 | case ObjCPropertyDecl::Assign: break; |
| 2550 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
| 2551 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | // It really isn't clear at all what this means, since properties |
| 2556 | // are "dynamic by default". |
| 2557 | if (Dynamic) |
| 2558 | S += ",D"; |
| 2559 | |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2560 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 2561 | S += ",N"; |
| 2562 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2563 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 2564 | S += ",G"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2565 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 2569 | S += ",S"; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2570 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
| 2573 | if (SynthesizePID) { |
| 2574 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 2575 | S += ",V"; |
Chris Lattner | 39f34e9 | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 2576 | S += OID->getNameAsString(); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | // FIXME: OBJCGC: weak & strong |
| 2580 | } |
| 2581 | |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2582 | /// getLegacyIntegralTypeEncoding - |
| 2583 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 2584 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2585 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 2586 | /// |
| 2587 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 2588 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2589 | if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) { |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 2590 | if (BT->getKind() == BuiltinType::ULong && |
| 2591 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2592 | PointeeTy = UnsignedIntTy; |
Fariborz Jahanian | c657eba | 2009-02-11 23:59:18 +0000 | [diff] [blame] | 2593 | else |
| 2594 | if (BT->getKind() == BuiltinType::Long && |
| 2595 | ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32)) |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2596 | PointeeTy = IntTy; |
| 2597 | } |
| 2598 | } |
| 2599 | } |
| 2600 | |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2601 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 2602 | const FieldDecl *Field) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 2603 | // We follow the behavior of gcc, expanding structures which are |
| 2604 | // directly pointed to, and expanding embedded structures. Note that |
| 2605 | // these rules are sufficient to prevent recursive encoding of the |
| 2606 | // same type. |
Fariborz Jahanian | 5b8c7d9 | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 2607 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
| 2608 | true /* outermost type */); |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 2611 | static void EncodeBitField(const ASTContext *Context, std::string& S, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 2612 | const FieldDecl *FD) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 2613 | const Expr *E = FD->getBitWidth(); |
| 2614 | assert(E && "bitfield width not there - getObjCEncodingForTypeImpl"); |
| 2615 | ASTContext *Ctx = const_cast<ASTContext*>(Context); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 2616 | unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue(); |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 2617 | S += 'b'; |
| 2618 | S += llvm::utostr(N); |
| 2619 | } |
| 2620 | |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 2621 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 2622 | bool ExpandPointedToStructures, |
| 2623 | bool ExpandStructures, |
Daniel Dunbar | 153bfe5 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 2624 | const FieldDecl *FD, |
Fariborz Jahanian | 090b3f7 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 2625 | bool OutermostType, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2626 | bool EncodingProperty) { |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 2627 | if (const BuiltinType *BT = T->getAsBuiltinType()) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2628 | if (FD && FD->isBitField()) |
| 2629 | return EncodeBitField(this, S, FD); |
| 2630 | char encoding; |
| 2631 | switch (BT->getKind()) { |
| 2632 | default: assert(0 && "Unhandled builtin type kind"); |
| 2633 | case BuiltinType::Void: encoding = 'v'; break; |
| 2634 | case BuiltinType::Bool: encoding = 'B'; break; |
| 2635 | case BuiltinType::Char_U: |
| 2636 | case BuiltinType::UChar: encoding = 'C'; break; |
| 2637 | case BuiltinType::UShort: encoding = 'S'; break; |
| 2638 | case BuiltinType::UInt: encoding = 'I'; break; |
| 2639 | case BuiltinType::ULong: |
Fariborz Jahanian | 72696e1 | 2009-02-11 22:31:45 +0000 | [diff] [blame] | 2640 | encoding = |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2641 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q'; |
Fariborz Jahanian | 72696e1 | 2009-02-11 22:31:45 +0000 | [diff] [blame] | 2642 | break; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2643 | case BuiltinType::UInt128: encoding = 'T'; break; |
| 2644 | case BuiltinType::ULongLong: encoding = 'Q'; break; |
| 2645 | case BuiltinType::Char_S: |
| 2646 | case BuiltinType::SChar: encoding = 'c'; break; |
| 2647 | case BuiltinType::Short: encoding = 's'; break; |
| 2648 | case BuiltinType::Int: encoding = 'i'; break; |
| 2649 | case BuiltinType::Long: |
| 2650 | encoding = |
| 2651 | (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q'; |
| 2652 | break; |
| 2653 | case BuiltinType::LongLong: encoding = 'q'; break; |
| 2654 | case BuiltinType::Int128: encoding = 't'; break; |
| 2655 | case BuiltinType::Float: encoding = 'f'; break; |
| 2656 | case BuiltinType::Double: encoding = 'd'; break; |
| 2657 | case BuiltinType::LongDouble: encoding = 'd'; break; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2658 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2659 | |
| 2660 | S += encoding; |
| 2661 | return; |
| 2662 | } |
| 2663 | |
| 2664 | if (const ComplexType *CT = T->getAsComplexType()) { |
Anders Carlsson | c612f7b | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 2665 | S += 'j'; |
| 2666 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
| 2667 | false); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2668 | return; |
| 2669 | } |
| 2670 | |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 2671 | if (const PointerType *PT = T->getAsPointerType()) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2672 | QualType PointeeTy = PT->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2673 | bool isReadOnly = false; |
| 2674 | // For historical/compatibility reasons, the read-only qualifier of the |
| 2675 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 2676 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
| 2677 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 2678 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2679 | if (OutermostType && T.isConstQualified()) { |
| 2680 | isReadOnly = true; |
| 2681 | S += 'r'; |
| 2682 | } |
| 2683 | } |
| 2684 | else if (OutermostType) { |
| 2685 | QualType P = PointeeTy; |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 2686 | while (P->getAsPointerType()) |
| 2687 | P = P->getAsPointerType()->getPointeeType(); |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2688 | if (P.isConstQualified()) { |
| 2689 | isReadOnly = true; |
| 2690 | S += 'r'; |
| 2691 | } |
| 2692 | } |
| 2693 | if (isReadOnly) { |
| 2694 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 2695 | // combinations need to be rearranged. |
| 2696 | // Rewrite "in const" from "nr" to "rn" |
| 2697 | const char * s = S.c_str(); |
| 2698 | int len = S.length(); |
| 2699 | if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') { |
| 2700 | std::string replace = "rn"; |
| 2701 | S.replace(S.end()-2, S.end(), replace); |
| 2702 | } |
| 2703 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2704 | if (isObjCSelType(PointeeTy)) { |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 2705 | S += ':'; |
| 2706 | return; |
Fariborz Jahanian | c2939bc | 2007-10-30 17:06:23 +0000 | [diff] [blame] | 2707 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2708 | |
| 2709 | if (PointeeTy->isCharType()) { |
| 2710 | // char pointer types should be encoded as '*' unless it is a |
| 2711 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | e8c4953 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 2712 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2713 | S += '*'; |
| 2714 | return; |
| 2715 | } |
Steve Naroff | 9533a7f | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 2716 | } else if (const RecordType *RTy = PointeeTy->getAsRecordType()) { |
| 2717 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 2718 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 2719 | S += '#'; |
| 2720 | return; |
| 2721 | } |
| 2722 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 2723 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 2724 | S += '@'; |
| 2725 | return; |
| 2726 | } |
| 2727 | // fall through... |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2728 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2729 | S += '^'; |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2730 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 2731 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2732 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2733 | NULL); |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2734 | return; |
| 2735 | } |
| 2736 | |
| 2737 | if (const ArrayType *AT = |
| 2738 | // Ignore type qualifiers etc. |
| 2739 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 2740 | if (isa<IncompleteArrayType>(AT)) { |
| 2741 | // Incomplete arrays are encoded as a pointer to the array element. |
| 2742 | S += '^'; |
| 2743 | |
| 2744 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
| 2745 | false, ExpandStructures, FD); |
| 2746 | } else { |
| 2747 | S += '['; |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2748 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 2749 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 2750 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 2751 | else { |
| 2752 | //Variable length arrays are encoded as a regular array with 0 elements. |
| 2753 | assert(isa<VariableArrayType>(AT) && "Unknown array type!"); |
| 2754 | S += '0'; |
| 2755 | } |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2756 | |
Anders Carlsson | 559a833 | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 2757 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
| 2758 | false, ExpandStructures, FD); |
| 2759 | S += ']'; |
| 2760 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2761 | return; |
| 2762 | } |
| 2763 | |
| 2764 | if (T->getAsFunctionType()) { |
Anders Carlsson | c0a87b7 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 2765 | S += '?'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2766 | return; |
| 2767 | } |
| 2768 | |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 2769 | if (const RecordType *RTy = T->getAsRecordType()) { |
Daniel Dunbar | 82a6cfb | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 2770 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2771 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 502a4a1 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 2772 | // Anonymous structures print as '?' |
| 2773 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 2774 | S += II->getName(); |
| 2775 | } else { |
| 2776 | S += '?'; |
| 2777 | } |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 2778 | if (ExpandStructures) { |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2779 | S += '='; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2780 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 2781 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2782 | Field != FieldEnd; ++Field) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2783 | if (FD) { |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2784 | S += '"'; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2785 | S += Field->getNameAsString(); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2786 | S += '"'; |
| 2787 | } |
| 2788 | |
| 2789 | // Special case bit-fields. |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2790 | if (Field->isBitField()) { |
| 2791 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
| 2792 | (*Field)); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2793 | } else { |
Fariborz Jahanian | a1c033e | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 2794 | QualType qt = Field->getType(); |
| 2795 | getLegacyIntegralTypeEncoding(qt); |
| 2796 | getObjCEncodingForTypeImpl(qt, S, false, true, |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2797 | FD); |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2798 | } |
Fariborz Jahanian | 7d6b46d | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 2799 | } |
Fariborz Jahanian | 6de88a8 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 2800 | } |
Daniel Dunbar | d96b35b | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 2801 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2802 | return; |
| 2803 | } |
| 2804 | |
| 2805 | if (T->isEnumeralType()) { |
Fariborz Jahanian | 8b4bf90 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 2806 | if (FD && FD->isBitField()) |
| 2807 | EncodeBitField(this, S, FD); |
| 2808 | else |
| 2809 | S += 'i'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2810 | return; |
| 2811 | } |
| 2812 | |
| 2813 | if (T->isBlockPointerType()) { |
Steve Naroff | 21a98b1 | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 2814 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2815 | return; |
| 2816 | } |
| 2817 | |
| 2818 | if (T->isObjCInterfaceType()) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2819 | // @encode(class_name) |
| 2820 | ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl(); |
| 2821 | S += '{'; |
| 2822 | const IdentifierInfo *II = OI->getIdentifier(); |
| 2823 | S += II->getName(); |
| 2824 | S += '='; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2825 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2826 | CollectObjCIvars(OI, RecFields); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2827 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2828 | if (RecFields[i]->isBitField()) |
| 2829 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
| 2830 | RecFields[i]); |
| 2831 | else |
| 2832 | getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true, |
| 2833 | FD); |
| 2834 | } |
| 2835 | S += '}'; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2836 | return; |
Fariborz Jahanian | 43822ea | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 2837 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2838 | |
| 2839 | if (const ObjCObjectPointerType *OPT = T->getAsObjCObjectPointerType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2840 | if (OPT->isObjCIdType()) { |
| 2841 | S += '@'; |
| 2842 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | if (OPT->isObjCClassType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2846 | S += '#'; |
| 2847 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | if (OPT->isObjCQualifiedIdType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2851 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
| 2852 | ExpandPointedToStructures, |
| 2853 | ExpandStructures, FD); |
| 2854 | if (FD || EncodingProperty) { |
| 2855 | // Note that we do extended encoding of protocol qualifer list |
| 2856 | // Only when doing ivar or property encoding. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2857 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 2858 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 2859 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2860 | S += '<'; |
| 2861 | S += (*I)->getNameAsString(); |
| 2862 | S += '>'; |
| 2863 | } |
| 2864 | S += '"'; |
| 2865 | } |
| 2866 | return; |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
| 2869 | QualType PointeeTy = OPT->getPointeeType(); |
| 2870 | if (!EncodingProperty && |
| 2871 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 2872 | // Another historical/compatibility reason. |
| 2873 | // We encode the underlying type which comes out as |
| 2874 | // {...}; |
| 2875 | S += '^'; |
| 2876 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 2877 | false, ExpandPointedToStructures, |
| 2878 | NULL); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2879 | return; |
| 2880 | } |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2881 | |
| 2882 | S += '@'; |
| 2883 | if (FD || EncodingProperty) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2884 | S += '"'; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 2885 | S += OPT->getInterfaceDecl()->getNameAsCString(); |
| 2886 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 2887 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2888 | S += '<'; |
| 2889 | S += (*I)->getNameAsString(); |
| 2890 | S += '>'; |
| 2891 | } |
| 2892 | S += '"'; |
| 2893 | } |
| 2894 | return; |
| 2895 | } |
| 2896 | |
| 2897 | assert(0 && "@encode for type not implemented!"); |
Anders Carlsson | 85f9bce | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2900 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ecb01e6 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 2901 | std::string& S) const { |
| 2902 | if (QT & Decl::OBJC_TQ_In) |
| 2903 | S += 'n'; |
| 2904 | if (QT & Decl::OBJC_TQ_Inout) |
| 2905 | S += 'N'; |
| 2906 | if (QT & Decl::OBJC_TQ_Out) |
| 2907 | S += 'o'; |
| 2908 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 2909 | S += 'O'; |
| 2910 | if (QT & Decl::OBJC_TQ_Byref) |
| 2911 | S += 'R'; |
| 2912 | if (QT & Decl::OBJC_TQ_Oneway) |
| 2913 | S += 'V'; |
| 2914 | } |
| 2915 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2916 | void ASTContext::setBuiltinVaListType(QualType T) { |
Anders Carlsson | b2cf357 | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 2917 | assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!"); |
| 2918 | |
| 2919 | BuiltinVaListType = T; |
| 2920 | } |
| 2921 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2922 | void ASTContext::setObjCIdType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2923 | ObjCIdTypedefType = T; |
Steve Naroff | 7e219e4 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2926 | void ASTContext::setObjCSelType(QualType T) { |
Douglas Gregor | 319ac89 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 2927 | ObjCSelType = T; |
| 2928 | |
| 2929 | const TypedefType *TT = T->getAsTypedefType(); |
| 2930 | if (!TT) |
| 2931 | return; |
| 2932 | TypedefDecl *TD = TT->getDecl(); |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2933 | |
| 2934 | // typedef struct objc_selector *SEL; |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 2935 | const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType(); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 2936 | if (!ptr) |
| 2937 | return; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2938 | const RecordType *rec = ptr->getPointeeType()->getAsStructureType(); |
Fariborz Jahanian | c55a240 | 2009-01-16 19:58:32 +0000 | [diff] [blame] | 2939 | if (!rec) |
| 2940 | return; |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2941 | SelStructType = rec; |
| 2942 | } |
| 2943 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2944 | void ASTContext::setObjCProtoType(QualType QT) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2945 | ObjCProtoType = QT; |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
Chris Lattner | ce7b38c | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 2948 | void ASTContext::setObjCClassType(QualType T) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2949 | ObjCClassTypedefType = T; |
Anders Carlsson | 8baaca5 | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2952 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
| 2953 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 2954 | "'NSConstantString' type already set!"); |
| 2955 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2956 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2959 | /// \brief Retrieve the template name that represents a qualified |
| 2960 | /// template name such as \c std::vector. |
| 2961 | TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 2962 | bool TemplateKeyword, |
| 2963 | TemplateDecl *Template) { |
| 2964 | llvm::FoldingSetNodeID ID; |
| 2965 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 2966 | |
| 2967 | void *InsertPos = 0; |
| 2968 | QualifiedTemplateName *QTN = |
| 2969 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 2970 | if (!QTN) { |
| 2971 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 2972 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 2973 | } |
| 2974 | |
| 2975 | return TemplateName(QTN); |
| 2976 | } |
| 2977 | |
| 2978 | /// \brief Retrieve the template name that represents a dependent |
| 2979 | /// template name such as \c MetaFun::template apply. |
| 2980 | TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 2981 | const IdentifierInfo *Name) { |
| 2982 | assert(NNS->isDependent() && "Nested name specifier must be dependent"); |
| 2983 | |
| 2984 | llvm::FoldingSetNodeID ID; |
| 2985 | DependentTemplateName::Profile(ID, NNS, Name); |
| 2986 | |
| 2987 | void *InsertPos = 0; |
| 2988 | DependentTemplateName *QTN = |
| 2989 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 2990 | |
| 2991 | if (QTN) |
| 2992 | return TemplateName(QTN); |
| 2993 | |
| 2994 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 2995 | if (CanonNNS == NNS) { |
| 2996 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 2997 | } else { |
| 2998 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 2999 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
| 3000 | } |
| 3001 | |
| 3002 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 3003 | return TemplateName(QTN); |
| 3004 | } |
| 3005 | |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3006 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | d934112 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 3007 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 3008 | /// is actually a value of type @c TargetInfo::IntType. |
| 3009 | QualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3010 | switch (Type) { |
| 3011 | case TargetInfo::NoInt: return QualType(); |
| 3012 | case TargetInfo::SignedShort: return ShortTy; |
| 3013 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 3014 | case TargetInfo::SignedInt: return IntTy; |
| 3015 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 3016 | case TargetInfo::SignedLong: return LongTy; |
| 3017 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 3018 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 3019 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 3020 | } |
| 3021 | |
| 3022 | assert(false && "Unhandled TargetInfo::IntType value"); |
Daniel Dunbar | b3ac543 | 2008-11-11 01:16:00 +0000 | [diff] [blame] | 3023 | return QualType(); |
Douglas Gregor | b4e66d5 | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 3024 | } |
Ted Kremenek | b6ccaac | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 3025 | |
| 3026 | //===----------------------------------------------------------------------===// |
| 3027 | // Type Predicates. |
| 3028 | //===----------------------------------------------------------------------===// |
| 3029 | |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3030 | /// isObjCNSObjectType - Return true if this is an NSObject object using |
| 3031 | /// NSObject attribute on a c-style pointer type. |
| 3032 | /// FIXME - Make it work directly on types. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3033 | /// FIXME: Move to Type. |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3034 | /// |
| 3035 | bool ASTContext::isObjCNSObjectType(QualType Ty) const { |
| 3036 | if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) { |
| 3037 | if (TypedefDecl *TD = TDT->getDecl()) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3038 | if (TD->getAttr<ObjCNSObjectAttr>()) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 3039 | return true; |
| 3040 | } |
| 3041 | return false; |
| 3042 | } |
| 3043 | |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3044 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 3045 | /// garbage collection attribute. |
| 3046 | /// |
| 3047 | QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3048 | QualType::GCAttrTypes GCAttrs = QualType::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3049 | if (getLangOptions().ObjC1 && |
| 3050 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3051 | GCAttrs = Ty.getObjCGCAttr(); |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3052 | // Default behavious under objective-c's gc is for objective-c pointers |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3053 | // (or pointers to them) be treated as though they were declared |
| 3054 | // as __strong. |
| 3055 | if (GCAttrs == QualType::GCNone) { |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3056 | if (Ty->isObjCObjectPointerType()) |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3057 | GCAttrs = QualType::Strong; |
| 3058 | else if (Ty->isPointerType()) |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 3059 | return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType()); |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 3060 | } |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 3061 | // Non-pointers have none gc'able attribute regardless of the attribute |
| 3062 | // set on them. |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3063 | else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType()) |
Fariborz Jahanian | c211218 | 2009-04-11 00:00:54 +0000 | [diff] [blame] | 3064 | return QualType::GCNone; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3065 | } |
Chris Lattner | b7d2553 | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 3066 | return GCAttrs; |
Fariborz Jahanian | 4fd83ea | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3069 | //===----------------------------------------------------------------------===// |
| 3070 | // Type Compatibility Testing |
| 3071 | //===----------------------------------------------------------------------===// |
Chris Lattner | 770951b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 3072 | |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3073 | /// areCompatVectorTypes - Return true if the two specified vector types are |
| 3074 | /// compatible. |
| 3075 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 3076 | const VectorType *RHS) { |
| 3077 | assert(LHS->isCanonical() && RHS->isCanonical()); |
| 3078 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3079 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3082 | //===----------------------------------------------------------------------===// |
| 3083 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 3084 | //===----------------------------------------------------------------------===// |
| 3085 | |
| 3086 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 3087 | /// inheritance hierarchy of 'rProto'. |
| 3088 | static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 3089 | ObjCProtocolDecl *rProto) { |
| 3090 | if (lProto == rProto) |
| 3091 | return true; |
| 3092 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 3093 | E = rProto->protocol_end(); PI != E; ++PI) |
| 3094 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 3095 | return true; |
| 3096 | return false; |
| 3097 | } |
| 3098 | |
| 3099 | /// ClassImplementsProtocol - Checks that 'lProto' protocol |
| 3100 | /// has been implemented in IDecl class, its super class or categories (if |
| 3101 | /// lookupCategory is true). |
| 3102 | static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto, |
| 3103 | ObjCInterfaceDecl *IDecl, |
| 3104 | bool lookupCategory, |
| 3105 | bool RHSIsQualifiedID = false) { |
| 3106 | |
| 3107 | // 1st, look up the class. |
| 3108 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 3109 | IDecl->getReferencedProtocols(); |
| 3110 | |
| 3111 | for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(), |
| 3112 | E = Protocols.end(); PI != E; ++PI) { |
| 3113 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 3114 | return true; |
| 3115 | // This is dubious and is added to be compatible with gcc. In gcc, it is |
| 3116 | // also allowed assigning a protocol-qualified 'id' type to a LHS object |
| 3117 | // when protocol in qualified LHS is in list of protocols in the rhs 'id' |
| 3118 | // object. This IMO, should be a bug. |
| 3119 | // FIXME: Treat this as an extension, and flag this as an error when GCC |
| 3120 | // extensions are not enabled. |
| 3121 | if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto)) |
| 3122 | return true; |
| 3123 | } |
| 3124 | |
| 3125 | // 2nd, look up the category. |
| 3126 | if (lookupCategory) |
| 3127 | for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl; |
| 3128 | CDecl = CDecl->getNextClassCategory()) { |
| 3129 | for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(), |
| 3130 | E = CDecl->protocol_end(); PI != E; ++PI) |
| 3131 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 3132 | return true; |
| 3133 | } |
| 3134 | |
| 3135 | // 3rd, look up the super class(s) |
| 3136 | if (IDecl->getSuperClass()) |
| 3137 | return |
| 3138 | ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory, |
| 3139 | RHSIsQualifiedID); |
| 3140 | |
| 3141 | return false; |
| 3142 | } |
| 3143 | |
| 3144 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 3145 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 3146 | /// otherwise. |
| 3147 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 3148 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 3149 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 3150 | return false; |
| 3151 | } |
| 3152 | |
| 3153 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 3154 | /// ObjCQualifiedIDType. |
| 3155 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 3156 | bool compare) { |
| 3157 | // Allow id<P..> and an 'id' or void* type in all cases. |
| 3158 | if (lhs->isVoidPointerType() || |
| 3159 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 3160 | return true; |
| 3161 | else if (rhs->isVoidPointerType() || |
| 3162 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 3163 | return true; |
| 3164 | |
| 3165 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
| 3166 | const ObjCObjectPointerType *rhsOPT = rhs->getAsObjCObjectPointerType(); |
| 3167 | |
| 3168 | if (!rhsOPT) return false; |
| 3169 | |
| 3170 | if (rhsOPT->qual_empty()) { |
| 3171 | // If the RHS is a unqualified interface pointer "NSString*", |
| 3172 | // make sure we check the class hierarchy. |
| 3173 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 3174 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3175 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3176 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3177 | // see if static class implements all of id's protocols, directly or |
| 3178 | // through its super class and categories. |
| 3179 | if (!ClassImplementsProtocol(*I, rhsID, true)) |
| 3180 | return false; |
| 3181 | } |
| 3182 | } |
| 3183 | // If there are no qualifiers and no interface, we have an 'id'. |
| 3184 | return true; |
| 3185 | } |
| 3186 | // Both the right and left sides have qualifiers. |
| 3187 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3188 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3189 | ObjCProtocolDecl *lhsProto = *I; |
| 3190 | bool match = false; |
| 3191 | |
| 3192 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3193 | // see if static class implements all of id's protocols, directly or |
| 3194 | // through its super class and categories. |
| 3195 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 3196 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 3197 | ObjCProtocolDecl *rhsProto = *J; |
| 3198 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 3199 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 3200 | match = true; |
| 3201 | break; |
| 3202 | } |
| 3203 | } |
| 3204 | // If the RHS is a qualified interface pointer "NSString<P>*", |
| 3205 | // make sure we check the class hierarchy. |
| 3206 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 3207 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 3208 | E = lhsQID->qual_end(); I != E; ++I) { |
| 3209 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3210 | // see if static class implements all of id's protocols, directly or |
| 3211 | // through its super class and categories. |
| 3212 | if (ClassImplementsProtocol(*I, rhsID, true)) { |
| 3213 | match = true; |
| 3214 | break; |
| 3215 | } |
| 3216 | } |
| 3217 | } |
| 3218 | if (!match) |
| 3219 | return false; |
| 3220 | } |
| 3221 | |
| 3222 | return true; |
| 3223 | } |
| 3224 | |
| 3225 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 3226 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 3227 | |
| 3228 | if (const ObjCObjectPointerType *lhsOPT = |
| 3229 | lhs->getAsObjCInterfacePointerType()) { |
| 3230 | if (lhsOPT->qual_empty()) { |
| 3231 | bool match = false; |
| 3232 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 3233 | for (ObjCObjectPointerType::qual_iterator I = rhsQID->qual_begin(), |
| 3234 | E = rhsQID->qual_end(); I != E; ++I) { |
| 3235 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3236 | // see if static class implements all of id's protocols, directly or |
| 3237 | // through its super class and categories. |
| 3238 | if (ClassImplementsProtocol(*I, lhsID, true)) { |
| 3239 | match = true; |
| 3240 | break; |
| 3241 | } |
| 3242 | } |
| 3243 | if (!match) |
| 3244 | return false; |
| 3245 | } |
| 3246 | return true; |
| 3247 | } |
| 3248 | // Both the right and left sides have qualifiers. |
| 3249 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 3250 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 3251 | ObjCProtocolDecl *lhsProto = *I; |
| 3252 | bool match = false; |
| 3253 | |
| 3254 | // when comparing an id<P> on lhs with a static type on rhs, |
| 3255 | // see if static class implements all of id's protocols, directly or |
| 3256 | // through its super class and categories. |
| 3257 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 3258 | E = rhsQID->qual_end(); J != E; ++J) { |
| 3259 | ObjCProtocolDecl *rhsProto = *J; |
| 3260 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 3261 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 3262 | match = true; |
| 3263 | break; |
| 3264 | } |
| 3265 | } |
| 3266 | if (!match) |
| 3267 | return false; |
| 3268 | } |
| 3269 | return true; |
| 3270 | } |
| 3271 | return false; |
| 3272 | } |
| 3273 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3274 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3275 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 3276 | /// protocol qualifiers on the LHS or RHS. |
| 3277 | /// |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3278 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 3279 | const ObjCObjectPointerType *RHSOPT) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3280 | // If either type represents the built-in 'id' or 'Class' types, return true. |
| 3281 | if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3282 | return true; |
| 3283 | |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3284 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
| 3285 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 3286 | QualType(RHSOPT,0), |
| 3287 | false); |
| 3288 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3289 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 3290 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3291 | if (LHS && RHS) // We have 2 user-defined types. |
| 3292 | return canAssignObjCInterfaces(LHS, RHS); |
| 3293 | |
| 3294 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3295 | } |
| 3296 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3297 | bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, |
| 3298 | const ObjCInterfaceType *RHS) { |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3299 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 3300 | // the LHS. |
| 3301 | if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
| 3302 | return false; |
| 3303 | |
| 3304 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 3305 | // protocol qualified at all, then we are good. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 3306 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3307 | return true; |
| 3308 | |
| 3309 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it |
| 3310 | // isn't a superset. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 3311 | if (RHS->getNumProtocols() == 0) |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3312 | return true; // FIXME: should return false! |
| 3313 | |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 3314 | for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(), |
| 3315 | LHSPE = LHS->qual_end(); |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 3316 | LHSPI != LHSPE; LHSPI++) { |
| 3317 | bool RHSImplementsProtocol = false; |
| 3318 | |
| 3319 | // If the RHS doesn't implement the protocol on the left, the types |
| 3320 | // are incompatible. |
Steve Naroff | c15cb2a | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 3321 | for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(), |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3322 | RHSPE = RHS->qual_end(); |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 3323 | RHSPI != RHSPE; RHSPI++) { |
| 3324 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 3325 | RHSImplementsProtocol = true; |
Steve Naroff | 8f16756 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 3326 | break; |
| 3327 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 3328 | } |
| 3329 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 3330 | if (!RHSImplementsProtocol) |
| 3331 | return false; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3332 | } |
Steve Naroff | 91b0b0c | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 3333 | // The RHS implements all protocols listed on the LHS. |
| 3334 | return true; |
Chris Lattner | 6ac46a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3337 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 3338 | // get the "pointed to" types |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3339 | const ObjCObjectPointerType *LHSOPT = LHS->getAsObjCObjectPointerType(); |
| 3340 | const ObjCObjectPointerType *RHSOPT = RHS->getAsObjCObjectPointerType(); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3341 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3342 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3343 | return false; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3344 | |
| 3345 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 3346 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 3349 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
| 3350 | /// both shall have the identically qualified version of a compatible type. |
| 3351 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
| 3352 | /// same. See 6.7.[2,3,5] for additional rules. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3353 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) { |
| 3354 | return !mergeTypes(LHS, RHS).isNull(); |
| 3355 | } |
| 3356 | |
| 3357 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) { |
| 3358 | const FunctionType *lbase = lhs->getAsFunctionType(); |
| 3359 | const FunctionType *rbase = rhs->getAsFunctionType(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3360 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 3361 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3362 | bool allLTypes = true; |
| 3363 | bool allRTypes = true; |
| 3364 | |
| 3365 | // Check return type |
| 3366 | QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType()); |
| 3367 | if (retType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3368 | if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType())) |
| 3369 | allLTypes = false; |
| 3370 | if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType())) |
| 3371 | allRTypes = false; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame^] | 3372 | bool NoReturn = lbase->getNoReturnAttr() || rbase->getNoReturnAttr(); |
| 3373 | if (NoReturn != lbase->getNoReturnAttr()) |
| 3374 | allLTypes = false; |
| 3375 | if (NoReturn != rbase->getNoReturnAttr()) |
| 3376 | allRTypes = false; |
| 3377 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3378 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3379 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 3380 | "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3381 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 3382 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 3383 | |
| 3384 | // Compatible functions must have the same number of arguments |
| 3385 | if (lproto_nargs != rproto_nargs) |
| 3386 | return QualType(); |
| 3387 | |
| 3388 | // Variadic and non-variadic functions aren't compatible |
| 3389 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 3390 | return QualType(); |
| 3391 | |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 3392 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 3393 | return QualType(); |
| 3394 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3395 | // Check argument compatibility |
| 3396 | llvm::SmallVector<QualType, 10> types; |
| 3397 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 3398 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 3399 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
| 3400 | QualType argtype = mergeTypes(largtype, rargtype); |
| 3401 | if (argtype.isNull()) return QualType(); |
| 3402 | types.push_back(argtype); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3403 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 3404 | allLTypes = false; |
| 3405 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 3406 | allRTypes = false; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3407 | } |
| 3408 | if (allLTypes) return lhs; |
| 3409 | if (allRTypes) return rhs; |
| 3410 | return getFunctionType(retType, types.begin(), types.size(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame^] | 3411 | lproto->isVariadic(), lproto->getTypeQuals(), |
| 3412 | NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | if (lproto) allRTypes = false; |
| 3416 | if (rproto) allLTypes = false; |
| 3417 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3418 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3419 | if (proto) { |
Sebastian Redl | 465226e | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3420 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3421 | if (proto->isVariadic()) return QualType(); |
| 3422 | // Check that the types are compatible with the types that |
| 3423 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 3424 | // The only types actually affected are promotable integer |
| 3425 | // types and floats, which would be passed as a different |
| 3426 | // type depending on whether the prototype is visible. |
| 3427 | unsigned proto_nargs = proto->getNumArgs(); |
| 3428 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 3429 | QualType argTy = proto->getArgType(i); |
| 3430 | if (argTy->isPromotableIntegerType() || |
| 3431 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 3432 | return QualType(); |
| 3433 | } |
| 3434 | |
| 3435 | if (allLTypes) return lhs; |
| 3436 | if (allRTypes) return rhs; |
| 3437 | return getFunctionType(retType, proto->arg_type_begin(), |
Argyrios Kyrtzidis | 7fb5e48 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 3438 | proto->getNumArgs(), lproto->isVariadic(), |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame^] | 3439 | lproto->getTypeQuals(), NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
| 3442 | if (allLTypes) return lhs; |
| 3443 | if (allRTypes) return rhs; |
Mike Stump | 2455636 | 2009-07-25 21:26:53 +0000 | [diff] [blame^] | 3444 | return getFunctionNoProtoType(retType, NoReturn); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) { |
Bill Wendling | 43d6975 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 3448 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 3449 | // type is adjusted to "T" prior to any further analysis, the expression |
| 3450 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3451 | // expression is an lvalue unless the reference is an rvalue reference and |
| 3452 | // the expression is a function call (possibly inside parentheses). |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3453 | // FIXME: C++ shouldn't be going through here! The rules are different |
| 3454 | // enough that they should be handled separately. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3455 | // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really* |
| 3456 | // shouldn't be going through here! |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 3457 | if (const ReferenceType *RT = LHS->getAsReferenceType()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 3458 | LHS = RT->getPointeeType(); |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 3459 | if (const ReferenceType *RT = RHS->getAsReferenceType()) |
Chris Lattner | c4e4059 | 2008-04-07 04:07:56 +0000 | [diff] [blame] | 3460 | RHS = RT->getPointeeType(); |
Chris Lattner | f3692dc | 2008-04-07 05:37:56 +0000 | [diff] [blame] | 3461 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3462 | QualType LHSCan = getCanonicalType(LHS), |
| 3463 | RHSCan = getCanonicalType(RHS); |
| 3464 | |
| 3465 | // If two types are identical, they are compatible. |
| 3466 | if (LHSCan == RHSCan) |
| 3467 | return LHS; |
| 3468 | |
| 3469 | // If the qualifiers are different, the types aren't compatible |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 3470 | // Note that we handle extended qualifiers later, in the |
| 3471 | // case for ExtQualType. |
| 3472 | if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers()) |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3473 | return QualType(); |
| 3474 | |
Eli Friedman | 852d63b | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 3475 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 3476 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3477 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3478 | // We want to consider the two function types to be the same for these |
| 3479 | // comparisons, just force one to the other. |
| 3480 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 3481 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 3482 | |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3483 | // Strip off objc_gc attributes off the top level so they can be merged. |
| 3484 | // 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] | 3485 | if (RHSClass == Type::ExtQual) { |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3486 | QualType::GCAttrTypes GCAttr = RHSCan.getObjCGCAttr(); |
| 3487 | if (GCAttr != QualType::GCNone) { |
Fariborz Jahanian | 86f4385 | 2009-06-02 20:58:58 +0000 | [diff] [blame] | 3488 | QualType::GCAttrTypes GCLHSAttr = LHSCan.getObjCGCAttr(); |
Fariborz Jahanian | 8df7a28 | 2009-06-02 18:32:00 +0000 | [diff] [blame] | 3489 | // __weak attribute must appear on both declarations. |
Fariborz Jahanian | 86f4385 | 2009-06-02 20:58:58 +0000 | [diff] [blame] | 3490 | // __strong attribue is redundant if other decl is an objective-c |
| 3491 | // object pointer (or decorated with __strong attribute); otherwise |
| 3492 | // issue error. |
| 3493 | if ((GCAttr == QualType::Weak && GCLHSAttr != GCAttr) || |
| 3494 | (GCAttr == QualType::Strong && GCLHSAttr != GCAttr && |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3495 | !LHSCan->isObjCObjectPointerType())) |
Fariborz Jahanian | 8df7a28 | 2009-06-02 18:32:00 +0000 | [diff] [blame] | 3496 | return QualType(); |
| 3497 | |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3498 | RHS = QualType(cast<ExtQualType>(RHS.getDesugaredType())->getBaseType(), |
| 3499 | RHS.getCVRQualifiers()); |
| 3500 | QualType Result = mergeTypes(LHS, RHS); |
Fariborz Jahanian | 8df7a28 | 2009-06-02 18:32:00 +0000 | [diff] [blame] | 3501 | if (!Result.isNull()) { |
| 3502 | if (Result.getObjCGCAttr() == QualType::GCNone) |
| 3503 | Result = getObjCGCQualType(Result, GCAttr); |
| 3504 | else if (Result.getObjCGCAttr() != GCAttr) |
| 3505 | Result = QualType(); |
| 3506 | } |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3507 | return Result; |
| 3508 | } |
Fariborz Jahanian | 585f7b2 | 2009-06-02 01:40:22 +0000 | [diff] [blame] | 3509 | } |
Fariborz Jahanian | 585f7b2 | 2009-06-02 01:40:22 +0000 | [diff] [blame] | 3510 | if (LHSClass == Type::ExtQual) { |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3511 | QualType::GCAttrTypes GCAttr = LHSCan.getObjCGCAttr(); |
| 3512 | if (GCAttr != QualType::GCNone) { |
Fariborz Jahanian | 8df7a28 | 2009-06-02 18:32:00 +0000 | [diff] [blame] | 3513 | QualType::GCAttrTypes GCRHSAttr = RHSCan.getObjCGCAttr(); |
| 3514 | // __weak attribute must appear on both declarations. __strong |
Fariborz Jahanian | 86f4385 | 2009-06-02 20:58:58 +0000 | [diff] [blame] | 3515 | // __strong attribue is redundant if other decl is an objective-c |
| 3516 | // object pointer (or decorated with __strong attribute); otherwise |
| 3517 | // issue error. |
| 3518 | if ((GCAttr == QualType::Weak && GCRHSAttr != GCAttr) || |
| 3519 | (GCAttr == QualType::Strong && GCRHSAttr != GCAttr && |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3520 | !RHSCan->isObjCObjectPointerType())) |
Fariborz Jahanian | 8df7a28 | 2009-06-02 18:32:00 +0000 | [diff] [blame] | 3521 | return QualType(); |
Fariborz Jahanian | 86f4385 | 2009-06-02 20:58:58 +0000 | [diff] [blame] | 3522 | |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3523 | LHS = QualType(cast<ExtQualType>(LHS.getDesugaredType())->getBaseType(), |
| 3524 | LHS.getCVRQualifiers()); |
| 3525 | QualType Result = mergeTypes(LHS, RHS); |
Fariborz Jahanian | 8df7a28 | 2009-06-02 18:32:00 +0000 | [diff] [blame] | 3526 | if (!Result.isNull()) { |
| 3527 | if (Result.getObjCGCAttr() == QualType::GCNone) |
| 3528 | Result = getObjCGCQualType(Result, GCAttr); |
| 3529 | else if (Result.getObjCGCAttr() != GCAttr) |
| 3530 | Result = QualType(); |
| 3531 | } |
Eli Friedman | 354e53d | 2009-06-02 07:45:37 +0000 | [diff] [blame] | 3532 | return Result; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3533 | } |
Fariborz Jahanian | 585f7b2 | 2009-06-02 01:40:22 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
Eli Friedman | 4c721d3 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 3536 | // Same as above for arrays |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 3537 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 3538 | LHSClass = Type::ConstantArray; |
| 3539 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 3540 | RHSClass = Type::ConstantArray; |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 3541 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3542 | // Canonicalize ExtVector -> Vector. |
| 3543 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 3544 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Chris Lattner | a36a61f | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 3545 | |
| 3546 | // If the canonical type classes don't match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3547 | if (LHSClass != RHSClass) { |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3548 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 3549 | // a signed integer type, or an unsigned integer type. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3550 | if (const EnumType* ETy = LHS->getAsEnumType()) { |
| 3551 | if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType()) |
| 3552 | return RHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 3553 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3554 | if (const EnumType* ETy = RHS->getAsEnumType()) { |
| 3555 | if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType()) |
| 3556 | return LHS; |
Eli Friedman | bab9696 | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 3557 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3558 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3559 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 3560 | } |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3561 | |
Steve Naroff | 4a74678 | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 3562 | // The canonical type classes match. |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3563 | switch (LHSClass) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3564 | #define TYPE(Class, Base) |
| 3565 | #define ABSTRACT_TYPE(Class, Base) |
| 3566 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 3567 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 3568 | #include "clang/AST/TypeNodes.def" |
| 3569 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 3570 | return QualType(); |
| 3571 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3572 | case Type::LValueReference: |
| 3573 | case Type::RValueReference: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3574 | case Type::MemberPointer: |
| 3575 | assert(false && "C++ should never be in mergeTypes"); |
| 3576 | return QualType(); |
| 3577 | |
| 3578 | case Type::IncompleteArray: |
| 3579 | case Type::VariableArray: |
| 3580 | case Type::FunctionProto: |
| 3581 | case Type::ExtVector: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3582 | assert(false && "Types are eliminated above"); |
| 3583 | return QualType(); |
| 3584 | |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3585 | case Type::Pointer: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3586 | { |
| 3587 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 3588 | QualType LHSPointee = LHS->getAsPointerType()->getPointeeType(); |
| 3589 | QualType RHSPointee = RHS->getAsPointerType()->getPointeeType(); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3590 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 3591 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3592 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3593 | return LHS; |
Eli Friedman | 07d2587 | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 3594 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3595 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3596 | return getPointerType(ResultType); |
| 3597 | } |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 3598 | case Type::BlockPointer: |
| 3599 | { |
| 3600 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 3601 | QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType(); |
| 3602 | QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType(); |
Steve Naroff | c0febd5 | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 3603 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee); |
| 3604 | if (ResultType.isNull()) return QualType(); |
| 3605 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 3606 | return LHS; |
| 3607 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 3608 | return RHS; |
| 3609 | return getBlockPointerType(ResultType); |
| 3610 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3611 | case Type::ConstantArray: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3612 | { |
| 3613 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 3614 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 3615 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 3616 | return QualType(); |
| 3617 | |
| 3618 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 3619 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
| 3620 | QualType ResultType = mergeTypes(LHSElem, RHSElem); |
| 3621 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3622 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 3623 | return LHS; |
| 3624 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 3625 | return RHS; |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 3626 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 3627 | ArrayType::ArraySizeModifier(), 0); |
| 3628 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 3629 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3630 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 3631 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3632 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 3633 | return LHS; |
| 3634 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 3635 | return RHS; |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3636 | if (LVAT) { |
| 3637 | // FIXME: This isn't correct! But tricky to implement because |
| 3638 | // the array's size has to be the size of LHS, but the type |
| 3639 | // has to be different. |
| 3640 | return LHS; |
| 3641 | } |
| 3642 | if (RVAT) { |
| 3643 | // FIXME: This isn't correct! But tricky to implement because |
| 3644 | // the array's size has to be the size of RHS, but the type |
| 3645 | // has to be different. |
| 3646 | return RHS; |
| 3647 | } |
Eli Friedman | 3bc0f45 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 3648 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 3649 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3650 | return getIncompleteArrayType(ResultType, |
| 3651 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3652 | } |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3653 | case Type::FunctionNoProto: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3654 | return mergeFunctionTypes(LHS, RHS); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3655 | case Type::Record: |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3656 | case Type::Enum: |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3657 | return QualType(); |
Chris Lattner | 1adb883 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 3658 | case Type::Builtin: |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 3659 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3660 | return QualType(); |
Daniel Dunbar | 64cfdb7 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 3661 | case Type::Complex: |
| 3662 | // Distinct complex types are incompatible. |
| 3663 | return QualType(); |
Chris Lattner | 3cc4c0c | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 3664 | case Type::Vector: |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 3665 | // FIXME: The merged type should be an ExtVector! |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3666 | if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType())) |
| 3667 | return LHS; |
Chris Lattner | 6171085 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 3668 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 3669 | case Type::ObjCInterface: { |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 3670 | // Check if the interfaces are assignment compatible. |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 3671 | // FIXME: This should be type compatibility, e.g. whether |
| 3672 | // "LHS x; RHS x;" at global scope is legal. |
Steve Naroff | 5fd659d | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 3673 | const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType(); |
| 3674 | const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType(); |
| 3675 | if (LHSIface && RHSIface && |
| 3676 | canAssignObjCInterfaces(LHSIface, RHSIface)) |
| 3677 | return LHS; |
| 3678 | |
Eli Friedman | 3d815e7 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 3679 | return QualType(); |
Cedric Venet | 61490e9 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 3680 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3681 | case Type::ObjCObjectPointer: { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3682 | if (canAssignObjCInterfaces(LHS->getAsObjCObjectPointerType(), |
| 3683 | RHS->getAsObjCObjectPointerType())) |
| 3684 | return LHS; |
| 3685 | |
Steve Naroff | bc76dd0 | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 3686 | return QualType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3687 | } |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 3688 | case Type::FixedWidthInt: |
| 3689 | // Distinct fixed-width integers are not compatible. |
| 3690 | return QualType(); |
Eli Friedman | 5a61f0e | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 3691 | case Type::ExtQual: |
| 3692 | // FIXME: ExtQual types can be compatible even if they're not |
| 3693 | // identical! |
| 3694 | return QualType(); |
| 3695 | // First attempt at an implementation, but I'm not really sure it's |
| 3696 | // right... |
| 3697 | #if 0 |
| 3698 | ExtQualType* LQual = cast<ExtQualType>(LHSCan); |
| 3699 | ExtQualType* RQual = cast<ExtQualType>(RHSCan); |
| 3700 | if (LQual->getAddressSpace() != RQual->getAddressSpace() || |
| 3701 | LQual->getObjCGCAttr() != RQual->getObjCGCAttr()) |
| 3702 | return QualType(); |
| 3703 | QualType LHSBase, RHSBase, ResultType, ResCanUnqual; |
| 3704 | LHSBase = QualType(LQual->getBaseType(), 0); |
| 3705 | RHSBase = QualType(RQual->getBaseType(), 0); |
| 3706 | ResultType = mergeTypes(LHSBase, RHSBase); |
| 3707 | if (ResultType.isNull()) return QualType(); |
| 3708 | ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType(); |
| 3709 | if (LHSCan.getUnqualifiedType() == ResCanUnqual) |
| 3710 | return LHS; |
| 3711 | if (RHSCan.getUnqualifiedType() == ResCanUnqual) |
| 3712 | return RHS; |
| 3713 | ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace()); |
| 3714 | ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr()); |
| 3715 | ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers()); |
| 3716 | return ResultType; |
| 3717 | #endif |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3718 | |
| 3719 | case Type::TemplateSpecialization: |
| 3720 | assert(false && "Dependent types have no size"); |
| 3721 | break; |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 3722 | } |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3723 | |
| 3724 | return QualType(); |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 3725 | } |
Ted Kremenek | 7192f8e | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 3726 | |
Chris Lattner | 5426bf6 | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 3727 | //===----------------------------------------------------------------------===// |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 3728 | // Integer Predicates |
| 3729 | //===----------------------------------------------------------------------===// |
Chris Lattner | 88054de | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 3730 | |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 3731 | unsigned ASTContext::getIntWidth(QualType T) { |
| 3732 | if (T == BoolTy) |
| 3733 | return 1; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3734 | if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) { |
| 3735 | return FWIT->getWidth(); |
| 3736 | } |
| 3737 | // For builtin types, just use the standard type sizing method |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 3738 | return (unsigned)getTypeSize(T); |
| 3739 | } |
| 3740 | |
| 3741 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
| 3742 | assert(T->isSignedIntegerType() && "Unexpected type"); |
| 3743 | if (const EnumType* ETy = T->getAsEnumType()) |
| 3744 | T = ETy->getDecl()->getIntegerType(); |
| 3745 | const BuiltinType* BTy = T->getAsBuiltinType(); |
| 3746 | assert (BTy && "Unexpected signed integer type"); |
| 3747 | switch (BTy->getKind()) { |
| 3748 | case BuiltinType::Char_S: |
| 3749 | case BuiltinType::SChar: |
| 3750 | return UnsignedCharTy; |
| 3751 | case BuiltinType::Short: |
| 3752 | return UnsignedShortTy; |
| 3753 | case BuiltinType::Int: |
| 3754 | return UnsignedIntTy; |
| 3755 | case BuiltinType::Long: |
| 3756 | return UnsignedLongTy; |
| 3757 | case BuiltinType::LongLong: |
| 3758 | return UnsignedLongLongTy; |
Chris Lattner | 2df9ced | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3759 | case BuiltinType::Int128: |
| 3760 | return UnsignedInt128Ty; |
Eli Friedman | ad74a75 | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 3761 | default: |
| 3762 | assert(0 && "Unexpected signed integer type"); |
| 3763 | return QualType(); |
| 3764 | } |
| 3765 | } |
| 3766 | |
Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 3767 | ExternalASTSource::~ExternalASTSource() { } |
| 3768 | |
| 3769 | void ExternalASTSource::PrintStats() { } |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 3770 | |
| 3771 | |
| 3772 | //===----------------------------------------------------------------------===// |
| 3773 | // Builtin Type Computation |
| 3774 | //===----------------------------------------------------------------------===// |
| 3775 | |
| 3776 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
| 3777 | /// pointer over the consumed characters. This returns the resultant type. |
| 3778 | static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context, |
| 3779 | ASTContext::GetBuiltinTypeError &Error, |
| 3780 | bool AllowTypeModifiers = true) { |
| 3781 | // Modifiers. |
| 3782 | int HowLong = 0; |
| 3783 | bool Signed = false, Unsigned = false; |
| 3784 | |
| 3785 | // Read the modifiers first. |
| 3786 | bool Done = false; |
| 3787 | while (!Done) { |
| 3788 | switch (*Str++) { |
| 3789 | default: Done = true; --Str; break; |
| 3790 | case 'S': |
| 3791 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 3792 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 3793 | Signed = true; |
| 3794 | break; |
| 3795 | case 'U': |
| 3796 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 3797 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 3798 | Unsigned = true; |
| 3799 | break; |
| 3800 | case 'L': |
| 3801 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 3802 | ++HowLong; |
| 3803 | break; |
| 3804 | } |
| 3805 | } |
| 3806 | |
| 3807 | QualType Type; |
| 3808 | |
| 3809 | // Read the base type. |
| 3810 | switch (*Str++) { |
| 3811 | default: assert(0 && "Unknown builtin type letter!"); |
| 3812 | case 'v': |
| 3813 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 3814 | "Bad modifiers used with 'v'!"); |
| 3815 | Type = Context.VoidTy; |
| 3816 | break; |
| 3817 | case 'f': |
| 3818 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 3819 | "Bad modifiers used with 'f'!"); |
| 3820 | Type = Context.FloatTy; |
| 3821 | break; |
| 3822 | case 'd': |
| 3823 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 3824 | "Bad modifiers used with 'd'!"); |
| 3825 | if (HowLong) |
| 3826 | Type = Context.LongDoubleTy; |
| 3827 | else |
| 3828 | Type = Context.DoubleTy; |
| 3829 | break; |
| 3830 | case 's': |
| 3831 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 3832 | if (Unsigned) |
| 3833 | Type = Context.UnsignedShortTy; |
| 3834 | else |
| 3835 | Type = Context.ShortTy; |
| 3836 | break; |
| 3837 | case 'i': |
| 3838 | if (HowLong == 3) |
| 3839 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 3840 | else if (HowLong == 2) |
| 3841 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 3842 | else if (HowLong == 1) |
| 3843 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 3844 | else |
| 3845 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 3846 | break; |
| 3847 | case 'c': |
| 3848 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 3849 | if (Signed) |
| 3850 | Type = Context.SignedCharTy; |
| 3851 | else if (Unsigned) |
| 3852 | Type = Context.UnsignedCharTy; |
| 3853 | else |
| 3854 | Type = Context.CharTy; |
| 3855 | break; |
| 3856 | case 'b': // boolean |
| 3857 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 3858 | Type = Context.BoolTy; |
| 3859 | break; |
| 3860 | case 'z': // size_t. |
| 3861 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 3862 | Type = Context.getSizeType(); |
| 3863 | break; |
| 3864 | case 'F': |
| 3865 | Type = Context.getCFConstantStringType(); |
| 3866 | break; |
| 3867 | case 'a': |
| 3868 | Type = Context.getBuiltinVaListType(); |
| 3869 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 3870 | break; |
| 3871 | case 'A': |
| 3872 | // This is a "reference" to a va_list; however, what exactly |
| 3873 | // this means depends on how va_list is defined. There are two |
| 3874 | // different kinds of va_list: ones passed by value, and ones |
| 3875 | // passed by reference. An example of a by-value va_list is |
| 3876 | // x86, where va_list is a char*. An example of by-ref va_list |
| 3877 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 3878 | // we want this argument to be a char*&; for x86-64, we want |
| 3879 | // it to be a __va_list_tag*. |
| 3880 | Type = Context.getBuiltinVaListType(); |
| 3881 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 3882 | if (Type->isArrayType()) { |
| 3883 | Type = Context.getArrayDecayedType(Type); |
| 3884 | } else { |
| 3885 | Type = Context.getLValueReferenceType(Type); |
| 3886 | } |
| 3887 | break; |
| 3888 | case 'V': { |
| 3889 | char *End; |
| 3890 | |
| 3891 | unsigned NumElements = strtoul(Str, &End, 10); |
| 3892 | assert(End != Str && "Missing vector size"); |
| 3893 | |
| 3894 | Str = End; |
| 3895 | |
| 3896 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false); |
| 3897 | Type = Context.getVectorType(ElementType, NumElements); |
| 3898 | break; |
| 3899 | } |
| 3900 | case 'P': { |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 3901 | Type = Context.getFILEType(); |
| 3902 | if (Type.isNull()) { |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 3903 | Error = ASTContext::GE_Missing_FILE; |
| 3904 | return QualType(); |
Douglas Gregor | c29f77b | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 3905 | } else { |
| 3906 | break; |
Chris Lattner | 86df27b | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 3907 | } |
| 3908 | } |
| 3909 | } |
| 3910 | |
| 3911 | if (!AllowTypeModifiers) |
| 3912 | return Type; |
| 3913 | |
| 3914 | Done = false; |
| 3915 | while (!Done) { |
| 3916 | switch (*Str++) { |
| 3917 | default: Done = true; --Str; break; |
| 3918 | case '*': |
| 3919 | Type = Context.getPointerType(Type); |
| 3920 | break; |
| 3921 | case '&': |
| 3922 | Type = Context.getLValueReferenceType(Type); |
| 3923 | break; |
| 3924 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 3925 | case 'C': |
| 3926 | Type = Type.getQualifiedType(QualType::Const); |
| 3927 | break; |
| 3928 | } |
| 3929 | } |
| 3930 | |
| 3931 | return Type; |
| 3932 | } |
| 3933 | |
| 3934 | /// GetBuiltinType - Return the type for the specified builtin. |
| 3935 | QualType ASTContext::GetBuiltinType(unsigned id, |
| 3936 | GetBuiltinTypeError &Error) { |
| 3937 | const char *TypeStr = BuiltinInfo.GetTypeString(id); |
| 3938 | |
| 3939 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 3940 | |
| 3941 | Error = GE_None; |
| 3942 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error); |
| 3943 | if (Error != GE_None) |
| 3944 | return QualType(); |
| 3945 | while (TypeStr[0] && TypeStr[0] != '.') { |
| 3946 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error); |
| 3947 | if (Error != GE_None) |
| 3948 | return QualType(); |
| 3949 | |
| 3950 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 3951 | if (Ty->isArrayType()) |
| 3952 | Ty = getArrayDecayedType(Ty); |
| 3953 | |
| 3954 | ArgTypes.push_back(Ty); |
| 3955 | } |
| 3956 | |
| 3957 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 3958 | "'.' should only occur at end of builtin type list!"); |
| 3959 | |
| 3960 | // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);". |
| 3961 | if (ArgTypes.size() == 0 && TypeStr[0] == '.') |
| 3962 | return getFunctionNoProtoType(ResType); |
| 3963 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), |
| 3964 | TypeStr[0] == '.', 0); |
| 3965 | } |