Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/CharUnits.h" |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 16 | #include "clang/AST/CommentLexer.h" |
| 17 | #include "clang/AST/CommentSema.h" |
| 18 | #include "clang/AST/CommentParser.h" |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 67391b8 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclTemplate.h" |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeLoc.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 23 | #include "clang/AST/Expr.h" |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExternalASTSource.h" |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 26 | #include "clang/AST/ASTMutationListener.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 27 | #include "clang/AST/RecordLayout.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 28 | #include "clang/AST/Mangle.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 29 | #include "clang/Basic/Builtins.h" |
Chris Lattner | d286851 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 30 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 7d39c9a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Capacity.h" |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 37 | #include "CXXABI.h" |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 38 | #include <map> |
Anders Carlsson | a4267a6 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 39 | |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 40 | using namespace clang; |
| 41 | |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 42 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 43 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 44 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 45 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 46 | unsigned ASTContext::NumImplicitMoveConstructors; |
| 47 | unsigned ASTContext::NumImplicitMoveConstructorsDeclared; |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 48 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 49 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 50 | unsigned ASTContext::NumImplicitMoveAssignmentOperators; |
| 51 | unsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 52 | unsigned ASTContext::NumImplicitDestructors; |
| 53 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 54 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 55 | enum FloatingRank { |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 56 | HalfRank, FloatRank, DoubleRank, LongDoubleRank |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 59 | const RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { |
| 60 | if (!CommentsLoaded && ExternalSource) { |
| 61 | ExternalSource->ReadComments(); |
| 62 | CommentsLoaded = true; |
| 63 | } |
| 64 | |
| 65 | assert(D); |
| 66 | |
Dmitri Gribenko | df17d64 | 2012-06-28 16:19:39 +0000 | [diff] [blame] | 67 | // User can not attach documentation to implicit declarations. |
| 68 | if (D->isImplicit()) |
| 69 | return NULL; |
| 70 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 71 | // TODO: handle comments for function parameters properly. |
| 72 | if (isa<ParmVarDecl>(D)) |
| 73 | return NULL; |
| 74 | |
| 75 | ArrayRef<RawComment> RawComments = Comments.getComments(); |
| 76 | |
| 77 | // If there are no comments anywhere, we won't find anything. |
| 78 | if (RawComments.empty()) |
| 79 | return NULL; |
| 80 | |
| 81 | // If the declaration doesn't map directly to a location in a file, we |
| 82 | // can't find the comment. |
| 83 | SourceLocation DeclLoc = D->getLocation(); |
| 84 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) |
| 85 | return NULL; |
| 86 | |
| 87 | // Find the comment that occurs just after this declaration. |
| 88 | ArrayRef<RawComment>::iterator Comment |
| 89 | = std::lower_bound(RawComments.begin(), |
| 90 | RawComments.end(), |
Dmitri Gribenko | fecc2e0 | 2012-06-21 21:02:45 +0000 | [diff] [blame] | 91 | RawComment(SourceMgr, SourceRange(DeclLoc)), |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 92 | BeforeThanCompare<RawComment>(SourceMgr)); |
| 93 | |
| 94 | // Decompose the location for the declaration and find the beginning of the |
| 95 | // file buffer. |
| 96 | std::pair<FileID, unsigned> DeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLoc); |
| 97 | |
| 98 | // First check whether we have a trailing comment. |
| 99 | if (Comment != RawComments.end() && |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 100 | Comment->isDocumentation() && Comment->isTrailingComment() && |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 101 | !isa<TagDecl>(D) && !isa<NamespaceDecl>(D)) { |
| 102 | std::pair<FileID, unsigned> CommentBeginDecomp |
| 103 | = SourceMgr.getDecomposedLoc(Comment->getSourceRange().getBegin()); |
| 104 | // Check that Doxygen trailing comment comes after the declaration, starts |
| 105 | // on the same line and in the same file as the declaration. |
| 106 | if (DeclLocDecomp.first == CommentBeginDecomp.first && |
| 107 | SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second) |
| 108 | == SourceMgr.getLineNumber(CommentBeginDecomp.first, |
| 109 | CommentBeginDecomp.second)) { |
| 110 | return &*Comment; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // The comment just after the declaration was not a trailing comment. |
| 115 | // Let's look at the previous comment. |
| 116 | if (Comment == RawComments.begin()) |
| 117 | return NULL; |
| 118 | --Comment; |
| 119 | |
| 120 | // Check that we actually have a non-member Doxygen comment. |
Dmitri Gribenko | 5188c4b | 2012-06-26 20:39:18 +0000 | [diff] [blame] | 121 | if (!Comment->isDocumentation() || Comment->isTrailingComment()) |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 122 | return NULL; |
| 123 | |
| 124 | // Decompose the end of the comment. |
| 125 | std::pair<FileID, unsigned> CommentEndDecomp |
| 126 | = SourceMgr.getDecomposedLoc(Comment->getSourceRange().getEnd()); |
| 127 | |
| 128 | // If the comment and the declaration aren't in the same file, then they |
| 129 | // aren't related. |
| 130 | if (DeclLocDecomp.first != CommentEndDecomp.first) |
| 131 | return NULL; |
| 132 | |
| 133 | // Get the corresponding buffer. |
| 134 | bool Invalid = false; |
| 135 | const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first, |
| 136 | &Invalid).data(); |
| 137 | if (Invalid) |
| 138 | return NULL; |
| 139 | |
| 140 | // Extract text between the comment and declaration. |
| 141 | StringRef Text(Buffer + CommentEndDecomp.second, |
| 142 | DeclLocDecomp.second - CommentEndDecomp.second); |
| 143 | |
Dmitri Gribenko | 7e8729b | 2012-06-27 23:43:37 +0000 | [diff] [blame] | 144 | // There should be no other declarations or preprocessor directives between |
| 145 | // comment and declaration. |
| 146 | if (Text.find_first_of(",;{}#") != StringRef::npos) |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 147 | return NULL; |
| 148 | |
| 149 | return &*Comment; |
| 150 | } |
| 151 | |
| 152 | const RawComment *ASTContext::getRawCommentForDecl(const Decl *D) const { |
| 153 | // Check whether we have cached a comment string for this declaration |
| 154 | // already. |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 155 | llvm::DenseMap<const Decl *, RawAndParsedComment>::iterator Pos |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 156 | = DeclComments.find(D); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 157 | if (Pos != DeclComments.end()) { |
| 158 | RawAndParsedComment C = Pos->second; |
| 159 | return C.first; |
| 160 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 161 | |
| 162 | const RawComment *RC = getRawCommentForDeclNoCache(D); |
Dmitri Gribenko | 5c8897d | 2012-06-28 16:25:36 +0000 | [diff] [blame] | 163 | // If we found a comment, it should be a documentation comment. |
| 164 | assert(!RC || RC->isDocumentation()); |
NAKAMURA Takumi | 3888172 | 2012-07-06 11:51:12 +0000 | [diff] [blame] | 165 | DeclComments[D] = RawAndParsedComment(RC, (comments::FullComment *)NULL); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 166 | return RC; |
| 167 | } |
| 168 | |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 169 | comments::FullComment *ASTContext::getCommentForDecl(const Decl *D) const { |
| 170 | llvm::DenseMap<const Decl *, RawAndParsedComment>::iterator Pos |
| 171 | = DeclComments.find(D); |
| 172 | const RawComment *RC; |
| 173 | if (Pos != DeclComments.end()) { |
| 174 | RawAndParsedComment C = Pos->second; |
| 175 | if (comments::FullComment *FC = C.second) |
| 176 | return FC; |
| 177 | RC = C.first; |
| 178 | } else |
| 179 | RC = getRawCommentForDecl(D); |
| 180 | |
| 181 | if (!RC) |
| 182 | return NULL; |
| 183 | |
| 184 | const StringRef RawText = RC->getRawText(SourceMgr); |
| 185 | comments::Lexer L(RC->getSourceRange().getBegin(), comments::CommentOptions(), |
| 186 | RawText.begin(), RawText.end()); |
| 187 | |
| 188 | comments::Sema S(this->BumpAlloc); |
| 189 | comments::Parser P(L, S, this->BumpAlloc); |
| 190 | |
| 191 | comments::FullComment *FC = P.parseFullComment(); |
| 192 | DeclComments[D].second = FC; |
| 193 | return FC; |
| 194 | } |
| 195 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 196 | void |
| 197 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 198 | TemplateTemplateParmDecl *Parm) { |
| 199 | ID.AddInteger(Parm->getDepth()); |
| 200 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 201 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 202 | |
| 203 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 204 | ID.AddInteger(Params->size()); |
| 205 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 206 | PEnd = Params->end(); |
| 207 | P != PEnd; ++P) { |
| 208 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 209 | ID.AddInteger(0); |
| 210 | ID.AddBoolean(TTP->isParameterPack()); |
| 211 | continue; |
| 212 | } |
| 213 | |
| 214 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 215 | ID.AddInteger(1); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 216 | ID.AddBoolean(NTTP->isParameterPack()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 217 | ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 218 | if (NTTP->isExpandedParameterPack()) { |
| 219 | ID.AddBoolean(true); |
| 220 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 221 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 222 | QualType T = NTTP->getExpansionType(I); |
| 223 | ID.AddPointer(T.getCanonicalType().getAsOpaquePtr()); |
| 224 | } |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 225 | } else |
| 226 | ID.AddBoolean(false); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 227 | continue; |
| 228 | } |
| 229 | |
| 230 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 231 | ID.AddInteger(2); |
| 232 | Profile(ID, TTP); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | TemplateTemplateParmDecl * |
| 237 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 238 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 239 | // Check if we already have a canonical template template parameter. |
| 240 | llvm::FoldingSetNodeID ID; |
| 241 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
| 242 | void *InsertPos = 0; |
| 243 | CanonicalTemplateTemplateParm *Canonical |
| 244 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 245 | if (Canonical) |
| 246 | return Canonical->getParam(); |
| 247 | |
| 248 | // Build a canonical template parameter list. |
| 249 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 250 | SmallVector<NamedDecl *, 4> CanonParams; |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 251 | CanonParams.reserve(Params->size()); |
| 252 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 253 | PEnd = Params->end(); |
| 254 | P != PEnd; ++P) { |
| 255 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 256 | CanonParams.push_back( |
| 257 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 258 | SourceLocation(), |
| 259 | SourceLocation(), |
| 260 | TTP->getDepth(), |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 261 | TTP->getIndex(), 0, false, |
| 262 | TTP->isParameterPack())); |
| 263 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 264 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 265 | QualType T = getCanonicalType(NTTP->getType()); |
| 266 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 267 | NonTypeTemplateParmDecl *Param; |
| 268 | if (NTTP->isExpandedParameterPack()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 269 | SmallVector<QualType, 2> ExpandedTypes; |
| 270 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 271 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 272 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 273 | ExpandedTInfos.push_back( |
| 274 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 275 | } |
| 276 | |
| 277 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 278 | SourceLocation(), |
| 279 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 280 | NTTP->getDepth(), |
| 281 | NTTP->getPosition(), 0, |
| 282 | T, |
| 283 | TInfo, |
| 284 | ExpandedTypes.data(), |
| 285 | ExpandedTypes.size(), |
| 286 | ExpandedTInfos.data()); |
| 287 | } else { |
| 288 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 289 | SourceLocation(), |
| 290 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 291 | NTTP->getDepth(), |
| 292 | NTTP->getPosition(), 0, |
| 293 | T, |
| 294 | NTTP->isParameterPack(), |
| 295 | TInfo); |
| 296 | } |
| 297 | CanonParams.push_back(Param); |
| 298 | |
| 299 | } else |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 300 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 301 | cast<TemplateTemplateParmDecl>(*P))); |
| 302 | } |
| 303 | |
| 304 | TemplateTemplateParmDecl *CanonTTP |
| 305 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 306 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 307 | TTP->getPosition(), |
| 308 | TTP->isParameterPack(), |
| 309 | 0, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 310 | TemplateParameterList::Create(*this, SourceLocation(), |
| 311 | SourceLocation(), |
| 312 | CanonParams.data(), |
| 313 | CanonParams.size(), |
| 314 | SourceLocation())); |
| 315 | |
| 316 | // Get the new insert position for the node we care about. |
| 317 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 318 | assert(Canonical == 0 && "Shouldn't be in the map!"); |
| 319 | (void)Canonical; |
| 320 | |
| 321 | // Create the canonical template template parameter entry. |
| 322 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 323 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 324 | return CanonTTP; |
| 325 | } |
| 326 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 327 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 328 | if (!LangOpts.CPlusPlus) return 0; |
| 329 | |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 330 | switch (T.getCXXABI()) { |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 331 | case CXXABI_ARM: |
| 332 | return CreateARMCXXABI(*this); |
| 333 | case CXXABI_Itanium: |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 334 | return CreateItaniumCXXABI(*this); |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 335 | case CXXABI_Microsoft: |
| 336 | return CreateMicrosoftCXXABI(*this); |
| 337 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 338 | llvm_unreachable("Invalid CXXABI type!"); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 341 | static const LangAS::Map *getAddressSpaceMap(const TargetInfo &T, |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 342 | const LangOptions &LOpts) { |
| 343 | if (LOpts.FakeAddressSpaceMap) { |
| 344 | // The fake address space map must have a distinct entry for each |
| 345 | // language-specific address space. |
| 346 | static const unsigned FakeAddrSpaceMap[] = { |
| 347 | 1, // opencl_global |
| 348 | 2, // opencl_local |
Peter Collingbourne | f44bdf9 | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 349 | 3, // opencl_constant |
| 350 | 4, // cuda_device |
| 351 | 5, // cuda_constant |
| 352 | 6 // cuda_shared |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 353 | }; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 354 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 355 | } else { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 356 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Douglas Gregor | 7018d5b | 2011-09-01 20:23:19 +0000 | [diff] [blame] | 360 | ASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 361 | const TargetInfo *t, |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 362 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 363 | Builtin::Context &builtins, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 364 | unsigned size_reserve, |
| 365 | bool DelayInitialization) |
| 366 | : FunctionProtoTypes(this_()), |
| 367 | TemplateSpecializationTypes(this_()), |
| 368 | DependentTemplateSpecializationTypes(this_()), |
| 369 | SubstTemplateTemplateParmPacks(this_()), |
| 370 | GlobalNestedNameSpecifier(0), |
| 371 | Int128Decl(0), UInt128Decl(0), |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 372 | BuiltinVaListDecl(0), |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 373 | ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), ObjCProtocolClassDecl(0), |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 374 | CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 375 | FILEDecl(0), |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 376 | jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0), |
| 377 | BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
| 378 | cudaConfigureCallDecl(0), |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 379 | NullTypeSourceInfo(QualType()), |
| 380 | FirstLocalImport(), LastLocalImport(), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 381 | SourceMgr(SM), LangOpts(LOpts), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 382 | AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 383 | Idents(idents), Selectors(sels), |
| 384 | BuiltinInfo(builtins), |
| 385 | DeclarationNames(*this), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 386 | ExternalSource(0), Listener(0), |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 387 | Comments(SM), CommentsLoaded(false), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 388 | LastSDM(0, 0), |
| 389 | UniqueBlockByRefTypeID(0) |
| 390 | { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 392 | TUDecl = TranslationUnitDecl::Create(*this); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 393 | |
| 394 | if (!DelayInitialization) { |
| 395 | assert(t && "No target supplied for ASTContext initialization"); |
| 396 | InitBuiltinTypes(*t); |
| 397 | } |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 400 | ASTContext::~ASTContext() { |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 401 | // Release the DenseMaps associated with DeclContext objects. |
| 402 | // FIXME: Is this the ideal solution? |
| 403 | ReleaseDeclContextMaps(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 404 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 405 | // Call all of the deallocation functions. |
| 406 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 407 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 408 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 409 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 410 | // because they can contain DenseMaps. |
| 411 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 412 | const ASTRecordLayout*>::iterator |
| 413 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 414 | // Increment in loop to prevent using deallocated memory. |
| 415 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 416 | R->Destroy(*this); |
| 417 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 418 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 419 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 420 | // Increment in loop to prevent using deallocated memory. |
| 421 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 422 | R->Destroy(*this); |
| 423 | } |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 424 | |
| 425 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 426 | AEnd = DeclAttrs.end(); |
| 427 | A != AEnd; ++A) |
| 428 | A->second->~AttrVec(); |
| 429 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 430 | |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 431 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 432 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 433 | } |
| 434 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | void |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 436 | ASTContext::setExternalSource(OwningPtr<ExternalASTSource> &Source) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 437 | ExternalSource.reset(Source.take()); |
| 438 | } |
| 439 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 440 | void ASTContext::PrintStats() const { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 441 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 442 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 443 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 444 | unsigned counts[] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 446 | #define ABSTRACT_TYPE(Name, Parent) |
| 447 | #include "clang/AST/TypeNodes.def" |
| 448 | 0 // Extra |
| 449 | }; |
Douglas Gregor | b1fe2c9 | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 451 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 452 | Type *T = Types[i]; |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 453 | counts[(unsigned)T->getTypeClass()]++; |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 456 | unsigned Idx = 0; |
| 457 | unsigned TotalBytes = 0; |
| 458 | #define TYPE(Name, Parent) \ |
| 459 | if (counts[Idx]) \ |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 460 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 461 | << " types\n"; \ |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 462 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 463 | ++Idx; |
| 464 | #define ABSTRACT_TYPE(Name, Parent) |
| 465 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 467 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 468 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 469 | // Implicit special member functions. |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 470 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 471 | << NumImplicitDefaultConstructors |
| 472 | << " implicit default constructors created\n"; |
| 473 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 474 | << NumImplicitCopyConstructors |
| 475 | << " implicit copy constructors created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 476 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 477 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 478 | << NumImplicitMoveConstructors |
| 479 | << " implicit move constructors created\n"; |
| 480 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 481 | << NumImplicitCopyAssignmentOperators |
| 482 | << " implicit copy assignment operators created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 483 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 484 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 485 | << NumImplicitMoveAssignmentOperators |
| 486 | << " implicit move assignment operators created\n"; |
| 487 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 488 | << NumImplicitDestructors |
| 489 | << " implicit destructors created\n"; |
| 490 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 491 | if (ExternalSource.get()) { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 492 | llvm::errs() << "\n"; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 493 | ExternalSource->PrintStats(); |
| 494 | } |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 495 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 496 | BumpAlloc.PrintStats(); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 499 | TypedefDecl *ASTContext::getInt128Decl() const { |
| 500 | if (!Int128Decl) { |
| 501 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty); |
| 502 | Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 503 | getTranslationUnitDecl(), |
| 504 | SourceLocation(), |
| 505 | SourceLocation(), |
| 506 | &Idents.get("__int128_t"), |
| 507 | TInfo); |
| 508 | } |
| 509 | |
| 510 | return Int128Decl; |
| 511 | } |
| 512 | |
| 513 | TypedefDecl *ASTContext::getUInt128Decl() const { |
| 514 | if (!UInt128Decl) { |
| 515 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty); |
| 516 | UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 517 | getTranslationUnitDecl(), |
| 518 | SourceLocation(), |
| 519 | SourceLocation(), |
| 520 | &Idents.get("__uint128_t"), |
| 521 | TInfo); |
| 522 | } |
| 523 | |
| 524 | return UInt128Decl; |
| 525 | } |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 526 | |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 527 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 528 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 529 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 530 | Types.push_back(Ty); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 533 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target) { |
| 534 | assert((!this->Target || this->Target == &Target) && |
| 535 | "Incorrect target reinitialization"); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 536 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 537 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 538 | this->Target = &Target; |
| 539 | |
| 540 | ABI.reset(createCXXABI(Target)); |
| 541 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
| 542 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 543 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 544 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 546 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 547 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 548 | // C99 6.2.5p3. |
Eli Friedman | 9ffd4a9 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 549 | if (LangOpts.CharIsSigned) |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 550 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 551 | else |
| 552 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 553 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 554 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 555 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 556 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 557 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 558 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 560 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 561 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 562 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 563 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 564 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 565 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 567 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 568 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 569 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 570 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 571 | |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 572 | // GNU extension, 128-bit integers. |
| 573 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 574 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 575 | |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 576 | if (LangOpts.CPlusPlus) { // C++ 3.9.1p5 |
Eli Friedman | 786d087 | 2011-04-30 19:24:24 +0000 | [diff] [blame] | 577 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 578 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 579 | else // -fshort-wchar makes wchar_t be unsigned. |
| 580 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 581 | } else // C99 |
Chris Lattner | 007cb02 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 582 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 583 | |
James Molloy | 3636554 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 584 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 585 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 586 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 587 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 588 | else // C99 |
| 589 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 590 | |
| 591 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 592 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 593 | else // C99 |
| 594 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 595 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 596 | // Placeholder type for type-dependent expressions whose type is |
| 597 | // completely unknown. No code should ever check a type against |
| 598 | // DependentTy and users should never see it; however, it is here to |
| 599 | // help diagnose failures to properly check for type-dependent |
| 600 | // expressions. |
| 601 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 602 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 603 | // Placeholder type for functions. |
| 604 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 605 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 606 | // Placeholder type for bound members. |
| 607 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 608 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 609 | // Placeholder type for pseudo-objects. |
| 610 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 611 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 612 | // "any" type; useful for debugger-like clients. |
| 613 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 614 | |
John McCall | 8a6b59a | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 615 | // Placeholder type for unbridged ARC casts. |
| 616 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 617 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 618 | // C99 6.2.5p11. |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 619 | FloatComplexTy = getComplexType(FloatTy); |
| 620 | DoubleComplexTy = getComplexType(DoubleTy); |
| 621 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 622 | |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 623 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 624 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 625 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 626 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 627 | |
| 628 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 29898f4 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 629 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 630 | SignedCharTy : BoolTy); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 631 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 632 | ObjCConstantStringType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 634 | // void * type |
| 635 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 636 | |
| 637 | // nullptr type (C++0x 2.14.7) |
| 638 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 639 | |
| 640 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 641 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 642 | |
| 643 | // Builtin type used to help define __builtin_va_list. |
| 644 | VaListTagTy = QualType(); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 645 | } |
| 646 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 647 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 648 | return SourceMgr.getDiagnostics(); |
| 649 | } |
| 650 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 651 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 652 | AttrVec *&Result = DeclAttrs[D]; |
| 653 | if (!Result) { |
| 654 | void *Mem = Allocate(sizeof(AttrVec)); |
| 655 | Result = new (Mem) AttrVec; |
| 656 | } |
| 657 | |
| 658 | return *Result; |
| 659 | } |
| 660 | |
| 661 | /// \brief Erase the attributes corresponding to the given declaration. |
| 662 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 663 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 664 | if (Pos != DeclAttrs.end()) { |
| 665 | Pos->second->~AttrVec(); |
| 666 | DeclAttrs.erase(Pos); |
| 667 | } |
| 668 | } |
| 669 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 670 | MemberSpecializationInfo * |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 671 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 672 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 673 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 674 | = InstantiatedFromStaticDataMember.find(Var); |
| 675 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 676 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 677 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 678 | return Pos->second; |
| 679 | } |
| 680 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | void |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 682 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 683 | TemplateSpecializationKind TSK, |
| 684 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 685 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 686 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 687 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 688 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 689 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 690 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 693 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 694 | const FunctionDecl *FD){ |
| 695 | assert(FD && "Specialization is 0"); |
| 696 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 697 | = ClassScopeSpecializationPattern.find(FD); |
| 698 | if (Pos == ClassScopeSpecializationPattern.end()) |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 699 | return 0; |
| 700 | |
| 701 | return Pos->second; |
| 702 | } |
| 703 | |
| 704 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 705 | FunctionDecl *Pattern) { |
| 706 | assert(FD && "Specialization is 0"); |
| 707 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 708 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 709 | } |
| 710 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 711 | NamedDecl * |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 712 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 713 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 714 | = InstantiatedFromUsingDecl.find(UUD); |
| 715 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 716 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 718 | return Pos->second; |
| 719 | } |
| 720 | |
| 721 | void |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 722 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 723 | assert((isa<UsingDecl>(Pattern) || |
| 724 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 725 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 726 | "pattern decl is not a using decl"); |
| 727 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 728 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 729 | } |
| 730 | |
| 731 | UsingShadowDecl * |
| 732 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 733 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 734 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 735 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 736 | return 0; |
| 737 | |
| 738 | return Pos->second; |
| 739 | } |
| 740 | |
| 741 | void |
| 742 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 743 | UsingShadowDecl *Pattern) { |
| 744 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 745 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 748 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 749 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 750 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 751 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 752 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 754 | return Pos->second; |
| 755 | } |
| 756 | |
| 757 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 758 | FieldDecl *Tmpl) { |
| 759 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 760 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 761 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 762 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 764 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 765 | } |
| 766 | |
Fariborz Jahanian | 595ec5d | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 767 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 768 | const FieldDecl *LastFD) const { |
| 769 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 770 | FD->getBitWidthValue(*this) == 0); |
Fariborz Jahanian | 595ec5d | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Fariborz Jahanian | eb39741 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 773 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 774 | const FieldDecl *LastFD) const { |
| 775 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 776 | FD->getBitWidthValue(*this) == 0 && |
| 777 | LastFD->getBitWidthValue(*this) != 0); |
Fariborz Jahanian | eb39741 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 780 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 781 | const FieldDecl *LastFD) const { |
| 782 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 783 | FD->getBitWidthValue(*this) && |
| 784 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Chad Rosier | f01a7dd | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 787 | bool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD, |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 788 | const FieldDecl *LastFD) const { |
| 789 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 790 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Chad Rosier | f01a7dd | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 793 | bool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD, |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 794 | const FieldDecl *LastFD) const { |
| 795 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 796 | FD->getBitWidthValue(*this)); |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 799 | ASTContext::overridden_cxx_method_iterator |
| 800 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 801 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 802 | = OverriddenMethods.find(Method); |
| 803 | if (Pos == OverriddenMethods.end()) |
| 804 | return 0; |
| 805 | |
| 806 | return Pos->second.begin(); |
| 807 | } |
| 808 | |
| 809 | ASTContext::overridden_cxx_method_iterator |
| 810 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 811 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 812 | = OverriddenMethods.find(Method); |
| 813 | if (Pos == OverriddenMethods.end()) |
| 814 | return 0; |
| 815 | |
| 816 | return Pos->second.end(); |
| 817 | } |
| 818 | |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 819 | unsigned |
| 820 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 821 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 822 | = OverriddenMethods.find(Method); |
| 823 | if (Pos == OverriddenMethods.end()) |
| 824 | return 0; |
| 825 | |
| 826 | return Pos->second.size(); |
| 827 | } |
| 828 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 829 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 830 | const CXXMethodDecl *Overridden) { |
| 831 | OverriddenMethods[Method].push_back(Overridden); |
| 832 | } |
| 833 | |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 834 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 835 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 836 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 837 | if (!FirstLocalImport) { |
| 838 | FirstLocalImport = Import; |
| 839 | LastLocalImport = Import; |
| 840 | return; |
| 841 | } |
| 842 | |
| 843 | LastLocalImport->NextLocalImport = Import; |
| 844 | LastLocalImport = Import; |
| 845 | } |
| 846 | |
Chris Lattner | 53cfe80 | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 847 | //===----------------------------------------------------------------------===// |
| 848 | // Type Sizing and Analysis |
| 849 | //===----------------------------------------------------------------------===// |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 850 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 851 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 852 | /// scalar floating point type. |
| 853 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 854 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 855 | assert(BT && "Not a floating point type!"); |
| 856 | switch (BT->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 857 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 858 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 859 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 860 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 861 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 865 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 866 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 867 | /// this method will assert on them. |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 868 | /// If @p RefAsPointee, references are treated like their underlying type |
| 869 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 870 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 871 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 872 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 873 | bool UseAlignAttrOnly = false; |
| 874 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 875 | Align = AlignFromAttr; |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 876 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 877 | // __attribute__((aligned)) can increase or decrease alignment |
| 878 | // *except* on a struct or struct member, where it only increases |
| 879 | // alignment unless 'packed' is also specified. |
| 880 | // |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 881 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 882 | // ignore that possibility; Sema should diagnose it. |
| 883 | if (isa<FieldDecl>(D)) { |
| 884 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 885 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 886 | } else { |
| 887 | UseAlignAttrOnly = true; |
| 888 | } |
| 889 | } |
Fariborz Jahanian | 9f10718 | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 890 | else if (isa<FieldDecl>(D)) |
| 891 | UseAlignAttrOnly = |
| 892 | D->hasAttr<PackedAttr>() || |
| 893 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 894 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 895 | // If we're using the align attribute only, just ignore everything |
| 896 | // else about the declaration and its type. |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 897 | if (UseAlignAttrOnly) { |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 898 | // do nothing |
| 899 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 900 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 901 | QualType T = VD->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 902 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 903 | if (RefAsPointee) |
| 904 | T = RT->getPointeeType(); |
| 905 | else |
| 906 | T = getPointerType(RT->getPointeeType()); |
| 907 | } |
| 908 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 909 | // Adjust alignments of declarations with array type by the |
| 910 | // large-array alignment on the target. |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 911 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 912 | const ArrayType *arrayType; |
| 913 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 914 | if (isa<VariableArrayType>(arrayType)) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 915 | Align = std::max(Align, Target->getLargeArrayAlign()); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 916 | else if (isa<ConstantArrayType>(arrayType) && |
| 917 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 918 | Align = std::max(Align, Target->getLargeArrayAlign()); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 919 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 920 | // Walk through any array types while we're at it. |
| 921 | T = getBaseElementType(arrayType); |
| 922 | } |
Chad Rosier | 99ee782 | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 923 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 924 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 925 | |
| 926 | // Fields can be subject to extra alignment constraints, like if |
| 927 | // the field is packed, the struct is packed, or the struct has a |
| 928 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 929 | // the actual alignment of the field within the struct, and then |
| 930 | // (as we're expected to) constrain that by the alignment of the type. |
| 931 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 932 | // So calculate the alignment of the field. |
| 933 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 934 | |
| 935 | // Start with the record's overall alignment. |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 936 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 937 | |
| 938 | // Use the GCD of that and the offset within the record. |
| 939 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 940 | if (offset > 0) { |
| 941 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 942 | // which means we get to do this crazy thing instead of Euclid's. |
| 943 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 944 | if (lowBitOfOffset < fieldAlign) |
| 945 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 946 | } |
| 947 | |
| 948 | Align = std::min(Align, fieldAlign); |
Charles Davis | 3fc5107 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 949 | } |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 950 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 951 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 952 | return toCharUnitsFromBits(Align); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 953 | } |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 954 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 955 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 956 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 957 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 958 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 959 | toCharUnitsFromBits(Info.second)); |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 963 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 964 | return getTypeInfoInChars(T.getTypePtr()); |
| 965 | } |
| 966 | |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 967 | std::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const { |
| 968 | TypeInfoMap::iterator it = MemoizedTypeInfo.find(T); |
| 969 | if (it != MemoizedTypeInfo.end()) |
| 970 | return it->second; |
| 971 | |
| 972 | std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T); |
| 973 | MemoizedTypeInfo.insert(std::make_pair(T, Info)); |
| 974 | return Info; |
| 975 | } |
| 976 | |
| 977 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 978 | /// method does not work on incomplete types. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 979 | /// |
| 980 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 981 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 982 | /// should take a QualType, &c. |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 983 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 984 | ASTContext::getTypeInfoImpl(const Type *T) const { |
Mike Stump | 5b9a3d5 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 985 | uint64_t Width=0; |
| 986 | unsigned Align=8; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 987 | switch (T->getTypeClass()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 988 | #define TYPE(Class, Base) |
| 989 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 990 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 991 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 992 | #include "clang/AST/TypeNodes.def" |
John McCall | 15547bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 993 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 994 | |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 995 | case Type::FunctionNoProto: |
| 996 | case Type::FunctionProto: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 997 | // GCC extension: alignof(function) = 32 bits |
| 998 | Width = 0; |
| 999 | Align = 32; |
| 1000 | break; |
| 1001 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1002 | case Type::IncompleteArray: |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1003 | case Type::VariableArray: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1004 | Width = 0; |
| 1005 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 1006 | break; |
| 1007 | |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1008 | case Type::ConstantArray: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1009 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1011 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | d541a4c | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1012 | uint64_t Size = CAT->getSize().getZExtValue(); |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1013 | assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) && |
| 1014 | "Overflow in array type bit size evaluation"); |
Abramo Bagnara | d541a4c | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1015 | Width = EltInfo.first*Size; |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1016 | Align = EltInfo.second; |
Argyrios Kyrtzidis | ae40e4e | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 1017 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1018 | break; |
Christopher Lamb | c5fafa2 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 1019 | } |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1020 | case Type::ExtVector: |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1021 | case Type::Vector: { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1022 | const VectorType *VT = cast<VectorType>(T); |
| 1023 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 1024 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 3df5efe | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1025 | Align = Width; |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1026 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 1027 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | ef78c8e | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 1028 | if (Align & (Align-1)) { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1029 | Align = llvm::NextPowerOf2(Align); |
| 1030 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 1031 | } |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1032 | break; |
| 1033 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1034 | |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1035 | case Type::Builtin: |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1036 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1037 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1038 | case BuiltinType::Void: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1039 | // GCC extension: alignof(void) = 8 bits. |
| 1040 | Width = 0; |
| 1041 | Align = 8; |
| 1042 | break; |
| 1043 | |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1044 | case BuiltinType::Bool: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1045 | Width = Target->getBoolWidth(); |
| 1046 | Align = Target->getBoolAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1047 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1048 | case BuiltinType::Char_S: |
| 1049 | case BuiltinType::Char_U: |
| 1050 | case BuiltinType::UChar: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1051 | case BuiltinType::SChar: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1052 | Width = Target->getCharWidth(); |
| 1053 | Align = Target->getCharAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1054 | break; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1055 | case BuiltinType::WChar_S: |
| 1056 | case BuiltinType::WChar_U: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1057 | Width = Target->getWCharWidth(); |
| 1058 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1059 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1060 | case BuiltinType::Char16: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1061 | Width = Target->getChar16Width(); |
| 1062 | Align = Target->getChar16Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1063 | break; |
| 1064 | case BuiltinType::Char32: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1065 | Width = Target->getChar32Width(); |
| 1066 | Align = Target->getChar32Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1067 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1068 | case BuiltinType::UShort: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1069 | case BuiltinType::Short: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1070 | Width = Target->getShortWidth(); |
| 1071 | Align = Target->getShortAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1072 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1073 | case BuiltinType::UInt: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1074 | case BuiltinType::Int: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1075 | Width = Target->getIntWidth(); |
| 1076 | Align = Target->getIntAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1077 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1078 | case BuiltinType::ULong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1079 | case BuiltinType::Long: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1080 | Width = Target->getLongWidth(); |
| 1081 | Align = Target->getLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1082 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1083 | case BuiltinType::ULongLong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1084 | case BuiltinType::LongLong: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1085 | Width = Target->getLongLongWidth(); |
| 1086 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1087 | break; |
Chris Lattner | 0a415ec | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 1088 | case BuiltinType::Int128: |
| 1089 | case BuiltinType::UInt128: |
| 1090 | Width = 128; |
| 1091 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 1092 | break; |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1093 | case BuiltinType::Half: |
| 1094 | Width = Target->getHalfWidth(); |
| 1095 | Align = Target->getHalfAlign(); |
| 1096 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1097 | case BuiltinType::Float: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1098 | Width = Target->getFloatWidth(); |
| 1099 | Align = Target->getFloatAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1100 | break; |
| 1101 | case BuiltinType::Double: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1102 | Width = Target->getDoubleWidth(); |
| 1103 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1104 | break; |
| 1105 | case BuiltinType::LongDouble: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1106 | Width = Target->getLongDoubleWidth(); |
| 1107 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1108 | break; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1109 | case BuiltinType::NullPtr: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1110 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 1111 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | a81b0b7 | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 1112 | break; |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1113 | case BuiltinType::ObjCId: |
| 1114 | case BuiltinType::ObjCClass: |
| 1115 | case BuiltinType::ObjCSel: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1116 | Width = Target->getPointerWidth(0); |
| 1117 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1118 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1119 | } |
Chris Lattner | 48f84b8 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 1120 | break; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1121 | case Type::ObjCObjectPointer: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1122 | Width = Target->getPointerWidth(0); |
| 1123 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1124 | break; |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1125 | case Type::BlockPointer: { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1126 | unsigned AS = getTargetAddressSpace( |
| 1127 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1128 | Width = Target->getPointerWidth(AS); |
| 1129 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1130 | break; |
| 1131 | } |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1132 | case Type::LValueReference: |
| 1133 | case Type::RValueReference: { |
| 1134 | // alignof and sizeof should never enter this code path here, so we go |
| 1135 | // the pointer route. |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1136 | unsigned AS = getTargetAddressSpace( |
| 1137 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1138 | Width = Target->getPointerWidth(AS); |
| 1139 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1140 | break; |
| 1141 | } |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1142 | case Type::Pointer: { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1143 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1144 | Width = Target->getPointerWidth(AS); |
| 1145 | Align = Target->getPointerAlign(AS); |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1146 | break; |
| 1147 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1148 | case Type::MemberPointer: { |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1149 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1151 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1152 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1153 | Align = PtrDiffInfo.second; |
| 1154 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1155 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1156 | case Type::Complex: { |
| 1157 | // Complex types have the same alignment as their elements, but twice the |
| 1158 | // size. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1160 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1161 | Width = EltInfo.first*2; |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1162 | Align = EltInfo.second; |
| 1163 | break; |
| 1164 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1165 | case Type::ObjCObject: |
| 1166 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1167 | case Type::ObjCInterface: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1168 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1169 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1170 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1171 | Align = toBits(Layout.getAlignment()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1172 | break; |
| 1173 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1174 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1175 | case Type::Enum: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1176 | const TagType *TT = cast<TagType>(T); |
| 1177 | |
| 1178 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 7f97189 | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1179 | Width = 8; |
| 1180 | Align = 8; |
Chris Lattner | 572100b | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1181 | break; |
| 1182 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1183 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1184 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1185 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 1186 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1187 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1188 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1189 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1190 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | 49a953a | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1191 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1192 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1193 | |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1194 | case Type::SubstTemplateTypeParm: |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1195 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1196 | getReplacementType().getTypePtr()); |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1197 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1198 | case Type::Auto: { |
| 1199 | const AutoType *A = cast<AutoType>(T); |
| 1200 | assert(A->isDeduced() && "Cannot request the size of a dependent type"); |
Matt Beaumont-Gay | 7a24210e | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1201 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1204 | case Type::Paren: |
| 1205 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1206 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1207 | case Type::Typedef: { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1208 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | f5bae22 | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1209 | std::pair<uint64_t, unsigned> Info |
| 1210 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1211 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1212 | // alignment we have. This violates the GCC documentation (which says that |
| 1213 | // attribute(aligned) can only round up) but matches its implementation. |
| 1214 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 1215 | Align = AttrAlign; |
| 1216 | else |
| 1217 | Align = Info.second; |
Douglas Gregor | f5bae22 | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1218 | Width = Info.first; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1219 | break; |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1220 | } |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1221 | |
| 1222 | case Type::TypeOfExpr: |
| 1223 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 1224 | .getTypePtr()); |
| 1225 | |
| 1226 | case Type::TypeOf: |
| 1227 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 1228 | |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1229 | case Type::Decltype: |
| 1230 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 1231 | .getTypePtr()); |
| 1232 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1233 | case Type::UnaryTransform: |
| 1234 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 1235 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1236 | case Type::Elaborated: |
| 1237 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1239 | case Type::Attributed: |
| 1240 | return getTypeInfo( |
| 1241 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1242 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1243 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1244 | assert(getCanonicalType(T) != T && |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1245 | "Cannot request the size of a dependent type"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1246 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1247 | // A type alias template specialization may refer to a typedef with the |
| 1248 | // aligned attribute on it. |
| 1249 | if (TST->isTypeAlias()) |
| 1250 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1251 | else |
| 1252 | return getTypeInfo(getCanonicalType(T)); |
| 1253 | } |
| 1254 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1255 | case Type::Atomic: { |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1256 | std::pair<uint64_t, unsigned> Info |
| 1257 | = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1258 | Width = Info.first; |
| 1259 | Align = Info.second; |
| 1260 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth() && |
| 1261 | llvm::isPowerOf2_64(Width)) { |
| 1262 | // We can potentially perform lock-free atomic operations for this |
| 1263 | // type; promote the alignment appropriately. |
| 1264 | // FIXME: We could potentially promote the width here as well... |
| 1265 | // is that worthwhile? (Non-struct atomic types generally have |
| 1266 | // power-of-two size anyway, but structs might not. Requires a bit |
| 1267 | // of implementation work to make sure we zero out the extra bits.) |
| 1268 | Align = static_cast<unsigned>(Width); |
| 1269 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1272 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1274 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1275 | return std::make_pair(Width, Align); |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1278 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1279 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1280 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1281 | } |
| 1282 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1283 | /// toBits - Convert a size in characters to a size in characters. |
| 1284 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1285 | return CharSize.getQuantity() * getCharWidth(); |
| 1286 | } |
| 1287 | |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1288 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1289 | /// This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1290 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1291 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1292 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1293 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1294 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Ken Dyck | a6046ab | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1297 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1298 | /// characters. This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1299 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1300 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1301 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1302 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1303 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1306 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1307 | /// type for the current target in bits. This can be different than the ABI |
| 1308 | /// alignment in cases where it is beneficial for performance to overalign |
| 1309 | /// a data type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1310 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1311 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1312 | |
| 1313 | // Double and long long should be naturally aligned if possible. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1314 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1315 | T = CT->getElementType().getTypePtr(); |
| 1316 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | b57321a | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 1317 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 1318 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1319 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1320 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1321 | return ABIAlign; |
| 1322 | } |
| 1323 | |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1324 | /// DeepCollectObjCIvars - |
| 1325 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1326 | /// super class and then collects all ivars, including those synthesized for |
| 1327 | /// current class. This routine is used for implementation of current class |
| 1328 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1329 | /// |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1330 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1331 | bool leafClass, |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1332 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1333 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1334 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1335 | if (!leafClass) { |
| 1336 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1337 | E = OI->ivar_end(); I != E; ++I) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1338 | Ivars.push_back(*I); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1339 | } else { |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1340 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1341 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1342 | Iv= Iv->getNextIvar()) |
| 1343 | Ivars.push_back(Iv); |
| 1344 | } |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1347 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1348 | /// those inherited by it. |
| 1349 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1350 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1351 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1352 | // We can use protocol_iterator here instead of |
| 1353 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1354 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1355 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1356 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1357 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1358 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1359 | PE = Proto->protocol_end(); P != PE; ++P) { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1360 | Protocols.insert((*P)->getCanonicalDecl()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1361 | CollectInheritedProtocols(*P, Protocols); |
| 1362 | } |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1363 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1364 | |
| 1365 | // Categories of this Interface. |
| 1366 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 1367 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 1368 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 1369 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1370 | while (SD) { |
| 1371 | CollectInheritedProtocols(SD, Protocols); |
| 1372 | SD = SD->getSuperClass(); |
| 1373 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1374 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1375 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1376 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1377 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1378 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1379 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1380 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1381 | CollectInheritedProtocols(*P, Protocols); |
| 1382 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1383 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1384 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1385 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1386 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1387 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1388 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1389 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1390 | CollectInheritedProtocols(*P, Protocols); |
| 1391 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1392 | } |
| 1393 | } |
| 1394 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1395 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1396 | unsigned count = 0; |
| 1397 | // Count ivars declared in class extension. |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1398 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 1399 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1400 | count += CDecl->ivar_size(); |
| 1401 | |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1402 | // Count ivar defined in this class's implementation. This |
| 1403 | // includes synthesized ivars. |
| 1404 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1405 | count += ImplDecl->ivar_size(); |
| 1406 | |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1407 | return count; |
| 1408 | } |
| 1409 | |
Argyrios Kyrtzidis | 2e809ce | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 1410 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 1411 | if (!E) |
| 1412 | return false; |
| 1413 | |
| 1414 | // nullptr_t is always treated as null. |
| 1415 | if (E->getType()->isNullPtrType()) return true; |
| 1416 | |
| 1417 | if (E->getType()->isAnyPointerType() && |
| 1418 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 1419 | Expr::NPC_ValueDependentIsNull)) |
| 1420 | return true; |
| 1421 | |
| 1422 | // Unfortunately, __null has type 'int'. |
| 1423 | if (isa<GNUNullExpr>(E)) return true; |
| 1424 | |
| 1425 | return false; |
| 1426 | } |
| 1427 | |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1428 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1429 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1430 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1431 | I = ObjCImpls.find(D); |
| 1432 | if (I != ObjCImpls.end()) |
| 1433 | return cast<ObjCImplementationDecl>(I->second); |
| 1434 | return 0; |
| 1435 | } |
| 1436 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1437 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1438 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1439 | I = ObjCImpls.find(D); |
| 1440 | if (I != ObjCImpls.end()) |
| 1441 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1442 | return 0; |
| 1443 | } |
| 1444 | |
| 1445 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1446 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1447 | ObjCImplementationDecl *ImplD) { |
| 1448 | assert(IFaceD && ImplD && "Passed null params"); |
| 1449 | ObjCImpls[IFaceD] = ImplD; |
| 1450 | } |
| 1451 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1452 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1453 | ObjCCategoryImplDecl *ImplD) { |
| 1454 | assert(CatD && ImplD && "Passed null params"); |
| 1455 | ObjCImpls[CatD] = ImplD; |
| 1456 | } |
| 1457 | |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1458 | ObjCInterfaceDecl *ASTContext::getObjContainingInterface(NamedDecl *ND) const { |
| 1459 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
| 1460 | return ID; |
| 1461 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
| 1462 | return CD->getClassInterface(); |
| 1463 | if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
| 1464 | return IMD->getClassInterface(); |
| 1465 | |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1469 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1470 | /// none exists. |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1471 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1472 | assert(VD && "Passed null params"); |
| 1473 | assert(VD->hasAttr<BlocksAttr>() && |
| 1474 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1475 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1476 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1477 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1478 | } |
| 1479 | |
| 1480 | /// \brief Set the copy inialization expression of a block var decl. |
| 1481 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1482 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1483 | assert(VD->hasAttr<BlocksAttr>() && |
| 1484 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1485 | BlockVarCopyInits[VD] = Init; |
| 1486 | } |
| 1487 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1488 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1489 | unsigned DataSize) const { |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1490 | if (!DataSize) |
| 1491 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1492 | else |
| 1493 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1494 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1495 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1496 | TypeSourceInfo *TInfo = |
| 1497 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1498 | new (TInfo) TypeSourceInfo(T); |
| 1499 | return TInfo; |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1502 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1503 | SourceLocation L) const { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1504 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | 2d525f0 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1505 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | 3665e00 | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1506 | return DI; |
| 1507 | } |
| 1508 | |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1509 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1510 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1511 | return getObjCLayout(D, 0); |
| 1512 | } |
| 1513 | |
| 1514 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1515 | ASTContext::getASTObjCImplementationLayout( |
| 1516 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1517 | return getObjCLayout(D->getClassInterface(), D); |
| 1518 | } |
| 1519 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1520 | //===----------------------------------------------------------------------===// |
| 1521 | // Type creation/memoization methods |
| 1522 | //===----------------------------------------------------------------------===// |
| 1523 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1524 | QualType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1525 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1526 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1527 | quals.removeFastQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1528 | |
| 1529 | // Check if we've already instantiated this type. |
| 1530 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1531 | ExtQuals::Profile(ID, baseType, quals); |
| 1532 | void *insertPos = 0; |
| 1533 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1534 | assert(eq->getQualifiers() == quals); |
| 1535 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1538 | // If the base type is not canonical, make the appropriate canonical type. |
| 1539 | QualType canon; |
| 1540 | if (!baseType->isCanonicalUnqualified()) { |
| 1541 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1542 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 1543 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1544 | |
| 1545 | // Re-find the insert position. |
| 1546 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1547 | } |
| 1548 | |
| 1549 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1550 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1551 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1554 | QualType |
| 1555 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1556 | QualType CanT = getCanonicalType(T); |
| 1557 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | 445fcab | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1558 | return T; |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1559 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1560 | // If we are composing extended qualifiers together, merge together |
| 1561 | // into one ExtQuals node. |
| 1562 | QualifierCollector Quals; |
| 1563 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1565 | // If this type already has an address space specified, it cannot get |
| 1566 | // another one. |
| 1567 | assert(!Quals.hasAddressSpace() && |
| 1568 | "Type cannot be in multiple addr spaces!"); |
| 1569 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1570 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1571 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | 025b5fb | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1574 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1575 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1576 | QualType CanT = getCanonicalType(T); |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1577 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1578 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1580 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1581 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1582 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | b68215c | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1583 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1584 | return getPointerType(ResultType); |
| 1585 | } |
| 1586 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1588 | // If we are composing extended qualifiers together, merge together |
| 1589 | // into one ExtQuals node. |
| 1590 | QualifierCollector Quals; |
| 1591 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1593 | // If this type already has an ObjCGC specified, it cannot get |
| 1594 | // another one. |
| 1595 | assert(!Quals.hasObjCGCAttr() && |
| 1596 | "Type cannot have multiple ObjCGCs!"); |
| 1597 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1598 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1599 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1600 | } |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1601 | |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1602 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1603 | FunctionType::ExtInfo Info) { |
| 1604 | if (T->getExtInfo() == Info) |
| 1605 | return T; |
| 1606 | |
| 1607 | QualType Result; |
| 1608 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1609 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1610 | } else { |
| 1611 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1612 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1613 | EPI.ExtInfo = Info; |
| 1614 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1615 | FPT->getNumArgs(), EPI); |
| 1616 | } |
| 1617 | |
| 1618 | return cast<FunctionType>(Result.getTypePtr()); |
| 1619 | } |
| 1620 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1621 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1622 | /// number with the specified element type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1623 | QualType ASTContext::getComplexType(QualType T) const { |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1624 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1625 | // structure. |
| 1626 | llvm::FoldingSetNodeID ID; |
| 1627 | ComplexType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1629 | void *InsertPos = 0; |
| 1630 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1631 | return QualType(CT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1633 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1634 | // so fill in the canonical type field. |
| 1635 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1636 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1637 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1638 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1639 | // Get the new insert position for the node we care about. |
| 1640 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1641 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1642 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1643 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1644 | Types.push_back(New); |
| 1645 | ComplexTypes.InsertNode(New, InsertPos); |
| 1646 | return QualType(New, 0); |
| 1647 | } |
| 1648 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1649 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1650 | /// the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1651 | QualType ASTContext::getPointerType(QualType T) const { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 1652 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1653 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1654 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1655 | PointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1656 | |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1657 | void *InsertPos = 0; |
| 1658 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1659 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 1661 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1662 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1663 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1664 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1665 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1666 | |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1667 | // Get the new insert position for the node we care about. |
| 1668 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1669 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1670 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1671 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1672 | Types.push_back(New); |
| 1673 | PointerTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1674 | return QualType(New, 0); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1677 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1678 | /// a pointer to the specified block. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1679 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 0ac01283 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1680 | assert(T->isFunctionType() && "block of function types only"); |
| 1681 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1682 | // structure. |
| 1683 | llvm::FoldingSetNodeID ID; |
| 1684 | BlockPointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1685 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1686 | void *InsertPos = 0; |
| 1687 | if (BlockPointerType *PT = |
| 1688 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1689 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1690 | |
| 1691 | // If the block pointee type isn't canonical, this won't be a canonical |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1692 | // type either so fill in the canonical type field. |
| 1693 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1694 | if (!T.isCanonical()) { |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1695 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1697 | // Get the new insert position for the node we care about. |
| 1698 | BlockPointerType *NewIP = |
| 1699 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1700 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1701 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1702 | BlockPointerType *New |
| 1703 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1704 | Types.push_back(New); |
| 1705 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1706 | return QualType(New, 0); |
| 1707 | } |
| 1708 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1709 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1710 | /// lvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1711 | QualType |
| 1712 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 291e8ee | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 1713 | assert(getCanonicalType(T) != OverloadTy && |
| 1714 | "Unresolved overloaded function type"); |
| 1715 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1716 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1717 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1718 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1719 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1720 | |
| 1721 | void *InsertPos = 0; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1722 | if (LValueReferenceType *RT = |
| 1723 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1724 | return QualType(RT, 0); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1725 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1726 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1727 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1728 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1729 | // either, so fill in the canonical type field. |
| 1730 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1731 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1732 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1733 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1734 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1735 | // Get the new insert position for the node we care about. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1736 | LValueReferenceType *NewIP = |
| 1737 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1738 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1741 | LValueReferenceType *New |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1742 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1743 | SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1744 | Types.push_back(New); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1745 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1746 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1747 | return QualType(New, 0); |
| 1748 | } |
| 1749 | |
| 1750 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1751 | /// rvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1752 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1753 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1754 | // structure. |
| 1755 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1756 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1757 | |
| 1758 | void *InsertPos = 0; |
| 1759 | if (RValueReferenceType *RT = |
| 1760 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1761 | return QualType(RT, 0); |
| 1762 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1763 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1764 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1765 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1766 | // either, so fill in the canonical type field. |
| 1767 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1768 | if (InnerRef || !T.isCanonical()) { |
| 1769 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1770 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1771 | |
| 1772 | // Get the new insert position for the node we care about. |
| 1773 | RValueReferenceType *NewIP = |
| 1774 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1775 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1778 | RValueReferenceType *New |
| 1779 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1780 | Types.push_back(New); |
| 1781 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1782 | return QualType(New, 0); |
| 1783 | } |
| 1784 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1785 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1786 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1787 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1788 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1789 | // structure. |
| 1790 | llvm::FoldingSetNodeID ID; |
| 1791 | MemberPointerType::Profile(ID, T, Cls); |
| 1792 | |
| 1793 | void *InsertPos = 0; |
| 1794 | if (MemberPointerType *PT = |
| 1795 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1796 | return QualType(PT, 0); |
| 1797 | |
| 1798 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1799 | // type either, so fill in the canonical type field. |
| 1800 | QualType Canonical; |
Douglas Gregor | 615ac67 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1801 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1802 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1803 | |
| 1804 | // Get the new insert position for the node we care about. |
| 1805 | MemberPointerType *NewIP = |
| 1806 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1807 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1808 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1809 | MemberPointerType *New |
| 1810 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1811 | Types.push_back(New); |
| 1812 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1813 | return QualType(New, 0); |
| 1814 | } |
| 1815 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1817 | /// array of the specified element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1819 | const llvm::APInt &ArySizeIn, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1820 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1821 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 2dfdb82 | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1822 | assert((EltTy->isDependentType() || |
| 1823 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1824 | "Constant array of VLAs is illegal!"); |
| 1825 | |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1826 | // Convert the array size into a canonical width matching the pointer size for |
| 1827 | // the target. |
| 1828 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1829 | ArySize = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1830 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1831 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1832 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1833 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1834 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1835 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | if (ConstantArrayType *ATP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1837 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1838 | return QualType(ATP, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1839 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1840 | // If the element type isn't canonical or has qualifiers, this won't |
| 1841 | // be a canonical type either, so fill in the canonical type field. |
| 1842 | QualType Canon; |
| 1843 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1844 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1845 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1846 | ASM, IndexTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1847 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1848 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1849 | // Get the new insert position for the node we care about. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1850 | ConstantArrayType *NewIP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1851 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1852 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1853 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1855 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1856 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1857 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1858 | Types.push_back(New); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1859 | return QualType(New, 0); |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1862 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 1863 | /// variably-modified, into the corresponding type with all the known |
| 1864 | /// sizes replaced with [*]. |
| 1865 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 1866 | // Vastly most common case. |
| 1867 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1868 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1869 | QualType result; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1870 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1871 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1872 | const Type *ty = split.Ty; |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1873 | switch (ty->getTypeClass()) { |
| 1874 | #define TYPE(Class, Base) |
| 1875 | #define ABSTRACT_TYPE(Class, Base) |
| 1876 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1877 | #include "clang/AST/TypeNodes.def" |
| 1878 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 1879 | |
| 1880 | // These types should never be variably-modified. |
| 1881 | case Type::Builtin: |
| 1882 | case Type::Complex: |
| 1883 | case Type::Vector: |
| 1884 | case Type::ExtVector: |
| 1885 | case Type::DependentSizedExtVector: |
| 1886 | case Type::ObjCObject: |
| 1887 | case Type::ObjCInterface: |
| 1888 | case Type::ObjCObjectPointer: |
| 1889 | case Type::Record: |
| 1890 | case Type::Enum: |
| 1891 | case Type::UnresolvedUsing: |
| 1892 | case Type::TypeOfExpr: |
| 1893 | case Type::TypeOf: |
| 1894 | case Type::Decltype: |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1895 | case Type::UnaryTransform: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1896 | case Type::DependentName: |
| 1897 | case Type::InjectedClassName: |
| 1898 | case Type::TemplateSpecialization: |
| 1899 | case Type::DependentTemplateSpecialization: |
| 1900 | case Type::TemplateTypeParm: |
| 1901 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1902 | case Type::Auto: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1903 | case Type::PackExpansion: |
| 1904 | llvm_unreachable("type should never be variably-modified"); |
| 1905 | |
| 1906 | // These types can be variably-modified but should never need to |
| 1907 | // further decay. |
| 1908 | case Type::FunctionNoProto: |
| 1909 | case Type::FunctionProto: |
| 1910 | case Type::BlockPointer: |
| 1911 | case Type::MemberPointer: |
| 1912 | return type; |
| 1913 | |
| 1914 | // These types can be variably-modified. All these modifications |
| 1915 | // preserve structure except as noted by comments. |
| 1916 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 1917 | // optimizations available here. |
| 1918 | case Type::Pointer: |
| 1919 | result = getPointerType(getVariableArrayDecayedType( |
| 1920 | cast<PointerType>(ty)->getPointeeType())); |
| 1921 | break; |
| 1922 | |
| 1923 | case Type::LValueReference: { |
| 1924 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 1925 | result = getLValueReferenceType( |
| 1926 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 1927 | lv->isSpelledAsLValue()); |
| 1928 | break; |
| 1929 | } |
| 1930 | |
| 1931 | case Type::RValueReference: { |
| 1932 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 1933 | result = getRValueReferenceType( |
| 1934 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 1935 | break; |
| 1936 | } |
| 1937 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1938 | case Type::Atomic: { |
| 1939 | const AtomicType *at = cast<AtomicType>(ty); |
| 1940 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 1941 | break; |
| 1942 | } |
| 1943 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1944 | case Type::ConstantArray: { |
| 1945 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 1946 | result = getConstantArrayType( |
| 1947 | getVariableArrayDecayedType(cat->getElementType()), |
| 1948 | cat->getSize(), |
| 1949 | cat->getSizeModifier(), |
| 1950 | cat->getIndexTypeCVRQualifiers()); |
| 1951 | break; |
| 1952 | } |
| 1953 | |
| 1954 | case Type::DependentSizedArray: { |
| 1955 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 1956 | result = getDependentSizedArrayType( |
| 1957 | getVariableArrayDecayedType(dat->getElementType()), |
| 1958 | dat->getSizeExpr(), |
| 1959 | dat->getSizeModifier(), |
| 1960 | dat->getIndexTypeCVRQualifiers(), |
| 1961 | dat->getBracketsRange()); |
| 1962 | break; |
| 1963 | } |
| 1964 | |
| 1965 | // Turn incomplete types into [*] types. |
| 1966 | case Type::IncompleteArray: { |
| 1967 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 1968 | result = getVariableArrayType( |
| 1969 | getVariableArrayDecayedType(iat->getElementType()), |
| 1970 | /*size*/ 0, |
| 1971 | ArrayType::Normal, |
| 1972 | iat->getIndexTypeCVRQualifiers(), |
| 1973 | SourceRange()); |
| 1974 | break; |
| 1975 | } |
| 1976 | |
| 1977 | // Turn VLA types into [*] types. |
| 1978 | case Type::VariableArray: { |
| 1979 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 1980 | result = getVariableArrayType( |
| 1981 | getVariableArrayDecayedType(vat->getElementType()), |
| 1982 | /*size*/ 0, |
| 1983 | ArrayType::Star, |
| 1984 | vat->getIndexTypeCVRQualifiers(), |
| 1985 | vat->getBracketsRange()); |
| 1986 | break; |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | // Apply the top-level qualifiers from the original. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1991 | return getQualifiedType(result, split.Quals); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1992 | } |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1993 | |
Steve Naroff | cadebd0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 1994 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 1995 | /// variable array of the specified element type. |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 1996 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 1997 | Expr *NumElts, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1998 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1999 | unsigned IndexTypeQuals, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2000 | SourceRange Brackets) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2001 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 2002 | // that have an expression provided for their size. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2003 | QualType Canon; |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2004 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2005 | // Be sure to pull qualifiers off the element type. |
| 2006 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2007 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2008 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2009 | IndexTypeQuals, Brackets); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2010 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2013 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2014 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2015 | |
| 2016 | VariableArrayTypes.push_back(New); |
| 2017 | Types.push_back(New); |
| 2018 | return QualType(New, 0); |
| 2019 | } |
| 2020 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2021 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 2022 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 2023 | /// type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2024 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 2025 | Expr *numElements, |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2026 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2027 | unsigned elementTypeQuals, |
| 2028 | SourceRange brackets) const { |
| 2029 | assert((!numElements || numElements->isTypeDependent() || |
| 2030 | numElements->isValueDependent()) && |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2031 | "Size must be type- or value-dependent!"); |
| 2032 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2033 | // Dependently-sized array types that do not have a specified number |
| 2034 | // of elements will have their sizes deduced from a dependent |
| 2035 | // initializer. We do no canonicalization here at all, which is okay |
| 2036 | // because they can't be used in most locations. |
| 2037 | if (!numElements) { |
| 2038 | DependentSizedArrayType *newType |
| 2039 | = new (*this, TypeAlignment) |
| 2040 | DependentSizedArrayType(*this, elementType, QualType(), |
| 2041 | numElements, ASM, elementTypeQuals, |
| 2042 | brackets); |
| 2043 | Types.push_back(newType); |
| 2044 | return QualType(newType, 0); |
| 2045 | } |
| 2046 | |
| 2047 | // Otherwise, we actually build a new type every time, but we |
| 2048 | // also build a canonical type. |
| 2049 | |
| 2050 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 2051 | |
| 2052 | void *insertPos = 0; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2053 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2054 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2055 | QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2056 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2057 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2058 | // Look for an existing type with these properties. |
| 2059 | DependentSizedArrayType *canonTy = |
| 2060 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2061 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2062 | // If we don't have one, build one. |
| 2063 | if (!canonTy) { |
| 2064 | canonTy = new (*this, TypeAlignment) |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2065 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2066 | QualType(), numElements, ASM, elementTypeQuals, |
| 2067 | brackets); |
| 2068 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 2069 | Types.push_back(canonTy); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2072 | // Apply qualifiers from the element type to the array. |
| 2073 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2074 | canonElementType.Quals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2076 | // If we didn't need extra canonicalization for the element type, |
| 2077 | // then just use that as our result. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2078 | if (QualType(canonElementType.Ty, 0) == elementType) |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2079 | return canon; |
| 2080 | |
| 2081 | // Otherwise, we need to build a type which follows the spelling |
| 2082 | // of the element type. |
| 2083 | DependentSizedArrayType *sugaredType |
| 2084 | = new (*this, TypeAlignment) |
| 2085 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 2086 | ASM, elementTypeQuals, brackets); |
| 2087 | Types.push_back(sugaredType); |
| 2088 | return QualType(sugaredType, 0); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2091 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2092 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2093 | unsigned elementTypeQuals) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2094 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2095 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2096 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2097 | void *insertPos = 0; |
| 2098 | if (IncompleteArrayType *iat = |
| 2099 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 2100 | return QualType(iat, 0); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2101 | |
| 2102 | // If the element type isn't canonical, this won't be a canonical type |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2103 | // either, so fill in the canonical type field. We also have to pull |
| 2104 | // qualifiers off the element type. |
| 2105 | QualType canon; |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2106 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2107 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 2108 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2109 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2110 | ASM, elementTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2111 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2112 | |
| 2113 | // Get the new insert position for the node we care about. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2114 | IncompleteArrayType *existing = |
| 2115 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 2116 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 843ebedd | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 2117 | } |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2118 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2119 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 2120 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2121 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2122 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 2123 | Types.push_back(newType); |
| 2124 | return QualType(newType, 0); |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2127 | /// getVectorType - Return the unique reference to a vector type of |
| 2128 | /// the specified element type and size. VectorType must be a built-in type. |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2129 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2130 | VectorType::VectorKind VecKind) const { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2131 | assert(vecType->isBuiltinType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2133 | // Check if we've already instantiated a vector of this type. |
| 2134 | llvm::FoldingSetNodeID ID; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2135 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2136 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2137 | void *InsertPos = 0; |
| 2138 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2139 | return QualType(VTP, 0); |
| 2140 | |
| 2141 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2142 | // so fill in the canonical type field. |
| 2143 | QualType Canonical; |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 2144 | if (!vecType.isCanonical()) { |
Bob Wilson | 7795480 | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 2145 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2147 | // Get the new insert position for the node we care about. |
| 2148 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2149 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2150 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2151 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2152 | VectorType(vecType, NumElts, Canonical, VecKind); |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2153 | VectorTypes.InsertNode(New, InsertPos); |
| 2154 | Types.push_back(New); |
| 2155 | return QualType(New, 0); |
| 2156 | } |
| 2157 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2158 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2159 | /// the specified element type and size. VectorType must be a built-in type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2160 | QualType |
| 2161 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2162 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2163 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2164 | // Check if we've already instantiated a vector of this type. |
| 2165 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2166 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2167 | VectorType::GenericVector); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2168 | void *InsertPos = 0; |
| 2169 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2170 | return QualType(VTP, 0); |
| 2171 | |
| 2172 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2173 | // so fill in the canonical type field. |
| 2174 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2175 | if (!vecType.isCanonical()) { |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2176 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2177 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2178 | // Get the new insert position for the node we care about. |
| 2179 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2180 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2181 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2182 | ExtVectorType *New = new (*this, TypeAlignment) |
| 2183 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2184 | VectorTypes.InsertNode(New, InsertPos); |
| 2185 | Types.push_back(New); |
| 2186 | return QualType(New, 0); |
| 2187 | } |
| 2188 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2189 | QualType |
| 2190 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 2191 | Expr *SizeExpr, |
| 2192 | SourceLocation AttrLoc) const { |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2193 | llvm::FoldingSetNodeID ID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2194 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2195 | SizeExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2196 | |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2197 | void *InsertPos = 0; |
| 2198 | DependentSizedExtVectorType *Canon |
| 2199 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2200 | DependentSizedExtVectorType *New; |
| 2201 | if (Canon) { |
| 2202 | // We already have a canonical version of this array type; use it as |
| 2203 | // the canonical type for a newly-built type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2204 | New = new (*this, TypeAlignment) |
| 2205 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2206 | SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2207 | } else { |
| 2208 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2209 | if (CanonVecTy == vecType) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2210 | New = new (*this, TypeAlignment) |
| 2211 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2212 | AttrLoc); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2213 | |
| 2214 | DependentSizedExtVectorType *CanonCheck |
| 2215 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2216 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2217 | (void)CanonCheck; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2218 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2219 | } else { |
| 2220 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2221 | SourceLocation()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2222 | New = new (*this, TypeAlignment) |
| 2223 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2224 | } |
| 2225 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2226 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2227 | Types.push_back(New); |
| 2228 | return QualType(New, 0); |
| 2229 | } |
| 2230 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2231 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2232 | /// |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2233 | QualType |
| 2234 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2235 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2236 | const CallingConv DefaultCC = Info.getCC(); |
| 2237 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2238 | CC_X86StdCall : DefaultCC; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2239 | // Unique functions, to guarantee there is only one function of a particular |
| 2240 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2241 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2242 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2243 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2244 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2245 | if (FunctionNoProtoType *FT = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2246 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2247 | return QualType(FT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2249 | QualType Canonical; |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 2250 | if (!ResultTy.isCanonical() || |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2251 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2252 | Canonical = |
| 2253 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 2254 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2255 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2256 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2257 | FunctionNoProtoType *NewIP = |
| 2258 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2259 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2260 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2261 | |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2262 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2263 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2264 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2265 | Types.push_back(New); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2266 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2267 | return QualType(New, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | /// getFunctionType - Return a normal function type with a typed argument |
| 2271 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2272 | QualType |
| 2273 | ASTContext::getFunctionType(QualType ResultTy, |
| 2274 | const QualType *ArgArray, unsigned NumArgs, |
| 2275 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2276 | // Unique functions, to guarantee there is only one function of a particular |
| 2277 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2278 | llvm::FoldingSetNodeID ID; |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2279 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this); |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 2280 | |
| 2281 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2282 | if (FunctionProtoType *FTP = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2283 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2284 | return QualType(FTP, 0); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2285 | |
| 2286 | // Determine whether the type being created is already canonical or not. |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2287 | bool isCanonical = |
| 2288 | EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical() && |
| 2289 | !EPI.HasTrailingReturn; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2290 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2291 | if (!ArgArray[i].isCanonicalAsParam()) |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2292 | isCanonical = false; |
| 2293 | |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2294 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2295 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2296 | CC_X86StdCall : DefaultCC; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2297 | |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2298 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2299 | // The exception spec is not part of the canonical type. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2300 | QualType Canonical; |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2301 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2302 | SmallVector<QualType, 16> CanonicalArgs; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2303 | CanonicalArgs.reserve(NumArgs); |
| 2304 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2305 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2306 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2307 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2308 | CanonicalEPI.HasTrailingReturn = false; |
Sebastian Redl | 7c6c9e9 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2309 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2310 | CanonicalEPI.NumExceptions = 0; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2311 | CanonicalEPI.ExtInfo |
| 2312 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2313 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2314 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2315 | CanonicalArgs.data(), NumArgs, |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2316 | CanonicalEPI); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2317 | |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 2318 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2319 | FunctionProtoType *NewIP = |
| 2320 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2321 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2322 | } |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2323 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2324 | // FunctionProtoType objects are allocated with extra bytes after |
| 2325 | // them for three variable size arrays at the end: |
| 2326 | // - parameter types |
| 2327 | // - exception types |
| 2328 | // - consumed-arguments flags |
| 2329 | // Instead of the exception types, there could be a noexcept |
| 2330 | // expression. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2331 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2332 | NumArgs * sizeof(QualType); |
| 2333 | if (EPI.ExceptionSpecType == EST_Dynamic) |
| 2334 | Size += EPI.NumExceptions * sizeof(QualType); |
| 2335 | else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2336 | Size += sizeof(Expr*); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2337 | } else if (EPI.ExceptionSpecType == EST_Uninstantiated) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2338 | Size += 2 * sizeof(FunctionDecl*); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2339 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2340 | if (EPI.ConsumedArguments) |
| 2341 | Size += NumArgs * sizeof(bool); |
| 2342 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2343 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2344 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2345 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2346 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2347 | Types.push_back(FTP); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2348 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2349 | return QualType(FTP, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2350 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 2351 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2352 | #ifndef NDEBUG |
| 2353 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2354 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2355 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2356 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2357 | return true; |
| 2358 | if (RD->getDescribedClassTemplate() && |
| 2359 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2360 | return true; |
| 2361 | return false; |
| 2362 | } |
| 2363 | #endif |
| 2364 | |
| 2365 | /// getInjectedClassNameType - Return the unique reference to the |
| 2366 | /// injected class name type for the specified templated declaration. |
| 2367 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2368 | QualType TST) const { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2369 | assert(NeedsInjectedClassNameType(Decl)); |
| 2370 | if (Decl->TypeForDecl) { |
| 2371 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2372 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2373 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2374 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2375 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2376 | } else { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2377 | Type *newType = |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2378 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2379 | Decl->TypeForDecl = newType; |
| 2380 | Types.push_back(newType); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2381 | } |
| 2382 | return QualType(Decl->TypeForDecl, 0); |
| 2383 | } |
| 2384 | |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2385 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2386 | /// specified type declaration. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2387 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 89656d2 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2388 | assert(Decl && "Passed null for Decl param"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2389 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2390 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2391 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2392 | return getTypedefType(Typedef); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2393 | |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2394 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2395 | "Template type parameter types are always available."); |
| 2396 | |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2397 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2398 | assert(!Record->getPreviousDecl() && |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2399 | "struct/union has previous declaration"); |
| 2400 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2401 | return getRecordType(Record); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2402 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2403 | assert(!Enum->getPreviousDecl() && |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2404 | "enum has previous declaration"); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2405 | return getEnumType(Enum); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2406 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2407 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2408 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2409 | Decl->TypeForDecl = newType; |
| 2410 | Types.push_back(newType); |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2411 | } else |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2412 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2413 | |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2414 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 2417 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2418 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2419 | QualType |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2420 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2421 | QualType Canonical) const { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2422 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2423 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2424 | if (Canonical.isNull()) |
| 2425 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2426 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2427 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2428 | Decl->TypeForDecl = newType; |
| 2429 | Types.push_back(newType); |
| 2430 | return QualType(newType, 0); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2433 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2434 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2435 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2436 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2437 | if (PrevDecl->TypeForDecl) |
| 2438 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2439 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2440 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2441 | Decl->TypeForDecl = newType; |
| 2442 | Types.push_back(newType); |
| 2443 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2446 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2447 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2448 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2449 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2450 | if (PrevDecl->TypeForDecl) |
| 2451 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2452 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2453 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2454 | Decl->TypeForDecl = newType; |
| 2455 | Types.push_back(newType); |
| 2456 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2459 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2460 | QualType modifiedType, |
| 2461 | QualType equivalentType) { |
| 2462 | llvm::FoldingSetNodeID id; |
| 2463 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2464 | |
| 2465 | void *insertPos = 0; |
| 2466 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2467 | if (type) return QualType(type, 0); |
| 2468 | |
| 2469 | QualType canon = getCanonicalType(equivalentType); |
| 2470 | type = new (*this, TypeAlignment) |
| 2471 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2472 | |
| 2473 | Types.push_back(type); |
| 2474 | AttributedTypes.InsertNode(type, insertPos); |
| 2475 | |
| 2476 | return QualType(type, 0); |
| 2477 | } |
| 2478 | |
| 2479 | |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2480 | /// \brief Retrieve a substitution-result type. |
| 2481 | QualType |
| 2482 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2483 | QualType Replacement) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2484 | assert(Replacement.isCanonical() |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2485 | && "replacement types must always be canonical"); |
| 2486 | |
| 2487 | llvm::FoldingSetNodeID ID; |
| 2488 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2489 | void *InsertPos = 0; |
| 2490 | SubstTemplateTypeParmType *SubstParm |
| 2491 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2492 | |
| 2493 | if (!SubstParm) { |
| 2494 | SubstParm = new (*this, TypeAlignment) |
| 2495 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2496 | Types.push_back(SubstParm); |
| 2497 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2498 | } |
| 2499 | |
| 2500 | return QualType(SubstParm, 0); |
| 2501 | } |
| 2502 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2503 | /// \brief Retrieve a |
| 2504 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2505 | const TemplateTypeParmType *Parm, |
| 2506 | const TemplateArgument &ArgPack) { |
| 2507 | #ifndef NDEBUG |
| 2508 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2509 | PEnd = ArgPack.pack_end(); |
| 2510 | P != PEnd; ++P) { |
| 2511 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2512 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2513 | } |
| 2514 | #endif |
| 2515 | |
| 2516 | llvm::FoldingSetNodeID ID; |
| 2517 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2518 | void *InsertPos = 0; |
| 2519 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2520 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2521 | return QualType(SubstParm, 0); |
| 2522 | |
| 2523 | QualType Canon; |
| 2524 | if (!Parm->isCanonicalUnqualified()) { |
| 2525 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2526 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2527 | ArgPack); |
| 2528 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2529 | } |
| 2530 | |
| 2531 | SubstTemplateTypeParmPackType *SubstParm |
| 2532 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2533 | ArgPack); |
| 2534 | Types.push_back(SubstParm); |
| 2535 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2536 | return QualType(SubstParm, 0); |
| 2537 | } |
| 2538 | |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2539 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2540 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2541 | /// name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2542 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2543 | bool ParameterPack, |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2544 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2545 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2546 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2547 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2548 | TemplateTypeParmType *TypeParm |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2549 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2550 | |
| 2551 | if (TypeParm) |
| 2552 | return QualType(TypeParm, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2553 | |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2554 | if (TTPDecl) { |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2555 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2556 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2557 | |
| 2558 | TemplateTypeParmType *TypeCheck |
| 2559 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2560 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2561 | (void)TypeCheck; |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2562 | } else |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2563 | TypeParm = new (*this, TypeAlignment) |
| 2564 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2565 | |
| 2566 | Types.push_back(TypeParm); |
| 2567 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2568 | |
| 2569 | return QualType(TypeParm, 0); |
| 2570 | } |
| 2571 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2572 | TypeSourceInfo * |
| 2573 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2574 | SourceLocation NameLoc, |
| 2575 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2576 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2577 | assert(!Name.getAsDependentTemplateName() && |
| 2578 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2579 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2580 | |
| 2581 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2582 | TemplateSpecializationTypeLoc TL |
| 2583 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2584 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2585 | TL.setTemplateNameLoc(NameLoc); |
| 2586 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2587 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2588 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2589 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2590 | return DI; |
| 2591 | } |
| 2592 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2593 | QualType |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2594 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2595 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2596 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2597 | assert(!Template.getAsDependentTemplateName() && |
| 2598 | "No dependent template names here!"); |
| 2599 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2600 | unsigned NumArgs = Args.size(); |
| 2601 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2602 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2603 | ArgVec.reserve(NumArgs); |
| 2604 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2605 | ArgVec.push_back(Args[i].getArgument()); |
| 2606 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2607 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2608 | Underlying); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2611 | #ifndef NDEBUG |
| 2612 | static bool hasAnyPackExpansions(const TemplateArgument *Args, |
| 2613 | unsigned NumArgs) { |
| 2614 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2615 | if (Args[I].isPackExpansion()) |
| 2616 | return true; |
| 2617 | |
| 2618 | return true; |
| 2619 | } |
| 2620 | #endif |
| 2621 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2622 | QualType |
| 2623 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2624 | const TemplateArgument *Args, |
| 2625 | unsigned NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2626 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2627 | assert(!Template.getAsDependentTemplateName() && |
| 2628 | "No dependent template names here!"); |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2629 | // Look through qualified template names. |
| 2630 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2631 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2632 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2633 | bool IsTypeAlias = |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2634 | Template.getAsTemplateDecl() && |
| 2635 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2636 | QualType CanonType; |
| 2637 | if (!Underlying.isNull()) |
| 2638 | CanonType = getCanonicalType(Underlying); |
| 2639 | else { |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2640 | // We can get here with an alias template when the specialization contains |
| 2641 | // a pack expansion that does not match up with a parameter pack. |
| 2642 | assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) && |
| 2643 | "Caller must compute aliased type"); |
| 2644 | IsTypeAlias = false; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2645 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 2646 | NumArgs); |
| 2647 | } |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2648 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2649 | // Allocate the (non-canonical) template specialization type, but don't |
| 2650 | // try to unique it: these types typically have location information that |
| 2651 | // we don't unique and don't want to lose. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2652 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 2653 | sizeof(TemplateArgument) * NumArgs + |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2654 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2655 | TypeAlignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2656 | TemplateSpecializationType *Spec |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2657 | = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType, |
| 2658 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2659 | |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2660 | Types.push_back(Spec); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2661 | return QualType(Spec, 0); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2664 | QualType |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2665 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2666 | const TemplateArgument *Args, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2667 | unsigned NumArgs) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2668 | assert(!Template.getAsDependentTemplateName() && |
| 2669 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2670 | |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2671 | // Look through qualified template names. |
| 2672 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2673 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2674 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2675 | // Build the canonical template specialization type. |
| 2676 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2677 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2678 | CanonArgs.reserve(NumArgs); |
| 2679 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2680 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2681 | |
| 2682 | // Determine whether this canonical template specialization type already |
| 2683 | // exists. |
| 2684 | llvm::FoldingSetNodeID ID; |
| 2685 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2686 | CanonArgs.data(), NumArgs, *this); |
| 2687 | |
| 2688 | void *InsertPos = 0; |
| 2689 | TemplateSpecializationType *Spec |
| 2690 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2691 | |
| 2692 | if (!Spec) { |
| 2693 | // Allocate a new canonical template specialization type. |
| 2694 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2695 | sizeof(TemplateArgument) * NumArgs), |
| 2696 | TypeAlignment); |
| 2697 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2698 | CanonArgs.data(), NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2699 | QualType(), QualType()); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2700 | Types.push_back(Spec); |
| 2701 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2702 | } |
| 2703 | |
| 2704 | assert(Spec->isDependentType() && |
| 2705 | "Non-dependent template-id type must have a canonical type"); |
| 2706 | return QualType(Spec, 0); |
| 2707 | } |
| 2708 | |
| 2709 | QualType |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2710 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2711 | NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2712 | QualType NamedType) const { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2713 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2714 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2715 | |
| 2716 | void *InsertPos = 0; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2717 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2718 | if (T) |
| 2719 | return QualType(T, 0); |
| 2720 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2721 | QualType Canon = NamedType; |
| 2722 | if (!Canon.isCanonical()) { |
| 2723 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2724 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2725 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2726 | (void)CheckT; |
| 2727 | } |
| 2728 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2729 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2730 | Types.push_back(T); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2731 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2732 | return QualType(T, 0); |
| 2733 | } |
| 2734 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2735 | QualType |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2736 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2737 | llvm::FoldingSetNodeID ID; |
| 2738 | ParenType::Profile(ID, InnerType); |
| 2739 | |
| 2740 | void *InsertPos = 0; |
| 2741 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2742 | if (T) |
| 2743 | return QualType(T, 0); |
| 2744 | |
| 2745 | QualType Canon = InnerType; |
| 2746 | if (!Canon.isCanonical()) { |
| 2747 | Canon = getCanonicalType(InnerType); |
| 2748 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2749 | assert(!CheckT && "Paren canonical type broken"); |
| 2750 | (void)CheckT; |
| 2751 | } |
| 2752 | |
| 2753 | T = new (*this) ParenType(InnerType, Canon); |
| 2754 | Types.push_back(T); |
| 2755 | ParenTypes.InsertNode(T, InsertPos); |
| 2756 | return QualType(T, 0); |
| 2757 | } |
| 2758 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2759 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2760 | NestedNameSpecifier *NNS, |
| 2761 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2762 | QualType Canon) const { |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2763 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2764 | |
| 2765 | if (Canon.isNull()) { |
| 2766 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2767 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2768 | if (Keyword == ETK_None) |
| 2769 | CanonKeyword = ETK_Typename; |
| 2770 | |
| 2771 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2772 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2776 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2777 | |
| 2778 | void *InsertPos = 0; |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2779 | DependentNameType *T |
| 2780 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2781 | if (T) |
| 2782 | return QualType(T, 0); |
| 2783 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2784 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2785 | Types.push_back(T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2786 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2787 | return QualType(T, 0); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | QualType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2791 | ASTContext::getDependentTemplateSpecializationType( |
| 2792 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2793 | NestedNameSpecifier *NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2794 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2795 | const TemplateArgumentListInfo &Args) const { |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2796 | // TODO: avoid this copy |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2797 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2798 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2799 | ArgCopy.push_back(Args[I].getArgument()); |
| 2800 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2801 | ArgCopy.size(), |
| 2802 | ArgCopy.data()); |
| 2803 | } |
| 2804 | |
| 2805 | QualType |
| 2806 | ASTContext::getDependentTemplateSpecializationType( |
| 2807 | ElaboratedTypeKeyword Keyword, |
| 2808 | NestedNameSpecifier *NNS, |
| 2809 | const IdentifierInfo *Name, |
| 2810 | unsigned NumArgs, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2811 | const TemplateArgument *Args) const { |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 2812 | assert((!NNS || NNS->isDependent()) && |
| 2813 | "nested-name-specifier must be dependent"); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2814 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2815 | llvm::FoldingSetNodeID ID; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2816 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2817 | Name, NumArgs, Args); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2818 | |
| 2819 | void *InsertPos = 0; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2820 | DependentTemplateSpecializationType *T |
| 2821 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2822 | if (T) |
| 2823 | return QualType(T, 0); |
| 2824 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2825 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2826 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2827 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2828 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2829 | |
| 2830 | bool AnyNonCanonArgs = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2831 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2832 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2833 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2834 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2835 | AnyNonCanonArgs = true; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2838 | QualType Canon; |
| 2839 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2840 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2841 | Name, NumArgs, |
| 2842 | CanonArgs.data()); |
| 2843 | |
| 2844 | // Find the insert position again. |
| 2845 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2846 | } |
| 2847 | |
| 2848 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2849 | sizeof(TemplateArgument) * NumArgs), |
| 2850 | TypeAlignment); |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2851 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2852 | Name, NumArgs, Args, Canon); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2853 | Types.push_back(T); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2854 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | return QualType(T, 0); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2858 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 2859 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2860 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2861 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2862 | |
| 2863 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2864 | "Pack expansions must expand one or more parameter packs"); |
| 2865 | void *InsertPos = 0; |
| 2866 | PackExpansionType *T |
| 2867 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2868 | if (T) |
| 2869 | return QualType(T, 0); |
| 2870 | |
| 2871 | QualType Canon; |
| 2872 | if (!Pattern.isCanonical()) { |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2873 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2874 | |
| 2875 | // Find the insert position again. |
| 2876 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2877 | } |
| 2878 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2879 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2880 | Types.push_back(T); |
| 2881 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2882 | return QualType(T, 0); |
| 2883 | } |
| 2884 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2885 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2886 | /// alphabetically. |
| 2887 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2888 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2889 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2892 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2893 | unsigned NumProtocols) { |
| 2894 | if (NumProtocols == 0) return true; |
| 2895 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2896 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 2897 | return false; |
| 2898 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2899 | for (unsigned i = 1; i != NumProtocols; ++i) |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2900 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) || |
| 2901 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2902 | return false; |
| 2903 | return true; |
| 2904 | } |
| 2905 | |
| 2906 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2907 | unsigned &NumProtocols) { |
| 2908 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2909 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2910 | // Sort protocols, keyed by name. |
| 2911 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2912 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2913 | // Canonicalize. |
| 2914 | for (unsigned I = 0, N = NumProtocols; I != N; ++I) |
| 2915 | Protocols[I] = Protocols[I]->getCanonicalDecl(); |
| 2916 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2917 | // Remove duplicates. |
| 2918 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2919 | NumProtocols = ProtocolsEnd-Protocols; |
| 2920 | } |
| 2921 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2922 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2923 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2924 | unsigned NumProtocols) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2925 | // If the base type is an interface and there aren't any protocols |
| 2926 | // to add, then the interface type will do just fine. |
| 2927 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2928 | return BaseType; |
| 2929 | |
| 2930 | // Look in the folding set for an existing type. |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2931 | llvm::FoldingSetNodeID ID; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2932 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2933 | void *InsertPos = 0; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2934 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2935 | return QualType(QT, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2936 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2937 | // Build the canonical type, which has the canonical base type and |
| 2938 | // a sorted-and-uniqued list of protocols. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2939 | QualType Canonical; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2940 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2941 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2942 | if (!ProtocolsSorted) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2943 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2944 | Protocols + NumProtocols); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2945 | unsigned UniqueCount = NumProtocols; |
| 2946 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2947 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2948 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2949 | &Sorted[0], UniqueCount); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2950 | } else { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2951 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2952 | Protocols, NumProtocols); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | // Regenerate InsertPos. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2956 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2957 | } |
| 2958 | |
| 2959 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2960 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2961 | void *Mem = Allocate(Size, TypeAlignment); |
| 2962 | ObjCObjectTypeImpl *T = |
| 2963 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2964 | |
| 2965 | Types.push_back(T); |
| 2966 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2967 | return QualType(T, 0); |
| 2968 | } |
| 2969 | |
| 2970 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 2971 | /// the given object type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2972 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2973 | llvm::FoldingSetNodeID ID; |
| 2974 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 2975 | |
| 2976 | void *InsertPos = 0; |
| 2977 | if (ObjCObjectPointerType *QT = |
| 2978 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2979 | return QualType(QT, 0); |
| 2980 | |
| 2981 | // Find the canonical object type. |
| 2982 | QualType Canonical; |
| 2983 | if (!ObjectT.isCanonical()) { |
| 2984 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 2985 | |
| 2986 | // Regenerate InsertPos. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2987 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2988 | } |
| 2989 | |
Douglas Gregor | f85bee6 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 2990 | // No match. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2991 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 2992 | ObjCObjectPointerType *QType = |
| 2993 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2994 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2995 | Types.push_back(QType); |
| 2996 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2997 | return QualType(QType, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2998 | } |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2999 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3000 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 3001 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3002 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 3003 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3004 | if (Decl->TypeForDecl) |
| 3005 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3006 | |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3007 | if (PrevDecl) { |
| 3008 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 3009 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3010 | return QualType(PrevDecl->TypeForDecl, 0); |
| 3011 | } |
| 3012 | |
Douglas Gregor | 7671e53 | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 3013 | // Prefer the definition, if there is one. |
| 3014 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 3015 | Decl = Def; |
| 3016 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3017 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 3018 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 3019 | Decl->TypeForDecl = T; |
| 3020 | Types.push_back(T); |
| 3021 | return QualType(T, 0); |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3024 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 3025 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3026 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3027 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3028 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3029 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3030 | TypeOfExprType *toe; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3031 | if (tofExpr->isTypeDependent()) { |
| 3032 | llvm::FoldingSetNodeID ID; |
| 3033 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3034 | |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3035 | void *InsertPos = 0; |
| 3036 | DependentTypeOfExprType *Canon |
| 3037 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3038 | if (Canon) { |
| 3039 | // We already have a "canonical" version of an identical, dependent |
| 3040 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3041 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3042 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3043 | } else { |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3044 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3045 | Canon |
| 3046 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3047 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 3048 | toe = Canon; |
| 3049 | } |
| 3050 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3051 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3052 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3053 | } |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3054 | Types.push_back(toe); |
| 3055 | return QualType(toe, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3056 | } |
| 3057 | |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3058 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 3059 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 3060 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3061 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3062 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3063 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3064 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3065 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3066 | Types.push_back(tot); |
| 3067 | return QualType(tot, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 3070 | |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3071 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 3072 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 3073 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3074 | /// an issue. This doesn't effect the type checker, since it operates |
David Blaikie | 876657b | 2011-11-06 22:28:03 +0000 | [diff] [blame] | 3075 | /// on canonical types (which are always unique). |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3076 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3077 | DecltypeType *dt; |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3078 | |
| 3079 | // C++0x [temp.type]p2: |
| 3080 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 3081 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 3082 | // type only if their expressions are equivalent (14.5.6.1). |
| 3083 | if (e->isInstantiationDependent()) { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3084 | llvm::FoldingSetNodeID ID; |
| 3085 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3086 | |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3087 | void *InsertPos = 0; |
| 3088 | DependentDecltypeType *Canon |
| 3089 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3090 | if (Canon) { |
| 3091 | // We already have a "canonical" version of an equivalent, dependent |
| 3092 | // decltype type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3093 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3094 | QualType((DecltypeType*)Canon, 0)); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3095 | } else { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3096 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3097 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3098 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 3099 | dt = Canon; |
| 3100 | } |
| 3101 | } else { |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3102 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
| 3103 | getCanonicalType(UnderlyingType)); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3104 | } |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3105 | Types.push_back(dt); |
| 3106 | return QualType(dt, 0); |
| 3107 | } |
| 3108 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3109 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 3110 | /// savings are minimal and these are rare. |
| 3111 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 3112 | QualType UnderlyingType, |
| 3113 | UnaryTransformType::UTTKind Kind) |
| 3114 | const { |
| 3115 | UnaryTransformType *Ty = |
Douglas Gregor | 6c6e676 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 3116 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 3117 | Kind, |
| 3118 | UnderlyingType->isDependentType() ? |
Peter Collingbourne | 15d48ec | 2012-03-05 16:02:06 +0000 | [diff] [blame] | 3119 | QualType() : getCanonicalType(UnderlyingType)); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3120 | Types.push_back(Ty); |
| 3121 | return QualType(Ty, 0); |
| 3122 | } |
| 3123 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3124 | /// getAutoType - We only unique auto types after they've been deduced. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3125 | QualType ASTContext::getAutoType(QualType DeducedType) const { |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3126 | void *InsertPos = 0; |
| 3127 | if (!DeducedType.isNull()) { |
| 3128 | // Look in the folding set for an existing type. |
| 3129 | llvm::FoldingSetNodeID ID; |
| 3130 | AutoType::Profile(ID, DeducedType); |
| 3131 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3132 | return QualType(AT, 0); |
| 3133 | } |
| 3134 | |
| 3135 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType); |
| 3136 | Types.push_back(AT); |
| 3137 | if (InsertPos) |
| 3138 | AutoTypes.InsertNode(AT, InsertPos); |
| 3139 | return QualType(AT, 0); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3142 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 3143 | /// the given value type. |
| 3144 | QualType ASTContext::getAtomicType(QualType T) const { |
| 3145 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3146 | // structure. |
| 3147 | llvm::FoldingSetNodeID ID; |
| 3148 | AtomicType::Profile(ID, T); |
| 3149 | |
| 3150 | void *InsertPos = 0; |
| 3151 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3152 | return QualType(AT, 0); |
| 3153 | |
| 3154 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 3155 | // either, so fill in the canonical type field. |
| 3156 | QualType Canonical; |
| 3157 | if (!T.isCanonical()) { |
| 3158 | Canonical = getAtomicType(getCanonicalType(T)); |
| 3159 | |
| 3160 | // Get the new insert position for the node we care about. |
| 3161 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3162 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 3163 | } |
| 3164 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 3165 | Types.push_back(New); |
| 3166 | AtomicTypes.InsertNode(New, InsertPos); |
| 3167 | return QualType(New, 0); |
| 3168 | } |
| 3169 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3170 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 3171 | QualType ASTContext::getAutoDeductType() const { |
| 3172 | if (AutoDeductTy.isNull()) |
| 3173 | AutoDeductTy = getAutoType(QualType()); |
| 3174 | assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern"); |
| 3175 | return AutoDeductTy; |
| 3176 | } |
| 3177 | |
| 3178 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 3179 | QualType ASTContext::getAutoRRefDeductType() const { |
| 3180 | if (AutoRRefDeductTy.isNull()) |
| 3181 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 3182 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 3183 | return AutoRRefDeductTy; |
| 3184 | } |
| 3185 | |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 3186 | /// getTagDeclType - Return the unique reference to the type for the |
| 3187 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3188 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | 2b0ce11 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 3189 | assert (Decl); |
Mike Stump | b93185d | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 3190 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 3191 | // away const? mutable? |
| 3192 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3195 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 3196 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 3197 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 3198 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3199 | return getFromTargetType(Target->getSizeType()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 3200 | } |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 3201 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3202 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 3203 | CanQualType ASTContext::getIntMaxType() const { |
| 3204 | return getFromTargetType(Target->getIntMaxType()); |
| 3205 | } |
| 3206 | |
| 3207 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 3208 | CanQualType ASTContext::getUIntMaxType() const { |
| 3209 | return getFromTargetType(Target->getUIntMaxType()); |
| 3210 | } |
| 3211 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3212 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 3213 | /// Used when in C++, as a GCC extension. |
| 3214 | QualType ASTContext::getSignedWCharType() const { |
| 3215 | // FIXME: derive from "Target" ? |
| 3216 | return WCharTy; |
| 3217 | } |
| 3218 | |
| 3219 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 3220 | /// Used when in C++, as a GCC extension. |
| 3221 | QualType ASTContext::getUnsignedWCharType() const { |
| 3222 | // FIXME: derive from "Target" ? |
| 3223 | return UnsignedIntTy; |
| 3224 | } |
| 3225 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3226 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3227 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 3228 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3229 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3232 | //===----------------------------------------------------------------------===// |
| 3233 | // Type Operators |
| 3234 | //===----------------------------------------------------------------------===// |
| 3235 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3236 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3237 | // Push qualifiers into arrays, and then discard any remaining |
| 3238 | // qualifiers. |
| 3239 | T = getCanonicalType(T); |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3240 | T = getVariableArrayDecayedType(T); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3241 | const Type *Ty = T.getTypePtr(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3242 | QualType Result; |
| 3243 | if (isa<ArrayType>(Ty)) { |
| 3244 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 3245 | } else if (isa<FunctionType>(Ty)) { |
| 3246 | Result = getPointerType(QualType(Ty, 0)); |
| 3247 | } else { |
| 3248 | Result = QualType(Ty, 0); |
| 3249 | } |
| 3250 | |
| 3251 | return CanQualType::CreateUnsafe(Result); |
| 3252 | } |
| 3253 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3254 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 3255 | Qualifiers &quals) { |
| 3256 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 3257 | |
| 3258 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 3259 | // the unqualified desugared type and then drops it on the floor. |
| 3260 | // We then have to strip that sugar back off with |
| 3261 | // getUnqualifiedDesugaredType(), which is silly. |
| 3262 | const ArrayType *AT = |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3263 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3264 | |
| 3265 | // If we don't have an array, just use the results in splitType. |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3266 | if (!AT) { |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3267 | quals = splitType.Quals; |
| 3268 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3271 | // Otherwise, recurse on the array's element type. |
| 3272 | QualType elementType = AT->getElementType(); |
| 3273 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 3274 | |
| 3275 | // If that didn't change the element type, AT has no qualifiers, so we |
| 3276 | // can just use the results in splitType. |
| 3277 | if (elementType == unqualElementType) { |
| 3278 | assert(quals.empty()); // from the recursive call |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3279 | quals = splitType.Quals; |
| 3280 | return QualType(splitType.Ty, 0); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
| 3283 | // Otherwise, add in the qualifiers from the outermost type, then |
| 3284 | // build the type back up. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3285 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3286 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3287 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3288 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3289 | CAT->getSizeModifier(), 0); |
| 3290 | } |
| 3291 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3292 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3293 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3296 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3297 | return getVariableArrayType(unqualElementType, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3298 | VAT->getSizeExpr(), |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3299 | VAT->getSizeModifier(), |
| 3300 | VAT->getIndexTypeCVRQualifiers(), |
| 3301 | VAT->getBracketsRange()); |
| 3302 | } |
| 3303 | |
| 3304 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3305 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3306 | DSAT->getSizeModifier(), 0, |
| 3307 | SourceRange()); |
| 3308 | } |
| 3309 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3310 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3311 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3312 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3313 | /// or pointer-to-member types, or if they are not similar at this |
| 3314 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3315 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3316 | /// be called in a loop that successively "unwraps" pointer and |
| 3317 | /// pointer-to-member types to compare them at each level. |
| 3318 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3319 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3320 | *T2PtrType = T2->getAs<PointerType>(); |
| 3321 | if (T1PtrType && T2PtrType) { |
| 3322 | T1 = T1PtrType->getPointeeType(); |
| 3323 | T2 = T2PtrType->getPointeeType(); |
| 3324 | return true; |
| 3325 | } |
| 3326 | |
| 3327 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3328 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3329 | if (T1MPType && T2MPType && |
| 3330 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3331 | QualType(T2MPType->getClass(), 0))) { |
| 3332 | T1 = T1MPType->getPointeeType(); |
| 3333 | T2 = T2MPType->getPointeeType(); |
| 3334 | return true; |
| 3335 | } |
| 3336 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3337 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3338 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3339 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3340 | if (T1OPType && T2OPType) { |
| 3341 | T1 = T1OPType->getPointeeType(); |
| 3342 | T2 = T2OPType->getPointeeType(); |
| 3343 | return true; |
| 3344 | } |
| 3345 | } |
| 3346 | |
| 3347 | // FIXME: Block pointers, too? |
| 3348 | |
| 3349 | return false; |
| 3350 | } |
| 3351 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3352 | DeclarationNameInfo |
| 3353 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3354 | SourceLocation NameLoc) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3355 | switch (Name.getKind()) { |
| 3356 | case TemplateName::QualifiedTemplate: |
| 3357 | case TemplateName::Template: |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3358 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3359 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3360 | NameLoc); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3361 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3362 | case TemplateName::OverloadedTemplate: { |
| 3363 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3364 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3365 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3366 | } |
| 3367 | |
| 3368 | case TemplateName::DependentTemplate: { |
| 3369 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3370 | DeclarationName DName; |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3371 | if (DTN->isIdentifier()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3372 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3373 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3374 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3375 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3376 | // DNInfo work in progress: FIXME: source locations? |
| 3377 | DeclarationNameLoc DNLoc; |
| 3378 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3379 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3380 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3381 | } |
| 3382 | } |
| 3383 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3384 | case TemplateName::SubstTemplateTemplateParm: { |
| 3385 | SubstTemplateTemplateParmStorage *subst |
| 3386 | = Name.getAsSubstTemplateTemplateParm(); |
| 3387 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3388 | NameLoc); |
| 3389 | } |
| 3390 | |
| 3391 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3392 | SubstTemplateTemplateParmPackStorage *subst |
| 3393 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3394 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3395 | NameLoc); |
| 3396 | } |
| 3397 | } |
| 3398 | |
| 3399 | llvm_unreachable("bad template name kind!"); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3402 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3403 | switch (Name.getKind()) { |
| 3404 | case TemplateName::QualifiedTemplate: |
| 3405 | case TemplateName::Template: { |
| 3406 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3407 | if (TemplateTemplateParmDecl *TTP |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3408 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3409 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3410 | |
| 3411 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3412 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3413 | } |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3414 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3415 | case TemplateName::OverloadedTemplate: |
| 3416 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3417 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3418 | case TemplateName::DependentTemplate: { |
| 3419 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 3420 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 3421 | return DTN->CanonicalTemplateName; |
| 3422 | } |
| 3423 | |
| 3424 | case TemplateName::SubstTemplateTemplateParm: { |
| 3425 | SubstTemplateTemplateParmStorage *subst |
| 3426 | = Name.getAsSubstTemplateTemplateParm(); |
| 3427 | return getCanonicalTemplateName(subst->getReplacement()); |
| 3428 | } |
| 3429 | |
| 3430 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3431 | SubstTemplateTemplateParmPackStorage *subst |
| 3432 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3433 | TemplateTemplateParmDecl *canonParameter |
| 3434 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 3435 | TemplateArgument canonArgPack |
| 3436 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 3437 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 3438 | } |
| 3439 | } |
| 3440 | |
| 3441 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3444 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 3445 | X = getCanonicalTemplateName(X); |
| 3446 | Y = getCanonicalTemplateName(Y); |
| 3447 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 3448 | } |
| 3449 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3450 | TemplateArgument |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3451 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3452 | switch (Arg.getKind()) { |
| 3453 | case TemplateArgument::Null: |
| 3454 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3455 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3456 | case TemplateArgument::Expression: |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3457 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3458 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3459 | case TemplateArgument::Declaration: { |
| 3460 | if (Decl *D = Arg.getAsDecl()) |
| 3461 | return TemplateArgument(D->getCanonicalDecl()); |
| 3462 | return TemplateArgument((Decl*)0); |
| 3463 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3465 | case TemplateArgument::Template: |
| 3466 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3467 | |
| 3468 | case TemplateArgument::TemplateExpansion: |
| 3469 | return TemplateArgument(getCanonicalTemplateName( |
| 3470 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3471 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3472 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3473 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 3474 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3475 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3476 | case TemplateArgument::Type: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3477 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3478 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3479 | case TemplateArgument::Pack: { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 3480 | if (Arg.pack_size() == 0) |
| 3481 | return Arg; |
| 3482 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3483 | TemplateArgument *CanonArgs |
| 3484 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3485 | unsigned Idx = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3486 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3487 | AEnd = Arg.pack_end(); |
| 3488 | A != AEnd; (void)++A, ++Idx) |
| 3489 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3490 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3491 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3492 | } |
| 3493 | } |
| 3494 | |
| 3495 | // Silence GCC warning |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3496 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3497 | } |
| 3498 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3499 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3500 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3501 | if (!NNS) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3502 | return 0; |
| 3503 | |
| 3504 | switch (NNS->getKind()) { |
| 3505 | case NestedNameSpecifier::Identifier: |
| 3506 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3507 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3508 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 3509 | NNS->getAsIdentifier()); |
| 3510 | |
| 3511 | case NestedNameSpecifier::Namespace: |
| 3512 | // A namespace is canonical; build a nested-name-specifier with |
| 3513 | // this namespace and no prefix. |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3514 | return NestedNameSpecifier::Create(*this, 0, |
| 3515 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 3516 | |
| 3517 | case NestedNameSpecifier::NamespaceAlias: |
| 3518 | // A namespace is canonical; build a nested-name-specifier with |
| 3519 | // this namespace and no prefix. |
| 3520 | return NestedNameSpecifier::Create(*this, 0, |
| 3521 | NNS->getAsNamespaceAlias()->getNamespace() |
| 3522 | ->getOriginalNamespace()); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3523 | |
| 3524 | case NestedNameSpecifier::TypeSpec: |
| 3525 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3526 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3527 | |
| 3528 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 3529 | // break it apart into its prefix and identifier, then reconsititute those |
| 3530 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 3531 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 3532 | // types, e.g., |
| 3533 | // typedef typename T::type T1; |
| 3534 | // typedef typename T1::type T2; |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3535 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) |
| 3536 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3537 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3538 | |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3539 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 3540 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 3541 | // first place? |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3542 | return NestedNameSpecifier::Create(*this, 0, false, |
| 3543 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | case NestedNameSpecifier::Global: |
| 3547 | // The global specifier is canonical and unique. |
| 3548 | return NNS; |
| 3549 | } |
| 3550 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3551 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3554 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3555 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3556 | // Handle the non-qualified case efficiently. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3557 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3558 | // Handle the common positive case fast. |
| 3559 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3560 | return AT; |
| 3561 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3562 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3563 | // Handle the common negative case fast. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3564 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3565 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3566 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3567 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3568 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3569 | // any type qualifiers, the element type is so qualified, not the array type." |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3570 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3571 | // If we get here, we either have type qualifiers on the type, or we have |
| 3572 | // sugar such as a typedef in the way. If we have type qualifiers on the type |
Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 3573 | // we must propagate them down into the element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3574 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3575 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3576 | Qualifiers qs = split.Quals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3577 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3578 | // If we have a simple case, just return now. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3579 | const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3580 | if (ATy == 0 || qs.empty()) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3581 | return ATy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3582 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3583 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3584 | // qualifiers into the array element type and return a new array type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3585 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3586 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3587 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3588 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3589 | CAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3590 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3591 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3592 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3593 | IAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3594 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3595 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3596 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3597 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3598 | return cast<ArrayType>( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3599 | getDependentSizedArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3600 | DSAT->getSizeExpr(), |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3601 | DSAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3602 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3603 | DSAT->getBracketsRange())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3604 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3605 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3606 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3607 | VAT->getSizeExpr(), |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3608 | VAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3609 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3610 | VAT->getBracketsRange())); |
Chris Lattner | ed0d079 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 3613 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3614 | // C99 6.7.5.3p7: |
| 3615 | // A declaration of a parameter as "array of type" shall be |
| 3616 | // adjusted to "qualified pointer to type", where the type |
| 3617 | // qualifiers (if any) are those specified within the [ and ] of |
| 3618 | // the array type derivation. |
| 3619 | if (T->isArrayType()) |
| 3620 | return getArrayDecayedType(T); |
| 3621 | |
| 3622 | // C99 6.7.5.3p8: |
| 3623 | // A declaration of a parameter as "function returning type" |
| 3624 | // shall be adjusted to "pointer to function returning type", as |
| 3625 | // in 6.3.2.1. |
| 3626 | if (T->isFunctionType()) |
| 3627 | return getPointerType(T); |
| 3628 | |
| 3629 | return T; |
| 3630 | } |
| 3631 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 3632 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3633 | T = getVariableArrayDecayedType(T); |
| 3634 | T = getAdjustedParameterType(T); |
| 3635 | return T.getUnqualifiedType(); |
| 3636 | } |
| 3637 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3638 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 3639 | /// specified array type to a pointer. This operation is non-trivial when |
| 3640 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 3641 | /// this returns a pointer to a properly qualified element of the array. |
| 3642 | /// |
| 3643 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3644 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3645 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 3646 | // typedefs in the element type of the array. This also handles propagation |
| 3647 | // of type qualifiers from the array type into the element type if present |
| 3648 | // (C99 6.7.3p8). |
| 3649 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 3650 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3651 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3652 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3653 | |
| 3654 | // int x[restrict 4] -> int *restrict |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3655 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3656 | } |
| 3657 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3658 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 3659 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 3660 | } |
| 3661 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3662 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 3663 | Qualifiers qs; |
| 3664 | while (true) { |
| 3665 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3666 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3667 | if (!array) break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3668 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3669 | type = array->getElementType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3670 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3671 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3672 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3673 | return getQualifiedType(type, qs); |
Anders Carlsson | e0808df | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3676 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3677 | uint64_t |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3678 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 3679 | uint64_t ElementCount = 1; |
| 3680 | do { |
| 3681 | ElementCount *= CA->getSize().getZExtValue(); |
| 3682 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 3683 | } while (CA); |
| 3684 | return ElementCount; |
| 3685 | } |
| 3686 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 3687 | /// getFloatingRank - Return a relative rank for floating point types. |
| 3688 | /// This routine will assert if passed a built-in type that isn't a float. |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3689 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3690 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 3691 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3692 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3693 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 3694 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3695 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3696 | case BuiltinType::Half: return HalfRank; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 3697 | case BuiltinType::Float: return FloatRank; |
| 3698 | case BuiltinType::Double: return DoubleRank; |
| 3699 | case BuiltinType::LongDouble: return LongDoubleRank; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3700 | } |
| 3701 | } |
| 3702 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3703 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 3704 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | fc6ffa2 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3705 | /// 'typeDomain' is a real floating point or complex type. |
| 3706 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3707 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 3708 | QualType Domain) const { |
| 3709 | FloatingRank EltRank = getFloatingRank(Size); |
| 3710 | if (Domain->isComplexType()) { |
| 3711 | switch (EltRank) { |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3712 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3713 | case FloatRank: return FloatComplexTy; |
| 3714 | case DoubleRank: return DoubleComplexTy; |
| 3715 | case LongDoubleRank: return LongDoubleComplexTy; |
| 3716 | } |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 3717 | } |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3718 | |
| 3719 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 3720 | switch (EltRank) { |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3721 | case HalfRank: llvm_unreachable("Half ranks are not valid here"); |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3722 | case FloatRank: return FloatTy; |
| 3723 | case DoubleRank: return DoubleTy; |
| 3724 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3725 | } |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3726 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3729 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 3730 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 3731 | /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3732 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3733 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3734 | FloatingRank LHSR = getFloatingRank(LHS); |
| 3735 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3736 | |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3737 | if (LHSR == RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3738 | return 0; |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3739 | if (LHSR > RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3740 | return 1; |
| 3741 | return -1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3744 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 3745 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 3746 | /// or if it is not canonicalized. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3747 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3748 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3749 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3750 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3751 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3752 | case BuiltinType::Bool: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3753 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3754 | case BuiltinType::Char_S: |
| 3755 | case BuiltinType::Char_U: |
| 3756 | case BuiltinType::SChar: |
| 3757 | case BuiltinType::UChar: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3758 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3759 | case BuiltinType::Short: |
| 3760 | case BuiltinType::UShort: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3761 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3762 | case BuiltinType::Int: |
| 3763 | case BuiltinType::UInt: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3764 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3765 | case BuiltinType::Long: |
| 3766 | case BuiltinType::ULong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3767 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3768 | case BuiltinType::LongLong: |
| 3769 | case BuiltinType::ULongLong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3770 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3771 | case BuiltinType::Int128: |
| 3772 | case BuiltinType::UInt128: |
| 3773 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3774 | } |
| 3775 | } |
| 3776 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3777 | /// \brief Whether this is a promotable bitfield reference according |
| 3778 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3779 | /// |
| 3780 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3781 | /// promotion occurs. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3782 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | e05d3cb | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3783 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3784 | return QualType(); |
| 3785 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3786 | FieldDecl *Field = E->getBitField(); |
| 3787 | if (!Field) |
| 3788 | return QualType(); |
| 3789 | |
| 3790 | QualType FT = Field->getType(); |
| 3791 | |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3792 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3793 | uint64_t IntSize = getTypeSize(IntTy); |
| 3794 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3795 | // to the size of int, it gets promoted no matter what its type is. |
| 3796 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3797 | if (BitWidth < IntSize) |
| 3798 | return IntTy; |
| 3799 | |
| 3800 | if (BitWidth == IntSize) |
| 3801 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3802 | |
| 3803 | // Types bigger than int are not subject to promotions, and therefore act |
| 3804 | // like the base type. |
| 3805 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3806 | // is ridiculous. |
| 3807 | return QualType(); |
| 3808 | } |
| 3809 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3810 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3811 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3812 | /// integer type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3813 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3814 | assert(!Promotable.isNull()); |
| 3815 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3816 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3817 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 3818 | |
| 3819 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 3820 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 3821 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 3822 | // types that can represent all the values of its underlying type: |
| 3823 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 3824 | // unsigned long long int [...] |
| 3825 | // FIXME: Is there some better way to compute this? |
| 3826 | if (BT->getKind() == BuiltinType::WChar_S || |
| 3827 | BT->getKind() == BuiltinType::WChar_U || |
| 3828 | BT->getKind() == BuiltinType::Char16 || |
| 3829 | BT->getKind() == BuiltinType::Char32) { |
| 3830 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 3831 | uint64_t FromSize = getTypeSize(BT); |
| 3832 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 3833 | LongLongTy, UnsignedLongLongTy }; |
| 3834 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 3835 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 3836 | if (FromSize < ToSize || |
| 3837 | (FromSize == ToSize && |
| 3838 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 3839 | return PromoteTypes[Idx]; |
| 3840 | } |
| 3841 | llvm_unreachable("char type should fit into long long"); |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3846 | if (Promotable->isSignedIntegerType()) |
| 3847 | return IntTy; |
| 3848 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3849 | uint64_t IntSize = getTypeSize(IntTy); |
| 3850 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3851 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3852 | } |
| 3853 | |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3854 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 3855 | /// type and returns its ownership. |
| 3856 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 3857 | while (!T.isNull()) { |
| 3858 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 3859 | return T.getObjCLifetime(); |
| 3860 | if (T->isArrayType()) |
| 3861 | T = getBaseElementType(T); |
| 3862 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 3863 | T = PT->getPointeeType(); |
| 3864 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 8e25253 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 3865 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3866 | else |
| 3867 | break; |
| 3868 | } |
| 3869 | |
| 3870 | return Qualifiers::OCL_None; |
| 3871 | } |
| 3872 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3873 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3874 | /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3875 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3876 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3877 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3878 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3879 | if (LHSC == RHSC) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3880 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3881 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3882 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3883 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3884 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3885 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3886 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3887 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3888 | if (LHSRank == RHSRank) return 0; |
| 3889 | return LHSRank > RHSRank ? 1 : -1; |
| 3890 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3891 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3892 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3893 | if (LHSUnsigned) { |
| 3894 | // If the unsigned [LHS] type is larger, return it. |
| 3895 | if (LHSRank >= RHSRank) |
| 3896 | return 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3897 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3898 | // If the signed type can represent all values of the unsigned type, it |
| 3899 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3900 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3901 | return -1; |
| 3902 | } |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3903 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3904 | // If the unsigned [RHS] type is larger, return it. |
| 3905 | if (RHSRank >= LHSRank) |
| 3906 | return -1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3907 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3908 | // If the signed type can represent all values of the unsigned type, it |
| 3909 | // wins. Because we are dealing with 2's complement and types that are |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3910 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3911 | return 1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3912 | } |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3913 | |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3914 | static RecordDecl * |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3915 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 3916 | DeclContext *DC, IdentifierInfo *Id) { |
| 3917 | SourceLocation Loc; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3918 | if (Ctx.getLangOpts().CPlusPlus) |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3919 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3920 | else |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3921 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3922 | } |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3923 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3924 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3925 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3926 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3927 | CFConstantStringTypeDecl = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3928 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3929 | &Idents.get("NSConstantString")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3930 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3931 | |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3932 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3933 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3934 | // const int *isa; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3935 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3936 | // int flags; |
| 3937 | FieldTypes[1] = IntTy; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3938 | // const char *str; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3939 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3940 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3941 | FieldTypes[3] = LongTy; |
| 3942 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3943 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3944 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3945 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3946 | SourceLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3947 | SourceLocation(), 0, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3948 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3949 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3950 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3951 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3952 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3953 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3956 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3957 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3958 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3959 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 412af03 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3960 | } |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3961 | |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3962 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3963 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3964 | assert(Rec && "Invalid CFConstantStringType"); |
| 3965 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3966 | } |
| 3967 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3968 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3969 | if (BlockDescriptorType) |
| 3970 | return getTagDeclType(BlockDescriptorType); |
| 3971 | |
| 3972 | RecordDecl *T; |
| 3973 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3974 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3975 | &Idents.get("__block_descriptor")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3976 | T->startDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3977 | |
| 3978 | QualType FieldTypes[] = { |
| 3979 | UnsignedLongTy, |
| 3980 | UnsignedLongTy, |
| 3981 | }; |
| 3982 | |
| 3983 | const char *FieldNames[] = { |
| 3984 | "reserved", |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 3985 | "Size" |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3986 | }; |
| 3987 | |
| 3988 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3989 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3990 | SourceLocation(), |
| 3991 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3992 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3993 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3994 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3995 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3996 | Field->setAccess(AS_public); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3997 | T->addDecl(Field); |
| 3998 | } |
| 3999 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4000 | T->completeDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4001 | |
| 4002 | BlockDescriptorType = T; |
| 4003 | |
| 4004 | return getTagDeclType(BlockDescriptorType); |
| 4005 | } |
| 4006 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4007 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4008 | if (BlockDescriptorExtendedType) |
| 4009 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4010 | |
| 4011 | RecordDecl *T; |
| 4012 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4013 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4014 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4015 | T->startDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4016 | |
| 4017 | QualType FieldTypes[] = { |
| 4018 | UnsignedLongTy, |
| 4019 | UnsignedLongTy, |
| 4020 | getPointerType(VoidPtrTy), |
| 4021 | getPointerType(VoidPtrTy) |
| 4022 | }; |
| 4023 | |
| 4024 | const char *FieldNames[] = { |
| 4025 | "reserved", |
| 4026 | "Size", |
| 4027 | "CopyFuncPtr", |
| 4028 | "DestroyFuncPtr" |
| 4029 | }; |
| 4030 | |
| 4031 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4032 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4033 | SourceLocation(), |
| 4034 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4035 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4036 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4037 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4038 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4039 | Field->setAccess(AS_public); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4040 | T->addDecl(Field); |
| 4041 | } |
| 4042 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4043 | T->completeDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4044 | |
| 4045 | BlockDescriptorExtendedType = T; |
| 4046 | |
| 4047 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4048 | } |
| 4049 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4050 | bool ASTContext::BlockRequiresCopying(QualType Ty) const { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4051 | if (Ty->isObjCRetainableType()) |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4052 | return true; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4053 | if (getLangOpts().CPlusPlus) { |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 4054 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4055 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 4056 | return RD->hasConstCopyConstructor(); |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 4057 | |
| 4058 | } |
| 4059 | } |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4060 | return false; |
| 4061 | } |
| 4062 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4063 | QualType |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4064 | ASTContext::BuildByRefType(StringRef DeclName, QualType Ty) const { |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4065 | // type = struct __Block_byref_1_X { |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 4066 | // void *__isa; |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4067 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 4068 | // unsigned int __flags; |
| 4069 | // unsigned int __size; |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 4070 | // void *__copy_helper; // as needed |
| 4071 | // void *__destroy_help // as needed |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4072 | // int X; |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 4073 | // } * |
| 4074 | |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4075 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 4076 | |
| 4077 | // FIXME: Move up |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4078 | SmallString<36> Name; |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 4079 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 4080 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4081 | RecordDecl *T; |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4082 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str())); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4083 | T->startDefinition(); |
| 4084 | QualType Int32Ty = IntTy; |
| 4085 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 4086 | QualType FieldTypes[] = { |
| 4087 | getPointerType(VoidPtrTy), |
| 4088 | getPointerType(getTagDeclType(T)), |
| 4089 | Int32Ty, |
| 4090 | Int32Ty, |
| 4091 | getPointerType(VoidPtrTy), |
| 4092 | getPointerType(VoidPtrTy), |
| 4093 | Ty |
| 4094 | }; |
| 4095 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4096 | StringRef FieldNames[] = { |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4097 | "__isa", |
| 4098 | "__forwarding", |
| 4099 | "__flags", |
| 4100 | "__size", |
| 4101 | "__copy_helper", |
| 4102 | "__destroy_helper", |
| 4103 | DeclName, |
| 4104 | }; |
| 4105 | |
| 4106 | for (size_t i = 0; i < 7; ++i) { |
| 4107 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 4108 | continue; |
| 4109 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4110 | SourceLocation(), |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4111 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4112 | FieldTypes[i], /*TInfo=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4113 | /*BitWidth=*/0, /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4114 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4115 | Field->setAccess(AS_public); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4116 | T->addDecl(Field); |
| 4117 | } |
| 4118 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4119 | T->completeDefinition(); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4120 | |
| 4121 | return getPointerType(getTagDeclType(T)); |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4124 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 4125 | if (!ObjCInstanceTypeDecl) |
| 4126 | ObjCInstanceTypeDecl = TypedefDecl::Create(*this, |
| 4127 | getTranslationUnitDecl(), |
| 4128 | SourceLocation(), |
| 4129 | SourceLocation(), |
| 4130 | &Idents.get("instancetype"), |
| 4131 | getTrivialTypeSourceInfo(getObjCIdType())); |
| 4132 | return ObjCInstanceTypeDecl; |
| 4133 | } |
| 4134 | |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4135 | // This returns true if a type has been typedefed to BOOL: |
| 4136 | // typedef <type> BOOL; |
Chris Lattner | e021899 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 4137 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4138 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 9b1f279 | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 4139 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 4140 | return II->isStr("BOOL"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4141 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4142 | return false; |
| 4143 | } |
| 4144 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4145 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4146 | /// purpose. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4147 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4148 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 4149 | return CharUnits::Zero(); |
| 4150 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4151 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4152 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4153 | // Make all integer and enum types at least as large as an int |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4154 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4155 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4156 | // Treat arrays as pointers, since that's how they're passed in. |
| 4157 | else if (type->isArrayType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4158 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4159 | return sz; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | static inline |
| 4163 | std::string charUnitsToString(const CharUnits &CU) { |
| 4164 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4167 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4168 | /// declaration. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4169 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 4170 | std::string S; |
| 4171 | |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4172 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 4173 | QualType BlockTy = |
| 4174 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 4175 | // Encode result type. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 4176 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4177 | // Compute size of all parameters. |
| 4178 | // Start with computing size of a pointer in number of bytes. |
| 4179 | // FIXME: There might(should) be a better way of doing this computation! |
| 4180 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4181 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 4182 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 590c352 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 4183 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4184 | E = Decl->param_end(); PI != E; ++PI) { |
| 4185 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4186 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | d487941 | 2012-06-30 00:48:59 +0000 | [diff] [blame] | 4187 | if (sz.isZero()) |
| 4188 | continue; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4189 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4190 | ParmOffset += sz; |
| 4191 | } |
| 4192 | // Size of the argument frame |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4193 | S += charUnitsToString(ParmOffset); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4194 | // Block pointer and offset. |
| 4195 | S += "@?0"; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4196 | |
| 4197 | // Argument types. |
| 4198 | ParmOffset = PtrSize; |
| 4199 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 4200 | Decl->param_end(); PI != E; ++PI) { |
| 4201 | ParmVarDecl *PVDecl = *PI; |
| 4202 | QualType PType = PVDecl->getOriginalType(); |
| 4203 | if (const ArrayType *AT = |
| 4204 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4205 | // Use array's original type only if it has known number of |
| 4206 | // elements. |
| 4207 | if (!isa<ConstantArrayType>(AT)) |
| 4208 | PType = PVDecl->getType(); |
| 4209 | } else if (PType->isFunctionType()) |
| 4210 | PType = PVDecl->getType(); |
| 4211 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4212 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4213 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4214 | } |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4215 | |
| 4216 | return S; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4217 | } |
| 4218 | |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4219 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4220 | std::string& S) { |
| 4221 | // Encode result type. |
| 4222 | getObjCEncodingForType(Decl->getResultType(), S); |
| 4223 | CharUnits ParmOffset; |
| 4224 | // Compute size of all parameters. |
| 4225 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4226 | E = Decl->param_end(); PI != E; ++PI) { |
| 4227 | QualType PType = (*PI)->getType(); |
| 4228 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4229 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4230 | continue; |
| 4231 | |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4232 | assert (sz.isPositive() && |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4233 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4234 | ParmOffset += sz; |
| 4235 | } |
| 4236 | S += charUnitsToString(ParmOffset); |
| 4237 | ParmOffset = CharUnits::Zero(); |
| 4238 | |
| 4239 | // Argument types. |
| 4240 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4241 | E = Decl->param_end(); PI != E; ++PI) { |
| 4242 | ParmVarDecl *PVDecl = *PI; |
| 4243 | QualType PType = PVDecl->getOriginalType(); |
| 4244 | if (const ArrayType *AT = |
| 4245 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4246 | // Use array's original type only if it has known number of |
| 4247 | // elements. |
| 4248 | if (!isa<ConstantArrayType>(AT)) |
| 4249 | PType = PVDecl->getType(); |
| 4250 | } else if (PType->isFunctionType()) |
| 4251 | PType = PVDecl->getType(); |
| 4252 | getObjCEncodingForType(PType, S); |
| 4253 | S += charUnitsToString(ParmOffset); |
| 4254 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 4255 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4256 | |
| 4257 | return false; |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4260 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 4261 | /// method parameter or return type. If Extended, include class names and |
| 4262 | /// block object types. |
| 4263 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 4264 | QualType T, std::string& S, |
| 4265 | bool Extended) const { |
| 4266 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 4267 | getObjCEncodingForTypeQualifier(QT, S); |
| 4268 | // Encode parameter type. |
| 4269 | getObjCEncodingForTypeImpl(T, S, true, true, 0, |
| 4270 | true /*OutermostType*/, |
| 4271 | false /*EncodingProperty*/, |
| 4272 | false /*StructField*/, |
| 4273 | Extended /*EncodeBlockParameters*/, |
| 4274 | Extended /*EncodeClassNames*/); |
| 4275 | } |
| 4276 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4277 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4278 | /// declaration. |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4279 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4280 | std::string& S, |
| 4281 | bool Extended) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4282 | // FIXME: This is not very efficient. |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4283 | // Encode return type. |
| 4284 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 4285 | Decl->getResultType(), S, Extended); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4286 | // Compute size of all parameters. |
| 4287 | // Start with computing size of a pointer in number of bytes. |
| 4288 | // FIXME: There might(should) be a better way of doing this computation! |
| 4289 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4290 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4291 | // The first two arguments (self and _cmd) are pointers; account for |
| 4292 | // their size. |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4293 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4294 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4295 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4296 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4297 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4298 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4299 | continue; |
| 4300 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4301 | assert (sz.isPositive() && |
| 4302 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4303 | ParmOffset += sz; |
| 4304 | } |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4305 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4306 | S += "@0:"; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4307 | S += charUnitsToString(PtrSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4308 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4309 | // Argument types. |
| 4310 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4311 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4312 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4313 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4314 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | a0befc0 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4315 | if (const ArrayType *AT = |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4316 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4317 | // Use array's original type only if it has known number of |
| 4318 | // elements. |
Steve Naroff | 323827e | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4319 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4320 | PType = PVDecl->getType(); |
| 4321 | } else if (PType->isFunctionType()) |
| 4322 | PType = PVDecl->getType(); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4323 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 4324 | PType, S, Extended); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4325 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4326 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4327 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4328 | |
| 4329 | return false; |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4330 | } |
| 4331 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4332 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4333 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4334 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4335 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4336 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4337 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4338 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4339 | /// encoded as single characters, followed by an identifier. Property types |
| 4340 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4341 | /// these attributes are defined by the following enumeration: |
| 4342 | /// @code |
| 4343 | /// enum PropertyAttributes { |
| 4344 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4345 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4346 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4347 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4348 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4349 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4350 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 8cf6185 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 4351 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4352 | /// kPropertyWeak = 'W' // 'weak' property |
| 4353 | /// kPropertyStrong = 'P' // property GC'able |
| 4354 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4355 | /// }; |
| 4356 | /// @endcode |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4357 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4358 | const Decl *Container, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4359 | std::string& S) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4360 | // Collect information from the property implementation decl(s). |
| 4361 | bool Dynamic = false; |
| 4362 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4363 | |
| 4364 | // FIXME: Duplicated code due to poor abstraction. |
| 4365 | if (Container) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4366 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4367 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4368 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4369 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4370 | i != e; ++i) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4371 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4372 | if (PID->getPropertyDecl() == PD) { |
| 4373 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4374 | Dynamic = true; |
| 4375 | } else { |
| 4376 | SynthesizePID = PID; |
| 4377 | } |
| 4378 | } |
| 4379 | } |
| 4380 | } else { |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4381 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4382 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4383 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4384 | i != e; ++i) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4385 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4386 | if (PID->getPropertyDecl() == PD) { |
| 4387 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4388 | Dynamic = true; |
| 4389 | } else { |
| 4390 | SynthesizePID = PID; |
| 4391 | } |
| 4392 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4393 | } |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4394 | } |
| 4395 | } |
| 4396 | |
| 4397 | // FIXME: This is not very efficient. |
| 4398 | S = "T"; |
| 4399 | |
| 4400 | // Encode result type. |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4401 | // GCC has some special rules regarding encoding of properties which |
| 4402 | // closely resembles encoding of ivars. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4403 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4404 | true /* outermost type */, |
| 4405 | true /* encoding for property */); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4406 | |
| 4407 | if (PD->isReadOnly()) { |
| 4408 | S += ",R"; |
| 4409 | } else { |
| 4410 | switch (PD->getSetterKind()) { |
| 4411 | case ObjCPropertyDecl::Assign: break; |
| 4412 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4413 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 70a315c | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 4414 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4415 | } |
| 4416 | } |
| 4417 | |
| 4418 | // It really isn't clear at all what this means, since properties |
| 4419 | // are "dynamic by default". |
| 4420 | if (Dynamic) |
| 4421 | S += ",D"; |
| 4422 | |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4423 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 4424 | S += ",N"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4425 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4426 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 4427 | S += ",G"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4428 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4429 | } |
| 4430 | |
| 4431 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 4432 | S += ",S"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4433 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | if (SynthesizePID) { |
| 4437 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 4438 | S += ",V"; |
Chris Lattner | 1cbaacc | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 4439 | S += OID->getNameAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | // FIXME: OBJCGC: weak & strong |
| 4443 | } |
| 4444 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4445 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4446 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 4447 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4448 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 4449 | /// |
| 4450 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4451 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4452 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4453 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4454 | PointeeTy = UnsignedIntTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4455 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4456 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4457 | PointeeTy = IntTy; |
| 4458 | } |
| 4459 | } |
| 4460 | } |
| 4461 | |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4462 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4463 | const FieldDecl *Field) const { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4464 | // We follow the behavior of gcc, expanding structures which are |
| 4465 | // directly pointed to, and expanding embedded structures. Note that |
| 4466 | // these rules are sufficient to prevent recursive encoding of the |
| 4467 | // same type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4468 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | daef00b | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 4469 | true /* outermost type */); |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4472 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 4473 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4474 | default: llvm_unreachable("Unhandled builtin type kind"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4475 | case BuiltinType::Void: return 'v'; |
| 4476 | case BuiltinType::Bool: return 'B'; |
| 4477 | case BuiltinType::Char_U: |
| 4478 | case BuiltinType::UChar: return 'C'; |
| 4479 | case BuiltinType::UShort: return 'S'; |
| 4480 | case BuiltinType::UInt: return 'I'; |
| 4481 | case BuiltinType::ULong: |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4482 | return C->getIntWidth(T) == 32 ? 'L' : 'Q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4483 | case BuiltinType::UInt128: return 'T'; |
| 4484 | case BuiltinType::ULongLong: return 'Q'; |
| 4485 | case BuiltinType::Char_S: |
| 4486 | case BuiltinType::SChar: return 'c'; |
| 4487 | case BuiltinType::Short: return 's'; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 4488 | case BuiltinType::WChar_S: |
| 4489 | case BuiltinType::WChar_U: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4490 | case BuiltinType::Int: return 'i'; |
| 4491 | case BuiltinType::Long: |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4492 | return C->getIntWidth(T) == 32 ? 'l' : 'q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4493 | case BuiltinType::LongLong: return 'q'; |
| 4494 | case BuiltinType::Int128: return 't'; |
| 4495 | case BuiltinType::Float: return 'f'; |
| 4496 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 7cba5a7 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 4497 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4498 | } |
| 4499 | } |
| 4500 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4501 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 4502 | EnumDecl *Enum = ET->getDecl(); |
| 4503 | |
| 4504 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 4505 | if (!Enum->isFixed()) |
| 4506 | return 'i'; |
| 4507 | |
| 4508 | // The encoding of a fixed enum type matches its fixed underlying type. |
| 4509 | return ObjCEncodingForPrimitiveKind(C, Enum->getIntegerType()); |
| 4510 | } |
| 4511 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4512 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4513 | QualType T, const FieldDecl *FD) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4514 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4515 | S += 'b'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4516 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 4517 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 4518 | // then the offset (in bits) of the first element, then the type of the |
| 4519 | // bitfield, then the size in bits. For example, in this structure: |
| 4520 | // |
| 4521 | // struct |
| 4522 | // { |
| 4523 | // int integer; |
| 4524 | // int flags:2; |
| 4525 | // }; |
| 4526 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4527 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4528 | // information is not especially sensible, but we're stuck with it for |
| 4529 | // compatibility with GCC, although providing it breaks anything that |
| 4530 | // actually uses runtime introspection and wants to work on both runtimes... |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4531 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4532 | const RecordDecl *RD = FD->getParent(); |
| 4533 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | a3c122d | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 4534 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4535 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 4536 | S += ObjCEncodingForEnumType(Ctx, ET); |
David Chisnall | 6f0a7d2 | 2010-12-26 20:12:30 +0000 | [diff] [blame] | 4537 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4538 | S += ObjCEncodingForPrimitiveKind(Ctx, T); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4539 | } |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4540 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4541 | } |
| 4542 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4543 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4544 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4545 | bool ExpandPointedToStructures, |
| 4546 | bool ExpandStructures, |
Daniel Dunbar | c040ce4 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4547 | const FieldDecl *FD, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4548 | bool OutermostType, |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4549 | bool EncodingProperty, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4550 | bool StructField, |
| 4551 | bool EncodeBlockParameters, |
| 4552 | bool EncodeClassNames) const { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4553 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4554 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4555 | return EncodeBitField(this, S, T, FD); |
| 4556 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4557 | return; |
| 4558 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4559 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4560 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4561 | S += 'j'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4562 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4563 | false); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4564 | return; |
| 4565 | } |
Fariborz Jahanian | d25c219 | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 4566 | |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4567 | // encoding for pointer or r3eference types. |
| 4568 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4569 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 89b660c | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 4570 | if (PT->isObjCSelType()) { |
| 4571 | S += ':'; |
| 4572 | return; |
| 4573 | } |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4574 | PointeeTy = PT->getPointeeType(); |
| 4575 | } |
| 4576 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 4577 | PointeeTy = RT->getPointeeType(); |
| 4578 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4579 | bool isReadOnly = false; |
| 4580 | // For historical/compatibility reasons, the read-only qualifier of the |
| 4581 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 4582 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4583 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4584 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4585 | if (OutermostType && T.isConstQualified()) { |
| 4586 | isReadOnly = true; |
| 4587 | S += 'r'; |
| 4588 | } |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 4589 | } else if (OutermostType) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4590 | QualType P = PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4591 | while (P->getAs<PointerType>()) |
| 4592 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4593 | if (P.isConstQualified()) { |
| 4594 | isReadOnly = true; |
| 4595 | S += 'r'; |
| 4596 | } |
| 4597 | } |
| 4598 | if (isReadOnly) { |
| 4599 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 4600 | // combinations need to be rearranged. |
| 4601 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4602 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 4603 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4604 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4605 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4606 | if (PointeeTy->isCharType()) { |
| 4607 | // char pointer types should be encoded as '*' unless it is a |
| 4608 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4609 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4610 | S += '*'; |
| 4611 | return; |
| 4612 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4613 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 3de6b70 | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 4614 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 4615 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 4616 | S += '#'; |
| 4617 | return; |
| 4618 | } |
| 4619 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 4620 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 4621 | S += '@'; |
| 4622 | return; |
| 4623 | } |
| 4624 | // fall through... |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4625 | } |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4626 | S += '^'; |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4627 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 4628 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4629 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4630 | NULL); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4631 | return; |
| 4632 | } |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4633 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4634 | if (const ArrayType *AT = |
| 4635 | // Ignore type qualifiers etc. |
| 4636 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4637 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4638 | // Incomplete arrays are encoded as a pointer to the array element. |
| 4639 | S += '^'; |
| 4640 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4641 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4642 | false, ExpandStructures, FD); |
| 4643 | } else { |
| 4644 | S += '['; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4645 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4646 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 4647 | if (getTypeSize(CAT->getElementType()) == 0) |
| 4648 | S += '0'; |
| 4649 | else |
| 4650 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 4651 | } else { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4652 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4653 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 4654 | "Unknown array type!"); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4655 | S += '0'; |
| 4656 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4657 | |
| 4658 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4659 | false, ExpandStructures, FD); |
| 4660 | S += ']'; |
| 4661 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4662 | return; |
| 4663 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4664 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4665 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | df4cc61 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 4666 | S += '?'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4667 | return; |
| 4668 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4669 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4670 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4671 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4672 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4673 | // Anonymous structures print as '?' |
| 4674 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 4675 | S += II->getName(); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4676 | if (ClassTemplateSpecializationDecl *Spec |
| 4677 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 4678 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 4679 | std::string TemplateArgsStr |
| 4680 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4681 | TemplateArgs.data(), |
| 4682 | TemplateArgs.size(), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4683 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4684 | |
| 4685 | S += TemplateArgsStr; |
| 4686 | } |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4687 | } else { |
| 4688 | S += '?'; |
| 4689 | } |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4690 | if (ExpandStructures) { |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4691 | S += '='; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4692 | if (!RDecl->isUnion()) { |
| 4693 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 4694 | } else { |
| 4695 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4696 | FieldEnd = RDecl->field_end(); |
| 4697 | Field != FieldEnd; ++Field) { |
| 4698 | if (FD) { |
| 4699 | S += '"'; |
| 4700 | S += Field->getNameAsString(); |
| 4701 | S += '"'; |
| 4702 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4703 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4704 | // Special case bit-fields. |
| 4705 | if (Field->isBitField()) { |
| 4706 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4707 | *Field); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4708 | } else { |
| 4709 | QualType qt = Field->getType(); |
| 4710 | getLegacyIntegralTypeEncoding(qt); |
| 4711 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 4712 | FD, /*OutermostType*/false, |
| 4713 | /*EncodingProperty*/false, |
| 4714 | /*StructField*/true); |
| 4715 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4716 | } |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4717 | } |
Fariborz Jahanian | bc92fd7 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 4718 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4719 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4720 | return; |
| 4721 | } |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4722 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4723 | if (const EnumType *ET = T->getAs<EnumType>()) { |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4724 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4725 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4726 | else |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4727 | S += ObjCEncodingForEnumType(this, ET); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4728 | return; |
| 4729 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4730 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4731 | if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) { |
Steve Naroff | 49140cb | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4732 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4733 | if (EncodeBlockParameters) { |
| 4734 | const FunctionType *FT = BT->getPointeeType()->getAs<FunctionType>(); |
| 4735 | |
| 4736 | S += '<'; |
| 4737 | // Block return type |
| 4738 | getObjCEncodingForTypeImpl(FT->getResultType(), S, |
| 4739 | ExpandPointedToStructures, ExpandStructures, |
| 4740 | FD, |
| 4741 | false /* OutermostType */, |
| 4742 | EncodingProperty, |
| 4743 | false /* StructField */, |
| 4744 | EncodeBlockParameters, |
| 4745 | EncodeClassNames); |
| 4746 | // Block self |
| 4747 | S += "@?"; |
| 4748 | // Block parameters |
| 4749 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
| 4750 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(), |
| 4751 | E = FPT->arg_type_end(); I && (I != E); ++I) { |
| 4752 | getObjCEncodingForTypeImpl(*I, S, |
| 4753 | ExpandPointedToStructures, |
| 4754 | ExpandStructures, |
| 4755 | FD, |
| 4756 | false /* OutermostType */, |
| 4757 | EncodingProperty, |
| 4758 | false /* StructField */, |
| 4759 | EncodeBlockParameters, |
| 4760 | EncodeClassNames); |
| 4761 | } |
| 4762 | } |
| 4763 | S += '>'; |
| 4764 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4765 | return; |
| 4766 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4767 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4768 | // Ignore protocol qualifiers when mangling at this level. |
| 4769 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4770 | T = OT->getBaseType(); |
| 4771 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4772 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4773 | // @encode(class_name) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4774 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4775 | S += '{'; |
| 4776 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4777 | S += II->getName(); |
| 4778 | S += '='; |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4779 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4780 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4781 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4782 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4783 | if (Field->isBitField()) |
| 4784 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4785 | else |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4786 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4787 | } |
| 4788 | S += '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4789 | return; |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4790 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4791 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4792 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4793 | if (OPT->isObjCIdType()) { |
| 4794 | S += '@'; |
| 4795 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4796 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4797 | |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4798 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4799 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4800 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4801 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4802 | S += '#'; |
| 4803 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4804 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4805 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4806 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4807 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4808 | ExpandPointedToStructures, |
| 4809 | ExpandStructures, FD); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4810 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4811 | // Note that we do extended encoding of protocol qualifer list |
| 4812 | // Only when doing ivar or property encoding. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4813 | S += '"'; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4814 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4815 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4816 | S += '<'; |
| 4817 | S += (*I)->getNameAsString(); |
| 4818 | S += '>'; |
| 4819 | } |
| 4820 | S += '"'; |
| 4821 | } |
| 4822 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4823 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4824 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4825 | QualType PointeeTy = OPT->getPointeeType(); |
| 4826 | if (!EncodingProperty && |
| 4827 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4828 | // Another historical/compatibility reason. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4829 | // We encode the underlying type which comes out as |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4830 | // {...}; |
| 4831 | S += '^'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4832 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4833 | false, ExpandPointedToStructures, |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4834 | NULL); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4835 | return; |
| 4836 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4837 | |
| 4838 | S += '@'; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4839 | if (OPT->getInterfaceDecl() && |
| 4840 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4841 | S += '"'; |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4842 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4843 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4844 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4845 | S += '<'; |
| 4846 | S += (*I)->getNameAsString(); |
| 4847 | S += '>'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4848 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4849 | S += '"'; |
| 4850 | } |
| 4851 | return; |
| 4852 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4853 | |
John McCall | a9e6e8d | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4854 | // gcc just blithely ignores member pointers. |
| 4855 | // TODO: maybe there should be a mangling for these |
| 4856 | if (T->getAs<MemberPointerType>()) |
| 4857 | return; |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4858 | |
| 4859 | if (T->isVectorType()) { |
| 4860 | // This matches gcc's encoding, even though technically it is |
| 4861 | // insufficient. |
| 4862 | // FIXME. We should do a better job than gcc. |
| 4863 | return; |
| 4864 | } |
| 4865 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4866 | llvm_unreachable("@encode for type not implemented!"); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4867 | } |
| 4868 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4869 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 4870 | std::string &S, |
| 4871 | const FieldDecl *FD, |
| 4872 | bool includeVBases) const { |
| 4873 | assert(RDecl && "Expected non-null RecordDecl"); |
| 4874 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 4875 | if (!RDecl->getDefinition()) |
| 4876 | return; |
| 4877 | |
| 4878 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 4879 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 4880 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 4881 | |
| 4882 | if (CXXRec) { |
| 4883 | for (CXXRecordDecl::base_class_iterator |
| 4884 | BI = CXXRec->bases_begin(), |
| 4885 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 4886 | if (!BI->isVirtual()) { |
| 4887 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4888 | if (base->isEmpty()) |
| 4889 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 4890 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4891 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4892 | std::make_pair(offs, base)); |
| 4893 | } |
| 4894 | } |
| 4895 | } |
| 4896 | |
| 4897 | unsigned i = 0; |
| 4898 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4899 | FieldEnd = RDecl->field_end(); |
| 4900 | Field != FieldEnd; ++Field, ++i) { |
| 4901 | uint64_t offs = layout.getFieldOffset(i); |
| 4902 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4903 | std::make_pair(offs, *Field)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | if (CXXRec && includeVBases) { |
| 4907 | for (CXXRecordDecl::base_class_iterator |
| 4908 | BI = CXXRec->vbases_begin(), |
| 4909 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 4910 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4911 | if (base->isEmpty()) |
| 4912 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 4913 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); |
Argyrios Kyrtzidis | 81c0b5c | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 4914 | if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
| 4915 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 4916 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4917 | } |
| 4918 | } |
| 4919 | |
| 4920 | CharUnits size; |
| 4921 | if (CXXRec) { |
| 4922 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 4923 | } else { |
| 4924 | size = layout.getSize(); |
| 4925 | } |
| 4926 | |
| 4927 | uint64_t CurOffs = 0; |
| 4928 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 4929 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 4930 | |
Douglas Gregor | f5d6c74 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 4931 | if (CXXRec && CXXRec->isDynamicClass() && |
| 4932 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4933 | if (FD) { |
| 4934 | S += "\"_vptr$"; |
| 4935 | std::string recname = CXXRec->getNameAsString(); |
| 4936 | if (recname.empty()) recname = "?"; |
| 4937 | S += recname; |
| 4938 | S += '"'; |
| 4939 | } |
| 4940 | S += "^^?"; |
| 4941 | CurOffs += getTypeSize(VoidPtrTy); |
| 4942 | } |
| 4943 | |
| 4944 | if (!RDecl->hasFlexibleArrayMember()) { |
| 4945 | // Mark the end of the structure. |
| 4946 | uint64_t offs = toBits(size); |
| 4947 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4948 | std::make_pair(offs, (NamedDecl*)0)); |
| 4949 | } |
| 4950 | |
| 4951 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 4952 | assert(CurOffs <= CurLayObj->first); |
| 4953 | |
| 4954 | if (CurOffs < CurLayObj->first) { |
| 4955 | uint64_t padding = CurLayObj->first - CurOffs; |
| 4956 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 4957 | // packing/alignment of members is different that normal, in which case |
| 4958 | // the encoding will be out-of-sync with the real layout. |
| 4959 | // If the runtime switches to just consider the size of types without |
| 4960 | // taking into account alignment, we could make padding explicit in the |
| 4961 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 4962 | // longer then though. |
| 4963 | CurOffs += padding; |
| 4964 | } |
| 4965 | |
| 4966 | NamedDecl *dcl = CurLayObj->second; |
| 4967 | if (dcl == 0) |
| 4968 | break; // reached end of structure. |
| 4969 | |
| 4970 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 4971 | // We expand the bases without their virtual bases since those are going |
| 4972 | // in the initial structure. Note that this differs from gcc which |
| 4973 | // expands virtual bases each time one is encountered in the hierarchy, |
| 4974 | // making the encoding type bigger than it really is. |
| 4975 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4976 | assert(!base->isEmpty()); |
| 4977 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4978 | } else { |
| 4979 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 4980 | if (FD) { |
| 4981 | S += '"'; |
| 4982 | S += field->getNameAsString(); |
| 4983 | S += '"'; |
| 4984 | } |
| 4985 | |
| 4986 | if (field->isBitField()) { |
| 4987 | EncodeBitField(this, S, field->getType(), field); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4988 | CurOffs += field->getBitWidthValue(*this); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4989 | } else { |
| 4990 | QualType qt = field->getType(); |
| 4991 | getLegacyIntegralTypeEncoding(qt); |
| 4992 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 4993 | /*OutermostType*/false, |
| 4994 | /*EncodingProperty*/false, |
| 4995 | /*StructField*/true); |
| 4996 | CurOffs += getTypeSize(field->getType()); |
| 4997 | } |
| 4998 | } |
| 4999 | } |
| 5000 | } |
| 5001 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5002 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 5003 | std::string& S) const { |
| 5004 | if (QT & Decl::OBJC_TQ_In) |
| 5005 | S += 'n'; |
| 5006 | if (QT & Decl::OBJC_TQ_Inout) |
| 5007 | S += 'N'; |
| 5008 | if (QT & Decl::OBJC_TQ_Out) |
| 5009 | S += 'o'; |
| 5010 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 5011 | S += 'O'; |
| 5012 | if (QT & Decl::OBJC_TQ_Byref) |
| 5013 | S += 'R'; |
| 5014 | if (QT & Decl::OBJC_TQ_Oneway) |
| 5015 | S += 'V'; |
| 5016 | } |
| 5017 | |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 5018 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 5019 | if (!ObjCIdDecl) { |
| 5020 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0); |
| 5021 | T = getObjCObjectPointerType(T); |
| 5022 | TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T); |
| 5023 | ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5024 | getTranslationUnitDecl(), |
| 5025 | SourceLocation(), SourceLocation(), |
| 5026 | &Idents.get("id"), IdInfo); |
| 5027 | } |
| 5028 | |
| 5029 | return ObjCIdDecl; |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 5030 | } |
| 5031 | |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 5032 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 5033 | if (!ObjCSelDecl) { |
| 5034 | QualType SelT = getPointerType(ObjCBuiltinSelTy); |
| 5035 | TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); |
| 5036 | ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5037 | getTranslationUnitDecl(), |
| 5038 | SourceLocation(), SourceLocation(), |
| 5039 | &Idents.get("SEL"), SelInfo); |
| 5040 | } |
| 5041 | return ObjCSelDecl; |
Fariborz Jahanian | 4bef462 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 5042 | } |
| 5043 | |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 5044 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 5045 | if (!ObjCClassDecl) { |
| 5046 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0); |
| 5047 | T = getObjCObjectPointerType(T); |
| 5048 | TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T); |
| 5049 | ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5050 | getTranslationUnitDecl(), |
| 5051 | SourceLocation(), SourceLocation(), |
| 5052 | &Idents.get("Class"), ClassInfo); |
| 5053 | } |
| 5054 | |
| 5055 | return ObjCClassDecl; |
Anders Carlsson | f56a7ae | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 5056 | } |
| 5057 | |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 5058 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 5059 | if (!ObjCProtocolClassDecl) { |
| 5060 | ObjCProtocolClassDecl |
| 5061 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 5062 | SourceLocation(), |
| 5063 | &Idents.get("Protocol"), |
| 5064 | /*PrevDecl=*/0, |
| 5065 | SourceLocation(), true); |
| 5066 | } |
| 5067 | |
| 5068 | return ObjCProtocolClassDecl; |
| 5069 | } |
| 5070 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5071 | //===----------------------------------------------------------------------===// |
| 5072 | // __builtin_va_list Construction Functions |
| 5073 | //===----------------------------------------------------------------------===// |
| 5074 | |
| 5075 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5076 | // typedef char* __builtin_va_list; |
| 5077 | QualType CharPtrType = Context->getPointerType(Context->CharTy); |
| 5078 | TypeSourceInfo *TInfo |
| 5079 | = Context->getTrivialTypeSourceInfo(CharPtrType); |
| 5080 | |
| 5081 | TypedefDecl *VaListTypeDecl |
| 5082 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5083 | Context->getTranslationUnitDecl(), |
| 5084 | SourceLocation(), SourceLocation(), |
| 5085 | &Context->Idents.get("__builtin_va_list"), |
| 5086 | TInfo); |
| 5087 | return VaListTypeDecl; |
| 5088 | } |
| 5089 | |
| 5090 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5091 | // typedef void* __builtin_va_list; |
| 5092 | QualType VoidPtrType = Context->getPointerType(Context->VoidTy); |
| 5093 | TypeSourceInfo *TInfo |
| 5094 | = Context->getTrivialTypeSourceInfo(VoidPtrType); |
| 5095 | |
| 5096 | TypedefDecl *VaListTypeDecl |
| 5097 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5098 | Context->getTranslationUnitDecl(), |
| 5099 | SourceLocation(), SourceLocation(), |
| 5100 | &Context->Idents.get("__builtin_va_list"), |
| 5101 | TInfo); |
| 5102 | return VaListTypeDecl; |
| 5103 | } |
| 5104 | |
| 5105 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5106 | // typedef struct __va_list_tag { |
| 5107 | RecordDecl *VaListTagDecl; |
| 5108 | |
| 5109 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5110 | Context->getTranslationUnitDecl(), |
| 5111 | &Context->Idents.get("__va_list_tag")); |
| 5112 | VaListTagDecl->startDefinition(); |
| 5113 | |
| 5114 | const size_t NumFields = 5; |
| 5115 | QualType FieldTypes[NumFields]; |
| 5116 | const char *FieldNames[NumFields]; |
| 5117 | |
| 5118 | // unsigned char gpr; |
| 5119 | FieldTypes[0] = Context->UnsignedCharTy; |
| 5120 | FieldNames[0] = "gpr"; |
| 5121 | |
| 5122 | // unsigned char fpr; |
| 5123 | FieldTypes[1] = Context->UnsignedCharTy; |
| 5124 | FieldNames[1] = "fpr"; |
| 5125 | |
| 5126 | // unsigned short reserved; |
| 5127 | FieldTypes[2] = Context->UnsignedShortTy; |
| 5128 | FieldNames[2] = "reserved"; |
| 5129 | |
| 5130 | // void* overflow_arg_area; |
| 5131 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5132 | FieldNames[3] = "overflow_arg_area"; |
| 5133 | |
| 5134 | // void* reg_save_area; |
| 5135 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
| 5136 | FieldNames[4] = "reg_save_area"; |
| 5137 | |
| 5138 | // Create fields |
| 5139 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5140 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
| 5141 | SourceLocation(), |
| 5142 | SourceLocation(), |
| 5143 | &Context->Idents.get(FieldNames[i]), |
| 5144 | FieldTypes[i], /*TInfo=*/0, |
| 5145 | /*BitWidth=*/0, |
| 5146 | /*Mutable=*/false, |
| 5147 | ICIS_NoInit); |
| 5148 | Field->setAccess(AS_public); |
| 5149 | VaListTagDecl->addDecl(Field); |
| 5150 | } |
| 5151 | VaListTagDecl->completeDefinition(); |
| 5152 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5153 | Context->VaListTagTy = VaListTagType; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5154 | |
| 5155 | // } __va_list_tag; |
| 5156 | TypedefDecl *VaListTagTypedefDecl |
| 5157 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5158 | Context->getTranslationUnitDecl(), |
| 5159 | SourceLocation(), SourceLocation(), |
| 5160 | &Context->Idents.get("__va_list_tag"), |
| 5161 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5162 | QualType VaListTagTypedefType = |
| 5163 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5164 | |
| 5165 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5166 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5167 | QualType VaListTagArrayType |
| 5168 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5169 | Size, ArrayType::Normal, 0); |
| 5170 | TypeSourceInfo *TInfo |
| 5171 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5172 | TypedefDecl *VaListTypedefDecl |
| 5173 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5174 | Context->getTranslationUnitDecl(), |
| 5175 | SourceLocation(), SourceLocation(), |
| 5176 | &Context->Idents.get("__builtin_va_list"), |
| 5177 | TInfo); |
| 5178 | |
| 5179 | return VaListTypedefDecl; |
| 5180 | } |
| 5181 | |
| 5182 | static TypedefDecl * |
| 5183 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5184 | // typedef struct __va_list_tag { |
| 5185 | RecordDecl *VaListTagDecl; |
| 5186 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5187 | Context->getTranslationUnitDecl(), |
| 5188 | &Context->Idents.get("__va_list_tag")); |
| 5189 | VaListTagDecl->startDefinition(); |
| 5190 | |
| 5191 | const size_t NumFields = 4; |
| 5192 | QualType FieldTypes[NumFields]; |
| 5193 | const char *FieldNames[NumFields]; |
| 5194 | |
| 5195 | // unsigned gp_offset; |
| 5196 | FieldTypes[0] = Context->UnsignedIntTy; |
| 5197 | FieldNames[0] = "gp_offset"; |
| 5198 | |
| 5199 | // unsigned fp_offset; |
| 5200 | FieldTypes[1] = Context->UnsignedIntTy; |
| 5201 | FieldNames[1] = "fp_offset"; |
| 5202 | |
| 5203 | // void* overflow_arg_area; |
| 5204 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 5205 | FieldNames[2] = "overflow_arg_area"; |
| 5206 | |
| 5207 | // void* reg_save_area; |
| 5208 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5209 | FieldNames[3] = "reg_save_area"; |
| 5210 | |
| 5211 | // Create fields |
| 5212 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5213 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5214 | VaListTagDecl, |
| 5215 | SourceLocation(), |
| 5216 | SourceLocation(), |
| 5217 | &Context->Idents.get(FieldNames[i]), |
| 5218 | FieldTypes[i], /*TInfo=*/0, |
| 5219 | /*BitWidth=*/0, |
| 5220 | /*Mutable=*/false, |
| 5221 | ICIS_NoInit); |
| 5222 | Field->setAccess(AS_public); |
| 5223 | VaListTagDecl->addDecl(Field); |
| 5224 | } |
| 5225 | VaListTagDecl->completeDefinition(); |
| 5226 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5227 | Context->VaListTagTy = VaListTagType; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5228 | |
| 5229 | // } __va_list_tag; |
| 5230 | TypedefDecl *VaListTagTypedefDecl |
| 5231 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5232 | Context->getTranslationUnitDecl(), |
| 5233 | SourceLocation(), SourceLocation(), |
| 5234 | &Context->Idents.get("__va_list_tag"), |
| 5235 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5236 | QualType VaListTagTypedefType = |
| 5237 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5238 | |
| 5239 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5240 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5241 | QualType VaListTagArrayType |
| 5242 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5243 | Size, ArrayType::Normal,0); |
| 5244 | TypeSourceInfo *TInfo |
| 5245 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5246 | TypedefDecl *VaListTypedefDecl |
| 5247 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5248 | Context->getTranslationUnitDecl(), |
| 5249 | SourceLocation(), SourceLocation(), |
| 5250 | &Context->Idents.get("__builtin_va_list"), |
| 5251 | TInfo); |
| 5252 | |
| 5253 | return VaListTypedefDecl; |
| 5254 | } |
| 5255 | |
| 5256 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5257 | // typedef int __builtin_va_list[4]; |
| 5258 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); |
| 5259 | QualType IntArrayType |
| 5260 | = Context->getConstantArrayType(Context->IntTy, |
| 5261 | Size, ArrayType::Normal, 0); |
| 5262 | TypedefDecl *VaListTypedefDecl |
| 5263 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5264 | Context->getTranslationUnitDecl(), |
| 5265 | SourceLocation(), SourceLocation(), |
| 5266 | &Context->Idents.get("__builtin_va_list"), |
| 5267 | Context->getTrivialTypeSourceInfo(IntArrayType)); |
| 5268 | |
| 5269 | return VaListTypedefDecl; |
| 5270 | } |
| 5271 | |
| 5272 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
| 5273 | TargetInfo::BuiltinVaListKind Kind) { |
| 5274 | switch (Kind) { |
| 5275 | case TargetInfo::CharPtrBuiltinVaList: |
| 5276 | return CreateCharPtrBuiltinVaListDecl(Context); |
| 5277 | case TargetInfo::VoidPtrBuiltinVaList: |
| 5278 | return CreateVoidPtrBuiltinVaListDecl(Context); |
| 5279 | case TargetInfo::PowerABIBuiltinVaList: |
| 5280 | return CreatePowerABIBuiltinVaListDecl(Context); |
| 5281 | case TargetInfo::X86_64ABIBuiltinVaList: |
| 5282 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
| 5283 | case TargetInfo::PNaClABIBuiltinVaList: |
| 5284 | return CreatePNaClABIBuiltinVaListDecl(Context); |
| 5285 | } |
| 5286 | |
| 5287 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
| 5288 | } |
| 5289 | |
| 5290 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
| 5291 | if (!BuiltinVaListDecl) |
| 5292 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); |
| 5293 | |
| 5294 | return BuiltinVaListDecl; |
| 5295 | } |
| 5296 | |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5297 | QualType ASTContext::getVaListTagType() const { |
| 5298 | // Force the creation of VaListTagTy by building the __builtin_va_list |
| 5299 | // declaration. |
| 5300 | if (VaListTagTy.isNull()) |
| 5301 | (void) getBuiltinVaListDecl(); |
| 5302 | |
| 5303 | return VaListTagTy; |
| 5304 | } |
| 5305 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5306 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5307 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 5308 | "'NSConstantString' type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5309 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5310 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5313 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 5314 | /// lookup. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5315 | TemplateName |
| 5316 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 5317 | UnresolvedSetIterator End) const { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5318 | unsigned size = End - Begin; |
| 5319 | assert(size > 1 && "set is not overloaded!"); |
| 5320 | |
| 5321 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 5322 | size * sizeof(FunctionTemplateDecl*)); |
| 5323 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 5324 | |
| 5325 | NamedDecl **Storage = OT->getStorage(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 5326 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5327 | NamedDecl *D = *I; |
| 5328 | assert(isa<FunctionTemplateDecl>(D) || |
| 5329 | (isa<UsingShadowDecl>(D) && |
| 5330 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 5331 | *Storage++ = D; |
| 5332 | } |
| 5333 | |
| 5334 | return TemplateName(OT); |
| 5335 | } |
| 5336 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5337 | /// \brief Retrieve the template name that represents a qualified |
| 5338 | /// template name such as \c std::vector. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5339 | TemplateName |
| 5340 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 5341 | bool TemplateKeyword, |
| 5342 | TemplateDecl *Template) const { |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 5343 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 5344 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5345 | // FIXME: Canonicalization? |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5346 | llvm::FoldingSetNodeID ID; |
| 5347 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 5348 | |
| 5349 | void *InsertPos = 0; |
| 5350 | QualifiedTemplateName *QTN = |
| 5351 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5352 | if (!QTN) { |
| 5353 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 5354 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 5355 | } |
| 5356 | |
| 5357 | return TemplateName(QTN); |
| 5358 | } |
| 5359 | |
| 5360 | /// \brief Retrieve the template name that represents a dependent |
| 5361 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5362 | TemplateName |
| 5363 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 5364 | const IdentifierInfo *Name) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5365 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 5366 | "Nested name specifier must be dependent"); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5367 | |
| 5368 | llvm::FoldingSetNodeID ID; |
| 5369 | DependentTemplateName::Profile(ID, NNS, Name); |
| 5370 | |
| 5371 | void *InsertPos = 0; |
| 5372 | DependentTemplateName *QTN = |
| 5373 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5374 | |
| 5375 | if (QTN) |
| 5376 | return TemplateName(QTN); |
| 5377 | |
| 5378 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5379 | if (CanonNNS == NNS) { |
| 5380 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 5381 | } else { |
| 5382 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 5383 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5384 | DependentTemplateName *CheckQTN = |
| 5385 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5386 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 5387 | (void)CheckQTN; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5391 | return TemplateName(QTN); |
| 5392 | } |
| 5393 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5394 | /// \brief Retrieve the template name that represents a dependent |
| 5395 | /// template name such as \c MetaFun::template operator+. |
| 5396 | TemplateName |
| 5397 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5398 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5399 | assert((!NNS || NNS->isDependent()) && |
| 5400 | "Nested name specifier must be dependent"); |
| 5401 | |
| 5402 | llvm::FoldingSetNodeID ID; |
| 5403 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 5404 | |
| 5405 | void *InsertPos = 0; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5406 | DependentTemplateName *QTN |
| 5407 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5408 | |
| 5409 | if (QTN) |
| 5410 | return TemplateName(QTN); |
| 5411 | |
| 5412 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5413 | if (CanonNNS == NNS) { |
| 5414 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 5415 | } else { |
| 5416 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 5417 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5418 | |
| 5419 | DependentTemplateName *CheckQTN |
| 5420 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5421 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 5422 | (void)CheckQTN; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5423 | } |
| 5424 | |
| 5425 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5426 | return TemplateName(QTN); |
| 5427 | } |
| 5428 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5429 | TemplateName |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5430 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 5431 | TemplateName replacement) const { |
| 5432 | llvm::FoldingSetNodeID ID; |
| 5433 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 5434 | |
| 5435 | void *insertPos = 0; |
| 5436 | SubstTemplateTemplateParmStorage *subst |
| 5437 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 5438 | |
| 5439 | if (!subst) { |
| 5440 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 5441 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 5442 | } |
| 5443 | |
| 5444 | return TemplateName(subst); |
| 5445 | } |
| 5446 | |
| 5447 | TemplateName |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5448 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 5449 | const TemplateArgument &ArgPack) const { |
| 5450 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 5451 | llvm::FoldingSetNodeID ID; |
| 5452 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 5453 | |
| 5454 | void *InsertPos = 0; |
| 5455 | SubstTemplateTemplateParmPackStorage *Subst |
| 5456 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 5457 | |
| 5458 | if (!Subst) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5459 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5460 | ArgPack.pack_size(), |
| 5461 | ArgPack.pack_begin()); |
| 5462 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 5463 | } |
| 5464 | |
| 5465 | return TemplateName(Subst); |
| 5466 | } |
| 5467 | |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5468 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | ab13857 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 5469 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 5470 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5471 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5472 | switch (Type) { |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5473 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5474 | case TargetInfo::SignedShort: return ShortTy; |
| 5475 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 5476 | case TargetInfo::SignedInt: return IntTy; |
| 5477 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 5478 | case TargetInfo::SignedLong: return LongTy; |
| 5479 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 5480 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 5481 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 5482 | } |
| 5483 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5484 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5485 | } |
Ted Kremenek | 77c51b2 | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 5486 | |
| 5487 | //===----------------------------------------------------------------------===// |
| 5488 | // Type Predicates. |
| 5489 | //===----------------------------------------------------------------------===// |
| 5490 | |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5491 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 5492 | /// garbage collection attribute. |
| 5493 | /// |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5494 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5495 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5496 | return Qualifiers::GCNone; |
| 5497 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5498 | assert(getLangOpts().ObjC1); |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5499 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 5500 | |
| 5501 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 5502 | // (or pointers to them) be treated as though they were declared |
| 5503 | // as __strong. |
| 5504 | if (GCAttrs == Qualifiers::GCNone) { |
| 5505 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 5506 | return Qualifiers::Strong; |
| 5507 | else if (Ty->isPointerType()) |
| 5508 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 5509 | } else { |
| 5510 | // It's not valid to set GC attributes on anything that isn't a |
| 5511 | // pointer. |
| 5512 | #ifndef NDEBUG |
| 5513 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 5514 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 5515 | CT = AT->getElementType(); |
| 5516 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 5517 | #endif |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5518 | } |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 5519 | return GCAttrs; |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5520 | } |
| 5521 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5522 | //===----------------------------------------------------------------------===// |
| 5523 | // Type Compatibility Testing |
| 5524 | //===----------------------------------------------------------------------===// |
Chris Lattner | b338a6b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 5525 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5526 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5527 | /// compatible. |
| 5528 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 5529 | const VectorType *RHS) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 5530 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5531 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5532 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5533 | } |
| 5534 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5535 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 5536 | QualType SecondVec) { |
| 5537 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 5538 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 5539 | |
| 5540 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 5541 | return true; |
| 5542 | |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5543 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 5544 | // equivalent GCC vector types. |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5545 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 5546 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5547 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5548 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5549 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 5550 | First->getVectorKind() != VectorType::AltiVecBool && |
| 5551 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 5552 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5553 | return true; |
| 5554 | |
| 5555 | return false; |
| 5556 | } |
| 5557 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5558 | //===----------------------------------------------------------------------===// |
| 5559 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 5560 | //===----------------------------------------------------------------------===// |
| 5561 | |
| 5562 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 5563 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5564 | bool |
| 5565 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 5566 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5567 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5568 | return true; |
| 5569 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 5570 | E = rProto->protocol_end(); PI != E; ++PI) |
| 5571 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 5572 | return true; |
| 5573 | return false; |
| 5574 | } |
| 5575 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5576 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 5577 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 5578 | /// otherwise. |
| 5579 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 5580 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 5581 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 5582 | return false; |
| 5583 | } |
| 5584 | |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5585 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 5586 | /// Class<p1, ...>. |
| 5587 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 5588 | QualType rhs) { |
| 5589 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 5590 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 5591 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 5592 | |
| 5593 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5594 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5595 | bool match = false; |
| 5596 | ObjCProtocolDecl *lhsProto = *I; |
| 5597 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5598 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5599 | ObjCProtocolDecl *rhsProto = *J; |
| 5600 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 5601 | match = true; |
| 5602 | break; |
| 5603 | } |
| 5604 | } |
| 5605 | if (!match) |
| 5606 | return false; |
| 5607 | } |
| 5608 | return true; |
| 5609 | } |
| 5610 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5611 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 5612 | /// ObjCQualifiedIDType. |
| 5613 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 5614 | bool compare) { |
| 5615 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5616 | if (lhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5617 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 5618 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5619 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5620 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 5621 | return true; |
| 5622 | |
| 5623 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5624 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5625 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5626 | if (!rhsOPT) return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5627 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5628 | if (rhsOPT->qual_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5629 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5630 | // make sure we check the class hierarchy. |
| 5631 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5632 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5633 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5634 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5635 | // see if static class implements all of id's protocols, directly or |
| 5636 | // through its super class and categories. |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5637 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5638 | return false; |
| 5639 | } |
| 5640 | } |
| 5641 | // If there are no qualifiers and no interface, we have an 'id'. |
| 5642 | return true; |
| 5643 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5644 | // Both the right and left sides have qualifiers. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5645 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5646 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5647 | ObjCProtocolDecl *lhsProto = *I; |
| 5648 | bool match = false; |
| 5649 | |
| 5650 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5651 | // see if static class implements all of id's protocols, directly or |
| 5652 | // through its super class and categories. |
| 5653 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5654 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5655 | ObjCProtocolDecl *rhsProto = *J; |
| 5656 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5657 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5658 | match = true; |
| 5659 | break; |
| 5660 | } |
| 5661 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5662 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5663 | // make sure we check the class hierarchy. |
| 5664 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5665 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5666 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5667 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5668 | // see if static class implements all of id's protocols, directly or |
| 5669 | // through its super class and categories. |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5670 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5671 | match = true; |
| 5672 | break; |
| 5673 | } |
| 5674 | } |
| 5675 | } |
| 5676 | if (!match) |
| 5677 | return false; |
| 5678 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5679 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5680 | return true; |
| 5681 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5682 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5683 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 5684 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 5685 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5686 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5687 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5688 | // If both the right and left sides have qualifiers. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5689 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 5690 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 5691 | ObjCProtocolDecl *lhsProto = *I; |
| 5692 | bool match = false; |
| 5693 | |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5694 | // when comparing an id<P> on rhs with a static type on lhs, |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5695 | // see if static class implements all of id's protocols, directly or |
| 5696 | // through its super class and categories. |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5697 | // First, lhs protocols in the qualifier list must be found, direct |
| 5698 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5699 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5700 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5701 | ObjCProtocolDecl *rhsProto = *J; |
| 5702 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5703 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5704 | match = true; |
| 5705 | break; |
| 5706 | } |
| 5707 | } |
| 5708 | if (!match) |
| 5709 | return false; |
| 5710 | } |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5711 | |
| 5712 | // Static class's protocols, or its super class or category protocols |
| 5713 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 5714 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 5715 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 5716 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 5717 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 5718 | // no type qualifier and its class has no static protocol(s) |
| 5719 | // assume that it is mismatch. |
| 5720 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 5721 | return false; |
| 5722 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5723 | LHSInheritedProtocols.begin(), |
| 5724 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 5725 | bool match = false; |
| 5726 | ObjCProtocolDecl *lhsProto = (*I); |
| 5727 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5728 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5729 | ObjCProtocolDecl *rhsProto = *J; |
| 5730 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5731 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5732 | match = true; |
| 5733 | break; |
| 5734 | } |
| 5735 | } |
| 5736 | if (!match) |
| 5737 | return false; |
| 5738 | } |
| 5739 | } |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5740 | return true; |
| 5741 | } |
| 5742 | return false; |
| 5743 | } |
| 5744 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5745 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5746 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 5747 | /// protocol qualifiers on the LHS or RHS. |
| 5748 | /// |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5749 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 5750 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5751 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5752 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5753 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 5754 | // If either type represents the built-in 'id' or 'Class' types, return true. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5755 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 5756 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5757 | return true; |
| 5758 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5759 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5760 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5761 | QualType(RHSOPT,0), |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5762 | false); |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5763 | |
| 5764 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 5765 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 5766 | QualType(RHSOPT,0)); |
| 5767 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5768 | // If we have 2 user-defined types, fall into that path. |
| 5769 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5770 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5771 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5772 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5773 | } |
| 5774 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5775 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 5776 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5777 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 5778 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 5779 | /// not OK. For the return type, the opposite is not OK. |
| 5780 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 5781 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5782 | const ObjCObjectPointerType *RHSOPT, |
| 5783 | bool BlockReturnType) { |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5784 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5785 | return true; |
| 5786 | |
| 5787 | if (LHSOPT->isObjCBuiltinType()) { |
| 5788 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 5789 | } |
| 5790 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5791 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5792 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5793 | QualType(RHSOPT,0), |
| 5794 | false); |
| 5795 | |
| 5796 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 5797 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 5798 | if (LHS && RHS) { // We have 2 user-defined types. |
| 5799 | if (LHS != RHS) { |
| 5800 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5801 | return BlockReturnType; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5802 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5803 | return !BlockReturnType; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5804 | } |
| 5805 | else |
| 5806 | return true; |
| 5807 | } |
| 5808 | return false; |
| 5809 | } |
| 5810 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5811 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 5812 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 5813 | /// It is used to build composite qualifier list of the composite type of |
| 5814 | /// the conditional expression involving two objective-c pointer objects. |
| 5815 | static |
| 5816 | void getIntersectionOfProtocols(ASTContext &Context, |
| 5817 | const ObjCObjectPointerType *LHSOPT, |
| 5818 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5819 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5820 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5821 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5822 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5823 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 5824 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5825 | |
| 5826 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 5827 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 5828 | if (LHSNumProtocols > 0) |
| 5829 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 5830 | else { |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5831 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5832 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 5833 | LHSInheritedProtocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5834 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 5835 | LHSInheritedProtocols.end()); |
| 5836 | } |
| 5837 | |
| 5838 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 5839 | if (RHSNumProtocols > 0) { |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 5840 | ObjCProtocolDecl **RHSProtocols = |
| 5841 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5842 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 5843 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 5844 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 5845 | } else { |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5846 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5847 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 5848 | RHSInheritedProtocols); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5849 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5850 | RHSInheritedProtocols.begin(), |
| 5851 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 5852 | if (InheritedProtocolSet.count((*I))) |
| 5853 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5854 | } |
| 5855 | } |
| 5856 | |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5857 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 5858 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 5859 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 5860 | /// warning is issued. So, its invokation is extremely rare. |
| 5861 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5862 | const ObjCObjectPointerType *Lptr, |
| 5863 | const ObjCObjectPointerType *Rptr) { |
| 5864 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 5865 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 5866 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 5867 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5868 | if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl))) |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5869 | return QualType(); |
| 5870 | |
Fariborz Jahanian | b107143 | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5871 | do { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5872 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5873 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5874 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5875 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 5876 | |
| 5877 | QualType Result = QualType(LHS, 0); |
| 5878 | if (!Protocols.empty()) |
| 5879 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 5880 | Result = getObjCObjectPointerType(Result); |
| 5881 | return Result; |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5882 | } |
Fariborz Jahanian | b107143 | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5883 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5884 | |
| 5885 | return QualType(); |
| 5886 | } |
| 5887 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5888 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 5889 | const ObjCObjectType *RHS) { |
| 5890 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 5891 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 5892 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5893 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 5894 | // the LHS. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5895 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5896 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5897 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5898 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 5899 | // protocol qualified at all, then we are good. |
Steve Naroff | c277ad1 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 5900 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5901 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5902 | |
Fariborz Jahanian | 4806ff8 | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5903 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 5904 | // more detailed analysis is required. |
| 5905 | if (RHS->getNumProtocols() == 0) { |
| 5906 | // OK, if LHS is a superclass of RHS *and* |
| 5907 | // this superclass is assignment compatible with LHS. |
| 5908 | // false otherwise. |
Fariborz Jahanian | 240400b | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5909 | bool IsSuperClass = |
| 5910 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 5911 | if (IsSuperClass) { |
Fariborz Jahanian | 4806ff8 | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5912 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 5913 | // ; i.e., SuperClass may implement at least one of the protocols |
| 5914 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 5915 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 5916 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 240400b | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5917 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | 4806ff8 | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5918 | // If super class has no protocols, it is not a match. |
| 5919 | if (SuperClassInheritedProtocols.empty()) |
| 5920 | return false; |
| 5921 | |
| 5922 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5923 | LHSPE = LHS->qual_end(); |
| 5924 | LHSPI != LHSPE; LHSPI++) { |
| 5925 | bool SuperImplementsProtocol = false; |
| 5926 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 5927 | |
| 5928 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5929 | SuperClassInheritedProtocols.begin(), |
| 5930 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 5931 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 5932 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 5933 | SuperImplementsProtocol = true; |
| 5934 | break; |
| 5935 | } |
| 5936 | } |
| 5937 | if (!SuperImplementsProtocol) |
| 5938 | return false; |
| 5939 | } |
| 5940 | return true; |
| 5941 | } |
| 5942 | return false; |
| 5943 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5944 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5945 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5946 | LHSPE = LHS->qual_end(); |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5947 | LHSPI != LHSPE; LHSPI++) { |
| 5948 | bool RHSImplementsProtocol = false; |
| 5949 | |
| 5950 | // If the RHS doesn't implement the protocol on the left, the types |
| 5951 | // are incompatible. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5952 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 5953 | RHSPE = RHS->qual_end(); |
Steve Naroff | fac5bc9 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5954 | RHSPI != RHSPE; RHSPI++) { |
| 5955 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5956 | RHSImplementsProtocol = true; |
Steve Naroff | fac5bc9 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5957 | break; |
| 5958 | } |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5959 | } |
| 5960 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 5961 | if (!RHSImplementsProtocol) |
| 5962 | return false; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5963 | } |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5964 | // The RHS implements all protocols listed on the LHS. |
| 5965 | return true; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5966 | } |
| 5967 | |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5968 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 5969 | // get the "pointed to" types |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5970 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 5971 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5972 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5973 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5974 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5975 | |
| 5976 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 5977 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5978 | } |
| 5979 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5980 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 5981 | return canAssignObjCInterfaces( |
| 5982 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 5983 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 5984 | } |
| 5985 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5986 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5987 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5988 | /// C99 6.2.7p1: Two types have compatible types if their types are the |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 5989 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5990 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 5991 | bool CompareUnqualified) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5992 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 5993 | return hasSameType(LHS, RHS); |
| 5994 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 5995 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5996 | } |
| 5997 | |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 5998 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | c87c879 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 5999 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6000 | } |
| 6001 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6002 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 6003 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 6004 | } |
| 6005 | |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6006 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 6007 | /// of T is compatible with SubType, return the merged type, else return |
| 6008 | /// QualType() |
| 6009 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 6010 | bool OfBlockPointer, |
| 6011 | bool Unqualified) { |
| 6012 | if (const RecordType *UT = T->getAsUnionType()) { |
| 6013 | RecordDecl *UD = UT->getDecl(); |
| 6014 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 6015 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6016 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | 19b961d | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 6017 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6018 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 6019 | if (!MT.isNull()) |
| 6020 | return MT; |
| 6021 | } |
| 6022 | } |
| 6023 | } |
| 6024 | |
| 6025 | return QualType(); |
| 6026 | } |
| 6027 | |
| 6028 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 6029 | /// argument types |
| 6030 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 6031 | bool OfBlockPointer, |
| 6032 | bool Unqualified) { |
| 6033 | // GNU extension: two types are compatible if they appear as a function |
| 6034 | // argument, one of the types is a transparent union type and the other |
| 6035 | // type is compatible with a union member |
| 6036 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 6037 | Unqualified); |
| 6038 | if (!lmerge.isNull()) |
| 6039 | return lmerge; |
| 6040 | |
| 6041 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 6042 | Unqualified); |
| 6043 | if (!rmerge.isNull()) |
| 6044 | return rmerge; |
| 6045 | |
| 6046 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 6047 | } |
| 6048 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6049 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6050 | bool OfBlockPointer, |
| 6051 | bool Unqualified) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6052 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 6053 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6054 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 6055 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6056 | bool allLTypes = true; |
| 6057 | bool allRTypes = true; |
| 6058 | |
| 6059 | // Check return type |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6060 | QualType retType; |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6061 | if (OfBlockPointer) { |
| 6062 | QualType RHS = rbase->getResultType(); |
| 6063 | QualType LHS = lbase->getResultType(); |
| 6064 | bool UnqualifiedResult = Unqualified; |
| 6065 | if (!UnqualifiedResult) |
| 6066 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6067 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6068 | } |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6069 | else |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6070 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 6071 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6072 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6073 | |
| 6074 | if (Unqualified) |
| 6075 | retType = retType.getUnqualifiedType(); |
| 6076 | |
| 6077 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 6078 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 6079 | if (Unqualified) { |
| 6080 | LRetType = LRetType.getUnqualifiedType(); |
| 6081 | RRetType = RRetType.getUnqualifiedType(); |
| 6082 | } |
| 6083 | |
| 6084 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6085 | allLTypes = false; |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6086 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6087 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6088 | |
Daniel Dunbar | edd5bae | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 6089 | // FIXME: double check this |
| 6090 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 6091 | // rbase->getRegParmAttr() != 0 && |
| 6092 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 6093 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 6094 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6095 | |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6096 | // Compatible functions must have compatible calling conventions |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6097 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6098 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6099 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6100 | // Regparm is part of the calling convention. |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 6101 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 6102 | return QualType(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6103 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 6104 | return QualType(); |
| 6105 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6106 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 6107 | return QualType(); |
| 6108 | |
Fariborz Jahanian | 48c6910 | 2011-10-05 00:05:34 +0000 | [diff] [blame] | 6109 | // functypes which return are preferred over those that do not. |
| 6110 | if (lbaseInfo.getNoReturn() && !rbaseInfo.getNoReturn()) |
| 6111 | allLTypes = false; |
| 6112 | else if (!lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn()) |
| 6113 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6114 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 6115 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6116 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6117 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6118 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6119 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6120 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 6121 | "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6122 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 6123 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 6124 | |
| 6125 | // Compatible functions must have the same number of arguments |
| 6126 | if (lproto_nargs != rproto_nargs) |
| 6127 | return QualType(); |
| 6128 | |
| 6129 | // Variadic and non-variadic functions aren't compatible |
| 6130 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 6131 | return QualType(); |
| 6132 | |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 6133 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 6134 | return QualType(); |
| 6135 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6136 | if (LangOpts.ObjCAutoRefCount && |
| 6137 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 6138 | return QualType(); |
| 6139 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6140 | // Check argument compatibility |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6141 | SmallVector<QualType, 10> types; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6142 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 6143 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 6144 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6145 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 6146 | OfBlockPointer, |
| 6147 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6148 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6149 | |
| 6150 | if (Unqualified) |
| 6151 | argtype = argtype.getUnqualifiedType(); |
| 6152 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6153 | types.push_back(argtype); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6154 | if (Unqualified) { |
| 6155 | largtype = largtype.getUnqualifiedType(); |
| 6156 | rargtype = rargtype.getUnqualifiedType(); |
| 6157 | } |
| 6158 | |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6159 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 6160 | allLTypes = false; |
| 6161 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 6162 | allRTypes = false; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6163 | } |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6164 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6165 | if (allLTypes) return lhs; |
| 6166 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6167 | |
| 6168 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 6169 | EPI.ExtInfo = einfo; |
| 6170 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6171 | } |
| 6172 | |
| 6173 | if (lproto) allRTypes = false; |
| 6174 | if (rproto) allLTypes = false; |
| 6175 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6176 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6177 | if (proto) { |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6178 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6179 | if (proto->isVariadic()) return QualType(); |
| 6180 | // Check that the types are compatible with the types that |
| 6181 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 6182 | // The only types actually affected are promotable integer |
| 6183 | // types and floats, which would be passed as a different |
| 6184 | // type depending on whether the prototype is visible. |
| 6185 | unsigned proto_nargs = proto->getNumArgs(); |
| 6186 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 6187 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | 2973d40 | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 6188 | |
| 6189 | // Look at the promotion type of enum types, since that is the type used |
| 6190 | // to pass enum values. |
| 6191 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 6192 | argTy = Enum->getDecl()->getPromotionType(); |
| 6193 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6194 | if (argTy->isPromotableIntegerType() || |
| 6195 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 6196 | return QualType(); |
| 6197 | } |
| 6198 | |
| 6199 | if (allLTypes) return lhs; |
| 6200 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6201 | |
| 6202 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 6203 | EPI.ExtInfo = einfo; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6204 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6205 | proto->getNumArgs(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6206 | } |
| 6207 | |
| 6208 | if (allLTypes) return lhs; |
| 6209 | if (allRTypes) return rhs; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6210 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6211 | } |
| 6212 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6213 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6214 | bool OfBlockPointer, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6215 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | db4e349 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 6216 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 6217 | // type is adjusted to "T" prior to any further analysis, the expression |
| 6218 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 6219 | // expression is an lvalue unless the reference is an rvalue reference and |
| 6220 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6221 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 6222 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6223 | |
| 6224 | if (Unqualified) { |
| 6225 | LHS = LHS.getUnqualifiedType(); |
| 6226 | RHS = RHS.getUnqualifiedType(); |
| 6227 | } |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6228 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6229 | QualType LHSCan = getCanonicalType(LHS), |
| 6230 | RHSCan = getCanonicalType(RHS); |
| 6231 | |
| 6232 | // If two types are identical, they are compatible. |
| 6233 | if (LHSCan == RHSCan) |
| 6234 | return LHS; |
| 6235 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6236 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6237 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 6238 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6239 | if (LQuals != RQuals) { |
| 6240 | // If any of these qualifiers are different, we have a type |
| 6241 | // mismatch. |
| 6242 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6243 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 6244 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6245 | return QualType(); |
| 6246 | |
| 6247 | // Exactly one GC qualifier difference is allowed: __strong is |
| 6248 | // okay if the other type has no GC qualifier but is an Objective |
| 6249 | // C object pointer (i.e. implicitly strong by default). We fix |
| 6250 | // this by pretending that the unqualified type was actually |
| 6251 | // qualified __strong. |
| 6252 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 6253 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 6254 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 6255 | |
| 6256 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 6257 | return QualType(); |
| 6258 | |
| 6259 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 6260 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 6261 | } |
| 6262 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 6263 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 6264 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6265 | return QualType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6266 | } |
| 6267 | |
| 6268 | // Okay, qualifiers are equal. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6269 | |
Eli Friedman | dcca633 | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 6270 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 6271 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6272 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6273 | // We want to consider the two function types to be the same for these |
| 6274 | // comparisons, just force one to the other. |
| 6275 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 6276 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 16f9096 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 6277 | |
| 6278 | // Same as above for arrays |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 6279 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 6280 | LHSClass = Type::ConstantArray; |
| 6281 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 6282 | RHSClass = Type::ConstantArray; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6283 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6284 | // ObjCInterfaces are just specialized ObjCObjects. |
| 6285 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 6286 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 6287 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 6288 | // Canonicalize ExtVector -> Vector. |
| 6289 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 6290 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6291 | |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 6292 | // If the canonical type classes don't match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6293 | if (LHSClass != RHSClass) { |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6294 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6295 | // a signed integer type, or an unsigned integer type. |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 6296 | // Compatibility is based on the underlying type, not the promotion |
| 6297 | // type. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6298 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Fariborz Jahanian | adfe905 | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 6299 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 6300 | if (!TINT.isNull() && hasSameType(TINT, RHSCan.getUnqualifiedType())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6301 | return RHS; |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 6302 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6303 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Fariborz Jahanian | adfe905 | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 6304 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 6305 | if (!TINT.isNull() && hasSameType(TINT, LHSCan.getUnqualifiedType())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6306 | return LHS; |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 6307 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 6308 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 194904e | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 6309 | if (OfBlockPointer && !BlockReturnType) { |
| 6310 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 6311 | return LHS; |
| 6312 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 6313 | return RHS; |
| 6314 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 6315 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6316 | return QualType(); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6317 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6318 | |
Steve Naroff | c6edcbd | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 6319 | // The canonical type classes match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6320 | switch (LHSClass) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6321 | #define TYPE(Class, Base) |
| 6322 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 6323 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6324 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 6325 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 6326 | #include "clang/AST/TypeNodes.def" |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6327 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6328 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 6329 | case Type::LValueReference: |
| 6330 | case Type::RValueReference: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6331 | case Type::MemberPointer: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6332 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6333 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6334 | case Type::ObjCInterface: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6335 | case Type::IncompleteArray: |
| 6336 | case Type::VariableArray: |
| 6337 | case Type::FunctionProto: |
| 6338 | case Type::ExtVector: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6339 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6340 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6341 | case Type::Pointer: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6342 | { |
| 6343 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6344 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 6345 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6346 | if (Unqualified) { |
| 6347 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 6348 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 6349 | } |
| 6350 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 6351 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6352 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 6353 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6354 | return LHS; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 6355 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6356 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6357 | return getPointerType(ResultType); |
| 6358 | } |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 6359 | case Type::BlockPointer: |
| 6360 | { |
| 6361 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6362 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 6363 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6364 | if (Unqualified) { |
| 6365 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 6366 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 6367 | } |
| 6368 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 6369 | Unqualified); |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 6370 | if (ResultType.isNull()) return QualType(); |
| 6371 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 6372 | return LHS; |
| 6373 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 6374 | return RHS; |
| 6375 | return getBlockPointerType(ResultType); |
| 6376 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6377 | case Type::Atomic: |
| 6378 | { |
| 6379 | // Merge two pointer types, while trying to preserve typedef info |
| 6380 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 6381 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 6382 | if (Unqualified) { |
| 6383 | LHSValue = LHSValue.getUnqualifiedType(); |
| 6384 | RHSValue = RHSValue.getUnqualifiedType(); |
| 6385 | } |
| 6386 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 6387 | Unqualified); |
| 6388 | if (ResultType.isNull()) return QualType(); |
| 6389 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 6390 | return LHS; |
| 6391 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 6392 | return RHS; |
| 6393 | return getAtomicType(ResultType); |
| 6394 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6395 | case Type::ConstantArray: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6396 | { |
| 6397 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 6398 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 6399 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 6400 | return QualType(); |
| 6401 | |
| 6402 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 6403 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6404 | if (Unqualified) { |
| 6405 | LHSElem = LHSElem.getUnqualifiedType(); |
| 6406 | RHSElem = RHSElem.getUnqualifiedType(); |
| 6407 | } |
| 6408 | |
| 6409 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6410 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6411 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6412 | return LHS; |
| 6413 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6414 | return RHS; |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6415 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 6416 | ArrayType::ArraySizeModifier(), 0); |
| 6417 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 6418 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6419 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 6420 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6421 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6422 | return LHS; |
| 6423 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6424 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6425 | if (LVAT) { |
| 6426 | // FIXME: This isn't correct! But tricky to implement because |
| 6427 | // the array's size has to be the size of LHS, but the type |
| 6428 | // has to be different. |
| 6429 | return LHS; |
| 6430 | } |
| 6431 | if (RVAT) { |
| 6432 | // FIXME: This isn't correct! But tricky to implement because |
| 6433 | // the array's size has to be the size of RHS, but the type |
| 6434 | // has to be different. |
| 6435 | return RHS; |
| 6436 | } |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6437 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 6438 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 6439 | return getIncompleteArrayType(ResultType, |
| 6440 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6441 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6442 | case Type::FunctionNoProto: |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6443 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6444 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6445 | case Type::Enum: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6446 | return QualType(); |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6447 | case Type::Builtin: |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6448 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6449 | return QualType(); |
Daniel Dunbar | 804c044 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 6450 | case Type::Complex: |
| 6451 | // Distinct complex types are incompatible. |
| 6452 | return QualType(); |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6453 | case Type::Vector: |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6454 | // FIXME: The merged type should be an ExtVector! |
John McCall | 44c064b | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 6455 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 6456 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6457 | return LHS; |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6458 | return QualType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6459 | case Type::ObjCObject: { |
| 6460 | // Check if the types are assignment compatible. |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6461 | // FIXME: This should be type compatibility, e.g. whether |
| 6462 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6463 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 6464 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 6465 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 7a7814c | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 6466 | return LHS; |
| 6467 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6468 | return QualType(); |
Cedric Venet | 4fc88b7 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 6469 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6470 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6471 | if (OfBlockPointer) { |
| 6472 | if (canAssignObjCInterfacesInBlockPointer( |
| 6473 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6474 | RHS->getAs<ObjCObjectPointerType>(), |
| 6475 | BlockReturnType)) |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6476 | return LHS; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6477 | return QualType(); |
| 6478 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6479 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 6480 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6481 | return LHS; |
| 6482 | |
Steve Naroff | c68cfcf | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 6483 | return QualType(); |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6484 | } |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6485 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6486 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6487 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6488 | } |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 6489 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6490 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 6491 | const FunctionProtoType *FromFunctionType, |
| 6492 | const FunctionProtoType *ToFunctionType) { |
| 6493 | if (FromFunctionType->hasAnyConsumedArgs() != |
| 6494 | ToFunctionType->hasAnyConsumedArgs()) |
| 6495 | return false; |
| 6496 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 6497 | FromFunctionType->getExtProtoInfo(); |
| 6498 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 6499 | ToFunctionType->getExtProtoInfo(); |
| 6500 | if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments) |
| 6501 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 6502 | ArgIdx != NumArgs; ++ArgIdx) { |
| 6503 | if (FromEPI.ConsumedArguments[ArgIdx] != |
| 6504 | ToEPI.ConsumedArguments[ArgIdx]) |
| 6505 | return false; |
| 6506 | } |
| 6507 | return true; |
| 6508 | } |
| 6509 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6510 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 6511 | /// 'RHS' attributes and returns the merged version; including for function |
| 6512 | /// return types. |
| 6513 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 6514 | QualType LHSCan = getCanonicalType(LHS), |
| 6515 | RHSCan = getCanonicalType(RHS); |
| 6516 | // If two types are identical, they are compatible. |
| 6517 | if (LHSCan == RHSCan) |
| 6518 | return LHS; |
| 6519 | if (RHSCan->isFunctionType()) { |
| 6520 | if (!LHSCan->isFunctionType()) |
| 6521 | return QualType(); |
| 6522 | QualType OldReturnType = |
| 6523 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 6524 | QualType NewReturnType = |
| 6525 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 6526 | QualType ResReturnType = |
| 6527 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 6528 | if (ResReturnType.isNull()) |
| 6529 | return QualType(); |
| 6530 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 6531 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 6532 | // In either case, use OldReturnType to build the new function type. |
| 6533 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 6534 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6535 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6536 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6537 | QualType ResultType |
| 6538 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6539 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6540 | return ResultType; |
| 6541 | } |
| 6542 | } |
| 6543 | return QualType(); |
| 6544 | } |
| 6545 | |
| 6546 | // If the qualifiers are different, the types can still be merged. |
| 6547 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 6548 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 6549 | if (LQuals != RQuals) { |
| 6550 | // If any of these qualifiers are different, we have a type mismatch. |
| 6551 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 6552 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 6553 | return QualType(); |
| 6554 | |
| 6555 | // Exactly one GC qualifier difference is allowed: __strong is |
| 6556 | // okay if the other type has no GC qualifier but is an Objective |
| 6557 | // C object pointer (i.e. implicitly strong by default). We fix |
| 6558 | // this by pretending that the unqualified type was actually |
| 6559 | // qualified __strong. |
| 6560 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 6561 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 6562 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 6563 | |
| 6564 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 6565 | return QualType(); |
| 6566 | |
| 6567 | if (GC_L == Qualifiers::Strong) |
| 6568 | return LHS; |
| 6569 | if (GC_R == Qualifiers::Strong) |
| 6570 | return RHS; |
| 6571 | return QualType(); |
| 6572 | } |
| 6573 | |
| 6574 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 6575 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6576 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6577 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 6578 | if (ResQT == LHSBaseQT) |
| 6579 | return LHS; |
| 6580 | if (ResQT == RHSBaseQT) |
| 6581 | return RHS; |
| 6582 | } |
| 6583 | return QualType(); |
| 6584 | } |
| 6585 | |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 6586 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6587 | // Integer Predicates |
| 6588 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3c91971 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 6589 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6590 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6591 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | ee275c8 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 6592 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6593 | if (T->isBooleanType()) |
| 6594 | return 1; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 6595 | // For builtin types, just use the standard type sizing method |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6596 | return (unsigned)getTypeSize(T); |
| 6597 | } |
| 6598 | |
| 6599 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6600 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6601 | |
| 6602 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 6603 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 6604 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6605 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6606 | |
| 6607 | // For enums, we return the unsigned version of the base type. |
| 6608 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6609 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6610 | |
| 6611 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 6612 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6613 | switch (BTy->getKind()) { |
| 6614 | case BuiltinType::Char_S: |
| 6615 | case BuiltinType::SChar: |
| 6616 | return UnsignedCharTy; |
| 6617 | case BuiltinType::Short: |
| 6618 | return UnsignedShortTy; |
| 6619 | case BuiltinType::Int: |
| 6620 | return UnsignedIntTy; |
| 6621 | case BuiltinType::Long: |
| 6622 | return UnsignedLongTy; |
| 6623 | case BuiltinType::LongLong: |
| 6624 | return UnsignedLongLongTy; |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 6625 | case BuiltinType::Int128: |
| 6626 | return UnsignedInt128Ty; |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6627 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6628 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6629 | } |
| 6630 | } |
| 6631 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 6632 | ASTMutationListener::~ASTMutationListener() { } |
| 6633 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6634 | |
| 6635 | //===----------------------------------------------------------------------===// |
| 6636 | // Builtin Type Computation |
| 6637 | //===----------------------------------------------------------------------===// |
| 6638 | |
| 6639 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6640 | /// pointer over the consumed characters. This returns the resultant type. If |
| 6641 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 6642 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 6643 | /// a vector of "i*". |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6644 | /// |
| 6645 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 6646 | /// to be an Integer Constant Expression. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6647 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6648 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6649 | bool &RequiresICE, |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6650 | bool AllowTypeModifiers) { |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6651 | // Modifiers. |
| 6652 | int HowLong = 0; |
| 6653 | bool Signed = false, Unsigned = false; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6654 | RequiresICE = false; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6655 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6656 | // Read the prefixed modifiers first. |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6657 | bool Done = false; |
| 6658 | while (!Done) { |
| 6659 | switch (*Str++) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6660 | default: Done = true; --Str; break; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6661 | case 'I': |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6662 | RequiresICE = true; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6663 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6664 | case 'S': |
| 6665 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 6666 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 6667 | Signed = true; |
| 6668 | break; |
| 6669 | case 'U': |
| 6670 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 6671 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 6672 | Unsigned = true; |
| 6673 | break; |
| 6674 | case 'L': |
| 6675 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 6676 | ++HowLong; |
| 6677 | break; |
| 6678 | } |
| 6679 | } |
| 6680 | |
| 6681 | QualType Type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6682 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6683 | // Read the base type. |
| 6684 | switch (*Str++) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6685 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6686 | case 'v': |
| 6687 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6688 | "Bad modifiers used with 'v'!"); |
| 6689 | Type = Context.VoidTy; |
| 6690 | break; |
| 6691 | case 'f': |
| 6692 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6693 | "Bad modifiers used with 'f'!"); |
| 6694 | Type = Context.FloatTy; |
| 6695 | break; |
| 6696 | case 'd': |
| 6697 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 6698 | "Bad modifiers used with 'd'!"); |
| 6699 | if (HowLong) |
| 6700 | Type = Context.LongDoubleTy; |
| 6701 | else |
| 6702 | Type = Context.DoubleTy; |
| 6703 | break; |
| 6704 | case 's': |
| 6705 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 6706 | if (Unsigned) |
| 6707 | Type = Context.UnsignedShortTy; |
| 6708 | else |
| 6709 | Type = Context.ShortTy; |
| 6710 | break; |
| 6711 | case 'i': |
| 6712 | if (HowLong == 3) |
| 6713 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 6714 | else if (HowLong == 2) |
| 6715 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 6716 | else if (HowLong == 1) |
| 6717 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 6718 | else |
| 6719 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 6720 | break; |
| 6721 | case 'c': |
| 6722 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 6723 | if (Signed) |
| 6724 | Type = Context.SignedCharTy; |
| 6725 | else if (Unsigned) |
| 6726 | Type = Context.UnsignedCharTy; |
| 6727 | else |
| 6728 | Type = Context.CharTy; |
| 6729 | break; |
| 6730 | case 'b': // boolean |
| 6731 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 6732 | Type = Context.BoolTy; |
| 6733 | break; |
| 6734 | case 'z': // size_t. |
| 6735 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 6736 | Type = Context.getSizeType(); |
| 6737 | break; |
| 6738 | case 'F': |
| 6739 | Type = Context.getCFConstantStringType(); |
| 6740 | break; |
Fariborz Jahanian | d11da7e | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 6741 | case 'G': |
| 6742 | Type = Context.getObjCIdType(); |
| 6743 | break; |
| 6744 | case 'H': |
| 6745 | Type = Context.getObjCSelType(); |
| 6746 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6747 | case 'a': |
| 6748 | Type = Context.getBuiltinVaListType(); |
| 6749 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 6750 | break; |
| 6751 | case 'A': |
| 6752 | // This is a "reference" to a va_list; however, what exactly |
| 6753 | // this means depends on how va_list is defined. There are two |
| 6754 | // different kinds of va_list: ones passed by value, and ones |
| 6755 | // passed by reference. An example of a by-value va_list is |
| 6756 | // x86, where va_list is a char*. An example of by-ref va_list |
| 6757 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 6758 | // we want this argument to be a char*&; for x86-64, we want |
| 6759 | // it to be a __va_list_tag*. |
| 6760 | Type = Context.getBuiltinVaListType(); |
| 6761 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6762 | if (Type->isArrayType()) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6763 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6764 | else |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6765 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6766 | break; |
| 6767 | case 'V': { |
| 6768 | char *End; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6769 | unsigned NumElements = strtoul(Str, &End, 10); |
| 6770 | assert(End != Str && "Missing vector size"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6771 | Str = End; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6772 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6773 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 6774 | RequiresICE, false); |
| 6775 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6776 | |
| 6777 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 6778 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6779 | VectorType::GenericVector); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6780 | break; |
| 6781 | } |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 6782 | case 'E': { |
| 6783 | char *End; |
| 6784 | |
| 6785 | unsigned NumElements = strtoul(Str, &End, 10); |
| 6786 | assert(End != Str && "Missing vector size"); |
| 6787 | |
| 6788 | Str = End; |
| 6789 | |
| 6790 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 6791 | false); |
| 6792 | Type = Context.getExtVectorType(ElementType, NumElements); |
| 6793 | break; |
| 6794 | } |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6795 | case 'X': { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6796 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 6797 | false); |
| 6798 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6799 | Type = Context.getComplexType(ElementType); |
| 6800 | break; |
Fariborz Jahanian | 73952fc | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 6801 | } |
| 6802 | case 'Y' : { |
| 6803 | Type = Context.getPointerDiffType(); |
| 6804 | break; |
| 6805 | } |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6806 | case 'P': |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 6807 | Type = Context.getFILEType(); |
| 6808 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6809 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6810 | return QualType(); |
| 6811 | } |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6812 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6813 | case 'J': |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6814 | if (Signed) |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6815 | Type = Context.getsigjmp_bufType(); |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6816 | else |
| 6817 | Type = Context.getjmp_bufType(); |
| 6818 | |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6819 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6820 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6821 | return QualType(); |
| 6822 | } |
| 6823 | break; |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 6824 | case 'K': |
| 6825 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 6826 | Type = Context.getucontext_tType(); |
| 6827 | |
| 6828 | if (Type.isNull()) { |
| 6829 | Error = ASTContext::GE_Missing_ucontext; |
| 6830 | return QualType(); |
| 6831 | } |
| 6832 | break; |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6833 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6834 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6835 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 6836 | Done = !AllowTypeModifiers; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6837 | while (!Done) { |
John McCall | b8b9466 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 6838 | switch (char c = *Str++) { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6839 | default: Done = true; --Str; break; |
| 6840 | case '*': |
| 6841 | case '&': { |
| 6842 | // Both pointers and references can have their pointee types |
| 6843 | // qualified with an address space. |
| 6844 | char *End; |
| 6845 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 6846 | if (End != Str && AddrSpace != 0) { |
| 6847 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 6848 | Str = End; |
| 6849 | } |
| 6850 | if (c == '*') |
| 6851 | Type = Context.getPointerType(Type); |
| 6852 | else |
| 6853 | Type = Context.getLValueReferenceType(Type); |
| 6854 | break; |
| 6855 | } |
| 6856 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 6857 | case 'C': |
| 6858 | Type = Type.withConst(); |
| 6859 | break; |
| 6860 | case 'D': |
| 6861 | Type = Context.getVolatileType(Type); |
| 6862 | break; |
Ted Kremenek | f2a2f5f | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 6863 | case 'R': |
| 6864 | Type = Type.withRestrict(); |
| 6865 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6866 | } |
| 6867 | } |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6868 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6869 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6870 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6871 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6872 | return Type; |
| 6873 | } |
| 6874 | |
| 6875 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6876 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6877 | GetBuiltinTypeError &Error, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6878 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6879 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6880 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6881 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6882 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6883 | bool RequiresICE = false; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6884 | Error = GE_None; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6885 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 6886 | RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6887 | if (Error != GE_None) |
| 6888 | return QualType(); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6889 | |
| 6890 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 6891 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6892 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6893 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6894 | if (Error != GE_None) |
| 6895 | return QualType(); |
| 6896 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6897 | // If this argument is required to be an IntegerConstantExpression and the |
| 6898 | // caller cares, fill in the bitmask we return. |
| 6899 | if (RequiresICE && IntegerConstantArgs) |
| 6900 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 6901 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6902 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 6903 | if (Ty->isArrayType()) |
| 6904 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6905 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6906 | ArgTypes.push_back(Ty); |
| 6907 | } |
| 6908 | |
| 6909 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 6910 | "'.' should only occur at end of builtin type list!"); |
| 6911 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6912 | FunctionType::ExtInfo EI; |
| 6913 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 6914 | |
| 6915 | bool Variadic = (TypeStr[0] == '.'); |
| 6916 | |
| 6917 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 6918 | if (ArgTypes.empty() && Variadic) |
| 6919 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 6920 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6921 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6922 | EPI.ExtInfo = EI; |
| 6923 | EPI.Variadic = Variadic; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6924 | |
| 6925 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6926 | } |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 6927 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6928 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 6929 | GVALinkage External = GVA_StrongExternal; |
| 6930 | |
| 6931 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6932 | switch (L) { |
| 6933 | case NoLinkage: |
| 6934 | case InternalLinkage: |
| 6935 | case UniqueExternalLinkage: |
| 6936 | return GVA_Internal; |
| 6937 | |
| 6938 | case ExternalLinkage: |
| 6939 | switch (FD->getTemplateSpecializationKind()) { |
| 6940 | case TSK_Undeclared: |
| 6941 | case TSK_ExplicitSpecialization: |
| 6942 | External = GVA_StrongExternal; |
| 6943 | break; |
| 6944 | |
| 6945 | case TSK_ExplicitInstantiationDefinition: |
| 6946 | return GVA_ExplicitTemplateInstantiation; |
| 6947 | |
| 6948 | case TSK_ExplicitInstantiationDeclaration: |
| 6949 | case TSK_ImplicitInstantiation: |
| 6950 | External = GVA_TemplateInstantiation; |
| 6951 | break; |
| 6952 | } |
| 6953 | } |
| 6954 | |
| 6955 | if (!FD->isInlined()) |
| 6956 | return External; |
| 6957 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6958 | if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6959 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 6960 | // externally visible. |
| 6961 | if (FD->isInlineDefinitionExternallyVisible()) |
| 6962 | return External; |
| 6963 | |
| 6964 | // C99 inline semantics, where the symbol is not externally visible. |
| 6965 | return GVA_C99Inline; |
| 6966 | } |
| 6967 | |
| 6968 | // C++0x [temp.explicit]p9: |
| 6969 | // [ Note: The intent is that an inline function that is the subject of |
| 6970 | // an explicit instantiation declaration will still be implicitly |
| 6971 | // instantiated when used so that the body can be considered for |
| 6972 | // inlining, but that no out-of-line copy of the inline function would be |
| 6973 | // generated in the translation unit. -- end note ] |
| 6974 | if (FD->getTemplateSpecializationKind() |
| 6975 | == TSK_ExplicitInstantiationDeclaration) |
| 6976 | return GVA_C99Inline; |
| 6977 | |
| 6978 | return GVA_CXXInline; |
| 6979 | } |
| 6980 | |
| 6981 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 6982 | // If this is a static data member, compute the kind of template |
| 6983 | // specialization. Otherwise, this variable is not part of a |
| 6984 | // template. |
| 6985 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 6986 | if (VD->isStaticDataMember()) |
| 6987 | TSK = VD->getTemplateSpecializationKind(); |
| 6988 | |
| 6989 | Linkage L = VD->getLinkage(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6990 | if (L == ExternalLinkage && getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6991 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 6992 | L = UniqueExternalLinkage; |
| 6993 | |
| 6994 | switch (L) { |
| 6995 | case NoLinkage: |
| 6996 | case InternalLinkage: |
| 6997 | case UniqueExternalLinkage: |
| 6998 | return GVA_Internal; |
| 6999 | |
| 7000 | case ExternalLinkage: |
| 7001 | switch (TSK) { |
| 7002 | case TSK_Undeclared: |
| 7003 | case TSK_ExplicitSpecialization: |
| 7004 | return GVA_StrongExternal; |
| 7005 | |
| 7006 | case TSK_ExplicitInstantiationDeclaration: |
| 7007 | llvm_unreachable("Variable should not be instantiated"); |
| 7008 | // Fall through to treat this like any other instantiation. |
| 7009 | |
| 7010 | case TSK_ExplicitInstantiationDefinition: |
| 7011 | return GVA_ExplicitTemplateInstantiation; |
| 7012 | |
| 7013 | case TSK_ImplicitInstantiation: |
| 7014 | return GVA_TemplateInstantiation; |
| 7015 | } |
| 7016 | } |
| 7017 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7018 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7019 | } |
| 7020 | |
Argyrios Kyrtzidis | c904933 | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 7021 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7022 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7023 | if (!VD->isFileVarDecl()) |
| 7024 | return false; |
Argyrios Kyrtzidis | a98e861 | 2011-09-13 21:35:00 +0000 | [diff] [blame] | 7025 | } else if (!isa<FunctionDecl>(D)) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7026 | return false; |
| 7027 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7028 | // Weak references don't produce any output by themselves. |
| 7029 | if (D->hasAttr<WeakRefAttr>()) |
| 7030 | return false; |
| 7031 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7032 | // Aliases and used decls are required. |
| 7033 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 7034 | return true; |
| 7035 | |
| 7036 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7037 | // Forward declarations aren't required. |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 7038 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 7039 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7040 | |
| 7041 | // Constructors and destructors are required. |
| 7042 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 7043 | return true; |
| 7044 | |
| 7045 | // The key function for a class is required. |
| 7046 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 7047 | const CXXRecordDecl *RD = MD->getParent(); |
| 7048 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 7049 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 7050 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 7051 | return true; |
| 7052 | } |
| 7053 | } |
| 7054 | |
| 7055 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 7056 | |
| 7057 | // static, static inline, always_inline, and extern inline functions can |
| 7058 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 7059 | // Implicit template instantiations can also be deferred in C++. |
| 7060 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
Anton Yartsev | 79de4d4 | 2012-02-02 06:06:34 +0000 | [diff] [blame] | 7061 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7062 | return false; |
| 7063 | return true; |
| 7064 | } |
Douglas Gregor | 87d8124 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 7065 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7066 | const VarDecl *VD = cast<VarDecl>(D); |
| 7067 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 7068 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7069 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 7070 | return false; |
| 7071 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7072 | // Structs that have non-trivial constructors or destructors are required. |
| 7073 | |
| 7074 | // FIXME: Handle references. |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 7075 | // FIXME: Be more selective about which constructors we care about. |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7076 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 7077 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 7078 | if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() && |
| 7079 | RD->hasTrivialCopyConstructor() && |
| 7080 | RD->hasTrivialMoveConstructor() && |
| 7081 | RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7082 | return true; |
| 7083 | } |
| 7084 | } |
| 7085 | |
| 7086 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 7087 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 7088 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 7089 | return false; |
| 7090 | } |
| 7091 | |
| 7092 | return true; |
| 7093 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7094 | |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7095 | CallingConv ASTContext::getDefaultMethodCallConv() { |
| 7096 | // Pass through to the C++ ABI object |
| 7097 | return ABI->getDefaultMethodCallConv(); |
| 7098 | } |
| 7099 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7100 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 7101 | // Pass through to the C++ ABI object |
| 7102 | return ABI->isNearlyEmpty(RD); |
| 7103 | } |
| 7104 | |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7105 | MangleContext *ASTContext::createMangleContext() { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 7106 | switch (Target->getCXXABI()) { |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7107 | case CXXABI_ARM: |
| 7108 | case CXXABI_Itanium: |
| 7109 | return createItaniumMangleContext(*this, getDiagnostics()); |
| 7110 | case CXXABI_Microsoft: |
| 7111 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 7112 | } |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7113 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7114 | } |
| 7115 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7116 | CXXABI::~CXXABI() {} |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 7117 | |
| 7118 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Ted Kremenek | 7d39c9a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 7119 | return ASTRecordLayouts.getMemorySize() |
| 7120 | + llvm::capacity_in_bytes(ObjCLayouts) |
| 7121 | + llvm::capacity_in_bytes(KeyFunctions) |
| 7122 | + llvm::capacity_in_bytes(ObjCImpls) |
| 7123 | + llvm::capacity_in_bytes(BlockVarCopyInits) |
| 7124 | + llvm::capacity_in_bytes(DeclAttrs) |
| 7125 | + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember) |
| 7126 | + llvm::capacity_in_bytes(InstantiatedFromUsingDecl) |
| 7127 | + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) |
| 7128 | + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) |
| 7129 | + llvm::capacity_in_bytes(OverriddenMethods) |
| 7130 | + llvm::capacity_in_bytes(Types) |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7131 | + llvm::capacity_in_bytes(VariableArrayTypes) |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 7132 | + llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 7133 | } |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 7134 | |
Douglas Gregor | 6379854 | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 7135 | unsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) { |
| 7136 | CXXRecordDecl *Lambda = CallOperator->getParent(); |
| 7137 | return LambdaMangleContexts[Lambda->getDeclContext()] |
| 7138 | .getManglingNumber(CallOperator); |
| 7139 | } |
| 7140 | |
| 7141 | |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 7142 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 7143 | ParamIndices[D] = index; |
| 7144 | } |
| 7145 | |
| 7146 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 7147 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 7148 | assert(I != ParamIndices.end() && |
| 7149 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 7150 | return I->second; |
| 7151 | } |