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 | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 59 | RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 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 | |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 75 | ArrayRef<RawComment *> RawComments = Comments.getComments(); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 76 | |
| 77 | // If there are no comments anywhere, we won't find anything. |
| 78 | if (RawComments.empty()) |
| 79 | return NULL; |
| 80 | |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 81 | // Find declaration location. |
| 82 | // For Objective-C declarations we generally don't expect to have multiple |
| 83 | // declarators, thus use declaration starting location as the "declaration |
| 84 | // location". |
| 85 | // For all other declarations multiple declarators are used quite frequently, |
| 86 | // so we use the location of the identifier as the "declaration location". |
| 87 | SourceLocation DeclLoc; |
| 88 | if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) || |
| 89 | isa<ObjCPropertyDecl>(D)) |
| 90 | DeclLoc = D->getLocStart(); |
| 91 | else |
| 92 | DeclLoc = D->getLocation(); |
| 93 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 94 | // If the declaration doesn't map directly to a location in a file, we |
| 95 | // can't find the comment. |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 96 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) |
| 97 | return NULL; |
| 98 | |
| 99 | // Find the comment that occurs just after this declaration. |
Dmitri Gribenko | 82ea947 | 2012-07-17 22:01:09 +0000 | [diff] [blame] | 100 | ArrayRef<RawComment *>::iterator Comment; |
| 101 | { |
| 102 | // When searching for comments during parsing, the comment we are looking |
| 103 | // for is usually among the last two comments we parsed -- check them |
| 104 | // first. |
| 105 | RawComment CommentAtDeclLoc(SourceMgr, SourceRange(DeclLoc)); |
| 106 | BeforeThanCompare<RawComment> Compare(SourceMgr); |
| 107 | ArrayRef<RawComment *>::iterator MaybeBeforeDecl = RawComments.end() - 1; |
| 108 | bool Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); |
| 109 | if (!Found && RawComments.size() >= 2) { |
| 110 | MaybeBeforeDecl--; |
| 111 | Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); |
| 112 | } |
| 113 | |
| 114 | if (Found) { |
| 115 | Comment = MaybeBeforeDecl + 1; |
| 116 | assert(Comment == std::lower_bound(RawComments.begin(), RawComments.end(), |
| 117 | &CommentAtDeclLoc, Compare)); |
| 118 | } else { |
| 119 | // Slow path. |
| 120 | Comment = std::lower_bound(RawComments.begin(), RawComments.end(), |
| 121 | &CommentAtDeclLoc, Compare); |
| 122 | } |
| 123 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 124 | |
| 125 | // Decompose the location for the declaration and find the beginning of the |
| 126 | // file buffer. |
| 127 | std::pair<FileID, unsigned> DeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLoc); |
| 128 | |
| 129 | // First check whether we have a trailing comment. |
| 130 | if (Comment != RawComments.end() && |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 131 | (*Comment)->isDocumentation() && (*Comment)->isTrailingComment() && |
Dmitri Gribenko | 44cd7e6 | 2012-07-06 23:27:33 +0000 | [diff] [blame] | 132 | (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D))) { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 133 | std::pair<FileID, unsigned> CommentBeginDecomp |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 134 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getBegin()); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 135 | // Check that Doxygen trailing comment comes after the declaration, starts |
| 136 | // on the same line and in the same file as the declaration. |
| 137 | if (DeclLocDecomp.first == CommentBeginDecomp.first && |
| 138 | SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second) |
| 139 | == SourceMgr.getLineNumber(CommentBeginDecomp.first, |
| 140 | CommentBeginDecomp.second)) { |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 141 | return *Comment; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
| 145 | // The comment just after the declaration was not a trailing comment. |
| 146 | // Let's look at the previous comment. |
| 147 | if (Comment == RawComments.begin()) |
| 148 | return NULL; |
| 149 | --Comment; |
| 150 | |
| 151 | // Check that we actually have a non-member Doxygen comment. |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 152 | if (!(*Comment)->isDocumentation() || (*Comment)->isTrailingComment()) |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 153 | return NULL; |
| 154 | |
| 155 | // Decompose the end of the comment. |
| 156 | std::pair<FileID, unsigned> CommentEndDecomp |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 157 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getEnd()); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 158 | |
| 159 | // If the comment and the declaration aren't in the same file, then they |
| 160 | // aren't related. |
| 161 | if (DeclLocDecomp.first != CommentEndDecomp.first) |
| 162 | return NULL; |
| 163 | |
| 164 | // Get the corresponding buffer. |
| 165 | bool Invalid = false; |
| 166 | const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first, |
| 167 | &Invalid).data(); |
| 168 | if (Invalid) |
| 169 | return NULL; |
| 170 | |
| 171 | // Extract text between the comment and declaration. |
| 172 | StringRef Text(Buffer + CommentEndDecomp.second, |
| 173 | DeclLocDecomp.second - CommentEndDecomp.second); |
| 174 | |
Dmitri Gribenko | 7e8729b | 2012-06-27 23:43:37 +0000 | [diff] [blame] | 175 | // There should be no other declarations or preprocessor directives between |
| 176 | // comment and declaration. |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 177 | if (Text.find_first_of(",;{}#@") != StringRef::npos) |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 178 | return NULL; |
| 179 | |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 180 | return *Comment; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | const RawComment *ASTContext::getRawCommentForDecl(const Decl *D) const { |
| 184 | // Check whether we have cached a comment string for this declaration |
| 185 | // already. |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 186 | llvm::DenseMap<const Decl *, RawAndParsedComment>::iterator Pos |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 187 | = DeclComments.find(D); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 188 | if (Pos != DeclComments.end()) { |
| 189 | RawAndParsedComment C = Pos->second; |
| 190 | return C.first; |
| 191 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 192 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 193 | RawComment *RC = getRawCommentForDeclNoCache(D); |
Dmitri Gribenko | 5c8897d | 2012-06-28 16:25:36 +0000 | [diff] [blame] | 194 | // If we found a comment, it should be a documentation comment. |
| 195 | assert(!RC || RC->isDocumentation()); |
Dmitri Gribenko | 0863005 | 2012-07-06 15:40:08 +0000 | [diff] [blame] | 196 | DeclComments[D] = |
| 197 | RawAndParsedComment(RC, static_cast<comments::FullComment *>(NULL)); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 198 | if (RC) |
| 199 | RC->setAttached(); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 200 | return RC; |
| 201 | } |
| 202 | |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 203 | comments::FullComment *ASTContext::getCommentForDecl(const Decl *D) const { |
| 204 | llvm::DenseMap<const Decl *, RawAndParsedComment>::iterator Pos |
| 205 | = DeclComments.find(D); |
| 206 | const RawComment *RC; |
| 207 | if (Pos != DeclComments.end()) { |
| 208 | RawAndParsedComment C = Pos->second; |
| 209 | if (comments::FullComment *FC = C.second) |
| 210 | return FC; |
| 211 | RC = C.first; |
| 212 | } else |
| 213 | RC = getRawCommentForDecl(D); |
| 214 | |
| 215 | if (!RC) |
| 216 | return NULL; |
| 217 | |
| 218 | const StringRef RawText = RC->getRawText(SourceMgr); |
| 219 | comments::Lexer L(RC->getSourceRange().getBegin(), comments::CommentOptions(), |
| 220 | RawText.begin(), RawText.end()); |
| 221 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 222 | comments::Sema S(getAllocator(), getSourceManager(), getDiagnostics()); |
| 223 | S.setDecl(D); |
| 224 | comments::Parser P(L, S, getAllocator(), getSourceManager(), |
| 225 | getDiagnostics()); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 226 | |
| 227 | comments::FullComment *FC = P.parseFullComment(); |
| 228 | DeclComments[D].second = FC; |
| 229 | return FC; |
| 230 | } |
| 231 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 232 | void |
| 233 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 234 | TemplateTemplateParmDecl *Parm) { |
| 235 | ID.AddInteger(Parm->getDepth()); |
| 236 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 237 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 238 | |
| 239 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 240 | ID.AddInteger(Params->size()); |
| 241 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 242 | PEnd = Params->end(); |
| 243 | P != PEnd; ++P) { |
| 244 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 245 | ID.AddInteger(0); |
| 246 | ID.AddBoolean(TTP->isParameterPack()); |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 251 | ID.AddInteger(1); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 252 | ID.AddBoolean(NTTP->isParameterPack()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 253 | ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 254 | if (NTTP->isExpandedParameterPack()) { |
| 255 | ID.AddBoolean(true); |
| 256 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 257 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 258 | QualType T = NTTP->getExpansionType(I); |
| 259 | ID.AddPointer(T.getCanonicalType().getAsOpaquePtr()); |
| 260 | } |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 261 | } else |
| 262 | ID.AddBoolean(false); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 263 | continue; |
| 264 | } |
| 265 | |
| 266 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 267 | ID.AddInteger(2); |
| 268 | Profile(ID, TTP); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | TemplateTemplateParmDecl * |
| 273 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 274 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 275 | // Check if we already have a canonical template template parameter. |
| 276 | llvm::FoldingSetNodeID ID; |
| 277 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
| 278 | void *InsertPos = 0; |
| 279 | CanonicalTemplateTemplateParm *Canonical |
| 280 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 281 | if (Canonical) |
| 282 | return Canonical->getParam(); |
| 283 | |
| 284 | // Build a canonical template parameter list. |
| 285 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 286 | SmallVector<NamedDecl *, 4> CanonParams; |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 287 | CanonParams.reserve(Params->size()); |
| 288 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 289 | PEnd = Params->end(); |
| 290 | P != PEnd; ++P) { |
| 291 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 292 | CanonParams.push_back( |
| 293 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 294 | SourceLocation(), |
| 295 | SourceLocation(), |
| 296 | TTP->getDepth(), |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 297 | TTP->getIndex(), 0, false, |
| 298 | TTP->isParameterPack())); |
| 299 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 300 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 301 | QualType T = getCanonicalType(NTTP->getType()); |
| 302 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 303 | NonTypeTemplateParmDecl *Param; |
| 304 | if (NTTP->isExpandedParameterPack()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 305 | SmallVector<QualType, 2> ExpandedTypes; |
| 306 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 307 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 308 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 309 | ExpandedTInfos.push_back( |
| 310 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 311 | } |
| 312 | |
| 313 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 314 | SourceLocation(), |
| 315 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 316 | NTTP->getDepth(), |
| 317 | NTTP->getPosition(), 0, |
| 318 | T, |
| 319 | TInfo, |
| 320 | ExpandedTypes.data(), |
| 321 | ExpandedTypes.size(), |
| 322 | ExpandedTInfos.data()); |
| 323 | } else { |
| 324 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 325 | SourceLocation(), |
| 326 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 327 | NTTP->getDepth(), |
| 328 | NTTP->getPosition(), 0, |
| 329 | T, |
| 330 | NTTP->isParameterPack(), |
| 331 | TInfo); |
| 332 | } |
| 333 | CanonParams.push_back(Param); |
| 334 | |
| 335 | } else |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 336 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 337 | cast<TemplateTemplateParmDecl>(*P))); |
| 338 | } |
| 339 | |
| 340 | TemplateTemplateParmDecl *CanonTTP |
| 341 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 342 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 343 | TTP->getPosition(), |
| 344 | TTP->isParameterPack(), |
| 345 | 0, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 346 | TemplateParameterList::Create(*this, SourceLocation(), |
| 347 | SourceLocation(), |
| 348 | CanonParams.data(), |
| 349 | CanonParams.size(), |
| 350 | SourceLocation())); |
| 351 | |
| 352 | // Get the new insert position for the node we care about. |
| 353 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 354 | assert(Canonical == 0 && "Shouldn't be in the map!"); |
| 355 | (void)Canonical; |
| 356 | |
| 357 | // Create the canonical template template parameter entry. |
| 358 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 359 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 360 | return CanonTTP; |
| 361 | } |
| 362 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 363 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 364 | if (!LangOpts.CPlusPlus) return 0; |
| 365 | |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 366 | switch (T.getCXXABI()) { |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 367 | case CXXABI_ARM: |
| 368 | return CreateARMCXXABI(*this); |
| 369 | case CXXABI_Itanium: |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 370 | return CreateItaniumCXXABI(*this); |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 371 | case CXXABI_Microsoft: |
| 372 | return CreateMicrosoftCXXABI(*this); |
| 373 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 374 | llvm_unreachable("Invalid CXXABI type!"); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 377 | static const LangAS::Map *getAddressSpaceMap(const TargetInfo &T, |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 378 | const LangOptions &LOpts) { |
| 379 | if (LOpts.FakeAddressSpaceMap) { |
| 380 | // The fake address space map must have a distinct entry for each |
| 381 | // language-specific address space. |
| 382 | static const unsigned FakeAddrSpaceMap[] = { |
| 383 | 1, // opencl_global |
| 384 | 2, // opencl_local |
Peter Collingbourne | f44bdf9 | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 385 | 3, // opencl_constant |
| 386 | 4, // cuda_device |
| 387 | 5, // cuda_constant |
| 388 | 6 // cuda_shared |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 389 | }; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 390 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 391 | } else { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 392 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Douglas Gregor | 7018d5b | 2011-09-01 20:23:19 +0000 | [diff] [blame] | 396 | ASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 397 | const TargetInfo *t, |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 398 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 399 | Builtin::Context &builtins, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 400 | unsigned size_reserve, |
| 401 | bool DelayInitialization) |
| 402 | : FunctionProtoTypes(this_()), |
| 403 | TemplateSpecializationTypes(this_()), |
| 404 | DependentTemplateSpecializationTypes(this_()), |
| 405 | SubstTemplateTemplateParmPacks(this_()), |
| 406 | GlobalNestedNameSpecifier(0), |
| 407 | Int128Decl(0), UInt128Decl(0), |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 408 | BuiltinVaListDecl(0), |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 409 | ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), ObjCProtocolClassDecl(0), |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 410 | CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 411 | FILEDecl(0), |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 412 | jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0), |
| 413 | BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
| 414 | cudaConfigureCallDecl(0), |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 415 | NullTypeSourceInfo(QualType()), |
| 416 | FirstLocalImport(), LastLocalImport(), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 417 | SourceMgr(SM), LangOpts(LOpts), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 418 | AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 419 | Idents(idents), Selectors(sels), |
| 420 | BuiltinInfo(builtins), |
| 421 | DeclarationNames(*this), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 422 | ExternalSource(0), Listener(0), |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 423 | Comments(SM), CommentsLoaded(false), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 424 | LastSDM(0, 0), |
| 425 | UniqueBlockByRefTypeID(0) |
| 426 | { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 428 | TUDecl = TranslationUnitDecl::Create(*this); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 429 | |
| 430 | if (!DelayInitialization) { |
| 431 | assert(t && "No target supplied for ASTContext initialization"); |
| 432 | InitBuiltinTypes(*t); |
| 433 | } |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 436 | ASTContext::~ASTContext() { |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 437 | // Release the DenseMaps associated with DeclContext objects. |
| 438 | // FIXME: Is this the ideal solution? |
| 439 | ReleaseDeclContextMaps(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 440 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 441 | // Call all of the deallocation functions. |
| 442 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 443 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 444 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 445 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 446 | // because they can contain DenseMaps. |
| 447 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 448 | const ASTRecordLayout*>::iterator |
| 449 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 450 | // Increment in loop to prevent using deallocated memory. |
| 451 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 452 | R->Destroy(*this); |
| 453 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 454 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 455 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 456 | // Increment in loop to prevent using deallocated memory. |
| 457 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 458 | R->Destroy(*this); |
| 459 | } |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 460 | |
| 461 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 462 | AEnd = DeclAttrs.end(); |
| 463 | A != AEnd; ++A) |
| 464 | A->second->~AttrVec(); |
| 465 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 466 | |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 467 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 468 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 469 | } |
| 470 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | void |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 472 | ASTContext::setExternalSource(OwningPtr<ExternalASTSource> &Source) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 473 | ExternalSource.reset(Source.take()); |
| 474 | } |
| 475 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 476 | void ASTContext::PrintStats() const { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 477 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 478 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 479 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 480 | unsigned counts[] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 482 | #define ABSTRACT_TYPE(Name, Parent) |
| 483 | #include "clang/AST/TypeNodes.def" |
| 484 | 0 // Extra |
| 485 | }; |
Douglas Gregor | b1fe2c9 | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 486 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 487 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 488 | Type *T = Types[i]; |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 489 | counts[(unsigned)T->getTypeClass()]++; |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 492 | unsigned Idx = 0; |
| 493 | unsigned TotalBytes = 0; |
| 494 | #define TYPE(Name, Parent) \ |
| 495 | if (counts[Idx]) \ |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 496 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 497 | << " types\n"; \ |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 498 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 499 | ++Idx; |
| 500 | #define ABSTRACT_TYPE(Name, Parent) |
| 501 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 503 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 504 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 505 | // Implicit special member functions. |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 506 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 507 | << NumImplicitDefaultConstructors |
| 508 | << " implicit default constructors created\n"; |
| 509 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 510 | << NumImplicitCopyConstructors |
| 511 | << " implicit copy constructors created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 512 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 513 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 514 | << NumImplicitMoveConstructors |
| 515 | << " implicit move constructors created\n"; |
| 516 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 517 | << NumImplicitCopyAssignmentOperators |
| 518 | << " implicit copy assignment operators created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 519 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 520 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 521 | << NumImplicitMoveAssignmentOperators |
| 522 | << " implicit move assignment operators created\n"; |
| 523 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 524 | << NumImplicitDestructors |
| 525 | << " implicit destructors created\n"; |
| 526 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 527 | if (ExternalSource.get()) { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 528 | llvm::errs() << "\n"; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 529 | ExternalSource->PrintStats(); |
| 530 | } |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 531 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 532 | BumpAlloc.PrintStats(); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 535 | TypedefDecl *ASTContext::getInt128Decl() const { |
| 536 | if (!Int128Decl) { |
| 537 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty); |
| 538 | Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 539 | getTranslationUnitDecl(), |
| 540 | SourceLocation(), |
| 541 | SourceLocation(), |
| 542 | &Idents.get("__int128_t"), |
| 543 | TInfo); |
| 544 | } |
| 545 | |
| 546 | return Int128Decl; |
| 547 | } |
| 548 | |
| 549 | TypedefDecl *ASTContext::getUInt128Decl() const { |
| 550 | if (!UInt128Decl) { |
| 551 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty); |
| 552 | UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 553 | getTranslationUnitDecl(), |
| 554 | SourceLocation(), |
| 555 | SourceLocation(), |
| 556 | &Idents.get("__uint128_t"), |
| 557 | TInfo); |
| 558 | } |
| 559 | |
| 560 | return UInt128Decl; |
| 561 | } |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 562 | |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 563 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 564 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 565 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 566 | Types.push_back(Ty); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 569 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target) { |
| 570 | assert((!this->Target || this->Target == &Target) && |
| 571 | "Incorrect target reinitialization"); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 572 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 574 | this->Target = &Target; |
| 575 | |
| 576 | ABI.reset(createCXXABI(Target)); |
| 577 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
| 578 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 579 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 580 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 582 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 583 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 584 | // C99 6.2.5p3. |
Eli Friedman | 9ffd4a9 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 585 | if (LangOpts.CharIsSigned) |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 586 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 587 | else |
| 588 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 589 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 590 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 591 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 592 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 593 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 594 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 596 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 597 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 598 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 599 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 600 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 601 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 602 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 603 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 604 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 605 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 606 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 607 | |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 608 | // GNU extension, 128-bit integers. |
| 609 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 610 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 611 | |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 612 | if (LangOpts.CPlusPlus) { // C++ 3.9.1p5 |
Eli Friedman | 786d087 | 2011-04-30 19:24:24 +0000 | [diff] [blame] | 613 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 614 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 615 | else // -fshort-wchar makes wchar_t be unsigned. |
| 616 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 617 | } else // C99 |
Chris Lattner | 007cb02 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 618 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 619 | |
James Molloy | 3636554 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 620 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 621 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 622 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 623 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 624 | else // C99 |
| 625 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 626 | |
| 627 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 628 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 629 | else // C99 |
| 630 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 631 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 632 | // Placeholder type for type-dependent expressions whose type is |
| 633 | // completely unknown. No code should ever check a type against |
| 634 | // DependentTy and users should never see it; however, it is here to |
| 635 | // help diagnose failures to properly check for type-dependent |
| 636 | // expressions. |
| 637 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 638 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 639 | // Placeholder type for functions. |
| 640 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 641 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 642 | // Placeholder type for bound members. |
| 643 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 644 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 645 | // Placeholder type for pseudo-objects. |
| 646 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 647 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 648 | // "any" type; useful for debugger-like clients. |
| 649 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 650 | |
John McCall | 8a6b59a | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 651 | // Placeholder type for unbridged ARC casts. |
| 652 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 653 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 654 | // C99 6.2.5p11. |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 655 | FloatComplexTy = getComplexType(FloatTy); |
| 656 | DoubleComplexTy = getComplexType(DoubleTy); |
| 657 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 658 | |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 659 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 660 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 661 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 662 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 663 | |
| 664 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 29898f4 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 665 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 666 | SignedCharTy : BoolTy); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 667 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 668 | ObjCConstantStringType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 669 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 670 | // void * type |
| 671 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 672 | |
| 673 | // nullptr type (C++0x 2.14.7) |
| 674 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 675 | |
| 676 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 677 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 678 | |
| 679 | // Builtin type used to help define __builtin_va_list. |
| 680 | VaListTagTy = QualType(); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 681 | } |
| 682 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 683 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 684 | return SourceMgr.getDiagnostics(); |
| 685 | } |
| 686 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 687 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 688 | AttrVec *&Result = DeclAttrs[D]; |
| 689 | if (!Result) { |
| 690 | void *Mem = Allocate(sizeof(AttrVec)); |
| 691 | Result = new (Mem) AttrVec; |
| 692 | } |
| 693 | |
| 694 | return *Result; |
| 695 | } |
| 696 | |
| 697 | /// \brief Erase the attributes corresponding to the given declaration. |
| 698 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 699 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 700 | if (Pos != DeclAttrs.end()) { |
| 701 | Pos->second->~AttrVec(); |
| 702 | DeclAttrs.erase(Pos); |
| 703 | } |
| 704 | } |
| 705 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 706 | MemberSpecializationInfo * |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 707 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 708 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 709 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 710 | = InstantiatedFromStaticDataMember.find(Var); |
| 711 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 712 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 714 | return Pos->second; |
| 715 | } |
| 716 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | void |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 718 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 719 | TemplateSpecializationKind TSK, |
| 720 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 721 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 722 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 723 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 724 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 725 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 726 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 729 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 730 | const FunctionDecl *FD){ |
| 731 | assert(FD && "Specialization is 0"); |
| 732 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 733 | = ClassScopeSpecializationPattern.find(FD); |
| 734 | if (Pos == ClassScopeSpecializationPattern.end()) |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 735 | return 0; |
| 736 | |
| 737 | return Pos->second; |
| 738 | } |
| 739 | |
| 740 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 741 | FunctionDecl *Pattern) { |
| 742 | assert(FD && "Specialization is 0"); |
| 743 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 744 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 745 | } |
| 746 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 747 | NamedDecl * |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 748 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 749 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 750 | = InstantiatedFromUsingDecl.find(UUD); |
| 751 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 752 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 754 | return Pos->second; |
| 755 | } |
| 756 | |
| 757 | void |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 758 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 759 | assert((isa<UsingDecl>(Pattern) || |
| 760 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 761 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 762 | "pattern decl is not a using decl"); |
| 763 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 764 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 765 | } |
| 766 | |
| 767 | UsingShadowDecl * |
| 768 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 769 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 770 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 771 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 772 | return 0; |
| 773 | |
| 774 | return Pos->second; |
| 775 | } |
| 776 | |
| 777 | void |
| 778 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 779 | UsingShadowDecl *Pattern) { |
| 780 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 781 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 784 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 785 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 786 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 787 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 788 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 789 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 790 | return Pos->second; |
| 791 | } |
| 792 | |
| 793 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 794 | FieldDecl *Tmpl) { |
| 795 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 796 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 797 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 798 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 800 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 801 | } |
| 802 | |
Fariborz Jahanian | 595ec5d | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 803 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 804 | const FieldDecl *LastFD) const { |
| 805 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 806 | FD->getBitWidthValue(*this) == 0); |
Fariborz Jahanian | 595ec5d | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Fariborz Jahanian | eb39741 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 809 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 810 | const FieldDecl *LastFD) const { |
| 811 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 812 | FD->getBitWidthValue(*this) == 0 && |
| 813 | LastFD->getBitWidthValue(*this) != 0); |
Fariborz Jahanian | eb39741 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 816 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 817 | const FieldDecl *LastFD) const { |
| 818 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 819 | FD->getBitWidthValue(*this) && |
| 820 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Chad Rosier | f01a7dd | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 823 | bool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD, |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 824 | const FieldDecl *LastFD) const { |
| 825 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 826 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Chad Rosier | f01a7dd | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 829 | bool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD, |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 830 | const FieldDecl *LastFD) const { |
| 831 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 832 | FD->getBitWidthValue(*this)); |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 835 | ASTContext::overridden_cxx_method_iterator |
| 836 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 837 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 838 | = OverriddenMethods.find(Method); |
| 839 | if (Pos == OverriddenMethods.end()) |
| 840 | return 0; |
| 841 | |
| 842 | return Pos->second.begin(); |
| 843 | } |
| 844 | |
| 845 | ASTContext::overridden_cxx_method_iterator |
| 846 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 847 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 848 | = OverriddenMethods.find(Method); |
| 849 | if (Pos == OverriddenMethods.end()) |
| 850 | return 0; |
| 851 | |
| 852 | return Pos->second.end(); |
| 853 | } |
| 854 | |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 855 | unsigned |
| 856 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 857 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 858 | = OverriddenMethods.find(Method); |
| 859 | if (Pos == OverriddenMethods.end()) |
| 860 | return 0; |
| 861 | |
| 862 | return Pos->second.size(); |
| 863 | } |
| 864 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 865 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 866 | const CXXMethodDecl *Overridden) { |
| 867 | OverriddenMethods[Method].push_back(Overridden); |
| 868 | } |
| 869 | |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 870 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 871 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 872 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 873 | if (!FirstLocalImport) { |
| 874 | FirstLocalImport = Import; |
| 875 | LastLocalImport = Import; |
| 876 | return; |
| 877 | } |
| 878 | |
| 879 | LastLocalImport->NextLocalImport = Import; |
| 880 | LastLocalImport = Import; |
| 881 | } |
| 882 | |
Chris Lattner | 53cfe80 | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 883 | //===----------------------------------------------------------------------===// |
| 884 | // Type Sizing and Analysis |
| 885 | //===----------------------------------------------------------------------===// |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 886 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 887 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 888 | /// scalar floating point type. |
| 889 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 890 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 891 | assert(BT && "Not a floating point type!"); |
| 892 | switch (BT->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 893 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 894 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 895 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 896 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 897 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 898 | } |
| 899 | } |
| 900 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 901 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 902 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 903 | /// this method will assert on them. |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 904 | /// If @p RefAsPointee, references are treated like their underlying type |
| 905 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 906 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 907 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 908 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 909 | bool UseAlignAttrOnly = false; |
| 910 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 911 | Align = AlignFromAttr; |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 912 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 913 | // __attribute__((aligned)) can increase or decrease alignment |
| 914 | // *except* on a struct or struct member, where it only increases |
| 915 | // alignment unless 'packed' is also specified. |
| 916 | // |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 917 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 918 | // ignore that possibility; Sema should diagnose it. |
| 919 | if (isa<FieldDecl>(D)) { |
| 920 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 921 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 922 | } else { |
| 923 | UseAlignAttrOnly = true; |
| 924 | } |
| 925 | } |
Fariborz Jahanian | 9f10718 | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 926 | else if (isa<FieldDecl>(D)) |
| 927 | UseAlignAttrOnly = |
| 928 | D->hasAttr<PackedAttr>() || |
| 929 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 930 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 931 | // If we're using the align attribute only, just ignore everything |
| 932 | // else about the declaration and its type. |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 933 | if (UseAlignAttrOnly) { |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 934 | // do nothing |
| 935 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 936 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 937 | QualType T = VD->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 938 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 939 | if (RefAsPointee) |
| 940 | T = RT->getPointeeType(); |
| 941 | else |
| 942 | T = getPointerType(RT->getPointeeType()); |
| 943 | } |
| 944 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 945 | // Adjust alignments of declarations with array type by the |
| 946 | // large-array alignment on the target. |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 947 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 948 | const ArrayType *arrayType; |
| 949 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 950 | if (isa<VariableArrayType>(arrayType)) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 951 | Align = std::max(Align, Target->getLargeArrayAlign()); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 952 | else if (isa<ConstantArrayType>(arrayType) && |
| 953 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 954 | Align = std::max(Align, Target->getLargeArrayAlign()); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 955 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 956 | // Walk through any array types while we're at it. |
| 957 | T = getBaseElementType(arrayType); |
| 958 | } |
Chad Rosier | 99ee782 | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 959 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 960 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 961 | |
| 962 | // Fields can be subject to extra alignment constraints, like if |
| 963 | // the field is packed, the struct is packed, or the struct has a |
| 964 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 965 | // the actual alignment of the field within the struct, and then |
| 966 | // (as we're expected to) constrain that by the alignment of the type. |
| 967 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 968 | // So calculate the alignment of the field. |
| 969 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 970 | |
| 971 | // Start with the record's overall alignment. |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 972 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 973 | |
| 974 | // Use the GCD of that and the offset within the record. |
| 975 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 976 | if (offset > 0) { |
| 977 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 978 | // which means we get to do this crazy thing instead of Euclid's. |
| 979 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 980 | if (lowBitOfOffset < fieldAlign) |
| 981 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 982 | } |
| 983 | |
| 984 | Align = std::min(Align, fieldAlign); |
Charles Davis | 3fc5107 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 985 | } |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 986 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 987 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 988 | return toCharUnitsFromBits(Align); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 989 | } |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 990 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 991 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 992 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 993 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 994 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 995 | toCharUnitsFromBits(Info.second)); |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 999 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1000 | return getTypeInfoInChars(T.getTypePtr()); |
| 1001 | } |
| 1002 | |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1003 | std::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const { |
| 1004 | TypeInfoMap::iterator it = MemoizedTypeInfo.find(T); |
| 1005 | if (it != MemoizedTypeInfo.end()) |
| 1006 | return it->second; |
| 1007 | |
| 1008 | std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T); |
| 1009 | MemoizedTypeInfo.insert(std::make_pair(T, Info)); |
| 1010 | return Info; |
| 1011 | } |
| 1012 | |
| 1013 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 1014 | /// method does not work on incomplete types. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1015 | /// |
| 1016 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 1017 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 1018 | /// should take a QualType, &c. |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1019 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1020 | ASTContext::getTypeInfoImpl(const Type *T) const { |
Mike Stump | 5b9a3d5 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 1021 | uint64_t Width=0; |
| 1022 | unsigned Align=8; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1023 | switch (T->getTypeClass()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1024 | #define TYPE(Class, Base) |
| 1025 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1026 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1027 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1028 | #include "clang/AST/TypeNodes.def" |
John McCall | 15547bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 1029 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1030 | |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1031 | case Type::FunctionNoProto: |
| 1032 | case Type::FunctionProto: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1033 | // GCC extension: alignof(function) = 32 bits |
| 1034 | Width = 0; |
| 1035 | Align = 32; |
| 1036 | break; |
| 1037 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1038 | case Type::IncompleteArray: |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1039 | case Type::VariableArray: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1040 | Width = 0; |
| 1041 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 1042 | break; |
| 1043 | |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1044 | case Type::ConstantArray: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1045 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1046 | |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1047 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | d541a4c | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1048 | uint64_t Size = CAT->getSize().getZExtValue(); |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1049 | assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) && |
| 1050 | "Overflow in array type bit size evaluation"); |
Abramo Bagnara | d541a4c | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1051 | Width = EltInfo.first*Size; |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1052 | Align = EltInfo.second; |
Argyrios Kyrtzidis | ae40e4e | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 1053 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1054 | break; |
Christopher Lamb | c5fafa2 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 1055 | } |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1056 | case Type::ExtVector: |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1057 | case Type::Vector: { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1058 | const VectorType *VT = cast<VectorType>(T); |
| 1059 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 1060 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 3df5efe | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1061 | Align = Width; |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1062 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 1063 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | ef78c8e | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 1064 | if (Align & (Align-1)) { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1065 | Align = llvm::NextPowerOf2(Align); |
| 1066 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 1067 | } |
Chad Rosier | cc40ea7 | 2012-07-13 23:57:43 +0000 | [diff] [blame] | 1068 | // Adjust the alignment based on the target max. |
| 1069 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
| 1070 | if (TargetVectorAlign && TargetVectorAlign < Align) |
| 1071 | Align = TargetVectorAlign; |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1072 | break; |
| 1073 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1074 | |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1075 | case Type::Builtin: |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1076 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1077 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1078 | case BuiltinType::Void: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1079 | // GCC extension: alignof(void) = 8 bits. |
| 1080 | Width = 0; |
| 1081 | Align = 8; |
| 1082 | break; |
| 1083 | |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1084 | case BuiltinType::Bool: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1085 | Width = Target->getBoolWidth(); |
| 1086 | Align = Target->getBoolAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1087 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1088 | case BuiltinType::Char_S: |
| 1089 | case BuiltinType::Char_U: |
| 1090 | case BuiltinType::UChar: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1091 | case BuiltinType::SChar: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1092 | Width = Target->getCharWidth(); |
| 1093 | Align = Target->getCharAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1094 | break; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1095 | case BuiltinType::WChar_S: |
| 1096 | case BuiltinType::WChar_U: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1097 | Width = Target->getWCharWidth(); |
| 1098 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1099 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1100 | case BuiltinType::Char16: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1101 | Width = Target->getChar16Width(); |
| 1102 | Align = Target->getChar16Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1103 | break; |
| 1104 | case BuiltinType::Char32: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1105 | Width = Target->getChar32Width(); |
| 1106 | Align = Target->getChar32Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1107 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1108 | case BuiltinType::UShort: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1109 | case BuiltinType::Short: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1110 | Width = Target->getShortWidth(); |
| 1111 | Align = Target->getShortAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1112 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1113 | case BuiltinType::UInt: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1114 | case BuiltinType::Int: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1115 | Width = Target->getIntWidth(); |
| 1116 | Align = Target->getIntAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1117 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1118 | case BuiltinType::ULong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1119 | case BuiltinType::Long: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1120 | Width = Target->getLongWidth(); |
| 1121 | Align = Target->getLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1122 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1123 | case BuiltinType::ULongLong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1124 | case BuiltinType::LongLong: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1125 | Width = Target->getLongLongWidth(); |
| 1126 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1127 | break; |
Chris Lattner | 0a415ec | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 1128 | case BuiltinType::Int128: |
| 1129 | case BuiltinType::UInt128: |
| 1130 | Width = 128; |
| 1131 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 1132 | break; |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1133 | case BuiltinType::Half: |
| 1134 | Width = Target->getHalfWidth(); |
| 1135 | Align = Target->getHalfAlign(); |
| 1136 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1137 | case BuiltinType::Float: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1138 | Width = Target->getFloatWidth(); |
| 1139 | Align = Target->getFloatAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1140 | break; |
| 1141 | case BuiltinType::Double: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1142 | Width = Target->getDoubleWidth(); |
| 1143 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1144 | break; |
| 1145 | case BuiltinType::LongDouble: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1146 | Width = Target->getLongDoubleWidth(); |
| 1147 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1148 | break; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1149 | case BuiltinType::NullPtr: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1150 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 1151 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | a81b0b7 | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 1152 | break; |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1153 | case BuiltinType::ObjCId: |
| 1154 | case BuiltinType::ObjCClass: |
| 1155 | case BuiltinType::ObjCSel: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1156 | Width = Target->getPointerWidth(0); |
| 1157 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1158 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1159 | } |
Chris Lattner | 48f84b8 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 1160 | break; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1161 | case Type::ObjCObjectPointer: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1162 | Width = Target->getPointerWidth(0); |
| 1163 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1164 | break; |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1165 | case Type::BlockPointer: { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1166 | unsigned AS = getTargetAddressSpace( |
| 1167 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1168 | Width = Target->getPointerWidth(AS); |
| 1169 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1170 | break; |
| 1171 | } |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1172 | case Type::LValueReference: |
| 1173 | case Type::RValueReference: { |
| 1174 | // alignof and sizeof should never enter this code path here, so we go |
| 1175 | // the pointer route. |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1176 | unsigned AS = getTargetAddressSpace( |
| 1177 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1178 | Width = Target->getPointerWidth(AS); |
| 1179 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1180 | break; |
| 1181 | } |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1182 | case Type::Pointer: { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1183 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1184 | Width = Target->getPointerWidth(AS); |
| 1185 | Align = Target->getPointerAlign(AS); |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1186 | break; |
| 1187 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1188 | case Type::MemberPointer: { |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1189 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1190 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1191 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1192 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1193 | Align = PtrDiffInfo.second; |
| 1194 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1195 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1196 | case Type::Complex: { |
| 1197 | // Complex types have the same alignment as their elements, but twice the |
| 1198 | // size. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1200 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1201 | Width = EltInfo.first*2; |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1202 | Align = EltInfo.second; |
| 1203 | break; |
| 1204 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1205 | case Type::ObjCObject: |
| 1206 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1207 | case Type::ObjCInterface: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1208 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1209 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1210 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1211 | Align = toBits(Layout.getAlignment()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1212 | break; |
| 1213 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1214 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1215 | case Type::Enum: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1216 | const TagType *TT = cast<TagType>(T); |
| 1217 | |
| 1218 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 7f97189 | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1219 | Width = 8; |
| 1220 | Align = 8; |
Chris Lattner | 572100b | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1221 | break; |
| 1222 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1224 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1225 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 1226 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1227 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1228 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1229 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1230 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | 49a953a | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1231 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1232 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1233 | |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1234 | case Type::SubstTemplateTypeParm: |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1235 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1236 | getReplacementType().getTypePtr()); |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1237 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1238 | case Type::Auto: { |
| 1239 | const AutoType *A = cast<AutoType>(T); |
| 1240 | assert(A->isDeduced() && "Cannot request the size of a dependent type"); |
Matt Beaumont-Gay | 7a24210e | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1241 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1244 | case Type::Paren: |
| 1245 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1246 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1247 | case Type::Typedef: { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1248 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | f5bae22 | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1249 | std::pair<uint64_t, unsigned> Info |
| 1250 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1251 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1252 | // alignment we have. This violates the GCC documentation (which says that |
| 1253 | // attribute(aligned) can only round up) but matches its implementation. |
| 1254 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 1255 | Align = AttrAlign; |
| 1256 | else |
| 1257 | Align = Info.second; |
Douglas Gregor | f5bae22 | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1258 | Width = Info.first; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1259 | break; |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1260 | } |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1261 | |
| 1262 | case Type::TypeOfExpr: |
| 1263 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 1264 | .getTypePtr()); |
| 1265 | |
| 1266 | case Type::TypeOf: |
| 1267 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 1268 | |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1269 | case Type::Decltype: |
| 1270 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 1271 | .getTypePtr()); |
| 1272 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1273 | case Type::UnaryTransform: |
| 1274 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 1275 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1276 | case Type::Elaborated: |
| 1277 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1279 | case Type::Attributed: |
| 1280 | return getTypeInfo( |
| 1281 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1282 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1283 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1284 | assert(getCanonicalType(T) != T && |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1285 | "Cannot request the size of a dependent type"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1286 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1287 | // A type alias template specialization may refer to a typedef with the |
| 1288 | // aligned attribute on it. |
| 1289 | if (TST->isTypeAlias()) |
| 1290 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1291 | else |
| 1292 | return getTypeInfo(getCanonicalType(T)); |
| 1293 | } |
| 1294 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1295 | case Type::Atomic: { |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1296 | std::pair<uint64_t, unsigned> Info |
| 1297 | = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1298 | Width = Info.first; |
| 1299 | Align = Info.second; |
| 1300 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth() && |
| 1301 | llvm::isPowerOf2_64(Width)) { |
| 1302 | // We can potentially perform lock-free atomic operations for this |
| 1303 | // type; promote the alignment appropriately. |
| 1304 | // FIXME: We could potentially promote the width here as well... |
| 1305 | // is that worthwhile? (Non-struct atomic types generally have |
| 1306 | // power-of-two size anyway, but structs might not. Requires a bit |
| 1307 | // of implementation work to make sure we zero out the extra bits.) |
| 1308 | Align = static_cast<unsigned>(Width); |
| 1309 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1312 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1314 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1315 | return std::make_pair(Width, Align); |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1318 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1319 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1320 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1321 | } |
| 1322 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1323 | /// toBits - Convert a size in characters to a size in characters. |
| 1324 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1325 | return CharSize.getQuantity() * getCharWidth(); |
| 1326 | } |
| 1327 | |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1328 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1329 | /// This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1330 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1331 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1332 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1333 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1334 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Ken Dyck | a6046ab | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1337 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1338 | /// characters. This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1339 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1340 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1341 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1342 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1343 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1346 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1347 | /// type for the current target in bits. This can be different than the ABI |
| 1348 | /// alignment in cases where it is beneficial for performance to overalign |
| 1349 | /// a data type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1350 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1351 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1352 | |
| 1353 | // Double and long long should be naturally aligned if possible. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1354 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1355 | T = CT->getElementType().getTypePtr(); |
| 1356 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | b57321a | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 1357 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 1358 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1359 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1360 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1361 | return ABIAlign; |
| 1362 | } |
| 1363 | |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1364 | /// DeepCollectObjCIvars - |
| 1365 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1366 | /// super class and then collects all ivars, including those synthesized for |
| 1367 | /// current class. This routine is used for implementation of current class |
| 1368 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1369 | /// |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1370 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1371 | bool leafClass, |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1372 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1373 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1374 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1375 | if (!leafClass) { |
| 1376 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1377 | E = OI->ivar_end(); I != E; ++I) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1378 | Ivars.push_back(*I); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1379 | } else { |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1380 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1381 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1382 | Iv= Iv->getNextIvar()) |
| 1383 | Ivars.push_back(Iv); |
| 1384 | } |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1387 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1388 | /// those inherited by it. |
| 1389 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1390 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1391 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1392 | // We can use protocol_iterator here instead of |
| 1393 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1394 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1395 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1396 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1397 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1398 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1399 | PE = Proto->protocol_end(); P != PE; ++P) { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1400 | Protocols.insert((*P)->getCanonicalDecl()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1401 | CollectInheritedProtocols(*P, Protocols); |
| 1402 | } |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1403 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1404 | |
| 1405 | // Categories of this Interface. |
| 1406 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 1407 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 1408 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 1409 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1410 | while (SD) { |
| 1411 | CollectInheritedProtocols(SD, Protocols); |
| 1412 | SD = SD->getSuperClass(); |
| 1413 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1414 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1415 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1416 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1417 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1418 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1419 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1420 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1421 | CollectInheritedProtocols(*P, Protocols); |
| 1422 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1423 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1424 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1425 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1426 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1427 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1428 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1429 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1430 | CollectInheritedProtocols(*P, Protocols); |
| 1431 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1432 | } |
| 1433 | } |
| 1434 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1435 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1436 | unsigned count = 0; |
| 1437 | // Count ivars declared in class extension. |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1438 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 1439 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1440 | count += CDecl->ivar_size(); |
| 1441 | |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1442 | // Count ivar defined in this class's implementation. This |
| 1443 | // includes synthesized ivars. |
| 1444 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1445 | count += ImplDecl->ivar_size(); |
| 1446 | |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1447 | return count; |
| 1448 | } |
| 1449 | |
Argyrios Kyrtzidis | 2e809ce | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 1450 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 1451 | if (!E) |
| 1452 | return false; |
| 1453 | |
| 1454 | // nullptr_t is always treated as null. |
| 1455 | if (E->getType()->isNullPtrType()) return true; |
| 1456 | |
| 1457 | if (E->getType()->isAnyPointerType() && |
| 1458 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 1459 | Expr::NPC_ValueDependentIsNull)) |
| 1460 | return true; |
| 1461 | |
| 1462 | // Unfortunately, __null has type 'int'. |
| 1463 | if (isa<GNUNullExpr>(E)) return true; |
| 1464 | |
| 1465 | return false; |
| 1466 | } |
| 1467 | |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1468 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1469 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1470 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1471 | I = ObjCImpls.find(D); |
| 1472 | if (I != ObjCImpls.end()) |
| 1473 | return cast<ObjCImplementationDecl>(I->second); |
| 1474 | return 0; |
| 1475 | } |
| 1476 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1477 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1478 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1479 | I = ObjCImpls.find(D); |
| 1480 | if (I != ObjCImpls.end()) |
| 1481 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1482 | return 0; |
| 1483 | } |
| 1484 | |
| 1485 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1486 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1487 | ObjCImplementationDecl *ImplD) { |
| 1488 | assert(IFaceD && ImplD && "Passed null params"); |
| 1489 | ObjCImpls[IFaceD] = ImplD; |
| 1490 | } |
| 1491 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1492 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1493 | ObjCCategoryImplDecl *ImplD) { |
| 1494 | assert(CatD && ImplD && "Passed null params"); |
| 1495 | ObjCImpls[CatD] = ImplD; |
| 1496 | } |
| 1497 | |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1498 | ObjCInterfaceDecl *ASTContext::getObjContainingInterface(NamedDecl *ND) const { |
| 1499 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
| 1500 | return ID; |
| 1501 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
| 1502 | return CD->getClassInterface(); |
| 1503 | if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
| 1504 | return IMD->getClassInterface(); |
| 1505 | |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1509 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1510 | /// none exists. |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1511 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1512 | assert(VD && "Passed null params"); |
| 1513 | assert(VD->hasAttr<BlocksAttr>() && |
| 1514 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1515 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1516 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1517 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1518 | } |
| 1519 | |
| 1520 | /// \brief Set the copy inialization expression of a block var decl. |
| 1521 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1522 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1523 | assert(VD->hasAttr<BlocksAttr>() && |
| 1524 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1525 | BlockVarCopyInits[VD] = Init; |
| 1526 | } |
| 1527 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1528 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1529 | unsigned DataSize) const { |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1530 | if (!DataSize) |
| 1531 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1532 | else |
| 1533 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1534 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1535 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1536 | TypeSourceInfo *TInfo = |
| 1537 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1538 | new (TInfo) TypeSourceInfo(T); |
| 1539 | return TInfo; |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1542 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1543 | SourceLocation L) const { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1544 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | 2d525f0 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1545 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | 3665e00 | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1546 | return DI; |
| 1547 | } |
| 1548 | |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1549 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1550 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1551 | return getObjCLayout(D, 0); |
| 1552 | } |
| 1553 | |
| 1554 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1555 | ASTContext::getASTObjCImplementationLayout( |
| 1556 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1557 | return getObjCLayout(D->getClassInterface(), D); |
| 1558 | } |
| 1559 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1560 | //===----------------------------------------------------------------------===// |
| 1561 | // Type creation/memoization methods |
| 1562 | //===----------------------------------------------------------------------===// |
| 1563 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1564 | QualType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1565 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1566 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1567 | quals.removeFastQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1568 | |
| 1569 | // Check if we've already instantiated this type. |
| 1570 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1571 | ExtQuals::Profile(ID, baseType, quals); |
| 1572 | void *insertPos = 0; |
| 1573 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1574 | assert(eq->getQualifiers() == quals); |
| 1575 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1578 | // If the base type is not canonical, make the appropriate canonical type. |
| 1579 | QualType canon; |
| 1580 | if (!baseType->isCanonicalUnqualified()) { |
| 1581 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1582 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 1583 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1584 | |
| 1585 | // Re-find the insert position. |
| 1586 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1587 | } |
| 1588 | |
| 1589 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1590 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1591 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1594 | QualType |
| 1595 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1596 | QualType CanT = getCanonicalType(T); |
| 1597 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | 445fcab | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1598 | return T; |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1599 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1600 | // If we are composing extended qualifiers together, merge together |
| 1601 | // into one ExtQuals node. |
| 1602 | QualifierCollector Quals; |
| 1603 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1605 | // If this type already has an address space specified, it cannot get |
| 1606 | // another one. |
| 1607 | assert(!Quals.hasAddressSpace() && |
| 1608 | "Type cannot be in multiple addr spaces!"); |
| 1609 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1610 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1611 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | 025b5fb | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1614 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1615 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1616 | QualType CanT = getCanonicalType(T); |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1617 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1618 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1620 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1621 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1622 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | b68215c | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1623 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1624 | return getPointerType(ResultType); |
| 1625 | } |
| 1626 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1627 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1628 | // If we are composing extended qualifiers together, merge together |
| 1629 | // into one ExtQuals node. |
| 1630 | QualifierCollector Quals; |
| 1631 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1633 | // If this type already has an ObjCGC specified, it cannot get |
| 1634 | // another one. |
| 1635 | assert(!Quals.hasObjCGCAttr() && |
| 1636 | "Type cannot have multiple ObjCGCs!"); |
| 1637 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1638 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1639 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1640 | } |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1641 | |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1642 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1643 | FunctionType::ExtInfo Info) { |
| 1644 | if (T->getExtInfo() == Info) |
| 1645 | return T; |
| 1646 | |
| 1647 | QualType Result; |
| 1648 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1649 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1650 | } else { |
| 1651 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1652 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1653 | EPI.ExtInfo = Info; |
| 1654 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1655 | FPT->getNumArgs(), EPI); |
| 1656 | } |
| 1657 | |
| 1658 | return cast<FunctionType>(Result.getTypePtr()); |
| 1659 | } |
| 1660 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1661 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1662 | /// number with the specified element type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1663 | QualType ASTContext::getComplexType(QualType T) const { |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1664 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1665 | // structure. |
| 1666 | llvm::FoldingSetNodeID ID; |
| 1667 | ComplexType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1669 | void *InsertPos = 0; |
| 1670 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1671 | return QualType(CT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1672 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1673 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1674 | // so fill in the canonical type field. |
| 1675 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1676 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1677 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1679 | // Get the new insert position for the node we care about. |
| 1680 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1681 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1682 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1683 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1684 | Types.push_back(New); |
| 1685 | ComplexTypes.InsertNode(New, InsertPos); |
| 1686 | return QualType(New, 0); |
| 1687 | } |
| 1688 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1689 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1690 | /// the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1691 | QualType ASTContext::getPointerType(QualType T) const { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 1692 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1693 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1694 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1695 | PointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1697 | void *InsertPos = 0; |
| 1698 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1699 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1700 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 1701 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1702 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1703 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1704 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1705 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1707 | // Get the new insert position for the node we care about. |
| 1708 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1709 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1710 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1711 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1712 | Types.push_back(New); |
| 1713 | PointerTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1714 | return QualType(New, 0); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1717 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1718 | /// a pointer to the specified block. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1719 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 0ac01283 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1720 | assert(T->isFunctionType() && "block of function types only"); |
| 1721 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1722 | // structure. |
| 1723 | llvm::FoldingSetNodeID ID; |
| 1724 | BlockPointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1725 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1726 | void *InsertPos = 0; |
| 1727 | if (BlockPointerType *PT = |
| 1728 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1729 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1730 | |
| 1731 | // 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] | 1732 | // type either so fill in the canonical type field. |
| 1733 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1734 | if (!T.isCanonical()) { |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1735 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1736 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1737 | // Get the new insert position for the node we care about. |
| 1738 | BlockPointerType *NewIP = |
| 1739 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1740 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1741 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1742 | BlockPointerType *New |
| 1743 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1744 | Types.push_back(New); |
| 1745 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1746 | return QualType(New, 0); |
| 1747 | } |
| 1748 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1749 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1750 | /// lvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1751 | QualType |
| 1752 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 291e8ee | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 1753 | assert(getCanonicalType(T) != OverloadTy && |
| 1754 | "Unresolved overloaded function type"); |
| 1755 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1756 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1757 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1758 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1759 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1760 | |
| 1761 | void *InsertPos = 0; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1762 | if (LValueReferenceType *RT = |
| 1763 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1764 | return QualType(RT, 0); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1765 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1766 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1767 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1768 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1769 | // either, so fill in the canonical type field. |
| 1770 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1771 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1772 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1773 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1774 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1775 | // Get the new insert position for the node we care about. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1776 | LValueReferenceType *NewIP = |
| 1777 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1778 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1781 | LValueReferenceType *New |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1782 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1783 | SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1784 | Types.push_back(New); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1785 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1786 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1787 | return QualType(New, 0); |
| 1788 | } |
| 1789 | |
| 1790 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1791 | /// rvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1792 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1793 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1794 | // structure. |
| 1795 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1796 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1797 | |
| 1798 | void *InsertPos = 0; |
| 1799 | if (RValueReferenceType *RT = |
| 1800 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1801 | return QualType(RT, 0); |
| 1802 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1803 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1804 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1805 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1806 | // either, so fill in the canonical type field. |
| 1807 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1808 | if (InnerRef || !T.isCanonical()) { |
| 1809 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1810 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1811 | |
| 1812 | // Get the new insert position for the node we care about. |
| 1813 | RValueReferenceType *NewIP = |
| 1814 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1815 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1818 | RValueReferenceType *New |
| 1819 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1820 | Types.push_back(New); |
| 1821 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1822 | return QualType(New, 0); |
| 1823 | } |
| 1824 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1825 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1826 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1827 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1828 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1829 | // structure. |
| 1830 | llvm::FoldingSetNodeID ID; |
| 1831 | MemberPointerType::Profile(ID, T, Cls); |
| 1832 | |
| 1833 | void *InsertPos = 0; |
| 1834 | if (MemberPointerType *PT = |
| 1835 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1836 | return QualType(PT, 0); |
| 1837 | |
| 1838 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1839 | // type either, so fill in the canonical type field. |
| 1840 | QualType Canonical; |
Douglas Gregor | 615ac67 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1841 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1842 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1843 | |
| 1844 | // Get the new insert position for the node we care about. |
| 1845 | MemberPointerType *NewIP = |
| 1846 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1847 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1848 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1849 | MemberPointerType *New |
| 1850 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1851 | Types.push_back(New); |
| 1852 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1853 | return QualType(New, 0); |
| 1854 | } |
| 1855 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1857 | /// array of the specified element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1858 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1859 | const llvm::APInt &ArySizeIn, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1860 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1861 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 2dfdb82 | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1862 | assert((EltTy->isDependentType() || |
| 1863 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1864 | "Constant array of VLAs is illegal!"); |
| 1865 | |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1866 | // Convert the array size into a canonical width matching the pointer size for |
| 1867 | // the target. |
| 1868 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1869 | ArySize = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1870 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1871 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1872 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1873 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1874 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1875 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1876 | if (ConstantArrayType *ATP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1877 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1878 | return QualType(ATP, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1880 | // If the element type isn't canonical or has qualifiers, this won't |
| 1881 | // be a canonical type either, so fill in the canonical type field. |
| 1882 | QualType Canon; |
| 1883 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1884 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1885 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1886 | ASM, IndexTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1887 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1888 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1889 | // Get the new insert position for the node we care about. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1890 | ConstantArrayType *NewIP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1891 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1892 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1893 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1894 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1895 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1896 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1897 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1898 | Types.push_back(New); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1899 | return QualType(New, 0); |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1902 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 1903 | /// variably-modified, into the corresponding type with all the known |
| 1904 | /// sizes replaced with [*]. |
| 1905 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 1906 | // Vastly most common case. |
| 1907 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1908 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1909 | QualType result; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1910 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1911 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1912 | const Type *ty = split.Ty; |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1913 | switch (ty->getTypeClass()) { |
| 1914 | #define TYPE(Class, Base) |
| 1915 | #define ABSTRACT_TYPE(Class, Base) |
| 1916 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1917 | #include "clang/AST/TypeNodes.def" |
| 1918 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 1919 | |
| 1920 | // These types should never be variably-modified. |
| 1921 | case Type::Builtin: |
| 1922 | case Type::Complex: |
| 1923 | case Type::Vector: |
| 1924 | case Type::ExtVector: |
| 1925 | case Type::DependentSizedExtVector: |
| 1926 | case Type::ObjCObject: |
| 1927 | case Type::ObjCInterface: |
| 1928 | case Type::ObjCObjectPointer: |
| 1929 | case Type::Record: |
| 1930 | case Type::Enum: |
| 1931 | case Type::UnresolvedUsing: |
| 1932 | case Type::TypeOfExpr: |
| 1933 | case Type::TypeOf: |
| 1934 | case Type::Decltype: |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1935 | case Type::UnaryTransform: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1936 | case Type::DependentName: |
| 1937 | case Type::InjectedClassName: |
| 1938 | case Type::TemplateSpecialization: |
| 1939 | case Type::DependentTemplateSpecialization: |
| 1940 | case Type::TemplateTypeParm: |
| 1941 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1942 | case Type::Auto: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1943 | case Type::PackExpansion: |
| 1944 | llvm_unreachable("type should never be variably-modified"); |
| 1945 | |
| 1946 | // These types can be variably-modified but should never need to |
| 1947 | // further decay. |
| 1948 | case Type::FunctionNoProto: |
| 1949 | case Type::FunctionProto: |
| 1950 | case Type::BlockPointer: |
| 1951 | case Type::MemberPointer: |
| 1952 | return type; |
| 1953 | |
| 1954 | // These types can be variably-modified. All these modifications |
| 1955 | // preserve structure except as noted by comments. |
| 1956 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 1957 | // optimizations available here. |
| 1958 | case Type::Pointer: |
| 1959 | result = getPointerType(getVariableArrayDecayedType( |
| 1960 | cast<PointerType>(ty)->getPointeeType())); |
| 1961 | break; |
| 1962 | |
| 1963 | case Type::LValueReference: { |
| 1964 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 1965 | result = getLValueReferenceType( |
| 1966 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 1967 | lv->isSpelledAsLValue()); |
| 1968 | break; |
| 1969 | } |
| 1970 | |
| 1971 | case Type::RValueReference: { |
| 1972 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 1973 | result = getRValueReferenceType( |
| 1974 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 1975 | break; |
| 1976 | } |
| 1977 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1978 | case Type::Atomic: { |
| 1979 | const AtomicType *at = cast<AtomicType>(ty); |
| 1980 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 1981 | break; |
| 1982 | } |
| 1983 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1984 | case Type::ConstantArray: { |
| 1985 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 1986 | result = getConstantArrayType( |
| 1987 | getVariableArrayDecayedType(cat->getElementType()), |
| 1988 | cat->getSize(), |
| 1989 | cat->getSizeModifier(), |
| 1990 | cat->getIndexTypeCVRQualifiers()); |
| 1991 | break; |
| 1992 | } |
| 1993 | |
| 1994 | case Type::DependentSizedArray: { |
| 1995 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 1996 | result = getDependentSizedArrayType( |
| 1997 | getVariableArrayDecayedType(dat->getElementType()), |
| 1998 | dat->getSizeExpr(), |
| 1999 | dat->getSizeModifier(), |
| 2000 | dat->getIndexTypeCVRQualifiers(), |
| 2001 | dat->getBracketsRange()); |
| 2002 | break; |
| 2003 | } |
| 2004 | |
| 2005 | // Turn incomplete types into [*] types. |
| 2006 | case Type::IncompleteArray: { |
| 2007 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 2008 | result = getVariableArrayType( |
| 2009 | getVariableArrayDecayedType(iat->getElementType()), |
| 2010 | /*size*/ 0, |
| 2011 | ArrayType::Normal, |
| 2012 | iat->getIndexTypeCVRQualifiers(), |
| 2013 | SourceRange()); |
| 2014 | break; |
| 2015 | } |
| 2016 | |
| 2017 | // Turn VLA types into [*] types. |
| 2018 | case Type::VariableArray: { |
| 2019 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 2020 | result = getVariableArrayType( |
| 2021 | getVariableArrayDecayedType(vat->getElementType()), |
| 2022 | /*size*/ 0, |
| 2023 | ArrayType::Star, |
| 2024 | vat->getIndexTypeCVRQualifiers(), |
| 2025 | vat->getBracketsRange()); |
| 2026 | break; |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | // Apply the top-level qualifiers from the original. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2031 | return getQualifiedType(result, split.Quals); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2032 | } |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2033 | |
Steve Naroff | cadebd0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 2034 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 2035 | /// variable array of the specified element type. |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2036 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 2037 | Expr *NumElts, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2038 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2039 | unsigned IndexTypeQuals, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2040 | SourceRange Brackets) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2041 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 2042 | // that have an expression provided for their size. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2043 | QualType Canon; |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2044 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2045 | // Be sure to pull qualifiers off the element type. |
| 2046 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2047 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2048 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2049 | IndexTypeQuals, Brackets); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2050 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2053 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2054 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2055 | |
| 2056 | VariableArrayTypes.push_back(New); |
| 2057 | Types.push_back(New); |
| 2058 | return QualType(New, 0); |
| 2059 | } |
| 2060 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2061 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 2062 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 2063 | /// type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2064 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 2065 | Expr *numElements, |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2066 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2067 | unsigned elementTypeQuals, |
| 2068 | SourceRange brackets) const { |
| 2069 | assert((!numElements || numElements->isTypeDependent() || |
| 2070 | numElements->isValueDependent()) && |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2071 | "Size must be type- or value-dependent!"); |
| 2072 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2073 | // Dependently-sized array types that do not have a specified number |
| 2074 | // of elements will have their sizes deduced from a dependent |
| 2075 | // initializer. We do no canonicalization here at all, which is okay |
| 2076 | // because they can't be used in most locations. |
| 2077 | if (!numElements) { |
| 2078 | DependentSizedArrayType *newType |
| 2079 | = new (*this, TypeAlignment) |
| 2080 | DependentSizedArrayType(*this, elementType, QualType(), |
| 2081 | numElements, ASM, elementTypeQuals, |
| 2082 | brackets); |
| 2083 | Types.push_back(newType); |
| 2084 | return QualType(newType, 0); |
| 2085 | } |
| 2086 | |
| 2087 | // Otherwise, we actually build a new type every time, but we |
| 2088 | // also build a canonical type. |
| 2089 | |
| 2090 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 2091 | |
| 2092 | void *insertPos = 0; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2093 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2094 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2095 | QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2096 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2097 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2098 | // Look for an existing type with these properties. |
| 2099 | DependentSizedArrayType *canonTy = |
| 2100 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2101 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2102 | // If we don't have one, build one. |
| 2103 | if (!canonTy) { |
| 2104 | canonTy = new (*this, TypeAlignment) |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2105 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2106 | QualType(), numElements, ASM, elementTypeQuals, |
| 2107 | brackets); |
| 2108 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 2109 | Types.push_back(canonTy); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2112 | // Apply qualifiers from the element type to the array. |
| 2113 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2114 | canonElementType.Quals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2115 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2116 | // If we didn't need extra canonicalization for the element type, |
| 2117 | // then just use that as our result. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2118 | if (QualType(canonElementType.Ty, 0) == elementType) |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2119 | return canon; |
| 2120 | |
| 2121 | // Otherwise, we need to build a type which follows the spelling |
| 2122 | // of the element type. |
| 2123 | DependentSizedArrayType *sugaredType |
| 2124 | = new (*this, TypeAlignment) |
| 2125 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 2126 | ASM, elementTypeQuals, brackets); |
| 2127 | Types.push_back(sugaredType); |
| 2128 | return QualType(sugaredType, 0); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2131 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2132 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2133 | unsigned elementTypeQuals) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2134 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2135 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2136 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2137 | void *insertPos = 0; |
| 2138 | if (IncompleteArrayType *iat = |
| 2139 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 2140 | return QualType(iat, 0); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2141 | |
| 2142 | // 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] | 2143 | // either, so fill in the canonical type field. We also have to pull |
| 2144 | // qualifiers off the element type. |
| 2145 | QualType canon; |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2146 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2147 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 2148 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2149 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2150 | ASM, elementTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2151 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2152 | |
| 2153 | // Get the new insert position for the node we care about. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2154 | IncompleteArrayType *existing = |
| 2155 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 2156 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 843ebedd | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 2157 | } |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2158 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2159 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 2160 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2161 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2162 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 2163 | Types.push_back(newType); |
| 2164 | return QualType(newType, 0); |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2167 | /// getVectorType - Return the unique reference to a vector type of |
| 2168 | /// 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] | 2169 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2170 | VectorType::VectorKind VecKind) const { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2171 | assert(vecType->isBuiltinType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2173 | // Check if we've already instantiated a vector of this type. |
| 2174 | llvm::FoldingSetNodeID ID; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2175 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2176 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2177 | void *InsertPos = 0; |
| 2178 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2179 | return QualType(VTP, 0); |
| 2180 | |
| 2181 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2182 | // so fill in the canonical type field. |
| 2183 | QualType Canonical; |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 2184 | if (!vecType.isCanonical()) { |
Bob Wilson | 7795480 | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 2185 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2186 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2187 | // Get the new insert position for the node we care about. |
| 2188 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2189 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2190 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2191 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2192 | VectorType(vecType, NumElts, Canonical, VecKind); |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2193 | VectorTypes.InsertNode(New, InsertPos); |
| 2194 | Types.push_back(New); |
| 2195 | return QualType(New, 0); |
| 2196 | } |
| 2197 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2198 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2199 | /// 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] | 2200 | QualType |
| 2201 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2202 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2203 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2204 | // Check if we've already instantiated a vector of this type. |
| 2205 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2206 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2207 | VectorType::GenericVector); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2208 | void *InsertPos = 0; |
| 2209 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2210 | return QualType(VTP, 0); |
| 2211 | |
| 2212 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2213 | // so fill in the canonical type field. |
| 2214 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2215 | if (!vecType.isCanonical()) { |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2216 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2217 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2218 | // Get the new insert position for the node we care about. |
| 2219 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2220 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2221 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2222 | ExtVectorType *New = new (*this, TypeAlignment) |
| 2223 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2224 | VectorTypes.InsertNode(New, InsertPos); |
| 2225 | Types.push_back(New); |
| 2226 | return QualType(New, 0); |
| 2227 | } |
| 2228 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2229 | QualType |
| 2230 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 2231 | Expr *SizeExpr, |
| 2232 | SourceLocation AttrLoc) const { |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2233 | llvm::FoldingSetNodeID ID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2234 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2235 | SizeExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2236 | |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2237 | void *InsertPos = 0; |
| 2238 | DependentSizedExtVectorType *Canon |
| 2239 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2240 | DependentSizedExtVectorType *New; |
| 2241 | if (Canon) { |
| 2242 | // We already have a canonical version of this array type; use it as |
| 2243 | // the canonical type for a newly-built type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2244 | New = new (*this, TypeAlignment) |
| 2245 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2246 | SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2247 | } else { |
| 2248 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2249 | if (CanonVecTy == vecType) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2250 | New = new (*this, TypeAlignment) |
| 2251 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2252 | AttrLoc); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2253 | |
| 2254 | DependentSizedExtVectorType *CanonCheck |
| 2255 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2256 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2257 | (void)CanonCheck; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2258 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2259 | } else { |
| 2260 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2261 | SourceLocation()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2262 | New = new (*this, TypeAlignment) |
| 2263 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2264 | } |
| 2265 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2266 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2267 | Types.push_back(New); |
| 2268 | return QualType(New, 0); |
| 2269 | } |
| 2270 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2271 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2272 | /// |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2273 | QualType |
| 2274 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2275 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2276 | const CallingConv DefaultCC = Info.getCC(); |
| 2277 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2278 | CC_X86StdCall : DefaultCC; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2279 | // Unique functions, to guarantee there is only one function of a particular |
| 2280 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2281 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2282 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2283 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2284 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2285 | if (FunctionNoProtoType *FT = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2286 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2287 | return QualType(FT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2288 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2289 | QualType Canonical; |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 2290 | if (!ResultTy.isCanonical() || |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2291 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2292 | Canonical = |
| 2293 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 2294 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2295 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2296 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2297 | FunctionNoProtoType *NewIP = |
| 2298 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2299 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2300 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2302 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2303 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2304 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2305 | Types.push_back(New); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2306 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2307 | return QualType(New, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | /// getFunctionType - Return a normal function type with a typed argument |
| 2311 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2312 | QualType |
| 2313 | ASTContext::getFunctionType(QualType ResultTy, |
| 2314 | const QualType *ArgArray, unsigned NumArgs, |
| 2315 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2316 | // Unique functions, to guarantee there is only one function of a particular |
| 2317 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2318 | llvm::FoldingSetNodeID ID; |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2319 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this); |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 2320 | |
| 2321 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2322 | if (FunctionProtoType *FTP = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2323 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2324 | return QualType(FTP, 0); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2325 | |
| 2326 | // Determine whether the type being created is already canonical or not. |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2327 | bool isCanonical = |
| 2328 | EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical() && |
| 2329 | !EPI.HasTrailingReturn; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2330 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2331 | if (!ArgArray[i].isCanonicalAsParam()) |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2332 | isCanonical = false; |
| 2333 | |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2334 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2335 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2336 | CC_X86StdCall : DefaultCC; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2337 | |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2338 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2339 | // The exception spec is not part of the canonical type. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2340 | QualType Canonical; |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2341 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2342 | SmallVector<QualType, 16> CanonicalArgs; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2343 | CanonicalArgs.reserve(NumArgs); |
| 2344 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2345 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2346 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2347 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2348 | CanonicalEPI.HasTrailingReturn = false; |
Sebastian Redl | 7c6c9e9 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2349 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2350 | CanonicalEPI.NumExceptions = 0; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2351 | CanonicalEPI.ExtInfo |
| 2352 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2353 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2354 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2355 | CanonicalArgs.data(), NumArgs, |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2356 | CanonicalEPI); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2357 | |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 2358 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2359 | FunctionProtoType *NewIP = |
| 2360 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2361 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2362 | } |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2363 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2364 | // FunctionProtoType objects are allocated with extra bytes after |
| 2365 | // them for three variable size arrays at the end: |
| 2366 | // - parameter types |
| 2367 | // - exception types |
| 2368 | // - consumed-arguments flags |
| 2369 | // Instead of the exception types, there could be a noexcept |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame^] | 2370 | // expression, or information used to resolve the exception |
| 2371 | // specification. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2372 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2373 | NumArgs * sizeof(QualType); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame^] | 2374 | if (EPI.ExceptionSpecType == EST_Dynamic) { |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2375 | Size += EPI.NumExceptions * sizeof(QualType); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame^] | 2376 | } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2377 | Size += sizeof(Expr*); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2378 | } else if (EPI.ExceptionSpecType == EST_Uninstantiated) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2379 | Size += 2 * sizeof(FunctionDecl*); |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame^] | 2380 | } else if (EPI.ExceptionSpecType == EST_Unevaluated) { |
| 2381 | Size += sizeof(FunctionDecl*); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2382 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2383 | if (EPI.ConsumedArguments) |
| 2384 | Size += NumArgs * sizeof(bool); |
| 2385 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2386 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2387 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2388 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2389 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2390 | Types.push_back(FTP); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2391 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2392 | return QualType(FTP, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2393 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 2394 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2395 | #ifndef NDEBUG |
| 2396 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2397 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2398 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2399 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2400 | return true; |
| 2401 | if (RD->getDescribedClassTemplate() && |
| 2402 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2403 | return true; |
| 2404 | return false; |
| 2405 | } |
| 2406 | #endif |
| 2407 | |
| 2408 | /// getInjectedClassNameType - Return the unique reference to the |
| 2409 | /// injected class name type for the specified templated declaration. |
| 2410 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2411 | QualType TST) const { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2412 | assert(NeedsInjectedClassNameType(Decl)); |
| 2413 | if (Decl->TypeForDecl) { |
| 2414 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2415 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2416 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2417 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2418 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2419 | } else { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2420 | Type *newType = |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2421 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2422 | Decl->TypeForDecl = newType; |
| 2423 | Types.push_back(newType); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2424 | } |
| 2425 | return QualType(Decl->TypeForDecl, 0); |
| 2426 | } |
| 2427 | |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2428 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2429 | /// specified type declaration. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2430 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 89656d2 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2431 | assert(Decl && "Passed null for Decl param"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2432 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2433 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2434 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2435 | return getTypedefType(Typedef); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2436 | |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2437 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2438 | "Template type parameter types are always available."); |
| 2439 | |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2440 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2441 | assert(!Record->getPreviousDecl() && |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2442 | "struct/union has previous declaration"); |
| 2443 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2444 | return getRecordType(Record); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2445 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2446 | assert(!Enum->getPreviousDecl() && |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2447 | "enum has previous declaration"); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2448 | return getEnumType(Enum); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2449 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2450 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2451 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2452 | Decl->TypeForDecl = newType; |
| 2453 | Types.push_back(newType); |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2454 | } else |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2455 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2456 | |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2457 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 2460 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2461 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2462 | QualType |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2463 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2464 | QualType Canonical) const { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2465 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2466 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2467 | if (Canonical.isNull()) |
| 2468 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2469 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2470 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2471 | Decl->TypeForDecl = newType; |
| 2472 | Types.push_back(newType); |
| 2473 | return QualType(newType, 0); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2476 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2477 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2478 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2479 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2480 | if (PrevDecl->TypeForDecl) |
| 2481 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2482 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2483 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2484 | Decl->TypeForDecl = newType; |
| 2485 | Types.push_back(newType); |
| 2486 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2489 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2490 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2491 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2492 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2493 | if (PrevDecl->TypeForDecl) |
| 2494 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2495 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2496 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2497 | Decl->TypeForDecl = newType; |
| 2498 | Types.push_back(newType); |
| 2499 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2502 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2503 | QualType modifiedType, |
| 2504 | QualType equivalentType) { |
| 2505 | llvm::FoldingSetNodeID id; |
| 2506 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2507 | |
| 2508 | void *insertPos = 0; |
| 2509 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2510 | if (type) return QualType(type, 0); |
| 2511 | |
| 2512 | QualType canon = getCanonicalType(equivalentType); |
| 2513 | type = new (*this, TypeAlignment) |
| 2514 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2515 | |
| 2516 | Types.push_back(type); |
| 2517 | AttributedTypes.InsertNode(type, insertPos); |
| 2518 | |
| 2519 | return QualType(type, 0); |
| 2520 | } |
| 2521 | |
| 2522 | |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2523 | /// \brief Retrieve a substitution-result type. |
| 2524 | QualType |
| 2525 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2526 | QualType Replacement) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2527 | assert(Replacement.isCanonical() |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2528 | && "replacement types must always be canonical"); |
| 2529 | |
| 2530 | llvm::FoldingSetNodeID ID; |
| 2531 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2532 | void *InsertPos = 0; |
| 2533 | SubstTemplateTypeParmType *SubstParm |
| 2534 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2535 | |
| 2536 | if (!SubstParm) { |
| 2537 | SubstParm = new (*this, TypeAlignment) |
| 2538 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2539 | Types.push_back(SubstParm); |
| 2540 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2541 | } |
| 2542 | |
| 2543 | return QualType(SubstParm, 0); |
| 2544 | } |
| 2545 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2546 | /// \brief Retrieve a |
| 2547 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2548 | const TemplateTypeParmType *Parm, |
| 2549 | const TemplateArgument &ArgPack) { |
| 2550 | #ifndef NDEBUG |
| 2551 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2552 | PEnd = ArgPack.pack_end(); |
| 2553 | P != PEnd; ++P) { |
| 2554 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2555 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2556 | } |
| 2557 | #endif |
| 2558 | |
| 2559 | llvm::FoldingSetNodeID ID; |
| 2560 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2561 | void *InsertPos = 0; |
| 2562 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2563 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2564 | return QualType(SubstParm, 0); |
| 2565 | |
| 2566 | QualType Canon; |
| 2567 | if (!Parm->isCanonicalUnqualified()) { |
| 2568 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2569 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2570 | ArgPack); |
| 2571 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2572 | } |
| 2573 | |
| 2574 | SubstTemplateTypeParmPackType *SubstParm |
| 2575 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2576 | ArgPack); |
| 2577 | Types.push_back(SubstParm); |
| 2578 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2579 | return QualType(SubstParm, 0); |
| 2580 | } |
| 2581 | |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2582 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2583 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2584 | /// name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2585 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2586 | bool ParameterPack, |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2587 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2588 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2589 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2590 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2591 | TemplateTypeParmType *TypeParm |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2592 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2593 | |
| 2594 | if (TypeParm) |
| 2595 | return QualType(TypeParm, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2596 | |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2597 | if (TTPDecl) { |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2598 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2599 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2600 | |
| 2601 | TemplateTypeParmType *TypeCheck |
| 2602 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2603 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2604 | (void)TypeCheck; |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2605 | } else |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2606 | TypeParm = new (*this, TypeAlignment) |
| 2607 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2608 | |
| 2609 | Types.push_back(TypeParm); |
| 2610 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2611 | |
| 2612 | return QualType(TypeParm, 0); |
| 2613 | } |
| 2614 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2615 | TypeSourceInfo * |
| 2616 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2617 | SourceLocation NameLoc, |
| 2618 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2619 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2620 | assert(!Name.getAsDependentTemplateName() && |
| 2621 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2622 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2623 | |
| 2624 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2625 | TemplateSpecializationTypeLoc TL |
| 2626 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2627 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2628 | TL.setTemplateNameLoc(NameLoc); |
| 2629 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2630 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2631 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2632 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2633 | return DI; |
| 2634 | } |
| 2635 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2636 | QualType |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2637 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2638 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2639 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2640 | assert(!Template.getAsDependentTemplateName() && |
| 2641 | "No dependent template names here!"); |
| 2642 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2643 | unsigned NumArgs = Args.size(); |
| 2644 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2645 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2646 | ArgVec.reserve(NumArgs); |
| 2647 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2648 | ArgVec.push_back(Args[i].getArgument()); |
| 2649 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2650 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2651 | Underlying); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2654 | #ifndef NDEBUG |
| 2655 | static bool hasAnyPackExpansions(const TemplateArgument *Args, |
| 2656 | unsigned NumArgs) { |
| 2657 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2658 | if (Args[I].isPackExpansion()) |
| 2659 | return true; |
| 2660 | |
| 2661 | return true; |
| 2662 | } |
| 2663 | #endif |
| 2664 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2665 | QualType |
| 2666 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2667 | const TemplateArgument *Args, |
| 2668 | unsigned NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2669 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2670 | assert(!Template.getAsDependentTemplateName() && |
| 2671 | "No dependent template names here!"); |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2672 | // Look through qualified template names. |
| 2673 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2674 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2675 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2676 | bool IsTypeAlias = |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2677 | Template.getAsTemplateDecl() && |
| 2678 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2679 | QualType CanonType; |
| 2680 | if (!Underlying.isNull()) |
| 2681 | CanonType = getCanonicalType(Underlying); |
| 2682 | else { |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2683 | // We can get here with an alias template when the specialization contains |
| 2684 | // a pack expansion that does not match up with a parameter pack. |
| 2685 | assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) && |
| 2686 | "Caller must compute aliased type"); |
| 2687 | IsTypeAlias = false; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2688 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 2689 | NumArgs); |
| 2690 | } |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2691 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2692 | // Allocate the (non-canonical) template specialization type, but don't |
| 2693 | // try to unique it: these types typically have location information that |
| 2694 | // we don't unique and don't want to lose. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2695 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 2696 | sizeof(TemplateArgument) * NumArgs + |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2697 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2698 | TypeAlignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2699 | TemplateSpecializationType *Spec |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2700 | = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType, |
| 2701 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2703 | Types.push_back(Spec); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2704 | return QualType(Spec, 0); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2707 | QualType |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2708 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2709 | const TemplateArgument *Args, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2710 | unsigned NumArgs) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2711 | assert(!Template.getAsDependentTemplateName() && |
| 2712 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2713 | |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2714 | // Look through qualified template names. |
| 2715 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2716 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2717 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2718 | // Build the canonical template specialization type. |
| 2719 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2720 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2721 | CanonArgs.reserve(NumArgs); |
| 2722 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2723 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2724 | |
| 2725 | // Determine whether this canonical template specialization type already |
| 2726 | // exists. |
| 2727 | llvm::FoldingSetNodeID ID; |
| 2728 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2729 | CanonArgs.data(), NumArgs, *this); |
| 2730 | |
| 2731 | void *InsertPos = 0; |
| 2732 | TemplateSpecializationType *Spec |
| 2733 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2734 | |
| 2735 | if (!Spec) { |
| 2736 | // Allocate a new canonical template specialization type. |
| 2737 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2738 | sizeof(TemplateArgument) * NumArgs), |
| 2739 | TypeAlignment); |
| 2740 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2741 | CanonArgs.data(), NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2742 | QualType(), QualType()); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2743 | Types.push_back(Spec); |
| 2744 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2745 | } |
| 2746 | |
| 2747 | assert(Spec->isDependentType() && |
| 2748 | "Non-dependent template-id type must have a canonical type"); |
| 2749 | return QualType(Spec, 0); |
| 2750 | } |
| 2751 | |
| 2752 | QualType |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2753 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2754 | NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2755 | QualType NamedType) const { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2756 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2757 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2758 | |
| 2759 | void *InsertPos = 0; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2760 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2761 | if (T) |
| 2762 | return QualType(T, 0); |
| 2763 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2764 | QualType Canon = NamedType; |
| 2765 | if (!Canon.isCanonical()) { |
| 2766 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2767 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2768 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2769 | (void)CheckT; |
| 2770 | } |
| 2771 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2772 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2773 | Types.push_back(T); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2774 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2775 | return QualType(T, 0); |
| 2776 | } |
| 2777 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2778 | QualType |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2779 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2780 | llvm::FoldingSetNodeID ID; |
| 2781 | ParenType::Profile(ID, InnerType); |
| 2782 | |
| 2783 | void *InsertPos = 0; |
| 2784 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2785 | if (T) |
| 2786 | return QualType(T, 0); |
| 2787 | |
| 2788 | QualType Canon = InnerType; |
| 2789 | if (!Canon.isCanonical()) { |
| 2790 | Canon = getCanonicalType(InnerType); |
| 2791 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2792 | assert(!CheckT && "Paren canonical type broken"); |
| 2793 | (void)CheckT; |
| 2794 | } |
| 2795 | |
| 2796 | T = new (*this) ParenType(InnerType, Canon); |
| 2797 | Types.push_back(T); |
| 2798 | ParenTypes.InsertNode(T, InsertPos); |
| 2799 | return QualType(T, 0); |
| 2800 | } |
| 2801 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2802 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2803 | NestedNameSpecifier *NNS, |
| 2804 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2805 | QualType Canon) const { |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2806 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2807 | |
| 2808 | if (Canon.isNull()) { |
| 2809 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2810 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2811 | if (Keyword == ETK_None) |
| 2812 | CanonKeyword = ETK_Typename; |
| 2813 | |
| 2814 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2815 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2819 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2820 | |
| 2821 | void *InsertPos = 0; |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2822 | DependentNameType *T |
| 2823 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2824 | if (T) |
| 2825 | return QualType(T, 0); |
| 2826 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2827 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2828 | Types.push_back(T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2829 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2830 | return QualType(T, 0); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2833 | QualType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2834 | ASTContext::getDependentTemplateSpecializationType( |
| 2835 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2836 | NestedNameSpecifier *NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2837 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2838 | const TemplateArgumentListInfo &Args) const { |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2839 | // TODO: avoid this copy |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2840 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2841 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2842 | ArgCopy.push_back(Args[I].getArgument()); |
| 2843 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2844 | ArgCopy.size(), |
| 2845 | ArgCopy.data()); |
| 2846 | } |
| 2847 | |
| 2848 | QualType |
| 2849 | ASTContext::getDependentTemplateSpecializationType( |
| 2850 | ElaboratedTypeKeyword Keyword, |
| 2851 | NestedNameSpecifier *NNS, |
| 2852 | const IdentifierInfo *Name, |
| 2853 | unsigned NumArgs, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2854 | const TemplateArgument *Args) const { |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 2855 | assert((!NNS || NNS->isDependent()) && |
| 2856 | "nested-name-specifier must be dependent"); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2857 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2858 | llvm::FoldingSetNodeID ID; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2859 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2860 | Name, NumArgs, Args); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2861 | |
| 2862 | void *InsertPos = 0; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2863 | DependentTemplateSpecializationType *T |
| 2864 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2865 | if (T) |
| 2866 | return QualType(T, 0); |
| 2867 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2868 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2869 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2870 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2871 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2872 | |
| 2873 | bool AnyNonCanonArgs = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2874 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2875 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2876 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2877 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2878 | AnyNonCanonArgs = true; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2881 | QualType Canon; |
| 2882 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2883 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2884 | Name, NumArgs, |
| 2885 | CanonArgs.data()); |
| 2886 | |
| 2887 | // Find the insert position again. |
| 2888 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2889 | } |
| 2890 | |
| 2891 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2892 | sizeof(TemplateArgument) * NumArgs), |
| 2893 | TypeAlignment); |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2894 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2895 | Name, NumArgs, Args, Canon); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2896 | Types.push_back(T); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2897 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2898 | return QualType(T, 0); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2901 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 2902 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2903 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2904 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2905 | |
| 2906 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2907 | "Pack expansions must expand one or more parameter packs"); |
| 2908 | void *InsertPos = 0; |
| 2909 | PackExpansionType *T |
| 2910 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2911 | if (T) |
| 2912 | return QualType(T, 0); |
| 2913 | |
| 2914 | QualType Canon; |
| 2915 | if (!Pattern.isCanonical()) { |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 2916 | Canon = getCanonicalType(Pattern); |
| 2917 | // The canonical type might not contain an unexpanded parameter pack, if it |
| 2918 | // contains an alias template specialization which ignores one of its |
| 2919 | // parameters. |
| 2920 | if (Canon->containsUnexpandedParameterPack()) { |
| 2921 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2922 | |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 2923 | // Find the insert position again, in case we inserted an element into |
| 2924 | // PackExpansionTypes and invalidated our insert position. |
| 2925 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2926 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2927 | } |
| 2928 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2929 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2930 | Types.push_back(T); |
| 2931 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2932 | return QualType(T, 0); |
| 2933 | } |
| 2934 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2935 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2936 | /// alphabetically. |
| 2937 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2938 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2939 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2942 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2943 | unsigned NumProtocols) { |
| 2944 | if (NumProtocols == 0) return true; |
| 2945 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2946 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 2947 | return false; |
| 2948 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2949 | for (unsigned i = 1; i != NumProtocols; ++i) |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2950 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) || |
| 2951 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2952 | return false; |
| 2953 | return true; |
| 2954 | } |
| 2955 | |
| 2956 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2957 | unsigned &NumProtocols) { |
| 2958 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2959 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2960 | // Sort protocols, keyed by name. |
| 2961 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2962 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2963 | // Canonicalize. |
| 2964 | for (unsigned I = 0, N = NumProtocols; I != N; ++I) |
| 2965 | Protocols[I] = Protocols[I]->getCanonicalDecl(); |
| 2966 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2967 | // Remove duplicates. |
| 2968 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2969 | NumProtocols = ProtocolsEnd-Protocols; |
| 2970 | } |
| 2971 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2972 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2973 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2974 | unsigned NumProtocols) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2975 | // If the base type is an interface and there aren't any protocols |
| 2976 | // to add, then the interface type will do just fine. |
| 2977 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2978 | return BaseType; |
| 2979 | |
| 2980 | // Look in the folding set for an existing type. |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2981 | llvm::FoldingSetNodeID ID; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2982 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2983 | void *InsertPos = 0; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2984 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2985 | return QualType(QT, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2986 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2987 | // Build the canonical type, which has the canonical base type and |
| 2988 | // a sorted-and-uniqued list of protocols. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2989 | QualType Canonical; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2990 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2991 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2992 | if (!ProtocolsSorted) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2993 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2994 | Protocols + NumProtocols); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2995 | unsigned UniqueCount = NumProtocols; |
| 2996 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2997 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2998 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2999 | &Sorted[0], UniqueCount); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3000 | } else { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3001 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 3002 | Protocols, NumProtocols); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | // Regenerate InsertPos. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3006 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3007 | } |
| 3008 | |
| 3009 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 3010 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 3011 | void *Mem = Allocate(Size, TypeAlignment); |
| 3012 | ObjCObjectTypeImpl *T = |
| 3013 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 3014 | |
| 3015 | Types.push_back(T); |
| 3016 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 3017 | return QualType(T, 0); |
| 3018 | } |
| 3019 | |
| 3020 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 3021 | /// the given object type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3022 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3023 | llvm::FoldingSetNodeID ID; |
| 3024 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 3025 | |
| 3026 | void *InsertPos = 0; |
| 3027 | if (ObjCObjectPointerType *QT = |
| 3028 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3029 | return QualType(QT, 0); |
| 3030 | |
| 3031 | // Find the canonical object type. |
| 3032 | QualType Canonical; |
| 3033 | if (!ObjectT.isCanonical()) { |
| 3034 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 3035 | |
| 3036 | // Regenerate InsertPos. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3037 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3038 | } |
| 3039 | |
Douglas Gregor | f85bee6 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 3040 | // No match. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3041 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 3042 | ObjCObjectPointerType *QType = |
| 3043 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3044 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3045 | Types.push_back(QType); |
| 3046 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3047 | return QualType(QType, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3048 | } |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3049 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3050 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 3051 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3052 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 3053 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3054 | if (Decl->TypeForDecl) |
| 3055 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3056 | |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3057 | if (PrevDecl) { |
| 3058 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 3059 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3060 | return QualType(PrevDecl->TypeForDecl, 0); |
| 3061 | } |
| 3062 | |
Douglas Gregor | 7671e53 | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 3063 | // Prefer the definition, if there is one. |
| 3064 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 3065 | Decl = Def; |
| 3066 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3067 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 3068 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 3069 | Decl->TypeForDecl = T; |
| 3070 | Types.push_back(T); |
| 3071 | return QualType(T, 0); |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3074 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 3075 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3076 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3077 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3078 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3079 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3080 | TypeOfExprType *toe; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3081 | if (tofExpr->isTypeDependent()) { |
| 3082 | llvm::FoldingSetNodeID ID; |
| 3083 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3084 | |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3085 | void *InsertPos = 0; |
| 3086 | DependentTypeOfExprType *Canon |
| 3087 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3088 | if (Canon) { |
| 3089 | // We already have a "canonical" version of an identical, dependent |
| 3090 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3091 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3092 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3093 | } else { |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3094 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3095 | Canon |
| 3096 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3097 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 3098 | toe = Canon; |
| 3099 | } |
| 3100 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3101 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3102 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3103 | } |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3104 | Types.push_back(toe); |
| 3105 | return QualType(toe, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3108 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 3109 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 3110 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3111 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3112 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3113 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3114 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3115 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3116 | Types.push_back(tot); |
| 3117 | return QualType(tot, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 3120 | |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3121 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 3122 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 3123 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3124 | /// an issue. This doesn't effect the type checker, since it operates |
David Blaikie | 876657b | 2011-11-06 22:28:03 +0000 | [diff] [blame] | 3125 | /// on canonical types (which are always unique). |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3126 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3127 | DecltypeType *dt; |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3128 | |
| 3129 | // C++0x [temp.type]p2: |
| 3130 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 3131 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 3132 | // type only if their expressions are equivalent (14.5.6.1). |
| 3133 | if (e->isInstantiationDependent()) { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3134 | llvm::FoldingSetNodeID ID; |
| 3135 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3136 | |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3137 | void *InsertPos = 0; |
| 3138 | DependentDecltypeType *Canon |
| 3139 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3140 | if (Canon) { |
| 3141 | // We already have a "canonical" version of an equivalent, dependent |
| 3142 | // decltype type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3143 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3144 | QualType((DecltypeType*)Canon, 0)); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3145 | } else { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3146 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3147 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3148 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 3149 | dt = Canon; |
| 3150 | } |
| 3151 | } else { |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3152 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
| 3153 | getCanonicalType(UnderlyingType)); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3154 | } |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3155 | Types.push_back(dt); |
| 3156 | return QualType(dt, 0); |
| 3157 | } |
| 3158 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3159 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 3160 | /// savings are minimal and these are rare. |
| 3161 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 3162 | QualType UnderlyingType, |
| 3163 | UnaryTransformType::UTTKind Kind) |
| 3164 | const { |
| 3165 | UnaryTransformType *Ty = |
Douglas Gregor | 6c6e676 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 3166 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 3167 | Kind, |
| 3168 | UnderlyingType->isDependentType() ? |
Peter Collingbourne | 15d48ec | 2012-03-05 16:02:06 +0000 | [diff] [blame] | 3169 | QualType() : getCanonicalType(UnderlyingType)); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3170 | Types.push_back(Ty); |
| 3171 | return QualType(Ty, 0); |
| 3172 | } |
| 3173 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3174 | /// getAutoType - We only unique auto types after they've been deduced. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3175 | QualType ASTContext::getAutoType(QualType DeducedType) const { |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3176 | void *InsertPos = 0; |
| 3177 | if (!DeducedType.isNull()) { |
| 3178 | // Look in the folding set for an existing type. |
| 3179 | llvm::FoldingSetNodeID ID; |
| 3180 | AutoType::Profile(ID, DeducedType); |
| 3181 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3182 | return QualType(AT, 0); |
| 3183 | } |
| 3184 | |
| 3185 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType); |
| 3186 | Types.push_back(AT); |
| 3187 | if (InsertPos) |
| 3188 | AutoTypes.InsertNode(AT, InsertPos); |
| 3189 | return QualType(AT, 0); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3192 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 3193 | /// the given value type. |
| 3194 | QualType ASTContext::getAtomicType(QualType T) const { |
| 3195 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3196 | // structure. |
| 3197 | llvm::FoldingSetNodeID ID; |
| 3198 | AtomicType::Profile(ID, T); |
| 3199 | |
| 3200 | void *InsertPos = 0; |
| 3201 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3202 | return QualType(AT, 0); |
| 3203 | |
| 3204 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 3205 | // either, so fill in the canonical type field. |
| 3206 | QualType Canonical; |
| 3207 | if (!T.isCanonical()) { |
| 3208 | Canonical = getAtomicType(getCanonicalType(T)); |
| 3209 | |
| 3210 | // Get the new insert position for the node we care about. |
| 3211 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3212 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 3213 | } |
| 3214 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 3215 | Types.push_back(New); |
| 3216 | AtomicTypes.InsertNode(New, InsertPos); |
| 3217 | return QualType(New, 0); |
| 3218 | } |
| 3219 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3220 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 3221 | QualType ASTContext::getAutoDeductType() const { |
| 3222 | if (AutoDeductTy.isNull()) |
| 3223 | AutoDeductTy = getAutoType(QualType()); |
| 3224 | assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern"); |
| 3225 | return AutoDeductTy; |
| 3226 | } |
| 3227 | |
| 3228 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 3229 | QualType ASTContext::getAutoRRefDeductType() const { |
| 3230 | if (AutoRRefDeductTy.isNull()) |
| 3231 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 3232 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 3233 | return AutoRRefDeductTy; |
| 3234 | } |
| 3235 | |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 3236 | /// getTagDeclType - Return the unique reference to the type for the |
| 3237 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3238 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | 2b0ce11 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 3239 | assert (Decl); |
Mike Stump | b93185d | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 3240 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 3241 | // away const? mutable? |
| 3242 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 3246 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 3247 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 3248 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3249 | return getFromTargetType(Target->getSizeType()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 3250 | } |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 3251 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3252 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 3253 | CanQualType ASTContext::getIntMaxType() const { |
| 3254 | return getFromTargetType(Target->getIntMaxType()); |
| 3255 | } |
| 3256 | |
| 3257 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 3258 | CanQualType ASTContext::getUIntMaxType() const { |
| 3259 | return getFromTargetType(Target->getUIntMaxType()); |
| 3260 | } |
| 3261 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3262 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 3263 | /// Used when in C++, as a GCC extension. |
| 3264 | QualType ASTContext::getSignedWCharType() const { |
| 3265 | // FIXME: derive from "Target" ? |
| 3266 | return WCharTy; |
| 3267 | } |
| 3268 | |
| 3269 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 3270 | /// Used when in C++, as a GCC extension. |
| 3271 | QualType ASTContext::getUnsignedWCharType() const { |
| 3272 | // FIXME: derive from "Target" ? |
| 3273 | return UnsignedIntTy; |
| 3274 | } |
| 3275 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3276 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3277 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 3278 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3279 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3282 | //===----------------------------------------------------------------------===// |
| 3283 | // Type Operators |
| 3284 | //===----------------------------------------------------------------------===// |
| 3285 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3286 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3287 | // Push qualifiers into arrays, and then discard any remaining |
| 3288 | // qualifiers. |
| 3289 | T = getCanonicalType(T); |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3290 | T = getVariableArrayDecayedType(T); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3291 | const Type *Ty = T.getTypePtr(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3292 | QualType Result; |
| 3293 | if (isa<ArrayType>(Ty)) { |
| 3294 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 3295 | } else if (isa<FunctionType>(Ty)) { |
| 3296 | Result = getPointerType(QualType(Ty, 0)); |
| 3297 | } else { |
| 3298 | Result = QualType(Ty, 0); |
| 3299 | } |
| 3300 | |
| 3301 | return CanQualType::CreateUnsafe(Result); |
| 3302 | } |
| 3303 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3304 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 3305 | Qualifiers &quals) { |
| 3306 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 3307 | |
| 3308 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 3309 | // the unqualified desugared type and then drops it on the floor. |
| 3310 | // We then have to strip that sugar back off with |
| 3311 | // getUnqualifiedDesugaredType(), which is silly. |
| 3312 | const ArrayType *AT = |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3313 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3314 | |
| 3315 | // 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] | 3316 | if (!AT) { |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3317 | quals = splitType.Quals; |
| 3318 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3321 | // Otherwise, recurse on the array's element type. |
| 3322 | QualType elementType = AT->getElementType(); |
| 3323 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 3324 | |
| 3325 | // If that didn't change the element type, AT has no qualifiers, so we |
| 3326 | // can just use the results in splitType. |
| 3327 | if (elementType == unqualElementType) { |
| 3328 | assert(quals.empty()); // from the recursive call |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3329 | quals = splitType.Quals; |
| 3330 | return QualType(splitType.Ty, 0); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | // Otherwise, add in the qualifiers from the outermost type, then |
| 3334 | // build the type back up. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3335 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3336 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3337 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3338 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3339 | CAT->getSizeModifier(), 0); |
| 3340 | } |
| 3341 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3342 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3343 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3346 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3347 | return getVariableArrayType(unqualElementType, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3348 | VAT->getSizeExpr(), |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3349 | VAT->getSizeModifier(), |
| 3350 | VAT->getIndexTypeCVRQualifiers(), |
| 3351 | VAT->getBracketsRange()); |
| 3352 | } |
| 3353 | |
| 3354 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3355 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3356 | DSAT->getSizeModifier(), 0, |
| 3357 | SourceRange()); |
| 3358 | } |
| 3359 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3360 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3361 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3362 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3363 | /// or pointer-to-member types, or if they are not similar at this |
| 3364 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3365 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3366 | /// be called in a loop that successively "unwraps" pointer and |
| 3367 | /// pointer-to-member types to compare them at each level. |
| 3368 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3369 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3370 | *T2PtrType = T2->getAs<PointerType>(); |
| 3371 | if (T1PtrType && T2PtrType) { |
| 3372 | T1 = T1PtrType->getPointeeType(); |
| 3373 | T2 = T2PtrType->getPointeeType(); |
| 3374 | return true; |
| 3375 | } |
| 3376 | |
| 3377 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3378 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3379 | if (T1MPType && T2MPType && |
| 3380 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3381 | QualType(T2MPType->getClass(), 0))) { |
| 3382 | T1 = T1MPType->getPointeeType(); |
| 3383 | T2 = T2MPType->getPointeeType(); |
| 3384 | return true; |
| 3385 | } |
| 3386 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3387 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3388 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3389 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3390 | if (T1OPType && T2OPType) { |
| 3391 | T1 = T1OPType->getPointeeType(); |
| 3392 | T2 = T2OPType->getPointeeType(); |
| 3393 | return true; |
| 3394 | } |
| 3395 | } |
| 3396 | |
| 3397 | // FIXME: Block pointers, too? |
| 3398 | |
| 3399 | return false; |
| 3400 | } |
| 3401 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3402 | DeclarationNameInfo |
| 3403 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3404 | SourceLocation NameLoc) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3405 | switch (Name.getKind()) { |
| 3406 | case TemplateName::QualifiedTemplate: |
| 3407 | case TemplateName::Template: |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3408 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3409 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3410 | NameLoc); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3411 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3412 | case TemplateName::OverloadedTemplate: { |
| 3413 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3414 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3415 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3416 | } |
| 3417 | |
| 3418 | case TemplateName::DependentTemplate: { |
| 3419 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3420 | DeclarationName DName; |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3421 | if (DTN->isIdentifier()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3422 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3423 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3424 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3425 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3426 | // DNInfo work in progress: FIXME: source locations? |
| 3427 | DeclarationNameLoc DNLoc; |
| 3428 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3429 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3430 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3431 | } |
| 3432 | } |
| 3433 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3434 | case TemplateName::SubstTemplateTemplateParm: { |
| 3435 | SubstTemplateTemplateParmStorage *subst |
| 3436 | = Name.getAsSubstTemplateTemplateParm(); |
| 3437 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3438 | NameLoc); |
| 3439 | } |
| 3440 | |
| 3441 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3442 | SubstTemplateTemplateParmPackStorage *subst |
| 3443 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3444 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3445 | NameLoc); |
| 3446 | } |
| 3447 | } |
| 3448 | |
| 3449 | llvm_unreachable("bad template name kind!"); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3452 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3453 | switch (Name.getKind()) { |
| 3454 | case TemplateName::QualifiedTemplate: |
| 3455 | case TemplateName::Template: { |
| 3456 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3457 | if (TemplateTemplateParmDecl *TTP |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3458 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3459 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3460 | |
| 3461 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3462 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3463 | } |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3464 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3465 | case TemplateName::OverloadedTemplate: |
| 3466 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3467 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3468 | case TemplateName::DependentTemplate: { |
| 3469 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 3470 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 3471 | return DTN->CanonicalTemplateName; |
| 3472 | } |
| 3473 | |
| 3474 | case TemplateName::SubstTemplateTemplateParm: { |
| 3475 | SubstTemplateTemplateParmStorage *subst |
| 3476 | = Name.getAsSubstTemplateTemplateParm(); |
| 3477 | return getCanonicalTemplateName(subst->getReplacement()); |
| 3478 | } |
| 3479 | |
| 3480 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3481 | SubstTemplateTemplateParmPackStorage *subst |
| 3482 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3483 | TemplateTemplateParmDecl *canonParameter |
| 3484 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 3485 | TemplateArgument canonArgPack |
| 3486 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 3487 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 3488 | } |
| 3489 | } |
| 3490 | |
| 3491 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3494 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 3495 | X = getCanonicalTemplateName(X); |
| 3496 | Y = getCanonicalTemplateName(Y); |
| 3497 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 3498 | } |
| 3499 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3500 | TemplateArgument |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3501 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3502 | switch (Arg.getKind()) { |
| 3503 | case TemplateArgument::Null: |
| 3504 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3505 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3506 | case TemplateArgument::Expression: |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3507 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3508 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3509 | case TemplateArgument::Declaration: { |
| 3510 | if (Decl *D = Arg.getAsDecl()) |
| 3511 | return TemplateArgument(D->getCanonicalDecl()); |
| 3512 | return TemplateArgument((Decl*)0); |
| 3513 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3514 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3515 | case TemplateArgument::Template: |
| 3516 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3517 | |
| 3518 | case TemplateArgument::TemplateExpansion: |
| 3519 | return TemplateArgument(getCanonicalTemplateName( |
| 3520 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3521 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3522 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3523 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 3524 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3525 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3526 | case TemplateArgument::Type: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3527 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3528 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3529 | case TemplateArgument::Pack: { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 3530 | if (Arg.pack_size() == 0) |
| 3531 | return Arg; |
| 3532 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3533 | TemplateArgument *CanonArgs |
| 3534 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3535 | unsigned Idx = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3536 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3537 | AEnd = Arg.pack_end(); |
| 3538 | A != AEnd; (void)++A, ++Idx) |
| 3539 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3540 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3541 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | // Silence GCC warning |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3546 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3549 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3550 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3551 | if (!NNS) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3552 | return 0; |
| 3553 | |
| 3554 | switch (NNS->getKind()) { |
| 3555 | case NestedNameSpecifier::Identifier: |
| 3556 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3557 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3558 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 3559 | NNS->getAsIdentifier()); |
| 3560 | |
| 3561 | case NestedNameSpecifier::Namespace: |
| 3562 | // A namespace is canonical; build a nested-name-specifier with |
| 3563 | // this namespace and no prefix. |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3564 | return NestedNameSpecifier::Create(*this, 0, |
| 3565 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 3566 | |
| 3567 | case NestedNameSpecifier::NamespaceAlias: |
| 3568 | // A namespace is canonical; build a nested-name-specifier with |
| 3569 | // this namespace and no prefix. |
| 3570 | return NestedNameSpecifier::Create(*this, 0, |
| 3571 | NNS->getAsNamespaceAlias()->getNamespace() |
| 3572 | ->getOriginalNamespace()); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3573 | |
| 3574 | case NestedNameSpecifier::TypeSpec: |
| 3575 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3576 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3577 | |
| 3578 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 3579 | // break it apart into its prefix and identifier, then reconsititute those |
| 3580 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 3581 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 3582 | // types, e.g., |
| 3583 | // typedef typename T::type T1; |
| 3584 | // typedef typename T1::type T2; |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3585 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) |
| 3586 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3587 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3588 | |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3589 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 3590 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 3591 | // first place? |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3592 | return NestedNameSpecifier::Create(*this, 0, false, |
| 3593 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | case NestedNameSpecifier::Global: |
| 3597 | // The global specifier is canonical and unique. |
| 3598 | return NNS; |
| 3599 | } |
| 3600 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3601 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3602 | } |
| 3603 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3604 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3605 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3606 | // Handle the non-qualified case efficiently. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3607 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3608 | // Handle the common positive case fast. |
| 3609 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3610 | return AT; |
| 3611 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3612 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3613 | // Handle the common negative case fast. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3614 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3615 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3616 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3617 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3618 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3619 | // 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] | 3620 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3621 | // If we get here, we either have type qualifiers on the type, or we have |
| 3622 | // 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] | 3623 | // we must propagate them down into the element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3624 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3625 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3626 | Qualifiers qs = split.Quals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3627 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3628 | // If we have a simple case, just return now. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3629 | const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3630 | if (ATy == 0 || qs.empty()) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3631 | return ATy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3632 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3633 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3634 | // qualifiers into the array element type and return a new array type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3635 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3636 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3637 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3638 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3639 | CAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3640 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3641 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3642 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3643 | IAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3644 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3645 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3646 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3647 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3648 | return cast<ArrayType>( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3649 | getDependentSizedArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3650 | DSAT->getSizeExpr(), |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3651 | DSAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3652 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3653 | DSAT->getBracketsRange())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3654 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3655 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3656 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3657 | VAT->getSizeExpr(), |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3658 | VAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3659 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3660 | VAT->getBracketsRange())); |
Chris Lattner | ed0d079 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 3661 | } |
| 3662 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 3663 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3664 | // C99 6.7.5.3p7: |
| 3665 | // A declaration of a parameter as "array of type" shall be |
| 3666 | // adjusted to "qualified pointer to type", where the type |
| 3667 | // qualifiers (if any) are those specified within the [ and ] of |
| 3668 | // the array type derivation. |
| 3669 | if (T->isArrayType()) |
| 3670 | return getArrayDecayedType(T); |
| 3671 | |
| 3672 | // C99 6.7.5.3p8: |
| 3673 | // A declaration of a parameter as "function returning type" |
| 3674 | // shall be adjusted to "pointer to function returning type", as |
| 3675 | // in 6.3.2.1. |
| 3676 | if (T->isFunctionType()) |
| 3677 | return getPointerType(T); |
| 3678 | |
| 3679 | return T; |
| 3680 | } |
| 3681 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 3682 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3683 | T = getVariableArrayDecayedType(T); |
| 3684 | T = getAdjustedParameterType(T); |
| 3685 | return T.getUnqualifiedType(); |
| 3686 | } |
| 3687 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3688 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 3689 | /// specified array type to a pointer. This operation is non-trivial when |
| 3690 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 3691 | /// this returns a pointer to a properly qualified element of the array. |
| 3692 | /// |
| 3693 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3694 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3695 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 3696 | // typedefs in the element type of the array. This also handles propagation |
| 3697 | // of type qualifiers from the array type into the element type if present |
| 3698 | // (C99 6.7.3p8). |
| 3699 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 3700 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3702 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3703 | |
| 3704 | // int x[restrict 4] -> int *restrict |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3705 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3708 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 3709 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3712 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 3713 | Qualifiers qs; |
| 3714 | while (true) { |
| 3715 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3716 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3717 | if (!array) break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3718 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3719 | type = array->getElementType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3720 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3721 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3722 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3723 | return getQualifiedType(type, qs); |
Anders Carlsson | e0808df | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3726 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3727 | uint64_t |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3728 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 3729 | uint64_t ElementCount = 1; |
| 3730 | do { |
| 3731 | ElementCount *= CA->getSize().getZExtValue(); |
| 3732 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 3733 | } while (CA); |
| 3734 | return ElementCount; |
| 3735 | } |
| 3736 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 3737 | /// getFloatingRank - Return a relative rank for floating point types. |
| 3738 | /// 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] | 3739 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3740 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 3741 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3742 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3743 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 3744 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3745 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3746 | case BuiltinType::Half: return HalfRank; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 3747 | case BuiltinType::Float: return FloatRank; |
| 3748 | case BuiltinType::Double: return DoubleRank; |
| 3749 | case BuiltinType::LongDouble: return LongDoubleRank; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3750 | } |
| 3751 | } |
| 3752 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3753 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 3754 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | fc6ffa2 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3755 | /// 'typeDomain' is a real floating point or complex type. |
| 3756 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3757 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 3758 | QualType Domain) const { |
| 3759 | FloatingRank EltRank = getFloatingRank(Size); |
| 3760 | if (Domain->isComplexType()) { |
| 3761 | switch (EltRank) { |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3762 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3763 | case FloatRank: return FloatComplexTy; |
| 3764 | case DoubleRank: return DoubleComplexTy; |
| 3765 | case LongDoubleRank: return LongDoubleComplexTy; |
| 3766 | } |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 3767 | } |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3768 | |
| 3769 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 3770 | switch (EltRank) { |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3771 | case HalfRank: llvm_unreachable("Half ranks are not valid here"); |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3772 | case FloatRank: return FloatTy; |
| 3773 | case DoubleRank: return DoubleTy; |
| 3774 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3775 | } |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3776 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3779 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 3780 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 3781 | /// '_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] | 3782 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3783 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3784 | FloatingRank LHSR = getFloatingRank(LHS); |
| 3785 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3786 | |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3787 | if (LHSR == RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3788 | return 0; |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3789 | if (LHSR > RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3790 | return 1; |
| 3791 | return -1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3792 | } |
| 3793 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3794 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 3795 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 3796 | /// or if it is not canonicalized. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3797 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3798 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3799 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3800 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3801 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3802 | case BuiltinType::Bool: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3803 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3804 | case BuiltinType::Char_S: |
| 3805 | case BuiltinType::Char_U: |
| 3806 | case BuiltinType::SChar: |
| 3807 | case BuiltinType::UChar: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3808 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3809 | case BuiltinType::Short: |
| 3810 | case BuiltinType::UShort: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3811 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3812 | case BuiltinType::Int: |
| 3813 | case BuiltinType::UInt: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3814 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3815 | case BuiltinType::Long: |
| 3816 | case BuiltinType::ULong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3817 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3818 | case BuiltinType::LongLong: |
| 3819 | case BuiltinType::ULongLong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3820 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3821 | case BuiltinType::Int128: |
| 3822 | case BuiltinType::UInt128: |
| 3823 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3824 | } |
| 3825 | } |
| 3826 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3827 | /// \brief Whether this is a promotable bitfield reference according |
| 3828 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3829 | /// |
| 3830 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3831 | /// promotion occurs. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3832 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | e05d3cb | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3833 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3834 | return QualType(); |
| 3835 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3836 | FieldDecl *Field = E->getBitField(); |
| 3837 | if (!Field) |
| 3838 | return QualType(); |
| 3839 | |
| 3840 | QualType FT = Field->getType(); |
| 3841 | |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3842 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3843 | uint64_t IntSize = getTypeSize(IntTy); |
| 3844 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3845 | // to the size of int, it gets promoted no matter what its type is. |
| 3846 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3847 | if (BitWidth < IntSize) |
| 3848 | return IntTy; |
| 3849 | |
| 3850 | if (BitWidth == IntSize) |
| 3851 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3852 | |
| 3853 | // Types bigger than int are not subject to promotions, and therefore act |
| 3854 | // like the base type. |
| 3855 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3856 | // is ridiculous. |
| 3857 | return QualType(); |
| 3858 | } |
| 3859 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3860 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3861 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3862 | /// integer type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3863 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3864 | assert(!Promotable.isNull()); |
| 3865 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3866 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3867 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 3868 | |
| 3869 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 3870 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 3871 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 3872 | // types that can represent all the values of its underlying type: |
| 3873 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 3874 | // unsigned long long int [...] |
| 3875 | // FIXME: Is there some better way to compute this? |
| 3876 | if (BT->getKind() == BuiltinType::WChar_S || |
| 3877 | BT->getKind() == BuiltinType::WChar_U || |
| 3878 | BT->getKind() == BuiltinType::Char16 || |
| 3879 | BT->getKind() == BuiltinType::Char32) { |
| 3880 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 3881 | uint64_t FromSize = getTypeSize(BT); |
| 3882 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 3883 | LongLongTy, UnsignedLongLongTy }; |
| 3884 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 3885 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 3886 | if (FromSize < ToSize || |
| 3887 | (FromSize == ToSize && |
| 3888 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 3889 | return PromoteTypes[Idx]; |
| 3890 | } |
| 3891 | llvm_unreachable("char type should fit into long long"); |
| 3892 | } |
| 3893 | } |
| 3894 | |
| 3895 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3896 | if (Promotable->isSignedIntegerType()) |
| 3897 | return IntTy; |
| 3898 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3899 | uint64_t IntSize = getTypeSize(IntTy); |
| 3900 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3901 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3902 | } |
| 3903 | |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3904 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 3905 | /// type and returns its ownership. |
| 3906 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 3907 | while (!T.isNull()) { |
| 3908 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 3909 | return T.getObjCLifetime(); |
| 3910 | if (T->isArrayType()) |
| 3911 | T = getBaseElementType(T); |
| 3912 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 3913 | T = PT->getPointeeType(); |
| 3914 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 8e25253 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 3915 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3916 | else |
| 3917 | break; |
| 3918 | } |
| 3919 | |
| 3920 | return Qualifiers::OCL_None; |
| 3921 | } |
| 3922 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3923 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3924 | /// 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] | 3925 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3926 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3927 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3928 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3929 | if (LHSC == RHSC) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3930 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3931 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3932 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3933 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3934 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3935 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3936 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3937 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3938 | if (LHSRank == RHSRank) return 0; |
| 3939 | return LHSRank > RHSRank ? 1 : -1; |
| 3940 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3941 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3942 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3943 | if (LHSUnsigned) { |
| 3944 | // If the unsigned [LHS] type is larger, return it. |
| 3945 | if (LHSRank >= RHSRank) |
| 3946 | return 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3947 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3948 | // If the signed type can represent all values of the unsigned type, it |
| 3949 | // 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] | 3950 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3951 | return -1; |
| 3952 | } |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3953 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3954 | // If the unsigned [RHS] type is larger, return it. |
| 3955 | if (RHSRank >= LHSRank) |
| 3956 | return -1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3957 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3958 | // If the signed type can represent all values of the unsigned type, it |
| 3959 | // 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] | 3960 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3961 | return 1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3962 | } |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3963 | |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3964 | static RecordDecl * |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3965 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 3966 | DeclContext *DC, IdentifierInfo *Id) { |
| 3967 | SourceLocation Loc; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3968 | if (Ctx.getLangOpts().CPlusPlus) |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3969 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3970 | else |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3971 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3972 | } |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3973 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3974 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3975 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3976 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3977 | CFConstantStringTypeDecl = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3978 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3979 | &Idents.get("NSConstantString")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3980 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3981 | |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3982 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3983 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3984 | // const int *isa; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3985 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3986 | // int flags; |
| 3987 | FieldTypes[1] = IntTy; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3988 | // const char *str; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3989 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3990 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3991 | FieldTypes[3] = LongTy; |
| 3992 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3993 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3994 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3995 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3996 | SourceLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3997 | SourceLocation(), 0, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3998 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3999 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4000 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4001 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4002 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4003 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4006 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4007 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4008 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4009 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 412af03 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 4010 | } |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4011 | |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4012 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4013 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4014 | assert(Rec && "Invalid CFConstantStringType"); |
| 4015 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 4016 | } |
| 4017 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4018 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4019 | if (BlockDescriptorType) |
| 4020 | return getTagDeclType(BlockDescriptorType); |
| 4021 | |
| 4022 | RecordDecl *T; |
| 4023 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4024 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4025 | &Idents.get("__block_descriptor")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4026 | T->startDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4027 | |
| 4028 | QualType FieldTypes[] = { |
| 4029 | UnsignedLongTy, |
| 4030 | UnsignedLongTy, |
| 4031 | }; |
| 4032 | |
| 4033 | const char *FieldNames[] = { |
| 4034 | "reserved", |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4035 | "Size" |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4036 | }; |
| 4037 | |
| 4038 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4039 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4040 | SourceLocation(), |
| 4041 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4042 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4043 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4044 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4045 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4046 | Field->setAccess(AS_public); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4047 | T->addDecl(Field); |
| 4048 | } |
| 4049 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4050 | T->completeDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4051 | |
| 4052 | BlockDescriptorType = T; |
| 4053 | |
| 4054 | return getTagDeclType(BlockDescriptorType); |
| 4055 | } |
| 4056 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4057 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4058 | if (BlockDescriptorExtendedType) |
| 4059 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4060 | |
| 4061 | RecordDecl *T; |
| 4062 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4063 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4064 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4065 | T->startDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4066 | |
| 4067 | QualType FieldTypes[] = { |
| 4068 | UnsignedLongTy, |
| 4069 | UnsignedLongTy, |
| 4070 | getPointerType(VoidPtrTy), |
| 4071 | getPointerType(VoidPtrTy) |
| 4072 | }; |
| 4073 | |
| 4074 | const char *FieldNames[] = { |
| 4075 | "reserved", |
| 4076 | "Size", |
| 4077 | "CopyFuncPtr", |
| 4078 | "DestroyFuncPtr" |
| 4079 | }; |
| 4080 | |
| 4081 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4082 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4083 | SourceLocation(), |
| 4084 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4085 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4086 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4087 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4088 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4089 | Field->setAccess(AS_public); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4090 | T->addDecl(Field); |
| 4091 | } |
| 4092 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4093 | T->completeDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4094 | |
| 4095 | BlockDescriptorExtendedType = T; |
| 4096 | |
| 4097 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4098 | } |
| 4099 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4100 | bool ASTContext::BlockRequiresCopying(QualType Ty) const { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4101 | if (Ty->isObjCRetainableType()) |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4102 | return true; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4103 | if (getLangOpts().CPlusPlus) { |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 4104 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4105 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 4106 | return RD->hasConstCopyConstructor(); |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 4107 | |
| 4108 | } |
| 4109 | } |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4110 | return false; |
| 4111 | } |
| 4112 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4113 | QualType |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4114 | ASTContext::BuildByRefType(StringRef DeclName, QualType Ty) const { |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4115 | // type = struct __Block_byref_1_X { |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 4116 | // void *__isa; |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4117 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 4118 | // unsigned int __flags; |
| 4119 | // unsigned int __size; |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 4120 | // void *__copy_helper; // as needed |
| 4121 | // void *__destroy_help // as needed |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4122 | // int X; |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 4123 | // } * |
| 4124 | |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4125 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 4126 | |
| 4127 | // FIXME: Move up |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4128 | SmallString<36> Name; |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 4129 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 4130 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4131 | RecordDecl *T; |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4132 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str())); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4133 | T->startDefinition(); |
| 4134 | QualType Int32Ty = IntTy; |
| 4135 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 4136 | QualType FieldTypes[] = { |
| 4137 | getPointerType(VoidPtrTy), |
| 4138 | getPointerType(getTagDeclType(T)), |
| 4139 | Int32Ty, |
| 4140 | Int32Ty, |
| 4141 | getPointerType(VoidPtrTy), |
| 4142 | getPointerType(VoidPtrTy), |
| 4143 | Ty |
| 4144 | }; |
| 4145 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4146 | StringRef FieldNames[] = { |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4147 | "__isa", |
| 4148 | "__forwarding", |
| 4149 | "__flags", |
| 4150 | "__size", |
| 4151 | "__copy_helper", |
| 4152 | "__destroy_helper", |
| 4153 | DeclName, |
| 4154 | }; |
| 4155 | |
| 4156 | for (size_t i = 0; i < 7; ++i) { |
| 4157 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 4158 | continue; |
| 4159 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4160 | SourceLocation(), |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4161 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4162 | FieldTypes[i], /*TInfo=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4163 | /*BitWidth=*/0, /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4164 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4165 | Field->setAccess(AS_public); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4166 | T->addDecl(Field); |
| 4167 | } |
| 4168 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4169 | T->completeDefinition(); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4170 | |
| 4171 | return getPointerType(getTagDeclType(T)); |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4174 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 4175 | if (!ObjCInstanceTypeDecl) |
| 4176 | ObjCInstanceTypeDecl = TypedefDecl::Create(*this, |
| 4177 | getTranslationUnitDecl(), |
| 4178 | SourceLocation(), |
| 4179 | SourceLocation(), |
| 4180 | &Idents.get("instancetype"), |
| 4181 | getTrivialTypeSourceInfo(getObjCIdType())); |
| 4182 | return ObjCInstanceTypeDecl; |
| 4183 | } |
| 4184 | |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4185 | // This returns true if a type has been typedefed to BOOL: |
| 4186 | // typedef <type> BOOL; |
Chris Lattner | e021899 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 4187 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4188 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 9b1f279 | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 4189 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 4190 | return II->isStr("BOOL"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4191 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4192 | return false; |
| 4193 | } |
| 4194 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4195 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4196 | /// purpose. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4197 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4198 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 4199 | return CharUnits::Zero(); |
| 4200 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4201 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4202 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4203 | // 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] | 4204 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4205 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4206 | // Treat arrays as pointers, since that's how they're passed in. |
| 4207 | else if (type->isArrayType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4208 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4209 | return sz; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4210 | } |
| 4211 | |
| 4212 | static inline |
| 4213 | std::string charUnitsToString(const CharUnits &CU) { |
| 4214 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4215 | } |
| 4216 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4217 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4218 | /// declaration. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4219 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 4220 | std::string S; |
| 4221 | |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4222 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 4223 | QualType BlockTy = |
| 4224 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 4225 | // Encode result type. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 4226 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4227 | // Compute size of all parameters. |
| 4228 | // Start with computing size of a pointer in number of bytes. |
| 4229 | // FIXME: There might(should) be a better way of doing this computation! |
| 4230 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4231 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 4232 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 590c352 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 4233 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4234 | E = Decl->param_end(); PI != E; ++PI) { |
| 4235 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4236 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | d487941 | 2012-06-30 00:48:59 +0000 | [diff] [blame] | 4237 | if (sz.isZero()) |
| 4238 | continue; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4239 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4240 | ParmOffset += sz; |
| 4241 | } |
| 4242 | // Size of the argument frame |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4243 | S += charUnitsToString(ParmOffset); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4244 | // Block pointer and offset. |
| 4245 | S += "@?0"; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4246 | |
| 4247 | // Argument types. |
| 4248 | ParmOffset = PtrSize; |
| 4249 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 4250 | Decl->param_end(); PI != E; ++PI) { |
| 4251 | ParmVarDecl *PVDecl = *PI; |
| 4252 | QualType PType = PVDecl->getOriginalType(); |
| 4253 | if (const ArrayType *AT = |
| 4254 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4255 | // Use array's original type only if it has known number of |
| 4256 | // elements. |
| 4257 | if (!isa<ConstantArrayType>(AT)) |
| 4258 | PType = PVDecl->getType(); |
| 4259 | } else if (PType->isFunctionType()) |
| 4260 | PType = PVDecl->getType(); |
| 4261 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4262 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4263 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4264 | } |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4265 | |
| 4266 | return S; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4269 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4270 | std::string& S) { |
| 4271 | // Encode result type. |
| 4272 | getObjCEncodingForType(Decl->getResultType(), S); |
| 4273 | CharUnits ParmOffset; |
| 4274 | // Compute size of all parameters. |
| 4275 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4276 | E = Decl->param_end(); PI != E; ++PI) { |
| 4277 | QualType PType = (*PI)->getType(); |
| 4278 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4279 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4280 | continue; |
| 4281 | |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4282 | assert (sz.isPositive() && |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4283 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4284 | ParmOffset += sz; |
| 4285 | } |
| 4286 | S += charUnitsToString(ParmOffset); |
| 4287 | ParmOffset = CharUnits::Zero(); |
| 4288 | |
| 4289 | // Argument types. |
| 4290 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4291 | E = Decl->param_end(); PI != E; ++PI) { |
| 4292 | ParmVarDecl *PVDecl = *PI; |
| 4293 | QualType PType = PVDecl->getOriginalType(); |
| 4294 | if (const ArrayType *AT = |
| 4295 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4296 | // Use array's original type only if it has known number of |
| 4297 | // elements. |
| 4298 | if (!isa<ConstantArrayType>(AT)) |
| 4299 | PType = PVDecl->getType(); |
| 4300 | } else if (PType->isFunctionType()) |
| 4301 | PType = PVDecl->getType(); |
| 4302 | getObjCEncodingForType(PType, S); |
| 4303 | S += charUnitsToString(ParmOffset); |
| 4304 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 4305 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4306 | |
| 4307 | return false; |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4310 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 4311 | /// method parameter or return type. If Extended, include class names and |
| 4312 | /// block object types. |
| 4313 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 4314 | QualType T, std::string& S, |
| 4315 | bool Extended) const { |
| 4316 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 4317 | getObjCEncodingForTypeQualifier(QT, S); |
| 4318 | // Encode parameter type. |
| 4319 | getObjCEncodingForTypeImpl(T, S, true, true, 0, |
| 4320 | true /*OutermostType*/, |
| 4321 | false /*EncodingProperty*/, |
| 4322 | false /*StructField*/, |
| 4323 | Extended /*EncodeBlockParameters*/, |
| 4324 | Extended /*EncodeClassNames*/); |
| 4325 | } |
| 4326 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4327 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4328 | /// declaration. |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4329 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4330 | std::string& S, |
| 4331 | bool Extended) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4332 | // FIXME: This is not very efficient. |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4333 | // Encode return type. |
| 4334 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 4335 | Decl->getResultType(), S, Extended); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4336 | // Compute size of all parameters. |
| 4337 | // Start with computing size of a pointer in number of bytes. |
| 4338 | // FIXME: There might(should) be a better way of doing this computation! |
| 4339 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4340 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4341 | // The first two arguments (self and _cmd) are pointers; account for |
| 4342 | // their size. |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4343 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4344 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4345 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4346 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4347 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4348 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4349 | continue; |
| 4350 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4351 | assert (sz.isPositive() && |
| 4352 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4353 | ParmOffset += sz; |
| 4354 | } |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4355 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4356 | S += "@0:"; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4357 | S += charUnitsToString(PtrSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4358 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4359 | // Argument types. |
| 4360 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4361 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4362 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4363 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4364 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | a0befc0 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4365 | if (const ArrayType *AT = |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4366 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4367 | // Use array's original type only if it has known number of |
| 4368 | // elements. |
Steve Naroff | 323827e | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4369 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4370 | PType = PVDecl->getType(); |
| 4371 | } else if (PType->isFunctionType()) |
| 4372 | PType = PVDecl->getType(); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4373 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 4374 | PType, S, Extended); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4375 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4376 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4377 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4378 | |
| 4379 | return false; |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4380 | } |
| 4381 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4382 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4383 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4384 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4385 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4386 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4387 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4388 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4389 | /// encoded as single characters, followed by an identifier. Property types |
| 4390 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4391 | /// these attributes are defined by the following enumeration: |
| 4392 | /// @code |
| 4393 | /// enum PropertyAttributes { |
| 4394 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4395 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4396 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4397 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4398 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4399 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4400 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 8cf6185 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 4401 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4402 | /// kPropertyWeak = 'W' // 'weak' property |
| 4403 | /// kPropertyStrong = 'P' // property GC'able |
| 4404 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4405 | /// }; |
| 4406 | /// @endcode |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4407 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4408 | const Decl *Container, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4409 | std::string& S) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4410 | // Collect information from the property implementation decl(s). |
| 4411 | bool Dynamic = false; |
| 4412 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4413 | |
| 4414 | // FIXME: Duplicated code due to poor abstraction. |
| 4415 | if (Container) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4416 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4417 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4418 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4419 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4420 | i != e; ++i) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4421 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4422 | if (PID->getPropertyDecl() == PD) { |
| 4423 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4424 | Dynamic = true; |
| 4425 | } else { |
| 4426 | SynthesizePID = PID; |
| 4427 | } |
| 4428 | } |
| 4429 | } |
| 4430 | } else { |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4431 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4432 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4433 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4434 | i != e; ++i) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4435 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4436 | if (PID->getPropertyDecl() == PD) { |
| 4437 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4438 | Dynamic = true; |
| 4439 | } else { |
| 4440 | SynthesizePID = PID; |
| 4441 | } |
| 4442 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4443 | } |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4444 | } |
| 4445 | } |
| 4446 | |
| 4447 | // FIXME: This is not very efficient. |
| 4448 | S = "T"; |
| 4449 | |
| 4450 | // Encode result type. |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4451 | // GCC has some special rules regarding encoding of properties which |
| 4452 | // closely resembles encoding of ivars. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4453 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4454 | true /* outermost type */, |
| 4455 | true /* encoding for property */); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4456 | |
| 4457 | if (PD->isReadOnly()) { |
| 4458 | S += ",R"; |
| 4459 | } else { |
| 4460 | switch (PD->getSetterKind()) { |
| 4461 | case ObjCPropertyDecl::Assign: break; |
| 4462 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4463 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 70a315c | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 4464 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4465 | } |
| 4466 | } |
| 4467 | |
| 4468 | // It really isn't clear at all what this means, since properties |
| 4469 | // are "dynamic by default". |
| 4470 | if (Dynamic) |
| 4471 | S += ",D"; |
| 4472 | |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4473 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 4474 | S += ",N"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4475 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4476 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 4477 | S += ",G"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4478 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 4482 | S += ",S"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4483 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4484 | } |
| 4485 | |
| 4486 | if (SynthesizePID) { |
| 4487 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 4488 | S += ",V"; |
Chris Lattner | 1cbaacc | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 4489 | S += OID->getNameAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | // FIXME: OBJCGC: weak & strong |
| 4493 | } |
| 4494 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4495 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4496 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 4497 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4498 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 4499 | /// |
| 4500 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4501 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4502 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4503 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4504 | PointeeTy = UnsignedIntTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4505 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4506 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4507 | PointeeTy = IntTy; |
| 4508 | } |
| 4509 | } |
| 4510 | } |
| 4511 | |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4512 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4513 | const FieldDecl *Field) const { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4514 | // We follow the behavior of gcc, expanding structures which are |
| 4515 | // directly pointed to, and expanding embedded structures. Note that |
| 4516 | // these rules are sufficient to prevent recursive encoding of the |
| 4517 | // same type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4518 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | daef00b | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 4519 | true /* outermost type */); |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4520 | } |
| 4521 | |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4522 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 4523 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4524 | default: llvm_unreachable("Unhandled builtin type kind"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4525 | case BuiltinType::Void: return 'v'; |
| 4526 | case BuiltinType::Bool: return 'B'; |
| 4527 | case BuiltinType::Char_U: |
| 4528 | case BuiltinType::UChar: return 'C'; |
| 4529 | case BuiltinType::UShort: return 'S'; |
| 4530 | case BuiltinType::UInt: return 'I'; |
| 4531 | case BuiltinType::ULong: |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4532 | return C->getIntWidth(T) == 32 ? 'L' : 'Q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4533 | case BuiltinType::UInt128: return 'T'; |
| 4534 | case BuiltinType::ULongLong: return 'Q'; |
| 4535 | case BuiltinType::Char_S: |
| 4536 | case BuiltinType::SChar: return 'c'; |
| 4537 | case BuiltinType::Short: return 's'; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 4538 | case BuiltinType::WChar_S: |
| 4539 | case BuiltinType::WChar_U: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4540 | case BuiltinType::Int: return 'i'; |
| 4541 | case BuiltinType::Long: |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4542 | return C->getIntWidth(T) == 32 ? 'l' : 'q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4543 | case BuiltinType::LongLong: return 'q'; |
| 4544 | case BuiltinType::Int128: return 't'; |
| 4545 | case BuiltinType::Float: return 'f'; |
| 4546 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 7cba5a7 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 4547 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4548 | } |
| 4549 | } |
| 4550 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4551 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 4552 | EnumDecl *Enum = ET->getDecl(); |
| 4553 | |
| 4554 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 4555 | if (!Enum->isFixed()) |
| 4556 | return 'i'; |
| 4557 | |
| 4558 | // The encoding of a fixed enum type matches its fixed underlying type. |
| 4559 | return ObjCEncodingForPrimitiveKind(C, Enum->getIntegerType()); |
| 4560 | } |
| 4561 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4562 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4563 | QualType T, const FieldDecl *FD) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4564 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4565 | S += 'b'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4566 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 4567 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 4568 | // then the offset (in bits) of the first element, then the type of the |
| 4569 | // bitfield, then the size in bits. For example, in this structure: |
| 4570 | // |
| 4571 | // struct |
| 4572 | // { |
| 4573 | // int integer; |
| 4574 | // int flags:2; |
| 4575 | // }; |
| 4576 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4577 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4578 | // information is not especially sensible, but we're stuck with it for |
| 4579 | // compatibility with GCC, although providing it breaks anything that |
| 4580 | // actually uses runtime introspection and wants to work on both runtimes... |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4581 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4582 | const RecordDecl *RD = FD->getParent(); |
| 4583 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | a3c122d | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 4584 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4585 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 4586 | S += ObjCEncodingForEnumType(Ctx, ET); |
David Chisnall | 6f0a7d2 | 2010-12-26 20:12:30 +0000 | [diff] [blame] | 4587 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4588 | S += ObjCEncodingForPrimitiveKind(Ctx, T); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4589 | } |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4590 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4591 | } |
| 4592 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4593 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4594 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4595 | bool ExpandPointedToStructures, |
| 4596 | bool ExpandStructures, |
Daniel Dunbar | c040ce4 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4597 | const FieldDecl *FD, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4598 | bool OutermostType, |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4599 | bool EncodingProperty, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4600 | bool StructField, |
| 4601 | bool EncodeBlockParameters, |
| 4602 | bool EncodeClassNames) const { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4603 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4604 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4605 | return EncodeBitField(this, S, T, FD); |
| 4606 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4607 | return; |
| 4608 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4609 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4610 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4611 | S += 'j'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4612 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4613 | false); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4614 | return; |
| 4615 | } |
Fariborz Jahanian | d25c219 | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 4616 | |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4617 | // encoding for pointer or r3eference types. |
| 4618 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4619 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 89b660c | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 4620 | if (PT->isObjCSelType()) { |
| 4621 | S += ':'; |
| 4622 | return; |
| 4623 | } |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4624 | PointeeTy = PT->getPointeeType(); |
| 4625 | } |
| 4626 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 4627 | PointeeTy = RT->getPointeeType(); |
| 4628 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4629 | bool isReadOnly = false; |
| 4630 | // For historical/compatibility reasons, the read-only qualifier of the |
| 4631 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 4632 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4633 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4634 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4635 | if (OutermostType && T.isConstQualified()) { |
| 4636 | isReadOnly = true; |
| 4637 | S += 'r'; |
| 4638 | } |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 4639 | } else if (OutermostType) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4640 | QualType P = PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4641 | while (P->getAs<PointerType>()) |
| 4642 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4643 | if (P.isConstQualified()) { |
| 4644 | isReadOnly = true; |
| 4645 | S += 'r'; |
| 4646 | } |
| 4647 | } |
| 4648 | if (isReadOnly) { |
| 4649 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 4650 | // combinations need to be rearranged. |
| 4651 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4652 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 4653 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4654 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4655 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4656 | if (PointeeTy->isCharType()) { |
| 4657 | // char pointer types should be encoded as '*' unless it is a |
| 4658 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4659 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4660 | S += '*'; |
| 4661 | return; |
| 4662 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4663 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 3de6b70 | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 4664 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 4665 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 4666 | S += '#'; |
| 4667 | return; |
| 4668 | } |
| 4669 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 4670 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 4671 | S += '@'; |
| 4672 | return; |
| 4673 | } |
| 4674 | // fall through... |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4675 | } |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4676 | S += '^'; |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4677 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 4678 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4679 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4680 | NULL); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4681 | return; |
| 4682 | } |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4683 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4684 | if (const ArrayType *AT = |
| 4685 | // Ignore type qualifiers etc. |
| 4686 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4687 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4688 | // Incomplete arrays are encoded as a pointer to the array element. |
| 4689 | S += '^'; |
| 4690 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4691 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4692 | false, ExpandStructures, FD); |
| 4693 | } else { |
| 4694 | S += '['; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4695 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4696 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 4697 | if (getTypeSize(CAT->getElementType()) == 0) |
| 4698 | S += '0'; |
| 4699 | else |
| 4700 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 4701 | } else { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4702 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4703 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 4704 | "Unknown array type!"); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4705 | S += '0'; |
| 4706 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4707 | |
| 4708 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4709 | false, ExpandStructures, FD); |
| 4710 | S += ']'; |
| 4711 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4712 | return; |
| 4713 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4714 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4715 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | df4cc61 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 4716 | S += '?'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4717 | return; |
| 4718 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4719 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4720 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4721 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4722 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4723 | // Anonymous structures print as '?' |
| 4724 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 4725 | S += II->getName(); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4726 | if (ClassTemplateSpecializationDecl *Spec |
| 4727 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 4728 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 4729 | std::string TemplateArgsStr |
| 4730 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4731 | TemplateArgs.data(), |
| 4732 | TemplateArgs.size(), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4733 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4734 | |
| 4735 | S += TemplateArgsStr; |
| 4736 | } |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4737 | } else { |
| 4738 | S += '?'; |
| 4739 | } |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4740 | if (ExpandStructures) { |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4741 | S += '='; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4742 | if (!RDecl->isUnion()) { |
| 4743 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 4744 | } else { |
| 4745 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4746 | FieldEnd = RDecl->field_end(); |
| 4747 | Field != FieldEnd; ++Field) { |
| 4748 | if (FD) { |
| 4749 | S += '"'; |
| 4750 | S += Field->getNameAsString(); |
| 4751 | S += '"'; |
| 4752 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4753 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4754 | // Special case bit-fields. |
| 4755 | if (Field->isBitField()) { |
| 4756 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4757 | *Field); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4758 | } else { |
| 4759 | QualType qt = Field->getType(); |
| 4760 | getLegacyIntegralTypeEncoding(qt); |
| 4761 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 4762 | FD, /*OutermostType*/false, |
| 4763 | /*EncodingProperty*/false, |
| 4764 | /*StructField*/true); |
| 4765 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4766 | } |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4767 | } |
Fariborz Jahanian | bc92fd7 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 4768 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4769 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4770 | return; |
| 4771 | } |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4772 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4773 | if (const EnumType *ET = T->getAs<EnumType>()) { |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4774 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4775 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4776 | else |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4777 | S += ObjCEncodingForEnumType(this, ET); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4778 | return; |
| 4779 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4780 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4781 | if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) { |
Steve Naroff | 49140cb | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4782 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4783 | if (EncodeBlockParameters) { |
| 4784 | const FunctionType *FT = BT->getPointeeType()->getAs<FunctionType>(); |
| 4785 | |
| 4786 | S += '<'; |
| 4787 | // Block return type |
| 4788 | getObjCEncodingForTypeImpl(FT->getResultType(), S, |
| 4789 | ExpandPointedToStructures, ExpandStructures, |
| 4790 | FD, |
| 4791 | false /* OutermostType */, |
| 4792 | EncodingProperty, |
| 4793 | false /* StructField */, |
| 4794 | EncodeBlockParameters, |
| 4795 | EncodeClassNames); |
| 4796 | // Block self |
| 4797 | S += "@?"; |
| 4798 | // Block parameters |
| 4799 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
| 4800 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(), |
| 4801 | E = FPT->arg_type_end(); I && (I != E); ++I) { |
| 4802 | getObjCEncodingForTypeImpl(*I, S, |
| 4803 | ExpandPointedToStructures, |
| 4804 | ExpandStructures, |
| 4805 | FD, |
| 4806 | false /* OutermostType */, |
| 4807 | EncodingProperty, |
| 4808 | false /* StructField */, |
| 4809 | EncodeBlockParameters, |
| 4810 | EncodeClassNames); |
| 4811 | } |
| 4812 | } |
| 4813 | S += '>'; |
| 4814 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4815 | return; |
| 4816 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4817 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4818 | // Ignore protocol qualifiers when mangling at this level. |
| 4819 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4820 | T = OT->getBaseType(); |
| 4821 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4822 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4823 | // @encode(class_name) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4824 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4825 | S += '{'; |
| 4826 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4827 | S += II->getName(); |
| 4828 | S += '='; |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4829 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4830 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4831 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4832 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4833 | if (Field->isBitField()) |
| 4834 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4835 | else |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4836 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4837 | } |
| 4838 | S += '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4839 | return; |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4840 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4841 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4842 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4843 | if (OPT->isObjCIdType()) { |
| 4844 | S += '@'; |
| 4845 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4846 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4847 | |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4848 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4849 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4850 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4851 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4852 | S += '#'; |
| 4853 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4854 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4855 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4856 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4857 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4858 | ExpandPointedToStructures, |
| 4859 | ExpandStructures, FD); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4860 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4861 | // Note that we do extended encoding of protocol qualifer list |
| 4862 | // Only when doing ivar or property encoding. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4863 | S += '"'; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4864 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4865 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4866 | S += '<'; |
| 4867 | S += (*I)->getNameAsString(); |
| 4868 | S += '>'; |
| 4869 | } |
| 4870 | S += '"'; |
| 4871 | } |
| 4872 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4873 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4874 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4875 | QualType PointeeTy = OPT->getPointeeType(); |
| 4876 | if (!EncodingProperty && |
| 4877 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4878 | // Another historical/compatibility reason. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4879 | // We encode the underlying type which comes out as |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4880 | // {...}; |
| 4881 | S += '^'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4882 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4883 | false, ExpandPointedToStructures, |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4884 | NULL); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4885 | return; |
| 4886 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4887 | |
| 4888 | S += '@'; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4889 | if (OPT->getInterfaceDecl() && |
| 4890 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4891 | S += '"'; |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4892 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4893 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4894 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4895 | S += '<'; |
| 4896 | S += (*I)->getNameAsString(); |
| 4897 | S += '>'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4898 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4899 | S += '"'; |
| 4900 | } |
| 4901 | return; |
| 4902 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4903 | |
John McCall | a9e6e8d | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4904 | // gcc just blithely ignores member pointers. |
| 4905 | // TODO: maybe there should be a mangling for these |
| 4906 | if (T->getAs<MemberPointerType>()) |
| 4907 | return; |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4908 | |
| 4909 | if (T->isVectorType()) { |
| 4910 | // This matches gcc's encoding, even though technically it is |
| 4911 | // insufficient. |
| 4912 | // FIXME. We should do a better job than gcc. |
| 4913 | return; |
| 4914 | } |
| 4915 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4916 | llvm_unreachable("@encode for type not implemented!"); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4917 | } |
| 4918 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4919 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 4920 | std::string &S, |
| 4921 | const FieldDecl *FD, |
| 4922 | bool includeVBases) const { |
| 4923 | assert(RDecl && "Expected non-null RecordDecl"); |
| 4924 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 4925 | if (!RDecl->getDefinition()) |
| 4926 | return; |
| 4927 | |
| 4928 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 4929 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 4930 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 4931 | |
| 4932 | if (CXXRec) { |
| 4933 | for (CXXRecordDecl::base_class_iterator |
| 4934 | BI = CXXRec->bases_begin(), |
| 4935 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 4936 | if (!BI->isVirtual()) { |
| 4937 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4938 | if (base->isEmpty()) |
| 4939 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 4940 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4941 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4942 | std::make_pair(offs, base)); |
| 4943 | } |
| 4944 | } |
| 4945 | } |
| 4946 | |
| 4947 | unsigned i = 0; |
| 4948 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4949 | FieldEnd = RDecl->field_end(); |
| 4950 | Field != FieldEnd; ++Field, ++i) { |
| 4951 | uint64_t offs = layout.getFieldOffset(i); |
| 4952 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4953 | std::make_pair(offs, *Field)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4954 | } |
| 4955 | |
| 4956 | if (CXXRec && includeVBases) { |
| 4957 | for (CXXRecordDecl::base_class_iterator |
| 4958 | BI = CXXRec->vbases_begin(), |
| 4959 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 4960 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4961 | if (base->isEmpty()) |
| 4962 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 4963 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); |
Argyrios Kyrtzidis | 81c0b5c | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 4964 | if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
| 4965 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 4966 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4967 | } |
| 4968 | } |
| 4969 | |
| 4970 | CharUnits size; |
| 4971 | if (CXXRec) { |
| 4972 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 4973 | } else { |
| 4974 | size = layout.getSize(); |
| 4975 | } |
| 4976 | |
| 4977 | uint64_t CurOffs = 0; |
| 4978 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 4979 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 4980 | |
Douglas Gregor | f5d6c74 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 4981 | if (CXXRec && CXXRec->isDynamicClass() && |
| 4982 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4983 | if (FD) { |
| 4984 | S += "\"_vptr$"; |
| 4985 | std::string recname = CXXRec->getNameAsString(); |
| 4986 | if (recname.empty()) recname = "?"; |
| 4987 | S += recname; |
| 4988 | S += '"'; |
| 4989 | } |
| 4990 | S += "^^?"; |
| 4991 | CurOffs += getTypeSize(VoidPtrTy); |
| 4992 | } |
| 4993 | |
| 4994 | if (!RDecl->hasFlexibleArrayMember()) { |
| 4995 | // Mark the end of the structure. |
| 4996 | uint64_t offs = toBits(size); |
| 4997 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4998 | std::make_pair(offs, (NamedDecl*)0)); |
| 4999 | } |
| 5000 | |
| 5001 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 5002 | assert(CurOffs <= CurLayObj->first); |
| 5003 | |
| 5004 | if (CurOffs < CurLayObj->first) { |
| 5005 | uint64_t padding = CurLayObj->first - CurOffs; |
| 5006 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 5007 | // packing/alignment of members is different that normal, in which case |
| 5008 | // the encoding will be out-of-sync with the real layout. |
| 5009 | // If the runtime switches to just consider the size of types without |
| 5010 | // taking into account alignment, we could make padding explicit in the |
| 5011 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 5012 | // longer then though. |
| 5013 | CurOffs += padding; |
| 5014 | } |
| 5015 | |
| 5016 | NamedDecl *dcl = CurLayObj->second; |
| 5017 | if (dcl == 0) |
| 5018 | break; // reached end of structure. |
| 5019 | |
| 5020 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 5021 | // We expand the bases without their virtual bases since those are going |
| 5022 | // in the initial structure. Note that this differs from gcc which |
| 5023 | // expands virtual bases each time one is encountered in the hierarchy, |
| 5024 | // making the encoding type bigger than it really is. |
| 5025 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5026 | assert(!base->isEmpty()); |
| 5027 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5028 | } else { |
| 5029 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 5030 | if (FD) { |
| 5031 | S += '"'; |
| 5032 | S += field->getNameAsString(); |
| 5033 | S += '"'; |
| 5034 | } |
| 5035 | |
| 5036 | if (field->isBitField()) { |
| 5037 | EncodeBitField(this, S, field->getType(), field); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5038 | CurOffs += field->getBitWidthValue(*this); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5039 | } else { |
| 5040 | QualType qt = field->getType(); |
| 5041 | getLegacyIntegralTypeEncoding(qt); |
| 5042 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 5043 | /*OutermostType*/false, |
| 5044 | /*EncodingProperty*/false, |
| 5045 | /*StructField*/true); |
| 5046 | CurOffs += getTypeSize(field->getType()); |
| 5047 | } |
| 5048 | } |
| 5049 | } |
| 5050 | } |
| 5051 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5052 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 5053 | std::string& S) const { |
| 5054 | if (QT & Decl::OBJC_TQ_In) |
| 5055 | S += 'n'; |
| 5056 | if (QT & Decl::OBJC_TQ_Inout) |
| 5057 | S += 'N'; |
| 5058 | if (QT & Decl::OBJC_TQ_Out) |
| 5059 | S += 'o'; |
| 5060 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 5061 | S += 'O'; |
| 5062 | if (QT & Decl::OBJC_TQ_Byref) |
| 5063 | S += 'R'; |
| 5064 | if (QT & Decl::OBJC_TQ_Oneway) |
| 5065 | S += 'V'; |
| 5066 | } |
| 5067 | |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 5068 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 5069 | if (!ObjCIdDecl) { |
| 5070 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0); |
| 5071 | T = getObjCObjectPointerType(T); |
| 5072 | TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T); |
| 5073 | ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5074 | getTranslationUnitDecl(), |
| 5075 | SourceLocation(), SourceLocation(), |
| 5076 | &Idents.get("id"), IdInfo); |
| 5077 | } |
| 5078 | |
| 5079 | return ObjCIdDecl; |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 5082 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 5083 | if (!ObjCSelDecl) { |
| 5084 | QualType SelT = getPointerType(ObjCBuiltinSelTy); |
| 5085 | TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); |
| 5086 | ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5087 | getTranslationUnitDecl(), |
| 5088 | SourceLocation(), SourceLocation(), |
| 5089 | &Idents.get("SEL"), SelInfo); |
| 5090 | } |
| 5091 | return ObjCSelDecl; |
Fariborz Jahanian | 4bef462 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 5092 | } |
| 5093 | |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 5094 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 5095 | if (!ObjCClassDecl) { |
| 5096 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0); |
| 5097 | T = getObjCObjectPointerType(T); |
| 5098 | TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T); |
| 5099 | ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5100 | getTranslationUnitDecl(), |
| 5101 | SourceLocation(), SourceLocation(), |
| 5102 | &Idents.get("Class"), ClassInfo); |
| 5103 | } |
| 5104 | |
| 5105 | return ObjCClassDecl; |
Anders Carlsson | f56a7ae | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 5106 | } |
| 5107 | |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 5108 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 5109 | if (!ObjCProtocolClassDecl) { |
| 5110 | ObjCProtocolClassDecl |
| 5111 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 5112 | SourceLocation(), |
| 5113 | &Idents.get("Protocol"), |
| 5114 | /*PrevDecl=*/0, |
| 5115 | SourceLocation(), true); |
| 5116 | } |
| 5117 | |
| 5118 | return ObjCProtocolClassDecl; |
| 5119 | } |
| 5120 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5121 | //===----------------------------------------------------------------------===// |
| 5122 | // __builtin_va_list Construction Functions |
| 5123 | //===----------------------------------------------------------------------===// |
| 5124 | |
| 5125 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5126 | // typedef char* __builtin_va_list; |
| 5127 | QualType CharPtrType = Context->getPointerType(Context->CharTy); |
| 5128 | TypeSourceInfo *TInfo |
| 5129 | = Context->getTrivialTypeSourceInfo(CharPtrType); |
| 5130 | |
| 5131 | TypedefDecl *VaListTypeDecl |
| 5132 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5133 | Context->getTranslationUnitDecl(), |
| 5134 | SourceLocation(), SourceLocation(), |
| 5135 | &Context->Idents.get("__builtin_va_list"), |
| 5136 | TInfo); |
| 5137 | return VaListTypeDecl; |
| 5138 | } |
| 5139 | |
| 5140 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5141 | // typedef void* __builtin_va_list; |
| 5142 | QualType VoidPtrType = Context->getPointerType(Context->VoidTy); |
| 5143 | TypeSourceInfo *TInfo |
| 5144 | = Context->getTrivialTypeSourceInfo(VoidPtrType); |
| 5145 | |
| 5146 | TypedefDecl *VaListTypeDecl |
| 5147 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5148 | Context->getTranslationUnitDecl(), |
| 5149 | SourceLocation(), SourceLocation(), |
| 5150 | &Context->Idents.get("__builtin_va_list"), |
| 5151 | TInfo); |
| 5152 | return VaListTypeDecl; |
| 5153 | } |
| 5154 | |
| 5155 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5156 | // typedef struct __va_list_tag { |
| 5157 | RecordDecl *VaListTagDecl; |
| 5158 | |
| 5159 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5160 | Context->getTranslationUnitDecl(), |
| 5161 | &Context->Idents.get("__va_list_tag")); |
| 5162 | VaListTagDecl->startDefinition(); |
| 5163 | |
| 5164 | const size_t NumFields = 5; |
| 5165 | QualType FieldTypes[NumFields]; |
| 5166 | const char *FieldNames[NumFields]; |
| 5167 | |
| 5168 | // unsigned char gpr; |
| 5169 | FieldTypes[0] = Context->UnsignedCharTy; |
| 5170 | FieldNames[0] = "gpr"; |
| 5171 | |
| 5172 | // unsigned char fpr; |
| 5173 | FieldTypes[1] = Context->UnsignedCharTy; |
| 5174 | FieldNames[1] = "fpr"; |
| 5175 | |
| 5176 | // unsigned short reserved; |
| 5177 | FieldTypes[2] = Context->UnsignedShortTy; |
| 5178 | FieldNames[2] = "reserved"; |
| 5179 | |
| 5180 | // void* overflow_arg_area; |
| 5181 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5182 | FieldNames[3] = "overflow_arg_area"; |
| 5183 | |
| 5184 | // void* reg_save_area; |
| 5185 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
| 5186 | FieldNames[4] = "reg_save_area"; |
| 5187 | |
| 5188 | // Create fields |
| 5189 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5190 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
| 5191 | SourceLocation(), |
| 5192 | SourceLocation(), |
| 5193 | &Context->Idents.get(FieldNames[i]), |
| 5194 | FieldTypes[i], /*TInfo=*/0, |
| 5195 | /*BitWidth=*/0, |
| 5196 | /*Mutable=*/false, |
| 5197 | ICIS_NoInit); |
| 5198 | Field->setAccess(AS_public); |
| 5199 | VaListTagDecl->addDecl(Field); |
| 5200 | } |
| 5201 | VaListTagDecl->completeDefinition(); |
| 5202 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5203 | Context->VaListTagTy = VaListTagType; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5204 | |
| 5205 | // } __va_list_tag; |
| 5206 | TypedefDecl *VaListTagTypedefDecl |
| 5207 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5208 | Context->getTranslationUnitDecl(), |
| 5209 | SourceLocation(), SourceLocation(), |
| 5210 | &Context->Idents.get("__va_list_tag"), |
| 5211 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5212 | QualType VaListTagTypedefType = |
| 5213 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5214 | |
| 5215 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5216 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5217 | QualType VaListTagArrayType |
| 5218 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5219 | Size, ArrayType::Normal, 0); |
| 5220 | TypeSourceInfo *TInfo |
| 5221 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5222 | TypedefDecl *VaListTypedefDecl |
| 5223 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5224 | Context->getTranslationUnitDecl(), |
| 5225 | SourceLocation(), SourceLocation(), |
| 5226 | &Context->Idents.get("__builtin_va_list"), |
| 5227 | TInfo); |
| 5228 | |
| 5229 | return VaListTypedefDecl; |
| 5230 | } |
| 5231 | |
| 5232 | static TypedefDecl * |
| 5233 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5234 | // typedef struct __va_list_tag { |
| 5235 | RecordDecl *VaListTagDecl; |
| 5236 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5237 | Context->getTranslationUnitDecl(), |
| 5238 | &Context->Idents.get("__va_list_tag")); |
| 5239 | VaListTagDecl->startDefinition(); |
| 5240 | |
| 5241 | const size_t NumFields = 4; |
| 5242 | QualType FieldTypes[NumFields]; |
| 5243 | const char *FieldNames[NumFields]; |
| 5244 | |
| 5245 | // unsigned gp_offset; |
| 5246 | FieldTypes[0] = Context->UnsignedIntTy; |
| 5247 | FieldNames[0] = "gp_offset"; |
| 5248 | |
| 5249 | // unsigned fp_offset; |
| 5250 | FieldTypes[1] = Context->UnsignedIntTy; |
| 5251 | FieldNames[1] = "fp_offset"; |
| 5252 | |
| 5253 | // void* overflow_arg_area; |
| 5254 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 5255 | FieldNames[2] = "overflow_arg_area"; |
| 5256 | |
| 5257 | // void* reg_save_area; |
| 5258 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5259 | FieldNames[3] = "reg_save_area"; |
| 5260 | |
| 5261 | // Create fields |
| 5262 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5263 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5264 | VaListTagDecl, |
| 5265 | SourceLocation(), |
| 5266 | SourceLocation(), |
| 5267 | &Context->Idents.get(FieldNames[i]), |
| 5268 | FieldTypes[i], /*TInfo=*/0, |
| 5269 | /*BitWidth=*/0, |
| 5270 | /*Mutable=*/false, |
| 5271 | ICIS_NoInit); |
| 5272 | Field->setAccess(AS_public); |
| 5273 | VaListTagDecl->addDecl(Field); |
| 5274 | } |
| 5275 | VaListTagDecl->completeDefinition(); |
| 5276 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5277 | Context->VaListTagTy = VaListTagType; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5278 | |
| 5279 | // } __va_list_tag; |
| 5280 | TypedefDecl *VaListTagTypedefDecl |
| 5281 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5282 | Context->getTranslationUnitDecl(), |
| 5283 | SourceLocation(), SourceLocation(), |
| 5284 | &Context->Idents.get("__va_list_tag"), |
| 5285 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5286 | QualType VaListTagTypedefType = |
| 5287 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5288 | |
| 5289 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5290 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5291 | QualType VaListTagArrayType |
| 5292 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5293 | Size, ArrayType::Normal,0); |
| 5294 | TypeSourceInfo *TInfo |
| 5295 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5296 | TypedefDecl *VaListTypedefDecl |
| 5297 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5298 | Context->getTranslationUnitDecl(), |
| 5299 | SourceLocation(), SourceLocation(), |
| 5300 | &Context->Idents.get("__builtin_va_list"), |
| 5301 | TInfo); |
| 5302 | |
| 5303 | return VaListTypedefDecl; |
| 5304 | } |
| 5305 | |
| 5306 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5307 | // typedef int __builtin_va_list[4]; |
| 5308 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); |
| 5309 | QualType IntArrayType |
| 5310 | = Context->getConstantArrayType(Context->IntTy, |
| 5311 | Size, ArrayType::Normal, 0); |
| 5312 | TypedefDecl *VaListTypedefDecl |
| 5313 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5314 | Context->getTranslationUnitDecl(), |
| 5315 | SourceLocation(), SourceLocation(), |
| 5316 | &Context->Idents.get("__builtin_va_list"), |
| 5317 | Context->getTrivialTypeSourceInfo(IntArrayType)); |
| 5318 | |
| 5319 | return VaListTypedefDecl; |
| 5320 | } |
| 5321 | |
| 5322 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
| 5323 | TargetInfo::BuiltinVaListKind Kind) { |
| 5324 | switch (Kind) { |
| 5325 | case TargetInfo::CharPtrBuiltinVaList: |
| 5326 | return CreateCharPtrBuiltinVaListDecl(Context); |
| 5327 | case TargetInfo::VoidPtrBuiltinVaList: |
| 5328 | return CreateVoidPtrBuiltinVaListDecl(Context); |
| 5329 | case TargetInfo::PowerABIBuiltinVaList: |
| 5330 | return CreatePowerABIBuiltinVaListDecl(Context); |
| 5331 | case TargetInfo::X86_64ABIBuiltinVaList: |
| 5332 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
| 5333 | case TargetInfo::PNaClABIBuiltinVaList: |
| 5334 | return CreatePNaClABIBuiltinVaListDecl(Context); |
| 5335 | } |
| 5336 | |
| 5337 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
| 5338 | } |
| 5339 | |
| 5340 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
| 5341 | if (!BuiltinVaListDecl) |
| 5342 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); |
| 5343 | |
| 5344 | return BuiltinVaListDecl; |
| 5345 | } |
| 5346 | |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5347 | QualType ASTContext::getVaListTagType() const { |
| 5348 | // Force the creation of VaListTagTy by building the __builtin_va_list |
| 5349 | // declaration. |
| 5350 | if (VaListTagTy.isNull()) |
| 5351 | (void) getBuiltinVaListDecl(); |
| 5352 | |
| 5353 | return VaListTagTy; |
| 5354 | } |
| 5355 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5356 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5357 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 5358 | "'NSConstantString' type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5359 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5360 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 5361 | } |
| 5362 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5363 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 5364 | /// lookup. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5365 | TemplateName |
| 5366 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 5367 | UnresolvedSetIterator End) const { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5368 | unsigned size = End - Begin; |
| 5369 | assert(size > 1 && "set is not overloaded!"); |
| 5370 | |
| 5371 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 5372 | size * sizeof(FunctionTemplateDecl*)); |
| 5373 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 5374 | |
| 5375 | NamedDecl **Storage = OT->getStorage(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 5376 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5377 | NamedDecl *D = *I; |
| 5378 | assert(isa<FunctionTemplateDecl>(D) || |
| 5379 | (isa<UsingShadowDecl>(D) && |
| 5380 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 5381 | *Storage++ = D; |
| 5382 | } |
| 5383 | |
| 5384 | return TemplateName(OT); |
| 5385 | } |
| 5386 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5387 | /// \brief Retrieve the template name that represents a qualified |
| 5388 | /// template name such as \c std::vector. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5389 | TemplateName |
| 5390 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 5391 | bool TemplateKeyword, |
| 5392 | TemplateDecl *Template) const { |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 5393 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 5394 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5395 | // FIXME: Canonicalization? |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5396 | llvm::FoldingSetNodeID ID; |
| 5397 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 5398 | |
| 5399 | void *InsertPos = 0; |
| 5400 | QualifiedTemplateName *QTN = |
| 5401 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5402 | if (!QTN) { |
| 5403 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 5404 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 5405 | } |
| 5406 | |
| 5407 | return TemplateName(QTN); |
| 5408 | } |
| 5409 | |
| 5410 | /// \brief Retrieve the template name that represents a dependent |
| 5411 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5412 | TemplateName |
| 5413 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 5414 | const IdentifierInfo *Name) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5415 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 5416 | "Nested name specifier must be dependent"); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5417 | |
| 5418 | llvm::FoldingSetNodeID ID; |
| 5419 | DependentTemplateName::Profile(ID, NNS, Name); |
| 5420 | |
| 5421 | void *InsertPos = 0; |
| 5422 | DependentTemplateName *QTN = |
| 5423 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5424 | |
| 5425 | if (QTN) |
| 5426 | return TemplateName(QTN); |
| 5427 | |
| 5428 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5429 | if (CanonNNS == NNS) { |
| 5430 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 5431 | } else { |
| 5432 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 5433 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5434 | DependentTemplateName *CheckQTN = |
| 5435 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5436 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 5437 | (void)CheckQTN; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5438 | } |
| 5439 | |
| 5440 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5441 | return TemplateName(QTN); |
| 5442 | } |
| 5443 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5444 | /// \brief Retrieve the template name that represents a dependent |
| 5445 | /// template name such as \c MetaFun::template operator+. |
| 5446 | TemplateName |
| 5447 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5448 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5449 | assert((!NNS || NNS->isDependent()) && |
| 5450 | "Nested name specifier must be dependent"); |
| 5451 | |
| 5452 | llvm::FoldingSetNodeID ID; |
| 5453 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 5454 | |
| 5455 | void *InsertPos = 0; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5456 | DependentTemplateName *QTN |
| 5457 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5458 | |
| 5459 | if (QTN) |
| 5460 | return TemplateName(QTN); |
| 5461 | |
| 5462 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5463 | if (CanonNNS == NNS) { |
| 5464 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 5465 | } else { |
| 5466 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 5467 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5468 | |
| 5469 | DependentTemplateName *CheckQTN |
| 5470 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5471 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 5472 | (void)CheckQTN; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5473 | } |
| 5474 | |
| 5475 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5476 | return TemplateName(QTN); |
| 5477 | } |
| 5478 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5479 | TemplateName |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5480 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 5481 | TemplateName replacement) const { |
| 5482 | llvm::FoldingSetNodeID ID; |
| 5483 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 5484 | |
| 5485 | void *insertPos = 0; |
| 5486 | SubstTemplateTemplateParmStorage *subst |
| 5487 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 5488 | |
| 5489 | if (!subst) { |
| 5490 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 5491 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 5492 | } |
| 5493 | |
| 5494 | return TemplateName(subst); |
| 5495 | } |
| 5496 | |
| 5497 | TemplateName |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5498 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 5499 | const TemplateArgument &ArgPack) const { |
| 5500 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 5501 | llvm::FoldingSetNodeID ID; |
| 5502 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 5503 | |
| 5504 | void *InsertPos = 0; |
| 5505 | SubstTemplateTemplateParmPackStorage *Subst |
| 5506 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 5507 | |
| 5508 | if (!Subst) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5509 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5510 | ArgPack.pack_size(), |
| 5511 | ArgPack.pack_begin()); |
| 5512 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 5513 | } |
| 5514 | |
| 5515 | return TemplateName(Subst); |
| 5516 | } |
| 5517 | |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5518 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | ab13857 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 5519 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 5520 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5521 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5522 | switch (Type) { |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5523 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5524 | case TargetInfo::SignedShort: return ShortTy; |
| 5525 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 5526 | case TargetInfo::SignedInt: return IntTy; |
| 5527 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 5528 | case TargetInfo::SignedLong: return LongTy; |
| 5529 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 5530 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 5531 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 5532 | } |
| 5533 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5534 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5535 | } |
Ted Kremenek | 77c51b2 | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 5536 | |
| 5537 | //===----------------------------------------------------------------------===// |
| 5538 | // Type Predicates. |
| 5539 | //===----------------------------------------------------------------------===// |
| 5540 | |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5541 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 5542 | /// garbage collection attribute. |
| 5543 | /// |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5544 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5545 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5546 | return Qualifiers::GCNone; |
| 5547 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5548 | assert(getLangOpts().ObjC1); |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5549 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 5550 | |
| 5551 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 5552 | // (or pointers to them) be treated as though they were declared |
| 5553 | // as __strong. |
| 5554 | if (GCAttrs == Qualifiers::GCNone) { |
| 5555 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 5556 | return Qualifiers::Strong; |
| 5557 | else if (Ty->isPointerType()) |
| 5558 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 5559 | } else { |
| 5560 | // It's not valid to set GC attributes on anything that isn't a |
| 5561 | // pointer. |
| 5562 | #ifndef NDEBUG |
| 5563 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 5564 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 5565 | CT = AT->getElementType(); |
| 5566 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 5567 | #endif |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5568 | } |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 5569 | return GCAttrs; |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5572 | //===----------------------------------------------------------------------===// |
| 5573 | // Type Compatibility Testing |
| 5574 | //===----------------------------------------------------------------------===// |
Chris Lattner | b338a6b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 5575 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5576 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5577 | /// compatible. |
| 5578 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 5579 | const VectorType *RHS) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 5580 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5581 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5582 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5583 | } |
| 5584 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5585 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 5586 | QualType SecondVec) { |
| 5587 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 5588 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 5589 | |
| 5590 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 5591 | return true; |
| 5592 | |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5593 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 5594 | // equivalent GCC vector types. |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5595 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 5596 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5597 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5598 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5599 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 5600 | First->getVectorKind() != VectorType::AltiVecBool && |
| 5601 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 5602 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5603 | return true; |
| 5604 | |
| 5605 | return false; |
| 5606 | } |
| 5607 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5608 | //===----------------------------------------------------------------------===// |
| 5609 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 5610 | //===----------------------------------------------------------------------===// |
| 5611 | |
| 5612 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 5613 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5614 | bool |
| 5615 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 5616 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5617 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5618 | return true; |
| 5619 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 5620 | E = rProto->protocol_end(); PI != E; ++PI) |
| 5621 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 5622 | return true; |
| 5623 | return false; |
| 5624 | } |
| 5625 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5626 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 5627 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 5628 | /// otherwise. |
| 5629 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 5630 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 5631 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 5632 | return false; |
| 5633 | } |
| 5634 | |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5635 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 5636 | /// Class<p1, ...>. |
| 5637 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 5638 | QualType rhs) { |
| 5639 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 5640 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 5641 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 5642 | |
| 5643 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5644 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5645 | bool match = false; |
| 5646 | ObjCProtocolDecl *lhsProto = *I; |
| 5647 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5648 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5649 | ObjCProtocolDecl *rhsProto = *J; |
| 5650 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 5651 | match = true; |
| 5652 | break; |
| 5653 | } |
| 5654 | } |
| 5655 | if (!match) |
| 5656 | return false; |
| 5657 | } |
| 5658 | return true; |
| 5659 | } |
| 5660 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5661 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 5662 | /// ObjCQualifiedIDType. |
| 5663 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 5664 | bool compare) { |
| 5665 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5666 | if (lhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5667 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 5668 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5669 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5670 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 5671 | return true; |
| 5672 | |
| 5673 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5674 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5675 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5676 | if (!rhsOPT) return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5677 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5678 | if (rhsOPT->qual_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5679 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5680 | // make sure we check the class hierarchy. |
| 5681 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5682 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5683 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5684 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5685 | // see if static class implements all of id's protocols, directly or |
| 5686 | // through its super class and categories. |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5687 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5688 | return false; |
| 5689 | } |
| 5690 | } |
| 5691 | // If there are no qualifiers and no interface, we have an 'id'. |
| 5692 | return true; |
| 5693 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5694 | // Both the right and left sides have qualifiers. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5695 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5696 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5697 | ObjCProtocolDecl *lhsProto = *I; |
| 5698 | bool match = false; |
| 5699 | |
| 5700 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5701 | // see if static class implements all of id's protocols, directly or |
| 5702 | // through its super class and categories. |
| 5703 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5704 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5705 | ObjCProtocolDecl *rhsProto = *J; |
| 5706 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5707 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5708 | match = true; |
| 5709 | break; |
| 5710 | } |
| 5711 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5712 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5713 | // make sure we check the class hierarchy. |
| 5714 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5715 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5716 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5717 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5718 | // see if static class implements all of id's protocols, directly or |
| 5719 | // through its super class and categories. |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5720 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5721 | match = true; |
| 5722 | break; |
| 5723 | } |
| 5724 | } |
| 5725 | } |
| 5726 | if (!match) |
| 5727 | return false; |
| 5728 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5729 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5730 | return true; |
| 5731 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5732 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5733 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 5734 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 5735 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5736 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5737 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5738 | // If both the right and left sides have qualifiers. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5739 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 5740 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 5741 | ObjCProtocolDecl *lhsProto = *I; |
| 5742 | bool match = false; |
| 5743 | |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5744 | // 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] | 5745 | // see if static class implements all of id's protocols, directly or |
| 5746 | // through its super class and categories. |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5747 | // First, lhs protocols in the qualifier list must be found, direct |
| 5748 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5749 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5750 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5751 | ObjCProtocolDecl *rhsProto = *J; |
| 5752 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5753 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5754 | match = true; |
| 5755 | break; |
| 5756 | } |
| 5757 | } |
| 5758 | if (!match) |
| 5759 | return false; |
| 5760 | } |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5761 | |
| 5762 | // Static class's protocols, or its super class or category protocols |
| 5763 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 5764 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 5765 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 5766 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 5767 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 5768 | // no type qualifier and its class has no static protocol(s) |
| 5769 | // assume that it is mismatch. |
| 5770 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 5771 | return false; |
| 5772 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5773 | LHSInheritedProtocols.begin(), |
| 5774 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 5775 | bool match = false; |
| 5776 | ObjCProtocolDecl *lhsProto = (*I); |
| 5777 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5778 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5779 | ObjCProtocolDecl *rhsProto = *J; |
| 5780 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5781 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5782 | match = true; |
| 5783 | break; |
| 5784 | } |
| 5785 | } |
| 5786 | if (!match) |
| 5787 | return false; |
| 5788 | } |
| 5789 | } |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5790 | return true; |
| 5791 | } |
| 5792 | return false; |
| 5793 | } |
| 5794 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5795 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5796 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 5797 | /// protocol qualifiers on the LHS or RHS. |
| 5798 | /// |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5799 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 5800 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5801 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5802 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5803 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 5804 | // 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] | 5805 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 5806 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5807 | return true; |
| 5808 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5809 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5810 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5811 | QualType(RHSOPT,0), |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5812 | false); |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5813 | |
| 5814 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 5815 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 5816 | QualType(RHSOPT,0)); |
| 5817 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5818 | // If we have 2 user-defined types, fall into that path. |
| 5819 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5820 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5821 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5822 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5823 | } |
| 5824 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5825 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 5826 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5827 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 5828 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 5829 | /// not OK. For the return type, the opposite is not OK. |
| 5830 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 5831 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5832 | const ObjCObjectPointerType *RHSOPT, |
| 5833 | bool BlockReturnType) { |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5834 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5835 | return true; |
| 5836 | |
| 5837 | if (LHSOPT->isObjCBuiltinType()) { |
| 5838 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 5839 | } |
| 5840 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5841 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5842 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5843 | QualType(RHSOPT,0), |
| 5844 | false); |
| 5845 | |
| 5846 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 5847 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 5848 | if (LHS && RHS) { // We have 2 user-defined types. |
| 5849 | if (LHS != RHS) { |
| 5850 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5851 | return BlockReturnType; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5852 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5853 | return !BlockReturnType; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5854 | } |
| 5855 | else |
| 5856 | return true; |
| 5857 | } |
| 5858 | return false; |
| 5859 | } |
| 5860 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5861 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 5862 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 5863 | /// It is used to build composite qualifier list of the composite type of |
| 5864 | /// the conditional expression involving two objective-c pointer objects. |
| 5865 | static |
| 5866 | void getIntersectionOfProtocols(ASTContext &Context, |
| 5867 | const ObjCObjectPointerType *LHSOPT, |
| 5868 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5869 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5870 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5871 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5872 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5873 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 5874 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5875 | |
| 5876 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 5877 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 5878 | if (LHSNumProtocols > 0) |
| 5879 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 5880 | else { |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5881 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5882 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 5883 | LHSInheritedProtocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5884 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 5885 | LHSInheritedProtocols.end()); |
| 5886 | } |
| 5887 | |
| 5888 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 5889 | if (RHSNumProtocols > 0) { |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 5890 | ObjCProtocolDecl **RHSProtocols = |
| 5891 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5892 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 5893 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 5894 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 5895 | } else { |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5896 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5897 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 5898 | RHSInheritedProtocols); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5899 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5900 | RHSInheritedProtocols.begin(), |
| 5901 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 5902 | if (InheritedProtocolSet.count((*I))) |
| 5903 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5904 | } |
| 5905 | } |
| 5906 | |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5907 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 5908 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 5909 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 5910 | /// warning is issued. So, its invokation is extremely rare. |
| 5911 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5912 | const ObjCObjectPointerType *Lptr, |
| 5913 | const ObjCObjectPointerType *Rptr) { |
| 5914 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 5915 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 5916 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 5917 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5918 | if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl))) |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5919 | return QualType(); |
| 5920 | |
Fariborz Jahanian | b107143 | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5921 | do { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5922 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5923 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5924 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5925 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 5926 | |
| 5927 | QualType Result = QualType(LHS, 0); |
| 5928 | if (!Protocols.empty()) |
| 5929 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 5930 | Result = getObjCObjectPointerType(Result); |
| 5931 | return Result; |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5932 | } |
Fariborz Jahanian | b107143 | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5933 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5934 | |
| 5935 | return QualType(); |
| 5936 | } |
| 5937 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5938 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 5939 | const ObjCObjectType *RHS) { |
| 5940 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 5941 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 5942 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5943 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 5944 | // the LHS. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5945 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5946 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5947 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5948 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 5949 | // protocol qualified at all, then we are good. |
Steve Naroff | c277ad1 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 5950 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5951 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5952 | |
Fariborz Jahanian | 4806ff8 | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5953 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 5954 | // more detailed analysis is required. |
| 5955 | if (RHS->getNumProtocols() == 0) { |
| 5956 | // OK, if LHS is a superclass of RHS *and* |
| 5957 | // this superclass is assignment compatible with LHS. |
| 5958 | // false otherwise. |
Fariborz Jahanian | 240400b | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5959 | bool IsSuperClass = |
| 5960 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 5961 | if (IsSuperClass) { |
Fariborz Jahanian | 4806ff8 | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5962 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 5963 | // ; i.e., SuperClass may implement at least one of the protocols |
| 5964 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 5965 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 5966 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 240400b | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5967 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | 4806ff8 | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5968 | // If super class has no protocols, it is not a match. |
| 5969 | if (SuperClassInheritedProtocols.empty()) |
| 5970 | return false; |
| 5971 | |
| 5972 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5973 | LHSPE = LHS->qual_end(); |
| 5974 | LHSPI != LHSPE; LHSPI++) { |
| 5975 | bool SuperImplementsProtocol = false; |
| 5976 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 5977 | |
| 5978 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5979 | SuperClassInheritedProtocols.begin(), |
| 5980 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 5981 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 5982 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 5983 | SuperImplementsProtocol = true; |
| 5984 | break; |
| 5985 | } |
| 5986 | } |
| 5987 | if (!SuperImplementsProtocol) |
| 5988 | return false; |
| 5989 | } |
| 5990 | return true; |
| 5991 | } |
| 5992 | return false; |
| 5993 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5994 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5995 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5996 | LHSPE = LHS->qual_end(); |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5997 | LHSPI != LHSPE; LHSPI++) { |
| 5998 | bool RHSImplementsProtocol = false; |
| 5999 | |
| 6000 | // If the RHS doesn't implement the protocol on the left, the types |
| 6001 | // are incompatible. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6002 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 6003 | RHSPE = RHS->qual_end(); |
Steve Naroff | fac5bc9 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 6004 | RHSPI != RHSPE; RHSPI++) { |
| 6005 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6006 | RHSImplementsProtocol = true; |
Steve Naroff | fac5bc9 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 6007 | break; |
| 6008 | } |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6009 | } |
| 6010 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 6011 | if (!RHSImplementsProtocol) |
| 6012 | return false; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6013 | } |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6014 | // The RHS implements all protocols listed on the LHS. |
| 6015 | return true; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6016 | } |
| 6017 | |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6018 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 6019 | // get the "pointed to" types |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6020 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 6021 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6022 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6023 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6024 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6025 | |
| 6026 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 6027 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6028 | } |
| 6029 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6030 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 6031 | return canAssignObjCInterfaces( |
| 6032 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 6033 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 6034 | } |
| 6035 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6036 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6037 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6038 | /// 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] | 6039 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6040 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 6041 | bool CompareUnqualified) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6042 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6043 | return hasSameType(LHS, RHS); |
| 6044 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6045 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6046 | } |
| 6047 | |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6048 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | c87c879 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 6049 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6050 | } |
| 6051 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6052 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 6053 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 6054 | } |
| 6055 | |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6056 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 6057 | /// of T is compatible with SubType, return the merged type, else return |
| 6058 | /// QualType() |
| 6059 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 6060 | bool OfBlockPointer, |
| 6061 | bool Unqualified) { |
| 6062 | if (const RecordType *UT = T->getAsUnionType()) { |
| 6063 | RecordDecl *UD = UT->getDecl(); |
| 6064 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 6065 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6066 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | 19b961d | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 6067 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6068 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 6069 | if (!MT.isNull()) |
| 6070 | return MT; |
| 6071 | } |
| 6072 | } |
| 6073 | } |
| 6074 | |
| 6075 | return QualType(); |
| 6076 | } |
| 6077 | |
| 6078 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 6079 | /// argument types |
| 6080 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 6081 | bool OfBlockPointer, |
| 6082 | bool Unqualified) { |
| 6083 | // GNU extension: two types are compatible if they appear as a function |
| 6084 | // argument, one of the types is a transparent union type and the other |
| 6085 | // type is compatible with a union member |
| 6086 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 6087 | Unqualified); |
| 6088 | if (!lmerge.isNull()) |
| 6089 | return lmerge; |
| 6090 | |
| 6091 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 6092 | Unqualified); |
| 6093 | if (!rmerge.isNull()) |
| 6094 | return rmerge; |
| 6095 | |
| 6096 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 6097 | } |
| 6098 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6099 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6100 | bool OfBlockPointer, |
| 6101 | bool Unqualified) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6102 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 6103 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6104 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 6105 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6106 | bool allLTypes = true; |
| 6107 | bool allRTypes = true; |
| 6108 | |
| 6109 | // Check return type |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6110 | QualType retType; |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6111 | if (OfBlockPointer) { |
| 6112 | QualType RHS = rbase->getResultType(); |
| 6113 | QualType LHS = lbase->getResultType(); |
| 6114 | bool UnqualifiedResult = Unqualified; |
| 6115 | if (!UnqualifiedResult) |
| 6116 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6117 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6118 | } |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6119 | else |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6120 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 6121 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6122 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6123 | |
| 6124 | if (Unqualified) |
| 6125 | retType = retType.getUnqualifiedType(); |
| 6126 | |
| 6127 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 6128 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 6129 | if (Unqualified) { |
| 6130 | LRetType = LRetType.getUnqualifiedType(); |
| 6131 | RRetType = RRetType.getUnqualifiedType(); |
| 6132 | } |
| 6133 | |
| 6134 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6135 | allLTypes = false; |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6136 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6137 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6138 | |
Daniel Dunbar | edd5bae | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 6139 | // FIXME: double check this |
| 6140 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 6141 | // rbase->getRegParmAttr() != 0 && |
| 6142 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 6143 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 6144 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6145 | |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6146 | // Compatible functions must have compatible calling conventions |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6147 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6148 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6149 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6150 | // Regparm is part of the calling convention. |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 6151 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 6152 | return QualType(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6153 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 6154 | return QualType(); |
| 6155 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6156 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 6157 | return QualType(); |
| 6158 | |
Fariborz Jahanian | 48c6910 | 2011-10-05 00:05:34 +0000 | [diff] [blame] | 6159 | // functypes which return are preferred over those that do not. |
| 6160 | if (lbaseInfo.getNoReturn() && !rbaseInfo.getNoReturn()) |
| 6161 | allLTypes = false; |
| 6162 | else if (!lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn()) |
| 6163 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6164 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 6165 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6166 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6167 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6168 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6169 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6170 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 6171 | "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6172 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 6173 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 6174 | |
| 6175 | // Compatible functions must have the same number of arguments |
| 6176 | if (lproto_nargs != rproto_nargs) |
| 6177 | return QualType(); |
| 6178 | |
| 6179 | // Variadic and non-variadic functions aren't compatible |
| 6180 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 6181 | return QualType(); |
| 6182 | |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 6183 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 6184 | return QualType(); |
| 6185 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6186 | if (LangOpts.ObjCAutoRefCount && |
| 6187 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 6188 | return QualType(); |
| 6189 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6190 | // Check argument compatibility |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6191 | SmallVector<QualType, 10> types; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6192 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 6193 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 6194 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6195 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 6196 | OfBlockPointer, |
| 6197 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6198 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6199 | |
| 6200 | if (Unqualified) |
| 6201 | argtype = argtype.getUnqualifiedType(); |
| 6202 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6203 | types.push_back(argtype); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6204 | if (Unqualified) { |
| 6205 | largtype = largtype.getUnqualifiedType(); |
| 6206 | rargtype = rargtype.getUnqualifiedType(); |
| 6207 | } |
| 6208 | |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6209 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 6210 | allLTypes = false; |
| 6211 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 6212 | allRTypes = false; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6213 | } |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6214 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6215 | if (allLTypes) return lhs; |
| 6216 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6217 | |
| 6218 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 6219 | EPI.ExtInfo = einfo; |
| 6220 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6221 | } |
| 6222 | |
| 6223 | if (lproto) allRTypes = false; |
| 6224 | if (rproto) allLTypes = false; |
| 6225 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6226 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6227 | if (proto) { |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6228 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6229 | if (proto->isVariadic()) return QualType(); |
| 6230 | // Check that the types are compatible with the types that |
| 6231 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 6232 | // The only types actually affected are promotable integer |
| 6233 | // types and floats, which would be passed as a different |
| 6234 | // type depending on whether the prototype is visible. |
| 6235 | unsigned proto_nargs = proto->getNumArgs(); |
| 6236 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 6237 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | 2973d40 | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 6238 | |
| 6239 | // Look at the promotion type of enum types, since that is the type used |
| 6240 | // to pass enum values. |
| 6241 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 6242 | argTy = Enum->getDecl()->getPromotionType(); |
| 6243 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6244 | if (argTy->isPromotableIntegerType() || |
| 6245 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 6246 | return QualType(); |
| 6247 | } |
| 6248 | |
| 6249 | if (allLTypes) return lhs; |
| 6250 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6251 | |
| 6252 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 6253 | EPI.ExtInfo = einfo; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6254 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6255 | proto->getNumArgs(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6256 | } |
| 6257 | |
| 6258 | if (allLTypes) return lhs; |
| 6259 | if (allRTypes) return rhs; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6260 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6261 | } |
| 6262 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6263 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6264 | bool OfBlockPointer, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6265 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | db4e349 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 6266 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 6267 | // type is adjusted to "T" prior to any further analysis, the expression |
| 6268 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 6269 | // expression is an lvalue unless the reference is an rvalue reference and |
| 6270 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6271 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 6272 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6273 | |
| 6274 | if (Unqualified) { |
| 6275 | LHS = LHS.getUnqualifiedType(); |
| 6276 | RHS = RHS.getUnqualifiedType(); |
| 6277 | } |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6278 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6279 | QualType LHSCan = getCanonicalType(LHS), |
| 6280 | RHSCan = getCanonicalType(RHS); |
| 6281 | |
| 6282 | // If two types are identical, they are compatible. |
| 6283 | if (LHSCan == RHSCan) |
| 6284 | return LHS; |
| 6285 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6286 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6287 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 6288 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6289 | if (LQuals != RQuals) { |
| 6290 | // If any of these qualifiers are different, we have a type |
| 6291 | // mismatch. |
| 6292 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6293 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 6294 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6295 | return QualType(); |
| 6296 | |
| 6297 | // Exactly one GC qualifier difference is allowed: __strong is |
| 6298 | // okay if the other type has no GC qualifier but is an Objective |
| 6299 | // C object pointer (i.e. implicitly strong by default). We fix |
| 6300 | // this by pretending that the unqualified type was actually |
| 6301 | // qualified __strong. |
| 6302 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 6303 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 6304 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 6305 | |
| 6306 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 6307 | return QualType(); |
| 6308 | |
| 6309 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 6310 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 6311 | } |
| 6312 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 6313 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 6314 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6315 | return QualType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6316 | } |
| 6317 | |
| 6318 | // Okay, qualifiers are equal. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6319 | |
Eli Friedman | dcca633 | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 6320 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 6321 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6322 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6323 | // We want to consider the two function types to be the same for these |
| 6324 | // comparisons, just force one to the other. |
| 6325 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 6326 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 16f9096 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 6327 | |
| 6328 | // Same as above for arrays |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 6329 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 6330 | LHSClass = Type::ConstantArray; |
| 6331 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 6332 | RHSClass = Type::ConstantArray; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6333 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6334 | // ObjCInterfaces are just specialized ObjCObjects. |
| 6335 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 6336 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 6337 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 6338 | // Canonicalize ExtVector -> Vector. |
| 6339 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 6340 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6341 | |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 6342 | // If the canonical type classes don't match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6343 | if (LHSClass != RHSClass) { |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6344 | // 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] | 6345 | // a signed integer type, or an unsigned integer type. |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 6346 | // Compatibility is based on the underlying type, not the promotion |
| 6347 | // type. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6348 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Fariborz Jahanian | adfe905 | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 6349 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 6350 | if (!TINT.isNull() && hasSameType(TINT, RHSCan.getUnqualifiedType())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6351 | return RHS; |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 6352 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6353 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Fariborz Jahanian | adfe905 | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 6354 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 6355 | if (!TINT.isNull() && hasSameType(TINT, LHSCan.getUnqualifiedType())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6356 | return LHS; |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 6357 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 6358 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 194904e | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 6359 | if (OfBlockPointer && !BlockReturnType) { |
| 6360 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 6361 | return LHS; |
| 6362 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 6363 | return RHS; |
| 6364 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 6365 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6366 | return QualType(); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6367 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6368 | |
Steve Naroff | c6edcbd | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 6369 | // The canonical type classes match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6370 | switch (LHSClass) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6371 | #define TYPE(Class, Base) |
| 6372 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 6373 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6374 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 6375 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 6376 | #include "clang/AST/TypeNodes.def" |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6377 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6378 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 6379 | case Type::LValueReference: |
| 6380 | case Type::RValueReference: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6381 | case Type::MemberPointer: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6382 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6383 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6384 | case Type::ObjCInterface: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6385 | case Type::IncompleteArray: |
| 6386 | case Type::VariableArray: |
| 6387 | case Type::FunctionProto: |
| 6388 | case Type::ExtVector: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6389 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6390 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6391 | case Type::Pointer: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6392 | { |
| 6393 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6394 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 6395 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6396 | if (Unqualified) { |
| 6397 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 6398 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 6399 | } |
| 6400 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 6401 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6402 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 6403 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6404 | return LHS; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 6405 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6406 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6407 | return getPointerType(ResultType); |
| 6408 | } |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 6409 | case Type::BlockPointer: |
| 6410 | { |
| 6411 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6412 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 6413 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6414 | if (Unqualified) { |
| 6415 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 6416 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 6417 | } |
| 6418 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 6419 | Unqualified); |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 6420 | if (ResultType.isNull()) return QualType(); |
| 6421 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 6422 | return LHS; |
| 6423 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 6424 | return RHS; |
| 6425 | return getBlockPointerType(ResultType); |
| 6426 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6427 | case Type::Atomic: |
| 6428 | { |
| 6429 | // Merge two pointer types, while trying to preserve typedef info |
| 6430 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 6431 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 6432 | if (Unqualified) { |
| 6433 | LHSValue = LHSValue.getUnqualifiedType(); |
| 6434 | RHSValue = RHSValue.getUnqualifiedType(); |
| 6435 | } |
| 6436 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 6437 | Unqualified); |
| 6438 | if (ResultType.isNull()) return QualType(); |
| 6439 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 6440 | return LHS; |
| 6441 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 6442 | return RHS; |
| 6443 | return getAtomicType(ResultType); |
| 6444 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6445 | case Type::ConstantArray: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6446 | { |
| 6447 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 6448 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 6449 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 6450 | return QualType(); |
| 6451 | |
| 6452 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 6453 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6454 | if (Unqualified) { |
| 6455 | LHSElem = LHSElem.getUnqualifiedType(); |
| 6456 | RHSElem = RHSElem.getUnqualifiedType(); |
| 6457 | } |
| 6458 | |
| 6459 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6460 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6461 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6462 | return LHS; |
| 6463 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6464 | return RHS; |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6465 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 6466 | ArrayType::ArraySizeModifier(), 0); |
| 6467 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 6468 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6469 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 6470 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6471 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6472 | return LHS; |
| 6473 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6474 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6475 | if (LVAT) { |
| 6476 | // FIXME: This isn't correct! But tricky to implement because |
| 6477 | // the array's size has to be the size of LHS, but the type |
| 6478 | // has to be different. |
| 6479 | return LHS; |
| 6480 | } |
| 6481 | if (RVAT) { |
| 6482 | // FIXME: This isn't correct! But tricky to implement because |
| 6483 | // the array's size has to be the size of RHS, but the type |
| 6484 | // has to be different. |
| 6485 | return RHS; |
| 6486 | } |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6487 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 6488 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 6489 | return getIncompleteArrayType(ResultType, |
| 6490 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6491 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6492 | case Type::FunctionNoProto: |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6493 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6494 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6495 | case Type::Enum: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6496 | return QualType(); |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6497 | case Type::Builtin: |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6498 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6499 | return QualType(); |
Daniel Dunbar | 804c044 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 6500 | case Type::Complex: |
| 6501 | // Distinct complex types are incompatible. |
| 6502 | return QualType(); |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6503 | case Type::Vector: |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6504 | // FIXME: The merged type should be an ExtVector! |
John McCall | 44c064b | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 6505 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 6506 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6507 | return LHS; |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6508 | return QualType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6509 | case Type::ObjCObject: { |
| 6510 | // Check if the types are assignment compatible. |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6511 | // FIXME: This should be type compatibility, e.g. whether |
| 6512 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6513 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 6514 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 6515 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 7a7814c | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 6516 | return LHS; |
| 6517 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6518 | return QualType(); |
Cedric Venet | 4fc88b7 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 6519 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6520 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6521 | if (OfBlockPointer) { |
| 6522 | if (canAssignObjCInterfacesInBlockPointer( |
| 6523 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6524 | RHS->getAs<ObjCObjectPointerType>(), |
| 6525 | BlockReturnType)) |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6526 | return LHS; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6527 | return QualType(); |
| 6528 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6529 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 6530 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6531 | return LHS; |
| 6532 | |
Steve Naroff | c68cfcf | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 6533 | return QualType(); |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6534 | } |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6535 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6536 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6537 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6538 | } |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 6539 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6540 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 6541 | const FunctionProtoType *FromFunctionType, |
| 6542 | const FunctionProtoType *ToFunctionType) { |
| 6543 | if (FromFunctionType->hasAnyConsumedArgs() != |
| 6544 | ToFunctionType->hasAnyConsumedArgs()) |
| 6545 | return false; |
| 6546 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 6547 | FromFunctionType->getExtProtoInfo(); |
| 6548 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 6549 | ToFunctionType->getExtProtoInfo(); |
| 6550 | if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments) |
| 6551 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 6552 | ArgIdx != NumArgs; ++ArgIdx) { |
| 6553 | if (FromEPI.ConsumedArguments[ArgIdx] != |
| 6554 | ToEPI.ConsumedArguments[ArgIdx]) |
| 6555 | return false; |
| 6556 | } |
| 6557 | return true; |
| 6558 | } |
| 6559 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6560 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 6561 | /// 'RHS' attributes and returns the merged version; including for function |
| 6562 | /// return types. |
| 6563 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 6564 | QualType LHSCan = getCanonicalType(LHS), |
| 6565 | RHSCan = getCanonicalType(RHS); |
| 6566 | // If two types are identical, they are compatible. |
| 6567 | if (LHSCan == RHSCan) |
| 6568 | return LHS; |
| 6569 | if (RHSCan->isFunctionType()) { |
| 6570 | if (!LHSCan->isFunctionType()) |
| 6571 | return QualType(); |
| 6572 | QualType OldReturnType = |
| 6573 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 6574 | QualType NewReturnType = |
| 6575 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 6576 | QualType ResReturnType = |
| 6577 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 6578 | if (ResReturnType.isNull()) |
| 6579 | return QualType(); |
| 6580 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 6581 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 6582 | // In either case, use OldReturnType to build the new function type. |
| 6583 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 6584 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6585 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6586 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6587 | QualType ResultType |
| 6588 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6589 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6590 | return ResultType; |
| 6591 | } |
| 6592 | } |
| 6593 | return QualType(); |
| 6594 | } |
| 6595 | |
| 6596 | // If the qualifiers are different, the types can still be merged. |
| 6597 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 6598 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 6599 | if (LQuals != RQuals) { |
| 6600 | // If any of these qualifiers are different, we have a type mismatch. |
| 6601 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 6602 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 6603 | return QualType(); |
| 6604 | |
| 6605 | // Exactly one GC qualifier difference is allowed: __strong is |
| 6606 | // okay if the other type has no GC qualifier but is an Objective |
| 6607 | // C object pointer (i.e. implicitly strong by default). We fix |
| 6608 | // this by pretending that the unqualified type was actually |
| 6609 | // qualified __strong. |
| 6610 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 6611 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 6612 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 6613 | |
| 6614 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 6615 | return QualType(); |
| 6616 | |
| 6617 | if (GC_L == Qualifiers::Strong) |
| 6618 | return LHS; |
| 6619 | if (GC_R == Qualifiers::Strong) |
| 6620 | return RHS; |
| 6621 | return QualType(); |
| 6622 | } |
| 6623 | |
| 6624 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 6625 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6626 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6627 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 6628 | if (ResQT == LHSBaseQT) |
| 6629 | return LHS; |
| 6630 | if (ResQT == RHSBaseQT) |
| 6631 | return RHS; |
| 6632 | } |
| 6633 | return QualType(); |
| 6634 | } |
| 6635 | |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 6636 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6637 | // Integer Predicates |
| 6638 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3c91971 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 6639 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6640 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6641 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | ee275c8 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 6642 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6643 | if (T->isBooleanType()) |
| 6644 | return 1; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 6645 | // For builtin types, just use the standard type sizing method |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6646 | return (unsigned)getTypeSize(T); |
| 6647 | } |
| 6648 | |
| 6649 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6650 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6651 | |
| 6652 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 6653 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 6654 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6655 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6656 | |
| 6657 | // For enums, we return the unsigned version of the base type. |
| 6658 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6659 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6660 | |
| 6661 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 6662 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6663 | switch (BTy->getKind()) { |
| 6664 | case BuiltinType::Char_S: |
| 6665 | case BuiltinType::SChar: |
| 6666 | return UnsignedCharTy; |
| 6667 | case BuiltinType::Short: |
| 6668 | return UnsignedShortTy; |
| 6669 | case BuiltinType::Int: |
| 6670 | return UnsignedIntTy; |
| 6671 | case BuiltinType::Long: |
| 6672 | return UnsignedLongTy; |
| 6673 | case BuiltinType::LongLong: |
| 6674 | return UnsignedLongLongTy; |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 6675 | case BuiltinType::Int128: |
| 6676 | return UnsignedInt128Ty; |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6677 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6678 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6679 | } |
| 6680 | } |
| 6681 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 6682 | ASTMutationListener::~ASTMutationListener() { } |
| 6683 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6684 | |
| 6685 | //===----------------------------------------------------------------------===// |
| 6686 | // Builtin Type Computation |
| 6687 | //===----------------------------------------------------------------------===// |
| 6688 | |
| 6689 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6690 | /// pointer over the consumed characters. This returns the resultant type. If |
| 6691 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 6692 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 6693 | /// a vector of "i*". |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6694 | /// |
| 6695 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 6696 | /// to be an Integer Constant Expression. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6697 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6698 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6699 | bool &RequiresICE, |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6700 | bool AllowTypeModifiers) { |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6701 | // Modifiers. |
| 6702 | int HowLong = 0; |
| 6703 | bool Signed = false, Unsigned = false; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6704 | RequiresICE = false; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6705 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6706 | // Read the prefixed modifiers first. |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6707 | bool Done = false; |
| 6708 | while (!Done) { |
| 6709 | switch (*Str++) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6710 | default: Done = true; --Str; break; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6711 | case 'I': |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6712 | RequiresICE = true; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6713 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6714 | case 'S': |
| 6715 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 6716 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 6717 | Signed = true; |
| 6718 | break; |
| 6719 | case 'U': |
| 6720 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 6721 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 6722 | Unsigned = true; |
| 6723 | break; |
| 6724 | case 'L': |
| 6725 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 6726 | ++HowLong; |
| 6727 | break; |
| 6728 | } |
| 6729 | } |
| 6730 | |
| 6731 | QualType Type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6732 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6733 | // Read the base type. |
| 6734 | switch (*Str++) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6735 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6736 | case 'v': |
| 6737 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6738 | "Bad modifiers used with 'v'!"); |
| 6739 | Type = Context.VoidTy; |
| 6740 | break; |
| 6741 | case 'f': |
| 6742 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6743 | "Bad modifiers used with 'f'!"); |
| 6744 | Type = Context.FloatTy; |
| 6745 | break; |
| 6746 | case 'd': |
| 6747 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 6748 | "Bad modifiers used with 'd'!"); |
| 6749 | if (HowLong) |
| 6750 | Type = Context.LongDoubleTy; |
| 6751 | else |
| 6752 | Type = Context.DoubleTy; |
| 6753 | break; |
| 6754 | case 's': |
| 6755 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 6756 | if (Unsigned) |
| 6757 | Type = Context.UnsignedShortTy; |
| 6758 | else |
| 6759 | Type = Context.ShortTy; |
| 6760 | break; |
| 6761 | case 'i': |
| 6762 | if (HowLong == 3) |
| 6763 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 6764 | else if (HowLong == 2) |
| 6765 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 6766 | else if (HowLong == 1) |
| 6767 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 6768 | else |
| 6769 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 6770 | break; |
| 6771 | case 'c': |
| 6772 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 6773 | if (Signed) |
| 6774 | Type = Context.SignedCharTy; |
| 6775 | else if (Unsigned) |
| 6776 | Type = Context.UnsignedCharTy; |
| 6777 | else |
| 6778 | Type = Context.CharTy; |
| 6779 | break; |
| 6780 | case 'b': // boolean |
| 6781 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 6782 | Type = Context.BoolTy; |
| 6783 | break; |
| 6784 | case 'z': // size_t. |
| 6785 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 6786 | Type = Context.getSizeType(); |
| 6787 | break; |
| 6788 | case 'F': |
| 6789 | Type = Context.getCFConstantStringType(); |
| 6790 | break; |
Fariborz Jahanian | d11da7e | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 6791 | case 'G': |
| 6792 | Type = Context.getObjCIdType(); |
| 6793 | break; |
| 6794 | case 'H': |
| 6795 | Type = Context.getObjCSelType(); |
| 6796 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6797 | case 'a': |
| 6798 | Type = Context.getBuiltinVaListType(); |
| 6799 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 6800 | break; |
| 6801 | case 'A': |
| 6802 | // This is a "reference" to a va_list; however, what exactly |
| 6803 | // this means depends on how va_list is defined. There are two |
| 6804 | // different kinds of va_list: ones passed by value, and ones |
| 6805 | // passed by reference. An example of a by-value va_list is |
| 6806 | // x86, where va_list is a char*. An example of by-ref va_list |
| 6807 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 6808 | // we want this argument to be a char*&; for x86-64, we want |
| 6809 | // it to be a __va_list_tag*. |
| 6810 | Type = Context.getBuiltinVaListType(); |
| 6811 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6812 | if (Type->isArrayType()) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6813 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6814 | else |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6815 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6816 | break; |
| 6817 | case 'V': { |
| 6818 | char *End; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6819 | unsigned NumElements = strtoul(Str, &End, 10); |
| 6820 | assert(End != Str && "Missing vector size"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6821 | Str = End; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6822 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6823 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 6824 | RequiresICE, false); |
| 6825 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6826 | |
| 6827 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 6828 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6829 | VectorType::GenericVector); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6830 | break; |
| 6831 | } |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 6832 | case 'E': { |
| 6833 | char *End; |
| 6834 | |
| 6835 | unsigned NumElements = strtoul(Str, &End, 10); |
| 6836 | assert(End != Str && "Missing vector size"); |
| 6837 | |
| 6838 | Str = End; |
| 6839 | |
| 6840 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 6841 | false); |
| 6842 | Type = Context.getExtVectorType(ElementType, NumElements); |
| 6843 | break; |
| 6844 | } |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6845 | case 'X': { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6846 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 6847 | false); |
| 6848 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6849 | Type = Context.getComplexType(ElementType); |
| 6850 | break; |
Fariborz Jahanian | 73952fc | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 6851 | } |
| 6852 | case 'Y' : { |
| 6853 | Type = Context.getPointerDiffType(); |
| 6854 | break; |
| 6855 | } |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6856 | case 'P': |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 6857 | Type = Context.getFILEType(); |
| 6858 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6859 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6860 | return QualType(); |
| 6861 | } |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6862 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6863 | case 'J': |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6864 | if (Signed) |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6865 | Type = Context.getsigjmp_bufType(); |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6866 | else |
| 6867 | Type = Context.getjmp_bufType(); |
| 6868 | |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6869 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6870 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6871 | return QualType(); |
| 6872 | } |
| 6873 | break; |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 6874 | case 'K': |
| 6875 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 6876 | Type = Context.getucontext_tType(); |
| 6877 | |
| 6878 | if (Type.isNull()) { |
| 6879 | Error = ASTContext::GE_Missing_ucontext; |
| 6880 | return QualType(); |
| 6881 | } |
| 6882 | break; |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6883 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6884 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6885 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 6886 | Done = !AllowTypeModifiers; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6887 | while (!Done) { |
John McCall | b8b9466 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 6888 | switch (char c = *Str++) { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6889 | default: Done = true; --Str; break; |
| 6890 | case '*': |
| 6891 | case '&': { |
| 6892 | // Both pointers and references can have their pointee types |
| 6893 | // qualified with an address space. |
| 6894 | char *End; |
| 6895 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 6896 | if (End != Str && AddrSpace != 0) { |
| 6897 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 6898 | Str = End; |
| 6899 | } |
| 6900 | if (c == '*') |
| 6901 | Type = Context.getPointerType(Type); |
| 6902 | else |
| 6903 | Type = Context.getLValueReferenceType(Type); |
| 6904 | break; |
| 6905 | } |
| 6906 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 6907 | case 'C': |
| 6908 | Type = Type.withConst(); |
| 6909 | break; |
| 6910 | case 'D': |
| 6911 | Type = Context.getVolatileType(Type); |
| 6912 | break; |
Ted Kremenek | f2a2f5f | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 6913 | case 'R': |
| 6914 | Type = Type.withRestrict(); |
| 6915 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6916 | } |
| 6917 | } |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6918 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6919 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6920 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6921 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6922 | return Type; |
| 6923 | } |
| 6924 | |
| 6925 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6926 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6927 | GetBuiltinTypeError &Error, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6928 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6929 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6930 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6931 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6932 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6933 | bool RequiresICE = false; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6934 | Error = GE_None; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6935 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 6936 | RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6937 | if (Error != GE_None) |
| 6938 | return QualType(); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6939 | |
| 6940 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 6941 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6942 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6943 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6944 | if (Error != GE_None) |
| 6945 | return QualType(); |
| 6946 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6947 | // If this argument is required to be an IntegerConstantExpression and the |
| 6948 | // caller cares, fill in the bitmask we return. |
| 6949 | if (RequiresICE && IntegerConstantArgs) |
| 6950 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 6951 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6952 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 6953 | if (Ty->isArrayType()) |
| 6954 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6955 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6956 | ArgTypes.push_back(Ty); |
| 6957 | } |
| 6958 | |
| 6959 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 6960 | "'.' should only occur at end of builtin type list!"); |
| 6961 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6962 | FunctionType::ExtInfo EI; |
| 6963 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 6964 | |
| 6965 | bool Variadic = (TypeStr[0] == '.'); |
| 6966 | |
| 6967 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 6968 | if (ArgTypes.empty() && Variadic) |
| 6969 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 6970 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6971 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6972 | EPI.ExtInfo = EI; |
| 6973 | EPI.Variadic = Variadic; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6974 | |
| 6975 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6976 | } |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 6977 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6978 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 6979 | GVALinkage External = GVA_StrongExternal; |
| 6980 | |
| 6981 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6982 | switch (L) { |
| 6983 | case NoLinkage: |
| 6984 | case InternalLinkage: |
| 6985 | case UniqueExternalLinkage: |
| 6986 | return GVA_Internal; |
| 6987 | |
| 6988 | case ExternalLinkage: |
| 6989 | switch (FD->getTemplateSpecializationKind()) { |
| 6990 | case TSK_Undeclared: |
| 6991 | case TSK_ExplicitSpecialization: |
| 6992 | External = GVA_StrongExternal; |
| 6993 | break; |
| 6994 | |
| 6995 | case TSK_ExplicitInstantiationDefinition: |
| 6996 | return GVA_ExplicitTemplateInstantiation; |
| 6997 | |
| 6998 | case TSK_ExplicitInstantiationDeclaration: |
| 6999 | case TSK_ImplicitInstantiation: |
| 7000 | External = GVA_TemplateInstantiation; |
| 7001 | break; |
| 7002 | } |
| 7003 | } |
| 7004 | |
| 7005 | if (!FD->isInlined()) |
| 7006 | return External; |
| 7007 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7008 | if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7009 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 7010 | // externally visible. |
| 7011 | if (FD->isInlineDefinitionExternallyVisible()) |
| 7012 | return External; |
| 7013 | |
| 7014 | // C99 inline semantics, where the symbol is not externally visible. |
| 7015 | return GVA_C99Inline; |
| 7016 | } |
| 7017 | |
| 7018 | // C++0x [temp.explicit]p9: |
| 7019 | // [ Note: The intent is that an inline function that is the subject of |
| 7020 | // an explicit instantiation declaration will still be implicitly |
| 7021 | // instantiated when used so that the body can be considered for |
| 7022 | // inlining, but that no out-of-line copy of the inline function would be |
| 7023 | // generated in the translation unit. -- end note ] |
| 7024 | if (FD->getTemplateSpecializationKind() |
| 7025 | == TSK_ExplicitInstantiationDeclaration) |
| 7026 | return GVA_C99Inline; |
| 7027 | |
| 7028 | return GVA_CXXInline; |
| 7029 | } |
| 7030 | |
| 7031 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 7032 | // If this is a static data member, compute the kind of template |
| 7033 | // specialization. Otherwise, this variable is not part of a |
| 7034 | // template. |
| 7035 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 7036 | if (VD->isStaticDataMember()) |
| 7037 | TSK = VD->getTemplateSpecializationKind(); |
| 7038 | |
| 7039 | Linkage L = VD->getLinkage(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7040 | if (L == ExternalLinkage && getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7041 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 7042 | L = UniqueExternalLinkage; |
| 7043 | |
| 7044 | switch (L) { |
| 7045 | case NoLinkage: |
| 7046 | case InternalLinkage: |
| 7047 | case UniqueExternalLinkage: |
| 7048 | return GVA_Internal; |
| 7049 | |
| 7050 | case ExternalLinkage: |
| 7051 | switch (TSK) { |
| 7052 | case TSK_Undeclared: |
| 7053 | case TSK_ExplicitSpecialization: |
| 7054 | return GVA_StrongExternal; |
| 7055 | |
| 7056 | case TSK_ExplicitInstantiationDeclaration: |
| 7057 | llvm_unreachable("Variable should not be instantiated"); |
| 7058 | // Fall through to treat this like any other instantiation. |
| 7059 | |
| 7060 | case TSK_ExplicitInstantiationDefinition: |
| 7061 | return GVA_ExplicitTemplateInstantiation; |
| 7062 | |
| 7063 | case TSK_ImplicitInstantiation: |
| 7064 | return GVA_TemplateInstantiation; |
| 7065 | } |
| 7066 | } |
| 7067 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7068 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7069 | } |
| 7070 | |
Argyrios Kyrtzidis | c904933 | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 7071 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7072 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7073 | if (!VD->isFileVarDecl()) |
| 7074 | return false; |
Argyrios Kyrtzidis | a98e861 | 2011-09-13 21:35:00 +0000 | [diff] [blame] | 7075 | } else if (!isa<FunctionDecl>(D)) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7076 | return false; |
| 7077 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7078 | // Weak references don't produce any output by themselves. |
| 7079 | if (D->hasAttr<WeakRefAttr>()) |
| 7080 | return false; |
| 7081 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7082 | // Aliases and used decls are required. |
| 7083 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 7084 | return true; |
| 7085 | |
| 7086 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7087 | // Forward declarations aren't required. |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 7088 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 7089 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7090 | |
| 7091 | // Constructors and destructors are required. |
| 7092 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 7093 | return true; |
| 7094 | |
| 7095 | // The key function for a class is required. |
| 7096 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 7097 | const CXXRecordDecl *RD = MD->getParent(); |
| 7098 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 7099 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 7100 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 7101 | return true; |
| 7102 | } |
| 7103 | } |
| 7104 | |
| 7105 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 7106 | |
| 7107 | // static, static inline, always_inline, and extern inline functions can |
| 7108 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 7109 | // Implicit template instantiations can also be deferred in C++. |
| 7110 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
Anton Yartsev | 79de4d4 | 2012-02-02 06:06:34 +0000 | [diff] [blame] | 7111 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7112 | return false; |
| 7113 | return true; |
| 7114 | } |
Douglas Gregor | 87d8124 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 7115 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7116 | const VarDecl *VD = cast<VarDecl>(D); |
| 7117 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 7118 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7119 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 7120 | return false; |
| 7121 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7122 | // Structs that have non-trivial constructors or destructors are required. |
| 7123 | |
| 7124 | // FIXME: Handle references. |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 7125 | // FIXME: Be more selective about which constructors we care about. |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7126 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 7127 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 7128 | if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() && |
| 7129 | RD->hasTrivialCopyConstructor() && |
| 7130 | RD->hasTrivialMoveConstructor() && |
| 7131 | RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7132 | return true; |
| 7133 | } |
| 7134 | } |
| 7135 | |
| 7136 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 7137 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 7138 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 7139 | return false; |
| 7140 | } |
| 7141 | |
| 7142 | return true; |
| 7143 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7144 | |
Timur Iskhodzhanov | c5098ad | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7145 | CallingConv ASTContext::getDefaultCXXMethodCallConv(bool isVariadic) { |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7146 | // Pass through to the C++ ABI object |
Timur Iskhodzhanov | c5098ad | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7147 | return ABI->getDefaultMethodCallConv(isVariadic); |
| 7148 | } |
| 7149 | |
| 7150 | CallingConv ASTContext::getCanonicalCallConv(CallingConv CC) const { |
| 7151 | if (CC == CC_C && !LangOpts.MRTD && getTargetInfo().getCXXABI() != CXXABI_Microsoft) |
| 7152 | return CC_Default; |
| 7153 | return CC; |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7154 | } |
| 7155 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7156 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 7157 | // Pass through to the C++ ABI object |
| 7158 | return ABI->isNearlyEmpty(RD); |
| 7159 | } |
| 7160 | |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7161 | MangleContext *ASTContext::createMangleContext() { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 7162 | switch (Target->getCXXABI()) { |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7163 | case CXXABI_ARM: |
| 7164 | case CXXABI_Itanium: |
| 7165 | return createItaniumMangleContext(*this, getDiagnostics()); |
| 7166 | case CXXABI_Microsoft: |
| 7167 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 7168 | } |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7169 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7170 | } |
| 7171 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7172 | CXXABI::~CXXABI() {} |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 7173 | |
| 7174 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Ted Kremenek | 7d39c9a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 7175 | return ASTRecordLayouts.getMemorySize() |
| 7176 | + llvm::capacity_in_bytes(ObjCLayouts) |
| 7177 | + llvm::capacity_in_bytes(KeyFunctions) |
| 7178 | + llvm::capacity_in_bytes(ObjCImpls) |
| 7179 | + llvm::capacity_in_bytes(BlockVarCopyInits) |
| 7180 | + llvm::capacity_in_bytes(DeclAttrs) |
| 7181 | + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember) |
| 7182 | + llvm::capacity_in_bytes(InstantiatedFromUsingDecl) |
| 7183 | + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) |
| 7184 | + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) |
| 7185 | + llvm::capacity_in_bytes(OverriddenMethods) |
| 7186 | + llvm::capacity_in_bytes(Types) |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7187 | + llvm::capacity_in_bytes(VariableArrayTypes) |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 7188 | + llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 7189 | } |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 7190 | |
Douglas Gregor | 6379854 | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 7191 | unsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) { |
| 7192 | CXXRecordDecl *Lambda = CallOperator->getParent(); |
| 7193 | return LambdaMangleContexts[Lambda->getDeclContext()] |
| 7194 | .getManglingNumber(CallOperator); |
| 7195 | } |
| 7196 | |
| 7197 | |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 7198 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 7199 | ParamIndices[D] = index; |
| 7200 | } |
| 7201 | |
| 7202 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 7203 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 7204 | assert(I != ParamIndices.end() && |
| 7205 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 7206 | return I->second; |
| 7207 | } |