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