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 | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 100 | RawComment CommentAtDeclLoc(SourceMgr, SourceRange(DeclLoc)); |
| 101 | ArrayRef<RawComment *>::iterator Comment |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 102 | = std::lower_bound(RawComments.begin(), |
| 103 | RawComments.end(), |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 104 | &CommentAtDeclLoc, |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 105 | BeforeThanCompare<RawComment>(SourceMgr)); |
| 106 | |
| 107 | // Decompose the location for the declaration and find the beginning of the |
| 108 | // file buffer. |
| 109 | std::pair<FileID, unsigned> DeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLoc); |
| 110 | |
| 111 | // First check whether we have a trailing comment. |
| 112 | if (Comment != RawComments.end() && |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 113 | (*Comment)->isDocumentation() && (*Comment)->isTrailingComment() && |
Dmitri Gribenko | 44cd7e6 | 2012-07-06 23:27:33 +0000 | [diff] [blame] | 114 | (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D))) { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 115 | std::pair<FileID, unsigned> CommentBeginDecomp |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 116 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getBegin()); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 117 | // Check that Doxygen trailing comment comes after the declaration, starts |
| 118 | // on the same line and in the same file as the declaration. |
| 119 | if (DeclLocDecomp.first == CommentBeginDecomp.first && |
| 120 | SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second) |
| 121 | == SourceMgr.getLineNumber(CommentBeginDecomp.first, |
| 122 | CommentBeginDecomp.second)) { |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 123 | return *Comment; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | // The comment just after the declaration was not a trailing comment. |
| 128 | // Let's look at the previous comment. |
| 129 | if (Comment == RawComments.begin()) |
| 130 | return NULL; |
| 131 | --Comment; |
| 132 | |
| 133 | // Check that we actually have a non-member Doxygen comment. |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 134 | if (!(*Comment)->isDocumentation() || (*Comment)->isTrailingComment()) |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 135 | return NULL; |
| 136 | |
| 137 | // Decompose the end of the comment. |
| 138 | std::pair<FileID, unsigned> CommentEndDecomp |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 139 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getEnd()); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 140 | |
| 141 | // If the comment and the declaration aren't in the same file, then they |
| 142 | // aren't related. |
| 143 | if (DeclLocDecomp.first != CommentEndDecomp.first) |
| 144 | return NULL; |
| 145 | |
| 146 | // Get the corresponding buffer. |
| 147 | bool Invalid = false; |
| 148 | const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first, |
| 149 | &Invalid).data(); |
| 150 | if (Invalid) |
| 151 | return NULL; |
| 152 | |
| 153 | // Extract text between the comment and declaration. |
| 154 | StringRef Text(Buffer + CommentEndDecomp.second, |
| 155 | DeclLocDecomp.second - CommentEndDecomp.second); |
| 156 | |
Dmitri Gribenko | 7e8729b | 2012-06-27 23:43:37 +0000 | [diff] [blame] | 157 | // There should be no other declarations or preprocessor directives between |
| 158 | // comment and declaration. |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 159 | if (Text.find_first_of(",;{}#@") != StringRef::npos) |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 160 | return NULL; |
| 161 | |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 162 | return *Comment; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | const RawComment *ASTContext::getRawCommentForDecl(const Decl *D) const { |
| 166 | // Check whether we have cached a comment string for this declaration |
| 167 | // already. |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 168 | llvm::DenseMap<const Decl *, RawAndParsedComment>::iterator Pos |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 169 | = DeclComments.find(D); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 170 | if (Pos != DeclComments.end()) { |
| 171 | RawAndParsedComment C = Pos->second; |
| 172 | return C.first; |
| 173 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 174 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 175 | RawComment *RC = getRawCommentForDeclNoCache(D); |
Dmitri Gribenko | 5c8897d | 2012-06-28 16:25:36 +0000 | [diff] [blame] | 176 | // If we found a comment, it should be a documentation comment. |
| 177 | assert(!RC || RC->isDocumentation()); |
Dmitri Gribenko | 0863005 | 2012-07-06 15:40:08 +0000 | [diff] [blame] | 178 | DeclComments[D] = |
| 179 | RawAndParsedComment(RC, static_cast<comments::FullComment *>(NULL)); |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 180 | if (RC) |
| 181 | RC->setAttached(); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 182 | return RC; |
| 183 | } |
| 184 | |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 185 | comments::FullComment *ASTContext::getCommentForDecl(const Decl *D) const { |
| 186 | llvm::DenseMap<const Decl *, RawAndParsedComment>::iterator Pos |
| 187 | = DeclComments.find(D); |
| 188 | const RawComment *RC; |
| 189 | if (Pos != DeclComments.end()) { |
| 190 | RawAndParsedComment C = Pos->second; |
| 191 | if (comments::FullComment *FC = C.second) |
| 192 | return FC; |
| 193 | RC = C.first; |
| 194 | } else |
| 195 | RC = getRawCommentForDecl(D); |
| 196 | |
| 197 | if (!RC) |
| 198 | return NULL; |
| 199 | |
| 200 | const StringRef RawText = RC->getRawText(SourceMgr); |
| 201 | comments::Lexer L(RC->getSourceRange().getBegin(), comments::CommentOptions(), |
| 202 | RawText.begin(), RawText.end()); |
| 203 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 204 | comments::Sema S(getAllocator(), getSourceManager(), getDiagnostics()); |
| 205 | S.setDecl(D); |
| 206 | comments::Parser P(L, S, getAllocator(), getSourceManager(), |
| 207 | getDiagnostics()); |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 208 | |
| 209 | comments::FullComment *FC = P.parseFullComment(); |
| 210 | DeclComments[D].second = FC; |
| 211 | return FC; |
| 212 | } |
| 213 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 214 | void |
| 215 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 216 | TemplateTemplateParmDecl *Parm) { |
| 217 | ID.AddInteger(Parm->getDepth()); |
| 218 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 219 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 220 | |
| 221 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 222 | ID.AddInteger(Params->size()); |
| 223 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 224 | PEnd = Params->end(); |
| 225 | P != PEnd; ++P) { |
| 226 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 227 | ID.AddInteger(0); |
| 228 | ID.AddBoolean(TTP->isParameterPack()); |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 233 | ID.AddInteger(1); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 234 | ID.AddBoolean(NTTP->isParameterPack()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 235 | ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 236 | if (NTTP->isExpandedParameterPack()) { |
| 237 | ID.AddBoolean(true); |
| 238 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 239 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 240 | QualType T = NTTP->getExpansionType(I); |
| 241 | ID.AddPointer(T.getCanonicalType().getAsOpaquePtr()); |
| 242 | } |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 243 | } else |
| 244 | ID.AddBoolean(false); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 245 | continue; |
| 246 | } |
| 247 | |
| 248 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 249 | ID.AddInteger(2); |
| 250 | Profile(ID, TTP); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | TemplateTemplateParmDecl * |
| 255 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 256 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 257 | // Check if we already have a canonical template template parameter. |
| 258 | llvm::FoldingSetNodeID ID; |
| 259 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
| 260 | void *InsertPos = 0; |
| 261 | CanonicalTemplateTemplateParm *Canonical |
| 262 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 263 | if (Canonical) |
| 264 | return Canonical->getParam(); |
| 265 | |
| 266 | // Build a canonical template parameter list. |
| 267 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 268 | SmallVector<NamedDecl *, 4> CanonParams; |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 269 | CanonParams.reserve(Params->size()); |
| 270 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 271 | PEnd = Params->end(); |
| 272 | P != PEnd; ++P) { |
| 273 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 274 | CanonParams.push_back( |
| 275 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 276 | SourceLocation(), |
| 277 | SourceLocation(), |
| 278 | TTP->getDepth(), |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 279 | TTP->getIndex(), 0, false, |
| 280 | TTP->isParameterPack())); |
| 281 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 282 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 283 | QualType T = getCanonicalType(NTTP->getType()); |
| 284 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 285 | NonTypeTemplateParmDecl *Param; |
| 286 | if (NTTP->isExpandedParameterPack()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 287 | SmallVector<QualType, 2> ExpandedTypes; |
| 288 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 289 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 290 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 291 | ExpandedTInfos.push_back( |
| 292 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 293 | } |
| 294 | |
| 295 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 296 | SourceLocation(), |
| 297 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 298 | NTTP->getDepth(), |
| 299 | NTTP->getPosition(), 0, |
| 300 | T, |
| 301 | TInfo, |
| 302 | ExpandedTypes.data(), |
| 303 | ExpandedTypes.size(), |
| 304 | ExpandedTInfos.data()); |
| 305 | } else { |
| 306 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 307 | SourceLocation(), |
| 308 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 309 | NTTP->getDepth(), |
| 310 | NTTP->getPosition(), 0, |
| 311 | T, |
| 312 | NTTP->isParameterPack(), |
| 313 | TInfo); |
| 314 | } |
| 315 | CanonParams.push_back(Param); |
| 316 | |
| 317 | } else |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 318 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 319 | cast<TemplateTemplateParmDecl>(*P))); |
| 320 | } |
| 321 | |
| 322 | TemplateTemplateParmDecl *CanonTTP |
| 323 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 324 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 325 | TTP->getPosition(), |
| 326 | TTP->isParameterPack(), |
| 327 | 0, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 328 | TemplateParameterList::Create(*this, SourceLocation(), |
| 329 | SourceLocation(), |
| 330 | CanonParams.data(), |
| 331 | CanonParams.size(), |
| 332 | SourceLocation())); |
| 333 | |
| 334 | // Get the new insert position for the node we care about. |
| 335 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 336 | assert(Canonical == 0 && "Shouldn't be in the map!"); |
| 337 | (void)Canonical; |
| 338 | |
| 339 | // Create the canonical template template parameter entry. |
| 340 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 341 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 342 | return CanonTTP; |
| 343 | } |
| 344 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 345 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 346 | if (!LangOpts.CPlusPlus) return 0; |
| 347 | |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 348 | switch (T.getCXXABI()) { |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 349 | case CXXABI_ARM: |
| 350 | return CreateARMCXXABI(*this); |
| 351 | case CXXABI_Itanium: |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 352 | return CreateItaniumCXXABI(*this); |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 353 | case CXXABI_Microsoft: |
| 354 | return CreateMicrosoftCXXABI(*this); |
| 355 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 356 | llvm_unreachable("Invalid CXXABI type!"); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 359 | static const LangAS::Map *getAddressSpaceMap(const TargetInfo &T, |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 360 | const LangOptions &LOpts) { |
| 361 | if (LOpts.FakeAddressSpaceMap) { |
| 362 | // The fake address space map must have a distinct entry for each |
| 363 | // language-specific address space. |
| 364 | static const unsigned FakeAddrSpaceMap[] = { |
| 365 | 1, // opencl_global |
| 366 | 2, // opencl_local |
Peter Collingbourne | f44bdf9 | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 367 | 3, // opencl_constant |
| 368 | 4, // cuda_device |
| 369 | 5, // cuda_constant |
| 370 | 6 // cuda_shared |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 371 | }; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 372 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 373 | } else { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 374 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
Douglas Gregor | 7018d5b | 2011-09-01 20:23:19 +0000 | [diff] [blame] | 378 | ASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 379 | const TargetInfo *t, |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 380 | IdentifierTable &idents, SelectorTable &sels, |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 381 | Builtin::Context &builtins, |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 382 | unsigned size_reserve, |
| 383 | bool DelayInitialization) |
| 384 | : FunctionProtoTypes(this_()), |
| 385 | TemplateSpecializationTypes(this_()), |
| 386 | DependentTemplateSpecializationTypes(this_()), |
| 387 | SubstTemplateTemplateParmPacks(this_()), |
| 388 | GlobalNestedNameSpecifier(0), |
| 389 | Int128Decl(0), UInt128Decl(0), |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 390 | BuiltinVaListDecl(0), |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 391 | ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), ObjCProtocolClassDecl(0), |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 392 | CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 393 | FILEDecl(0), |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 394 | jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0), |
| 395 | BlockDescriptorType(0), BlockDescriptorExtendedType(0), |
| 396 | cudaConfigureCallDecl(0), |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 397 | NullTypeSourceInfo(QualType()), |
| 398 | FirstLocalImport(), LastLocalImport(), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 399 | SourceMgr(SM), LangOpts(LOpts), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 400 | AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 401 | Idents(idents), Selectors(sels), |
| 402 | BuiltinInfo(builtins), |
| 403 | DeclarationNames(*this), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 404 | ExternalSource(0), Listener(0), |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 405 | Comments(SM), CommentsLoaded(false), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 406 | LastSDM(0, 0), |
| 407 | UniqueBlockByRefTypeID(0) |
| 408 | { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | if (size_reserve > 0) Types.reserve(size_reserve); |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 410 | TUDecl = TranslationUnitDecl::Create(*this); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 411 | |
| 412 | if (!DelayInitialization) { |
| 413 | assert(t && "No target supplied for ASTContext initialization"); |
| 414 | InitBuiltinTypes(*t); |
| 415 | } |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 418 | ASTContext::~ASTContext() { |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 419 | // Release the DenseMaps associated with DeclContext objects. |
| 420 | // FIXME: Is this the ideal solution? |
| 421 | ReleaseDeclContextMaps(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 422 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 423 | // Call all of the deallocation functions. |
| 424 | for (unsigned I = 0, N = Deallocations.size(); I != N; ++I) |
| 425 | Deallocations[I].first(Deallocations[I].second); |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 426 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 427 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 428 | // because they can contain DenseMaps. |
| 429 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 430 | const ASTRecordLayout*>::iterator |
| 431 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 432 | // Increment in loop to prevent using deallocated memory. |
| 433 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 434 | R->Destroy(*this); |
| 435 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 436 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 437 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 438 | // Increment in loop to prevent using deallocated memory. |
| 439 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 440 | R->Destroy(*this); |
| 441 | } |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 442 | |
| 443 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 444 | AEnd = DeclAttrs.end(); |
| 445 | A != AEnd; ++A) |
| 446 | A->second->~AttrVec(); |
| 447 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 448 | |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 449 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
| 450 | Deallocations.push_back(std::make_pair(Callback, Data)); |
| 451 | } |
| 452 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | void |
Dylan Noblesmith | e277899 | 2012-02-05 02:12:40 +0000 | [diff] [blame] | 454 | ASTContext::setExternalSource(OwningPtr<ExternalASTSource> &Source) { |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 455 | ExternalSource.reset(Source.take()); |
| 456 | } |
| 457 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 458 | void ASTContext::PrintStats() const { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 459 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 460 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 461 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 462 | unsigned counts[] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 464 | #define ABSTRACT_TYPE(Name, Parent) |
| 465 | #include "clang/AST/TypeNodes.def" |
| 466 | 0 // Extra |
| 467 | }; |
Douglas Gregor | b1fe2c9 | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 468 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 469 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 470 | Type *T = Types[i]; |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 471 | counts[(unsigned)T->getTypeClass()]++; |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 474 | unsigned Idx = 0; |
| 475 | unsigned TotalBytes = 0; |
| 476 | #define TYPE(Name, Parent) \ |
| 477 | if (counts[Idx]) \ |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 478 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 479 | << " types\n"; \ |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 480 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 481 | ++Idx; |
| 482 | #define ABSTRACT_TYPE(Name, Parent) |
| 483 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 485 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 486 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 487 | // Implicit special member functions. |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 488 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 489 | << NumImplicitDefaultConstructors |
| 490 | << " implicit default constructors created\n"; |
| 491 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 492 | << NumImplicitCopyConstructors |
| 493 | << " implicit copy constructors created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 494 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 495 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 496 | << NumImplicitMoveConstructors |
| 497 | << " implicit move constructors created\n"; |
| 498 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 499 | << NumImplicitCopyAssignmentOperators |
| 500 | << " implicit copy assignment operators created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 501 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 502 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 503 | << NumImplicitMoveAssignmentOperators |
| 504 | << " implicit move assignment operators created\n"; |
| 505 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 506 | << NumImplicitDestructors |
| 507 | << " implicit destructors created\n"; |
| 508 | |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 509 | if (ExternalSource.get()) { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 510 | llvm::errs() << "\n"; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 511 | ExternalSource->PrintStats(); |
| 512 | } |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 513 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 514 | BumpAlloc.PrintStats(); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 517 | TypedefDecl *ASTContext::getInt128Decl() const { |
| 518 | if (!Int128Decl) { |
| 519 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty); |
| 520 | Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 521 | getTranslationUnitDecl(), |
| 522 | SourceLocation(), |
| 523 | SourceLocation(), |
| 524 | &Idents.get("__int128_t"), |
| 525 | TInfo); |
| 526 | } |
| 527 | |
| 528 | return Int128Decl; |
| 529 | } |
| 530 | |
| 531 | TypedefDecl *ASTContext::getUInt128Decl() const { |
| 532 | if (!UInt128Decl) { |
| 533 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty); |
| 534 | UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 535 | getTranslationUnitDecl(), |
| 536 | SourceLocation(), |
| 537 | SourceLocation(), |
| 538 | &Idents.get("__uint128_t"), |
| 539 | TInfo); |
| 540 | } |
| 541 | |
| 542 | return UInt128Decl; |
| 543 | } |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 544 | |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 545 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 546 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 547 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 548 | Types.push_back(Ty); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 551 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target) { |
| 552 | assert((!this->Target || this->Target == &Target) && |
| 553 | "Incorrect target reinitialization"); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 554 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 556 | this->Target = &Target; |
| 557 | |
| 558 | ABI.reset(createCXXABI(Target)); |
| 559 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
| 560 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 561 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 562 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 564 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 565 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 566 | // C99 6.2.5p3. |
Eli Friedman | 9ffd4a9 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 567 | if (LangOpts.CharIsSigned) |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 568 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 569 | else |
| 570 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 571 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 572 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 573 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 574 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 575 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 576 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 578 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 579 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 580 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 581 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 582 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 583 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 585 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 586 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 587 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 588 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 589 | |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 590 | // GNU extension, 128-bit integers. |
| 591 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 592 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 593 | |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 594 | if (LangOpts.CPlusPlus) { // C++ 3.9.1p5 |
Eli Friedman | 786d087 | 2011-04-30 19:24:24 +0000 | [diff] [blame] | 595 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 596 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 597 | else // -fshort-wchar makes wchar_t be unsigned. |
| 598 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 599 | } else // C99 |
Chris Lattner | 007cb02 | 2009-02-26 23:43:47 +0000 | [diff] [blame] | 600 | WCharTy = getFromTargetType(Target.getWCharType()); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 601 | |
James Molloy | 3636554 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 602 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 603 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 604 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 605 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 606 | else // C99 |
| 607 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 608 | |
| 609 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 610 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 611 | else // C99 |
| 612 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 613 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 614 | // Placeholder type for type-dependent expressions whose type is |
| 615 | // completely unknown. No code should ever check a type against |
| 616 | // DependentTy and users should never see it; however, it is here to |
| 617 | // help diagnose failures to properly check for type-dependent |
| 618 | // expressions. |
| 619 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 620 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 621 | // Placeholder type for functions. |
| 622 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 623 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 624 | // Placeholder type for bound members. |
| 625 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 626 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 627 | // Placeholder type for pseudo-objects. |
| 628 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 629 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 630 | // "any" type; useful for debugger-like clients. |
| 631 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 632 | |
John McCall | 8a6b59a | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 633 | // Placeholder type for unbridged ARC casts. |
| 634 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 635 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 636 | // C99 6.2.5p11. |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 637 | FloatComplexTy = getComplexType(FloatTy); |
| 638 | DoubleComplexTy = getComplexType(DoubleTy); |
| 639 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 640 | |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 641 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 642 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 643 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 644 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 645 | |
| 646 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 29898f4 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 647 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 648 | SignedCharTy : BoolTy); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 649 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 650 | ObjCConstantStringType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 651 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 652 | // void * type |
| 653 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 654 | |
| 655 | // nullptr type (C++0x 2.14.7) |
| 656 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 657 | |
| 658 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 659 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 660 | |
| 661 | // Builtin type used to help define __builtin_va_list. |
| 662 | VaListTagTy = QualType(); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 663 | } |
| 664 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 665 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 666 | return SourceMgr.getDiagnostics(); |
| 667 | } |
| 668 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 669 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 670 | AttrVec *&Result = DeclAttrs[D]; |
| 671 | if (!Result) { |
| 672 | void *Mem = Allocate(sizeof(AttrVec)); |
| 673 | Result = new (Mem) AttrVec; |
| 674 | } |
| 675 | |
| 676 | return *Result; |
| 677 | } |
| 678 | |
| 679 | /// \brief Erase the attributes corresponding to the given declaration. |
| 680 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 681 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 682 | if (Pos != DeclAttrs.end()) { |
| 683 | Pos->second->~AttrVec(); |
| 684 | DeclAttrs.erase(Pos); |
| 685 | } |
| 686 | } |
| 687 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 688 | MemberSpecializationInfo * |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 689 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 690 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 691 | llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 692 | = InstantiatedFromStaticDataMember.find(Var); |
| 693 | if (Pos == InstantiatedFromStaticDataMember.end()) |
| 694 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 695 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 696 | return Pos->second; |
| 697 | } |
| 698 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 699 | void |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 700 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 701 | TemplateSpecializationKind TSK, |
| 702 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 703 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 704 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
| 705 | assert(!InstantiatedFromStaticDataMember[Inst] && |
| 706 | "Already noted what static data member was instantiated from"); |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 707 | InstantiatedFromStaticDataMember[Inst] |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 708 | = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 711 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 712 | const FunctionDecl *FD){ |
| 713 | assert(FD && "Specialization is 0"); |
| 714 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 715 | = ClassScopeSpecializationPattern.find(FD); |
| 716 | if (Pos == ClassScopeSpecializationPattern.end()) |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 717 | return 0; |
| 718 | |
| 719 | return Pos->second; |
| 720 | } |
| 721 | |
| 722 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 723 | FunctionDecl *Pattern) { |
| 724 | assert(FD && "Specialization is 0"); |
| 725 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 726 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 727 | } |
| 728 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 729 | NamedDecl * |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 730 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 731 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 732 | = InstantiatedFromUsingDecl.find(UUD); |
| 733 | if (Pos == InstantiatedFromUsingDecl.end()) |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 734 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 735 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 736 | return Pos->second; |
| 737 | } |
| 738 | |
| 739 | void |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 740 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 741 | assert((isa<UsingDecl>(Pattern) || |
| 742 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 743 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 744 | "pattern decl is not a using decl"); |
| 745 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 746 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 747 | } |
| 748 | |
| 749 | UsingShadowDecl * |
| 750 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 751 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 752 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 753 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
| 754 | return 0; |
| 755 | |
| 756 | return Pos->second; |
| 757 | } |
| 758 | |
| 759 | void |
| 760 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 761 | UsingShadowDecl *Pattern) { |
| 762 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 763 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 766 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 767 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 768 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 769 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
| 770 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 771 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 772 | return Pos->second; |
| 773 | } |
| 774 | |
| 775 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 776 | FieldDecl *Tmpl) { |
| 777 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 778 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 779 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 780 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 782 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 783 | } |
| 784 | |
Fariborz Jahanian | 595ec5d | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 785 | bool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, |
| 786 | const FieldDecl *LastFD) const { |
| 787 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 788 | FD->getBitWidthValue(*this) == 0); |
Fariborz Jahanian | 595ec5d | 2011-04-27 17:14:21 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Fariborz Jahanian | eb39741 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 791 | bool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD, |
| 792 | const FieldDecl *LastFD) const { |
| 793 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 794 | FD->getBitWidthValue(*this) == 0 && |
| 795 | LastFD->getBitWidthValue(*this) != 0); |
Fariborz Jahanian | eb39741 | 2011-05-02 17:20:56 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 798 | bool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD, |
| 799 | const FieldDecl *LastFD) const { |
| 800 | return (FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 801 | FD->getBitWidthValue(*this) && |
| 802 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | 84335f7 | 2011-05-04 18:51:37 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Chad Rosier | f01a7dd | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 805 | bool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD, |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 806 | const FieldDecl *LastFD) const { |
| 807 | return (!FD->isBitField() && LastFD && LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 808 | LastFD->getBitWidthValue(*this)); |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 809 | } |
| 810 | |
Chad Rosier | f01a7dd | 2011-08-04 23:34:15 +0000 | [diff] [blame] | 811 | bool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD, |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 812 | const FieldDecl *LastFD) const { |
| 813 | return (FD->isBitField() && LastFD && !LastFD->isBitField() && |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 814 | FD->getBitWidthValue(*this)); |
Fariborz Jahanian | b7a2879 | 2011-05-06 21:56:12 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 817 | ASTContext::overridden_cxx_method_iterator |
| 818 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 819 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 820 | = OverriddenMethods.find(Method); |
| 821 | if (Pos == OverriddenMethods.end()) |
| 822 | return 0; |
| 823 | |
| 824 | return Pos->second.begin(); |
| 825 | } |
| 826 | |
| 827 | ASTContext::overridden_cxx_method_iterator |
| 828 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 829 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 830 | = OverriddenMethods.find(Method); |
| 831 | if (Pos == OverriddenMethods.end()) |
| 832 | return 0; |
| 833 | |
| 834 | return Pos->second.end(); |
| 835 | } |
| 836 | |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 837 | unsigned |
| 838 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 839 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
| 840 | = OverriddenMethods.find(Method); |
| 841 | if (Pos == OverriddenMethods.end()) |
| 842 | return 0; |
| 843 | |
| 844 | return Pos->second.size(); |
| 845 | } |
| 846 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 847 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 848 | const CXXMethodDecl *Overridden) { |
| 849 | OverriddenMethods[Method].push_back(Overridden); |
| 850 | } |
| 851 | |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 852 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 853 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 854 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 855 | if (!FirstLocalImport) { |
| 856 | FirstLocalImport = Import; |
| 857 | LastLocalImport = Import; |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | LastLocalImport->NextLocalImport = Import; |
| 862 | LastLocalImport = Import; |
| 863 | } |
| 864 | |
Chris Lattner | 53cfe80 | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 865 | //===----------------------------------------------------------------------===// |
| 866 | // Type Sizing and Analysis |
| 867 | //===----------------------------------------------------------------------===// |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 868 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 869 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 870 | /// scalar floating point type. |
| 871 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 872 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 873 | assert(BT && "Not a floating point type!"); |
| 874 | switch (BT->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 875 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 876 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 877 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 878 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 879 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
Ken Dyck | 160146e | 2010-01-27 17:10:57 +0000 | [diff] [blame] | 883 | /// getDeclAlign - Return a conservative estimate of the alignment of the |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 884 | /// specified decl. Note that bitfields do not have a valid alignment, so |
| 885 | /// this method will assert on them. |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 886 | /// If @p RefAsPointee, references are treated like their underlying type |
| 887 | /// (for alignof), else they're treated like pointers (for CodeGen). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 888 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 889 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 890 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 891 | bool UseAlignAttrOnly = false; |
| 892 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 893 | Align = AlignFromAttr; |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 894 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 895 | // __attribute__((aligned)) can increase or decrease alignment |
| 896 | // *except* on a struct or struct member, where it only increases |
| 897 | // alignment unless 'packed' is also specified. |
| 898 | // |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 899 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 900 | // ignore that possibility; Sema should diagnose it. |
| 901 | if (isa<FieldDecl>(D)) { |
| 902 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 903 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 904 | } else { |
| 905 | UseAlignAttrOnly = true; |
| 906 | } |
| 907 | } |
Fariborz Jahanian | 9f10718 | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 908 | else if (isa<FieldDecl>(D)) |
| 909 | UseAlignAttrOnly = |
| 910 | D->hasAttr<PackedAttr>() || |
| 911 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 912 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 913 | // If we're using the align attribute only, just ignore everything |
| 914 | // else about the declaration and its type. |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 915 | if (UseAlignAttrOnly) { |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 916 | // do nothing |
| 917 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 918 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 919 | QualType T = VD->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 920 | if (const ReferenceType* RT = T->getAs<ReferenceType>()) { |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 921 | if (RefAsPointee) |
| 922 | T = RT->getPointeeType(); |
| 923 | else |
| 924 | T = getPointerType(RT->getPointeeType()); |
| 925 | } |
| 926 | if (!T->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 927 | // Adjust alignments of declarations with array type by the |
| 928 | // large-array alignment on the target. |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 929 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 930 | const ArrayType *arrayType; |
| 931 | if (MinWidth && (arrayType = getAsArrayType(T))) { |
| 932 | if (isa<VariableArrayType>(arrayType)) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 933 | Align = std::max(Align, Target->getLargeArrayAlign()); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 934 | else if (isa<ConstantArrayType>(arrayType) && |
| 935 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 936 | Align = std::max(Align, Target->getLargeArrayAlign()); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 937 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 938 | // Walk through any array types while we're at it. |
| 939 | T = getBaseElementType(arrayType); |
| 940 | } |
Chad Rosier | 99ee782 | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 941 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 942 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 943 | |
| 944 | // Fields can be subject to extra alignment constraints, like if |
| 945 | // the field is packed, the struct is packed, or the struct has a |
| 946 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 947 | // the actual alignment of the field within the struct, and then |
| 948 | // (as we're expected to) constrain that by the alignment of the type. |
| 949 | if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) { |
| 950 | // So calculate the alignment of the field. |
| 951 | const ASTRecordLayout &layout = getASTRecordLayout(field->getParent()); |
| 952 | |
| 953 | // Start with the record's overall alignment. |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 954 | unsigned fieldAlign = toBits(layout.getAlignment()); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 955 | |
| 956 | // Use the GCD of that and the offset within the record. |
| 957 | uint64_t offset = layout.getFieldOffset(field->getFieldIndex()); |
| 958 | if (offset > 0) { |
| 959 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 960 | // which means we get to do this crazy thing instead of Euclid's. |
| 961 | uint64_t lowBitOfOffset = offset & (~offset + 1); |
| 962 | if (lowBitOfOffset < fieldAlign) |
| 963 | fieldAlign = static_cast<unsigned>(lowBitOfOffset); |
| 964 | } |
| 965 | |
| 966 | Align = std::min(Align, fieldAlign); |
Charles Davis | 3fc5107 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 967 | } |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 968 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 969 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 970 | return toCharUnitsFromBits(Align); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 971 | } |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 972 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 973 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 974 | ASTContext::getTypeInfoInChars(const Type *T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 975 | std::pair<uint64_t, unsigned> Info = getTypeInfo(T); |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 976 | return std::make_pair(toCharUnitsFromBits(Info.first), |
| 977 | toCharUnitsFromBits(Info.second)); |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 981 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 982 | return getTypeInfoInChars(T.getTypePtr()); |
| 983 | } |
| 984 | |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 985 | std::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const { |
| 986 | TypeInfoMap::iterator it = MemoizedTypeInfo.find(T); |
| 987 | if (it != MemoizedTypeInfo.end()) |
| 988 | return it->second; |
| 989 | |
| 990 | std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T); |
| 991 | MemoizedTypeInfo.insert(std::make_pair(T, Info)); |
| 992 | return Info; |
| 993 | } |
| 994 | |
| 995 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 996 | /// method does not work on incomplete types. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 997 | /// |
| 998 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 999 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 1000 | /// should take a QualType, &c. |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1001 | std::pair<uint64_t, unsigned> |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1002 | ASTContext::getTypeInfoImpl(const Type *T) const { |
Mike Stump | 5b9a3d5 | 2009-02-27 18:32:39 +0000 | [diff] [blame] | 1003 | uint64_t Width=0; |
| 1004 | unsigned Align=8; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1005 | switch (T->getTypeClass()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1006 | #define TYPE(Class, Base) |
| 1007 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1008 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1009 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 1010 | #include "clang/AST/TypeNodes.def" |
John McCall | 15547bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 1011 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1012 | |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1013 | case Type::FunctionNoProto: |
| 1014 | case Type::FunctionProto: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1015 | // GCC extension: alignof(function) = 32 bits |
| 1016 | Width = 0; |
| 1017 | Align = 32; |
| 1018 | break; |
| 1019 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1020 | case Type::IncompleteArray: |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1021 | case Type::VariableArray: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1022 | Width = 0; |
| 1023 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 1024 | break; |
| 1025 | |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1026 | case Type::ConstantArray: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1027 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1029 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | d541a4c | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1030 | uint64_t Size = CAT->getSize().getZExtValue(); |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1031 | assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) && |
| 1032 | "Overflow in array type bit size evaluation"); |
Abramo Bagnara | d541a4c | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1033 | Width = EltInfo.first*Size; |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1034 | Align = EltInfo.second; |
Argyrios Kyrtzidis | ae40e4e | 2011-04-26 21:05:39 +0000 | [diff] [blame] | 1035 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1036 | break; |
Christopher Lamb | c5fafa2 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 1037 | } |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1038 | case Type::ExtVector: |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1039 | case Type::Vector: { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1040 | const VectorType *VT = cast<VectorType>(T); |
| 1041 | std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType()); |
| 1042 | Width = EltInfo.first*VT->getNumElements(); |
Eli Friedman | 3df5efe | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1043 | Align = Width; |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1044 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 1045 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | ef78c8e | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 1046 | if (Align & (Align-1)) { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1047 | Align = llvm::NextPowerOf2(Align); |
| 1048 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 1049 | } |
Chad Rosier | cc40ea7 | 2012-07-13 23:57:43 +0000 | [diff] [blame] | 1050 | // Adjust the alignment based on the target max. |
| 1051 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
| 1052 | if (TargetVectorAlign && TargetVectorAlign < Align) |
| 1053 | Align = TargetVectorAlign; |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1054 | break; |
| 1055 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1056 | |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1057 | case Type::Builtin: |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1058 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1059 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1060 | case BuiltinType::Void: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1061 | // GCC extension: alignof(void) = 8 bits. |
| 1062 | Width = 0; |
| 1063 | Align = 8; |
| 1064 | break; |
| 1065 | |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1066 | case BuiltinType::Bool: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1067 | Width = Target->getBoolWidth(); |
| 1068 | Align = Target->getBoolAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1069 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1070 | case BuiltinType::Char_S: |
| 1071 | case BuiltinType::Char_U: |
| 1072 | case BuiltinType::UChar: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1073 | case BuiltinType::SChar: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1074 | Width = Target->getCharWidth(); |
| 1075 | Align = Target->getCharAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1076 | break; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1077 | case BuiltinType::WChar_S: |
| 1078 | case BuiltinType::WChar_U: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1079 | Width = Target->getWCharWidth(); |
| 1080 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1081 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1082 | case BuiltinType::Char16: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1083 | Width = Target->getChar16Width(); |
| 1084 | Align = Target->getChar16Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1085 | break; |
| 1086 | case BuiltinType::Char32: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1087 | Width = Target->getChar32Width(); |
| 1088 | Align = Target->getChar32Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1089 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1090 | case BuiltinType::UShort: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1091 | case BuiltinType::Short: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1092 | Width = Target->getShortWidth(); |
| 1093 | Align = Target->getShortAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1094 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1095 | case BuiltinType::UInt: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1096 | case BuiltinType::Int: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1097 | Width = Target->getIntWidth(); |
| 1098 | Align = Target->getIntAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1099 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1100 | case BuiltinType::ULong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1101 | case BuiltinType::Long: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1102 | Width = Target->getLongWidth(); |
| 1103 | Align = Target->getLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1104 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1105 | case BuiltinType::ULongLong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1106 | case BuiltinType::LongLong: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1107 | Width = Target->getLongLongWidth(); |
| 1108 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1109 | break; |
Chris Lattner | 0a415ec | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 1110 | case BuiltinType::Int128: |
| 1111 | case BuiltinType::UInt128: |
| 1112 | Width = 128; |
| 1113 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 1114 | break; |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1115 | case BuiltinType::Half: |
| 1116 | Width = Target->getHalfWidth(); |
| 1117 | Align = Target->getHalfAlign(); |
| 1118 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1119 | case BuiltinType::Float: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1120 | Width = Target->getFloatWidth(); |
| 1121 | Align = Target->getFloatAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1122 | break; |
| 1123 | case BuiltinType::Double: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1124 | Width = Target->getDoubleWidth(); |
| 1125 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1126 | break; |
| 1127 | case BuiltinType::LongDouble: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1128 | Width = Target->getLongDoubleWidth(); |
| 1129 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1130 | break; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1131 | case BuiltinType::NullPtr: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1132 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 1133 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | a81b0b7 | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 1134 | break; |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1135 | case BuiltinType::ObjCId: |
| 1136 | case BuiltinType::ObjCClass: |
| 1137 | case BuiltinType::ObjCSel: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1138 | Width = Target->getPointerWidth(0); |
| 1139 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1140 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1141 | } |
Chris Lattner | 48f84b8 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 1142 | break; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1143 | case Type::ObjCObjectPointer: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1144 | Width = Target->getPointerWidth(0); |
| 1145 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1146 | break; |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1147 | case Type::BlockPointer: { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1148 | unsigned AS = getTargetAddressSpace( |
| 1149 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1150 | Width = Target->getPointerWidth(AS); |
| 1151 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1152 | break; |
| 1153 | } |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1154 | case Type::LValueReference: |
| 1155 | case Type::RValueReference: { |
| 1156 | // alignof and sizeof should never enter this code path here, so we go |
| 1157 | // the pointer route. |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1158 | unsigned AS = getTargetAddressSpace( |
| 1159 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1160 | Width = Target->getPointerWidth(AS); |
| 1161 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1162 | break; |
| 1163 | } |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1164 | case Type::Pointer: { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1165 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1166 | Width = Target->getPointerWidth(AS); |
| 1167 | Align = Target->getPointerAlign(AS); |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1168 | break; |
| 1169 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1170 | case Type::MemberPointer: { |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1171 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1172 | std::pair<uint64_t, unsigned> PtrDiffInfo = |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1173 | getTypeInfo(getPointerDiffType()); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1174 | Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT); |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1175 | Align = PtrDiffInfo.second; |
| 1176 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1177 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1178 | case Type::Complex: { |
| 1179 | // Complex types have the same alignment as their elements, but twice the |
| 1180 | // size. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1181 | std::pair<uint64_t, unsigned> EltInfo = |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1182 | getTypeInfo(cast<ComplexType>(T)->getElementType()); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1183 | Width = EltInfo.first*2; |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1184 | Align = EltInfo.second; |
| 1185 | break; |
| 1186 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1187 | case Type::ObjCObject: |
| 1188 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1189 | case Type::ObjCInterface: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1190 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1191 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1192 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1193 | Align = toBits(Layout.getAlignment()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1194 | break; |
| 1195 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1196 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1197 | case Type::Enum: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1198 | const TagType *TT = cast<TagType>(T); |
| 1199 | |
| 1200 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 7f97189 | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1201 | Width = 8; |
| 1202 | Align = 8; |
Chris Lattner | 572100b | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1203 | break; |
| 1204 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1206 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1207 | return getTypeInfo(ET->getDecl()->getIntegerType()); |
| 1208 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1209 | const RecordType *RT = cast<RecordType>(TT); |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1210 | const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1211 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1212 | Align = toBits(Layout.getAlignment()); |
Chris Lattner | 49a953a | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1213 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1214 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1215 | |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1216 | case Type::SubstTemplateTypeParm: |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1217 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1218 | getReplacementType().getTypePtr()); |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1219 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1220 | case Type::Auto: { |
| 1221 | const AutoType *A = cast<AutoType>(T); |
| 1222 | assert(A->isDeduced() && "Cannot request the size of a dependent type"); |
Matt Beaumont-Gay | 7a24210e | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1223 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1226 | case Type::Paren: |
| 1227 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1228 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1229 | case Type::Typedef: { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1230 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
Douglas Gregor | f5bae22 | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1231 | std::pair<uint64_t, unsigned> Info |
| 1232 | = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1233 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1234 | // alignment we have. This violates the GCC documentation (which says that |
| 1235 | // attribute(aligned) can only round up) but matches its implementation. |
| 1236 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) |
| 1237 | Align = AttrAlign; |
| 1238 | else |
| 1239 | Align = Info.second; |
Douglas Gregor | f5bae22 | 2010-08-27 00:11:28 +0000 | [diff] [blame] | 1240 | Width = Info.first; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1241 | break; |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1242 | } |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1243 | |
| 1244 | case Type::TypeOfExpr: |
| 1245 | return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType() |
| 1246 | .getTypePtr()); |
| 1247 | |
| 1248 | case Type::TypeOf: |
| 1249 | return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr()); |
| 1250 | |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 1251 | case Type::Decltype: |
| 1252 | return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType() |
| 1253 | .getTypePtr()); |
| 1254 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1255 | case Type::UnaryTransform: |
| 1256 | return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType()); |
| 1257 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1258 | case Type::Elaborated: |
| 1259 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1260 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1261 | case Type::Attributed: |
| 1262 | return getTypeInfo( |
| 1263 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1264 | |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1265 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | assert(getCanonicalType(T) != T && |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1267 | "Cannot request the size of a dependent type"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 1268 | const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T); |
| 1269 | // A type alias template specialization may refer to a typedef with the |
| 1270 | // aligned attribute on it. |
| 1271 | if (TST->isTypeAlias()) |
| 1272 | return getTypeInfo(TST->getAliasedType().getTypePtr()); |
| 1273 | else |
| 1274 | return getTypeInfo(getCanonicalType(T)); |
| 1275 | } |
| 1276 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1277 | case Type::Atomic: { |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1278 | std::pair<uint64_t, unsigned> Info |
| 1279 | = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1280 | Width = Info.first; |
| 1281 | Align = Info.second; |
| 1282 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth() && |
| 1283 | llvm::isPowerOf2_64(Width)) { |
| 1284 | // We can potentially perform lock-free atomic operations for this |
| 1285 | // type; promote the alignment appropriately. |
| 1286 | // FIXME: We could potentially promote the width here as well... |
| 1287 | // is that worthwhile? (Non-struct atomic types generally have |
| 1288 | // power-of-two size anyway, but structs might not. Requires a bit |
| 1289 | // of implementation work to make sure we zero out the extra bits.) |
| 1290 | Align = static_cast<unsigned>(Width); |
| 1291 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1294 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1295 | |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1296 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1297 | return std::make_pair(Width, Align); |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1300 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1301 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1302 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1303 | } |
| 1304 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1305 | /// toBits - Convert a size in characters to a size in characters. |
| 1306 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1307 | return CharSize.getQuantity() * getCharWidth(); |
| 1308 | } |
| 1309 | |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1310 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1311 | /// This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1312 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1313 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1314 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1315 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1316 | return toCharUnitsFromBits(getTypeSize(T)); |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Ken Dyck | a6046ab | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1319 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1320 | /// characters. This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1321 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1322 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1323 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1324 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1325 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1328 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1329 | /// type for the current target in bits. This can be different than the ABI |
| 1330 | /// alignment in cases where it is beneficial for performance to overalign |
| 1331 | /// a data type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1332 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1333 | unsigned ABIAlign = getTypeAlign(T); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1334 | |
| 1335 | // Double and long long should be naturally aligned if possible. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1336 | if (const ComplexType* CT = T->getAs<ComplexType>()) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1337 | T = CT->getElementType().getTypePtr(); |
| 1338 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | b57321a | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 1339 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 1340 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1341 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
| 1342 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1343 | return ABIAlign; |
| 1344 | } |
| 1345 | |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1346 | /// DeepCollectObjCIvars - |
| 1347 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1348 | /// super class and then collects all ivars, including those synthesized for |
| 1349 | /// current class. This routine is used for implementation of current class |
| 1350 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1351 | /// |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1352 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1353 | bool leafClass, |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1354 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1355 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1356 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1357 | if (!leafClass) { |
| 1358 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1359 | E = OI->ivar_end(); I != E; ++I) |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1360 | Ivars.push_back(*I); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1361 | } else { |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1362 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1363 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1364 | Iv= Iv->getNextIvar()) |
| 1365 | Ivars.push_back(Iv); |
| 1366 | } |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1369 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1370 | /// those inherited by it. |
| 1371 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1372 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1373 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1374 | // We can use protocol_iterator here instead of |
| 1375 | // all_referenced_protocol_iterator since we are walking all categories. |
| 1376 | for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(), |
| 1377 | PE = OI->all_referenced_protocol_end(); P != PE; ++P) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1378 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1379 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1380 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1381 | PE = Proto->protocol_end(); P != PE; ++P) { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1382 | Protocols.insert((*P)->getCanonicalDecl()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1383 | CollectInheritedProtocols(*P, Protocols); |
| 1384 | } |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1385 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1386 | |
| 1387 | // Categories of this Interface. |
| 1388 | for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList(); |
| 1389 | CDeclChain; CDeclChain = CDeclChain->getNextClassCategory()) |
| 1390 | CollectInheritedProtocols(CDeclChain, Protocols); |
| 1391 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1392 | while (SD) { |
| 1393 | CollectInheritedProtocols(SD, Protocols); |
| 1394 | SD = SD->getSuperClass(); |
| 1395 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1396 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1397 | for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(), |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1398 | PE = OC->protocol_end(); P != PE; ++P) { |
| 1399 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1400 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1401 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1402 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1403 | CollectInheritedProtocols(*P, Protocols); |
| 1404 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1405 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1406 | for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(), |
| 1407 | PE = OP->protocol_end(); P != PE; ++P) { |
| 1408 | ObjCProtocolDecl *Proto = (*P); |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1409 | Protocols.insert(Proto->getCanonicalDecl()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1410 | for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(), |
| 1411 | PE = Proto->protocol_end(); P != PE; ++P) |
| 1412 | CollectInheritedProtocols(*P, Protocols); |
| 1413 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1417 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1418 | unsigned count = 0; |
| 1419 | // Count ivars declared in class extension. |
Fariborz Jahanian | 3bf0ded | 2010-06-22 23:20:40 +0000 | [diff] [blame] | 1420 | for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl; |
| 1421 | CDecl = CDecl->getNextClassExtension()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1422 | count += CDecl->ivar_size(); |
| 1423 | |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1424 | // Count ivar defined in this class's implementation. This |
| 1425 | // includes synthesized ivars. |
| 1426 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1427 | count += ImplDecl->ivar_size(); |
| 1428 | |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1429 | return count; |
| 1430 | } |
| 1431 | |
Argyrios Kyrtzidis | 2e809ce | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 1432 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 1433 | if (!E) |
| 1434 | return false; |
| 1435 | |
| 1436 | // nullptr_t is always treated as null. |
| 1437 | if (E->getType()->isNullPtrType()) return true; |
| 1438 | |
| 1439 | if (E->getType()->isAnyPointerType() && |
| 1440 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 1441 | Expr::NPC_ValueDependentIsNull)) |
| 1442 | return true; |
| 1443 | |
| 1444 | // Unfortunately, __null has type 'int'. |
| 1445 | if (isa<GNUNullExpr>(E)) return true; |
| 1446 | |
| 1447 | return false; |
| 1448 | } |
| 1449 | |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1450 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1451 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1452 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1453 | I = ObjCImpls.find(D); |
| 1454 | if (I != ObjCImpls.end()) |
| 1455 | return cast<ObjCImplementationDecl>(I->second); |
| 1456 | return 0; |
| 1457 | } |
| 1458 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1459 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1460 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1461 | I = ObjCImpls.find(D); |
| 1462 | if (I != ObjCImpls.end()) |
| 1463 | return cast<ObjCCategoryImplDecl>(I->second); |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1468 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1469 | ObjCImplementationDecl *ImplD) { |
| 1470 | assert(IFaceD && ImplD && "Passed null params"); |
| 1471 | ObjCImpls[IFaceD] = ImplD; |
| 1472 | } |
| 1473 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1474 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1475 | ObjCCategoryImplDecl *ImplD) { |
| 1476 | assert(CatD && ImplD && "Passed null params"); |
| 1477 | ObjCImpls[CatD] = ImplD; |
| 1478 | } |
| 1479 | |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1480 | ObjCInterfaceDecl *ASTContext::getObjContainingInterface(NamedDecl *ND) const { |
| 1481 | if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
| 1482 | return ID; |
| 1483 | if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
| 1484 | return CD->getClassInterface(); |
| 1485 | if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
| 1486 | return IMD->getClassInterface(); |
| 1487 | |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1491 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1492 | /// none exists. |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1493 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1494 | assert(VD && "Passed null params"); |
| 1495 | assert(VD->hasAttr<BlocksAttr>() && |
| 1496 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1497 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1498 | I = BlockVarCopyInits.find(VD); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1499 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0; |
| 1500 | } |
| 1501 | |
| 1502 | /// \brief Set the copy inialization expression of a block var decl. |
| 1503 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1504 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1505 | assert(VD->hasAttr<BlocksAttr>() && |
| 1506 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1507 | BlockVarCopyInits[VD] = Init; |
| 1508 | } |
| 1509 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1510 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1511 | unsigned DataSize) const { |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1512 | if (!DataSize) |
| 1513 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 1514 | else |
| 1515 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1516 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 1517 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1518 | TypeSourceInfo *TInfo = |
| 1519 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 1520 | new (TInfo) TypeSourceInfo(T); |
| 1521 | return TInfo; |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1524 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1525 | SourceLocation L) const { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1526 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | 2d525f0 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 1527 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | 3665e00 | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 1528 | return DI; |
| 1529 | } |
| 1530 | |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1531 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1532 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1533 | return getObjCLayout(D, 0); |
| 1534 | } |
| 1535 | |
| 1536 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1537 | ASTContext::getASTObjCImplementationLayout( |
| 1538 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 1539 | return getObjCLayout(D->getClassInterface(), D); |
| 1540 | } |
| 1541 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1542 | //===----------------------------------------------------------------------===// |
| 1543 | // Type creation/memoization methods |
| 1544 | //===----------------------------------------------------------------------===// |
| 1545 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1546 | QualType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1547 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 1548 | unsigned fastQuals = quals.getFastQualifiers(); |
| 1549 | quals.removeFastQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1550 | |
| 1551 | // Check if we've already instantiated this type. |
| 1552 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1553 | ExtQuals::Profile(ID, baseType, quals); |
| 1554 | void *insertPos = 0; |
| 1555 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 1556 | assert(eq->getQualifiers() == quals); |
| 1557 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1560 | // If the base type is not canonical, make the appropriate canonical type. |
| 1561 | QualType canon; |
| 1562 | if (!baseType->isCanonicalUnqualified()) { |
| 1563 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1564 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 1565 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1566 | |
| 1567 | // Re-find the insert position. |
| 1568 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 1569 | } |
| 1570 | |
| 1571 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 1572 | ExtQualNodes.InsertNode(eq, insertPos); |
| 1573 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1576 | QualType |
| 1577 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1578 | QualType CanT = getCanonicalType(T); |
| 1579 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | 445fcab | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1580 | return T; |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1581 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1582 | // If we are composing extended qualifiers together, merge together |
| 1583 | // into one ExtQuals node. |
| 1584 | QualifierCollector Quals; |
| 1585 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1587 | // If this type already has an address space specified, it cannot get |
| 1588 | // another one. |
| 1589 | assert(!Quals.hasAddressSpace() && |
| 1590 | "Type cannot be in multiple addr spaces!"); |
| 1591 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1593 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | 025b5fb | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1596 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1597 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1598 | QualType CanT = getCanonicalType(T); |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 1599 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1600 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1602 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 1603 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 1604 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | b68215c | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 1605 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 1606 | return getPointerType(ResultType); |
| 1607 | } |
| 1608 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1609 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1610 | // If we are composing extended qualifiers together, merge together |
| 1611 | // into one ExtQuals node. |
| 1612 | QualifierCollector Quals; |
| 1613 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1615 | // If this type already has an ObjCGC specified, it cannot get |
| 1616 | // another one. |
| 1617 | assert(!Quals.hasObjCGCAttr() && |
| 1618 | "Type cannot have multiple ObjCGCs!"); |
| 1619 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1621 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 1622 | } |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1623 | |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 1624 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 1625 | FunctionType::ExtInfo Info) { |
| 1626 | if (T->getExtInfo() == Info) |
| 1627 | return T; |
| 1628 | |
| 1629 | QualType Result; |
| 1630 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
| 1631 | Result = getFunctionNoProtoType(FNPT->getResultType(), Info); |
| 1632 | } else { |
| 1633 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 1634 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 1635 | EPI.ExtInfo = Info; |
| 1636 | Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(), |
| 1637 | FPT->getNumArgs(), EPI); |
| 1638 | } |
| 1639 | |
| 1640 | return cast<FunctionType>(Result.getTypePtr()); |
| 1641 | } |
| 1642 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1643 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 1644 | /// number with the specified element type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1645 | QualType ASTContext::getComplexType(QualType T) const { |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1646 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1647 | // structure. |
| 1648 | llvm::FoldingSetNodeID ID; |
| 1649 | ComplexType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1651 | void *InsertPos = 0; |
| 1652 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1653 | return QualType(CT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1654 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1655 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1656 | // so fill in the canonical type field. |
| 1657 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1658 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1659 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1661 | // Get the new insert position for the node we care about. |
| 1662 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1663 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1664 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1665 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1666 | Types.push_back(New); |
| 1667 | ComplexTypes.InsertNode(New, InsertPos); |
| 1668 | return QualType(New, 0); |
| 1669 | } |
| 1670 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1671 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 1672 | /// the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1673 | QualType ASTContext::getPointerType(QualType T) const { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 1674 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1675 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1676 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1677 | PointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1679 | void *InsertPos = 0; |
| 1680 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1681 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1682 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 1683 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 1684 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1685 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1686 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 1687 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1688 | |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1689 | // Get the new insert position for the node we care about. |
| 1690 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1691 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1692 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1693 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 1694 | Types.push_back(New); |
| 1695 | PointerTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1696 | return QualType(New, 0); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1700 | /// a pointer to the specified block. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1701 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 0ac01283 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 1702 | assert(T->isFunctionType() && "block of function types only"); |
| 1703 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1704 | // structure. |
| 1705 | llvm::FoldingSetNodeID ID; |
| 1706 | BlockPointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1708 | void *InsertPos = 0; |
| 1709 | if (BlockPointerType *PT = |
| 1710 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1711 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1712 | |
| 1713 | // 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] | 1714 | // type either so fill in the canonical type field. |
| 1715 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 1716 | if (!T.isCanonical()) { |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1717 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1719 | // Get the new insert position for the node we care about. |
| 1720 | BlockPointerType *NewIP = |
| 1721 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1722 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1723 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1724 | BlockPointerType *New |
| 1725 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 1726 | Types.push_back(New); |
| 1727 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 1728 | return QualType(New, 0); |
| 1729 | } |
| 1730 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1731 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 1732 | /// lvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1733 | QualType |
| 1734 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 291e8ee | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 1735 | assert(getCanonicalType(T) != OverloadTy && |
| 1736 | "Unresolved overloaded function type"); |
| 1737 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1738 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1739 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1740 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1741 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1742 | |
| 1743 | void *InsertPos = 0; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1744 | if (LValueReferenceType *RT = |
| 1745 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1746 | return QualType(RT, 0); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1747 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1748 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1749 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1750 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1751 | // either, so fill in the canonical type field. |
| 1752 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1753 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 1754 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1755 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1756 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1757 | // Get the new insert position for the node we care about. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1758 | LValueReferenceType *NewIP = |
| 1759 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1760 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1763 | LValueReferenceType *New |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1764 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 1765 | SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1766 | Types.push_back(New); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1767 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1768 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1769 | return QualType(New, 0); |
| 1770 | } |
| 1771 | |
| 1772 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 1773 | /// rvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1774 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1775 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1776 | // structure. |
| 1777 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1778 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1779 | |
| 1780 | void *InsertPos = 0; |
| 1781 | if (RValueReferenceType *RT = |
| 1782 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1783 | return QualType(RT, 0); |
| 1784 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1785 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 1786 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1787 | // If the referencee type isn't canonical, this won't be a canonical type |
| 1788 | // either, so fill in the canonical type field. |
| 1789 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 1790 | if (InnerRef || !T.isCanonical()) { |
| 1791 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 1792 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1793 | |
| 1794 | // Get the new insert position for the node we care about. |
| 1795 | RValueReferenceType *NewIP = |
| 1796 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1797 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1800 | RValueReferenceType *New |
| 1801 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1802 | Types.push_back(New); |
| 1803 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 1804 | return QualType(New, 0); |
| 1805 | } |
| 1806 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1807 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 1808 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1809 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1810 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 1811 | // structure. |
| 1812 | llvm::FoldingSetNodeID ID; |
| 1813 | MemberPointerType::Profile(ID, T, Cls); |
| 1814 | |
| 1815 | void *InsertPos = 0; |
| 1816 | if (MemberPointerType *PT = |
| 1817 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 1818 | return QualType(PT, 0); |
| 1819 | |
| 1820 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 1821 | // type either, so fill in the canonical type field. |
| 1822 | QualType Canonical; |
Douglas Gregor | 615ac67 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 1823 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1824 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 1825 | |
| 1826 | // Get the new insert position for the node we care about. |
| 1827 | MemberPointerType *NewIP = |
| 1828 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1829 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1830 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1831 | MemberPointerType *New |
| 1832 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1833 | Types.push_back(New); |
| 1834 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 1835 | return QualType(New, 0); |
| 1836 | } |
| 1837 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1839 | /// array of the specified element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1841 | const llvm::APInt &ArySizeIn, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 1842 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1843 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 2dfdb82 | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 1844 | assert((EltTy->isDependentType() || |
| 1845 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 1846 | "Constant array of VLAs is illegal!"); |
| 1847 | |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 1848 | // Convert the array size into a canonical width matching the pointer size for |
| 1849 | // the target. |
| 1850 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1851 | ArySize = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1852 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1854 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1855 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1857 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1858 | if (ConstantArrayType *ATP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1859 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1860 | return QualType(ATP, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1861 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1862 | // If the element type isn't canonical or has qualifiers, this won't |
| 1863 | // be a canonical type either, so fill in the canonical type field. |
| 1864 | QualType Canon; |
| 1865 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 1866 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1867 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1868 | ASM, IndexTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1869 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1870 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1871 | // Get the new insert position for the node we care about. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1872 | ConstantArrayType *NewIP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1873 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 1874 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1875 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1876 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1877 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 1878 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 1879 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 1880 | Types.push_back(New); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 1881 | return QualType(New, 0); |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1884 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 1885 | /// variably-modified, into the corresponding type with all the known |
| 1886 | /// sizes replaced with [*]. |
| 1887 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 1888 | // Vastly most common case. |
| 1889 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1890 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1891 | QualType result; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1892 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1893 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 1894 | const Type *ty = split.Ty; |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1895 | switch (ty->getTypeClass()) { |
| 1896 | #define TYPE(Class, Base) |
| 1897 | #define ABSTRACT_TYPE(Class, Base) |
| 1898 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1899 | #include "clang/AST/TypeNodes.def" |
| 1900 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 1901 | |
| 1902 | // These types should never be variably-modified. |
| 1903 | case Type::Builtin: |
| 1904 | case Type::Complex: |
| 1905 | case Type::Vector: |
| 1906 | case Type::ExtVector: |
| 1907 | case Type::DependentSizedExtVector: |
| 1908 | case Type::ObjCObject: |
| 1909 | case Type::ObjCInterface: |
| 1910 | case Type::ObjCObjectPointer: |
| 1911 | case Type::Record: |
| 1912 | case Type::Enum: |
| 1913 | case Type::UnresolvedUsing: |
| 1914 | case Type::TypeOfExpr: |
| 1915 | case Type::TypeOf: |
| 1916 | case Type::Decltype: |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 1917 | case Type::UnaryTransform: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1918 | case Type::DependentName: |
| 1919 | case Type::InjectedClassName: |
| 1920 | case Type::TemplateSpecialization: |
| 1921 | case Type::DependentTemplateSpecialization: |
| 1922 | case Type::TemplateTypeParm: |
| 1923 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1924 | case Type::Auto: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1925 | case Type::PackExpansion: |
| 1926 | llvm_unreachable("type should never be variably-modified"); |
| 1927 | |
| 1928 | // These types can be variably-modified but should never need to |
| 1929 | // further decay. |
| 1930 | case Type::FunctionNoProto: |
| 1931 | case Type::FunctionProto: |
| 1932 | case Type::BlockPointer: |
| 1933 | case Type::MemberPointer: |
| 1934 | return type; |
| 1935 | |
| 1936 | // These types can be variably-modified. All these modifications |
| 1937 | // preserve structure except as noted by comments. |
| 1938 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 1939 | // optimizations available here. |
| 1940 | case Type::Pointer: |
| 1941 | result = getPointerType(getVariableArrayDecayedType( |
| 1942 | cast<PointerType>(ty)->getPointeeType())); |
| 1943 | break; |
| 1944 | |
| 1945 | case Type::LValueReference: { |
| 1946 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 1947 | result = getLValueReferenceType( |
| 1948 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 1949 | lv->isSpelledAsLValue()); |
| 1950 | break; |
| 1951 | } |
| 1952 | |
| 1953 | case Type::RValueReference: { |
| 1954 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 1955 | result = getRValueReferenceType( |
| 1956 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 1957 | break; |
| 1958 | } |
| 1959 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1960 | case Type::Atomic: { |
| 1961 | const AtomicType *at = cast<AtomicType>(ty); |
| 1962 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 1963 | break; |
| 1964 | } |
| 1965 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 1966 | case Type::ConstantArray: { |
| 1967 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 1968 | result = getConstantArrayType( |
| 1969 | getVariableArrayDecayedType(cat->getElementType()), |
| 1970 | cat->getSize(), |
| 1971 | cat->getSizeModifier(), |
| 1972 | cat->getIndexTypeCVRQualifiers()); |
| 1973 | break; |
| 1974 | } |
| 1975 | |
| 1976 | case Type::DependentSizedArray: { |
| 1977 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 1978 | result = getDependentSizedArrayType( |
| 1979 | getVariableArrayDecayedType(dat->getElementType()), |
| 1980 | dat->getSizeExpr(), |
| 1981 | dat->getSizeModifier(), |
| 1982 | dat->getIndexTypeCVRQualifiers(), |
| 1983 | dat->getBracketsRange()); |
| 1984 | break; |
| 1985 | } |
| 1986 | |
| 1987 | // Turn incomplete types into [*] types. |
| 1988 | case Type::IncompleteArray: { |
| 1989 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 1990 | result = getVariableArrayType( |
| 1991 | getVariableArrayDecayedType(iat->getElementType()), |
| 1992 | /*size*/ 0, |
| 1993 | ArrayType::Normal, |
| 1994 | iat->getIndexTypeCVRQualifiers(), |
| 1995 | SourceRange()); |
| 1996 | break; |
| 1997 | } |
| 1998 | |
| 1999 | // Turn VLA types into [*] types. |
| 2000 | case Type::VariableArray: { |
| 2001 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 2002 | result = getVariableArrayType( |
| 2003 | getVariableArrayDecayedType(vat->getElementType()), |
| 2004 | /*size*/ 0, |
| 2005 | ArrayType::Star, |
| 2006 | vat->getIndexTypeCVRQualifiers(), |
| 2007 | vat->getBracketsRange()); |
| 2008 | break; |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | // Apply the top-level qualifiers from the original. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2013 | return getQualifiedType(result, split.Quals); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2014 | } |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2015 | |
Steve Naroff | cadebd0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 2016 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 2017 | /// variable array of the specified element type. |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2018 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 2019 | Expr *NumElts, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2020 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2021 | unsigned IndexTypeQuals, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2022 | SourceRange Brackets) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2023 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 2024 | // that have an expression provided for their size. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2025 | QualType Canon; |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2026 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2027 | // Be sure to pull qualifiers off the element type. |
| 2028 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2029 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2030 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2031 | IndexTypeQuals, Brackets); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2032 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2035 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2036 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2037 | |
| 2038 | VariableArrayTypes.push_back(New); |
| 2039 | Types.push_back(New); |
| 2040 | return QualType(New, 0); |
| 2041 | } |
| 2042 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2043 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 2044 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 2045 | /// type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2046 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 2047 | Expr *numElements, |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2048 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2049 | unsigned elementTypeQuals, |
| 2050 | SourceRange brackets) const { |
| 2051 | assert((!numElements || numElements->isTypeDependent() || |
| 2052 | numElements->isValueDependent()) && |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2053 | "Size must be type- or value-dependent!"); |
| 2054 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2055 | // Dependently-sized array types that do not have a specified number |
| 2056 | // of elements will have their sizes deduced from a dependent |
| 2057 | // initializer. We do no canonicalization here at all, which is okay |
| 2058 | // because they can't be used in most locations. |
| 2059 | if (!numElements) { |
| 2060 | DependentSizedArrayType *newType |
| 2061 | = new (*this, TypeAlignment) |
| 2062 | DependentSizedArrayType(*this, elementType, QualType(), |
| 2063 | numElements, ASM, elementTypeQuals, |
| 2064 | brackets); |
| 2065 | Types.push_back(newType); |
| 2066 | return QualType(newType, 0); |
| 2067 | } |
| 2068 | |
| 2069 | // Otherwise, we actually build a new type every time, but we |
| 2070 | // also build a canonical type. |
| 2071 | |
| 2072 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 2073 | |
| 2074 | void *insertPos = 0; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2075 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2076 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2077 | QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2078 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2079 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2080 | // Look for an existing type with these properties. |
| 2081 | DependentSizedArrayType *canonTy = |
| 2082 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2083 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2084 | // If we don't have one, build one. |
| 2085 | if (!canonTy) { |
| 2086 | canonTy = new (*this, TypeAlignment) |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2087 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2088 | QualType(), numElements, ASM, elementTypeQuals, |
| 2089 | brackets); |
| 2090 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 2091 | Types.push_back(canonTy); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2094 | // Apply qualifiers from the element type to the array. |
| 2095 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2096 | canonElementType.Quals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2097 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2098 | // If we didn't need extra canonicalization for the element type, |
| 2099 | // then just use that as our result. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2100 | if (QualType(canonElementType.Ty, 0) == elementType) |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2101 | return canon; |
| 2102 | |
| 2103 | // Otherwise, we need to build a type which follows the spelling |
| 2104 | // of the element type. |
| 2105 | DependentSizedArrayType *sugaredType |
| 2106 | = new (*this, TypeAlignment) |
| 2107 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 2108 | ASM, elementTypeQuals, brackets); |
| 2109 | Types.push_back(sugaredType); |
| 2110 | return QualType(sugaredType, 0); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2113 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2114 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2115 | unsigned elementTypeQuals) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2116 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2117 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2118 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2119 | void *insertPos = 0; |
| 2120 | if (IncompleteArrayType *iat = |
| 2121 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 2122 | return QualType(iat, 0); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2123 | |
| 2124 | // 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] | 2125 | // either, so fill in the canonical type field. We also have to pull |
| 2126 | // qualifiers off the element type. |
| 2127 | QualType canon; |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2128 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2129 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 2130 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2131 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2132 | ASM, elementTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2133 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2134 | |
| 2135 | // Get the new insert position for the node we care about. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2136 | IncompleteArrayType *existing = |
| 2137 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 2138 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 843ebedd | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 2139 | } |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2140 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2141 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 2142 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2143 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2144 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 2145 | Types.push_back(newType); |
| 2146 | return QualType(newType, 0); |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2149 | /// getVectorType - Return the unique reference to a vector type of |
| 2150 | /// 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] | 2151 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2152 | VectorType::VectorKind VecKind) const { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2153 | assert(vecType->isBuiltinType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2154 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2155 | // Check if we've already instantiated a vector of this type. |
| 2156 | llvm::FoldingSetNodeID ID; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2157 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2158 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2159 | void *InsertPos = 0; |
| 2160 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2161 | return QualType(VTP, 0); |
| 2162 | |
| 2163 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2164 | // so fill in the canonical type field. |
| 2165 | QualType Canonical; |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 2166 | if (!vecType.isCanonical()) { |
Bob Wilson | 7795480 | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 2167 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2169 | // Get the new insert position for the node we care about. |
| 2170 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2171 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2172 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2173 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2174 | VectorType(vecType, NumElts, Canonical, VecKind); |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2175 | VectorTypes.InsertNode(New, InsertPos); |
| 2176 | Types.push_back(New); |
| 2177 | return QualType(New, 0); |
| 2178 | } |
| 2179 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2180 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2181 | /// 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] | 2182 | QualType |
| 2183 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2184 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2186 | // Check if we've already instantiated a vector of this type. |
| 2187 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2188 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2189 | VectorType::GenericVector); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2190 | void *InsertPos = 0; |
| 2191 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2192 | return QualType(VTP, 0); |
| 2193 | |
| 2194 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2195 | // so fill in the canonical type field. |
| 2196 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2197 | if (!vecType.isCanonical()) { |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2198 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2199 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2200 | // Get the new insert position for the node we care about. |
| 2201 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2202 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2203 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2204 | ExtVectorType *New = new (*this, TypeAlignment) |
| 2205 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2206 | VectorTypes.InsertNode(New, InsertPos); |
| 2207 | Types.push_back(New); |
| 2208 | return QualType(New, 0); |
| 2209 | } |
| 2210 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2211 | QualType |
| 2212 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 2213 | Expr *SizeExpr, |
| 2214 | SourceLocation AttrLoc) const { |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2215 | llvm::FoldingSetNodeID ID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2216 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2217 | SizeExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2218 | |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2219 | void *InsertPos = 0; |
| 2220 | DependentSizedExtVectorType *Canon |
| 2221 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2222 | DependentSizedExtVectorType *New; |
| 2223 | if (Canon) { |
| 2224 | // We already have a canonical version of this array type; use it as |
| 2225 | // the canonical type for a newly-built type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2226 | New = new (*this, TypeAlignment) |
| 2227 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2228 | SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2229 | } else { |
| 2230 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2231 | if (CanonVecTy == vecType) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2232 | New = new (*this, TypeAlignment) |
| 2233 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2234 | AttrLoc); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2235 | |
| 2236 | DependentSizedExtVectorType *CanonCheck |
| 2237 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2238 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2239 | (void)CanonCheck; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2240 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2241 | } else { |
| 2242 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2243 | SourceLocation()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2244 | New = new (*this, TypeAlignment) |
| 2245 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2246 | } |
| 2247 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2249 | Types.push_back(New); |
| 2250 | return QualType(New, 0); |
| 2251 | } |
| 2252 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2253 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2254 | /// |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2255 | QualType |
| 2256 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2257 | const FunctionType::ExtInfo &Info) const { |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2258 | const CallingConv DefaultCC = Info.getCC(); |
| 2259 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2260 | CC_X86StdCall : DefaultCC; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2261 | // Unique functions, to guarantee there is only one function of a particular |
| 2262 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2263 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2264 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2265 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2266 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2267 | if (FunctionNoProtoType *FT = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2268 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2269 | return QualType(FT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2271 | QualType Canonical; |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 2272 | if (!ResultTy.isCanonical() || |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2273 | getCanonicalCallConv(CallConv) != CallConv) { |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2274 | Canonical = |
| 2275 | getFunctionNoProtoType(getCanonicalType(ResultTy), |
| 2276 | Info.withCallingConv(getCanonicalCallConv(CallConv))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2277 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2278 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2279 | FunctionNoProtoType *NewIP = |
| 2280 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2281 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2282 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2283 | |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2284 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2285 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2286 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2287 | Types.push_back(New); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2288 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2289 | return QualType(New, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | /// getFunctionType - Return a normal function type with a typed argument |
| 2293 | /// list. isVariadic indicates whether the argument list includes '...'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2294 | QualType |
| 2295 | ASTContext::getFunctionType(QualType ResultTy, |
| 2296 | const QualType *ArgArray, unsigned NumArgs, |
| 2297 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2298 | // Unique functions, to guarantee there is only one function of a particular |
| 2299 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2300 | llvm::FoldingSetNodeID ID; |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2301 | FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this); |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 2302 | |
| 2303 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2304 | if (FunctionProtoType *FTP = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2305 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2306 | return QualType(FTP, 0); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2307 | |
| 2308 | // Determine whether the type being created is already canonical or not. |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2309 | bool isCanonical = |
| 2310 | EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical() && |
| 2311 | !EPI.HasTrailingReturn; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2312 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2313 | if (!ArgArray[i].isCanonicalAsParam()) |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2314 | isCanonical = false; |
| 2315 | |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2316 | const CallingConv DefaultCC = EPI.ExtInfo.getCC(); |
| 2317 | const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ? |
| 2318 | CC_X86StdCall : DefaultCC; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2319 | |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2320 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2321 | // The exception spec is not part of the canonical type. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2322 | QualType Canonical; |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2323 | if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2324 | SmallVector<QualType, 16> CanonicalArgs; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2325 | CanonicalArgs.reserve(NumArgs); |
| 2326 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2327 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2328 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2329 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2330 | CanonicalEPI.HasTrailingReturn = false; |
Sebastian Redl | 7c6c9e9 | 2011-03-06 10:52:04 +0000 | [diff] [blame] | 2331 | CanonicalEPI.ExceptionSpecType = EST_None; |
| 2332 | CanonicalEPI.NumExceptions = 0; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2333 | CanonicalEPI.ExtInfo |
| 2334 | = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv)); |
| 2335 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2336 | Canonical = getFunctionType(getCanonicalType(ResultTy), |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2337 | CanonicalArgs.data(), NumArgs, |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2338 | CanonicalEPI); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2339 | |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 2340 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2341 | FunctionProtoType *NewIP = |
| 2342 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Jeffrey Yasskin | b332153 | 2010-12-23 01:01:28 +0000 | [diff] [blame] | 2343 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2344 | } |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2345 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2346 | // FunctionProtoType objects are allocated with extra bytes after |
| 2347 | // them for three variable size arrays at the end: |
| 2348 | // - parameter types |
| 2349 | // - exception types |
| 2350 | // - consumed-arguments flags |
| 2351 | // Instead of the exception types, there could be a noexcept |
| 2352 | // expression. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2353 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2354 | NumArgs * sizeof(QualType); |
| 2355 | if (EPI.ExceptionSpecType == EST_Dynamic) |
| 2356 | Size += EPI.NumExceptions * sizeof(QualType); |
| 2357 | else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) { |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2358 | Size += sizeof(Expr*); |
Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 2359 | } else if (EPI.ExceptionSpecType == EST_Uninstantiated) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2360 | Size += 2 * sizeof(FunctionDecl*); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2361 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2362 | if (EPI.ConsumedArguments) |
| 2363 | Size += NumArgs * sizeof(bool); |
| 2364 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2365 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2366 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
| 2367 | newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv); |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2368 | new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2369 | Types.push_back(FTP); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2370 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2371 | return QualType(FTP, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2372 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 2373 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2374 | #ifndef NDEBUG |
| 2375 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 2376 | if (!isa<CXXRecordDecl>(D)) return false; |
| 2377 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 2378 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 2379 | return true; |
| 2380 | if (RD->getDescribedClassTemplate() && |
| 2381 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 2382 | return true; |
| 2383 | return false; |
| 2384 | } |
| 2385 | #endif |
| 2386 | |
| 2387 | /// getInjectedClassNameType - Return the unique reference to the |
| 2388 | /// injected class name type for the specified templated declaration. |
| 2389 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2390 | QualType TST) const { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2391 | assert(NeedsInjectedClassNameType(Decl)); |
| 2392 | if (Decl->TypeForDecl) { |
| 2393 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2394 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2395 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 2396 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 2397 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 2398 | } else { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2399 | Type *newType = |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2400 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2401 | Decl->TypeForDecl = newType; |
| 2402 | Types.push_back(newType); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2403 | } |
| 2404 | return QualType(Decl->TypeForDecl, 0); |
| 2405 | } |
| 2406 | |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2407 | /// getTypeDeclType - Return the unique reference to the type for the |
| 2408 | /// specified type declaration. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2409 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 89656d2 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 2410 | assert(Decl && "Passed null for Decl param"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2411 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2412 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2413 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2414 | return getTypedefType(Typedef); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2415 | |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2416 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 2417 | "Template type parameter types are always available."); |
| 2418 | |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2419 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2420 | assert(!Record->getPreviousDecl() && |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2421 | "struct/union has previous declaration"); |
| 2422 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2423 | return getRecordType(Record); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2424 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2425 | assert(!Enum->getPreviousDecl() && |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2426 | "enum has previous declaration"); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2427 | return getEnumType(Enum); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 2428 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2429 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2430 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 2431 | Decl->TypeForDecl = newType; |
| 2432 | Types.push_back(newType); |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 2433 | } else |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 2434 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2435 | |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 2436 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 2439 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2440 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2441 | QualType |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2442 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 2443 | QualType Canonical) const { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2444 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2445 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2446 | if (Canonical.isNull()) |
| 2447 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2448 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2449 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2450 | Decl->TypeForDecl = newType; |
| 2451 | Types.push_back(newType); |
| 2452 | return QualType(newType, 0); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 2453 | } |
| 2454 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2455 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2456 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2457 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2458 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2459 | if (PrevDecl->TypeForDecl) |
| 2460 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2461 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2462 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 2463 | Decl->TypeForDecl = newType; |
| 2464 | Types.push_back(newType); |
| 2465 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2468 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2469 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 2470 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 2471 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2472 | if (PrevDecl->TypeForDecl) |
| 2473 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 2474 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2475 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 2476 | Decl->TypeForDecl = newType; |
| 2477 | Types.push_back(newType); |
| 2478 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2481 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 2482 | QualType modifiedType, |
| 2483 | QualType equivalentType) { |
| 2484 | llvm::FoldingSetNodeID id; |
| 2485 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 2486 | |
| 2487 | void *insertPos = 0; |
| 2488 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 2489 | if (type) return QualType(type, 0); |
| 2490 | |
| 2491 | QualType canon = getCanonicalType(equivalentType); |
| 2492 | type = new (*this, TypeAlignment) |
| 2493 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 2494 | |
| 2495 | Types.push_back(type); |
| 2496 | AttributedTypes.InsertNode(type, insertPos); |
| 2497 | |
| 2498 | return QualType(type, 0); |
| 2499 | } |
| 2500 | |
| 2501 | |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2502 | /// \brief Retrieve a substitution-result type. |
| 2503 | QualType |
| 2504 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2505 | QualType Replacement) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2506 | assert(Replacement.isCanonical() |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2507 | && "replacement types must always be canonical"); |
| 2508 | |
| 2509 | llvm::FoldingSetNodeID ID; |
| 2510 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
| 2511 | void *InsertPos = 0; |
| 2512 | SubstTemplateTypeParmType *SubstParm |
| 2513 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2514 | |
| 2515 | if (!SubstParm) { |
| 2516 | SubstParm = new (*this, TypeAlignment) |
| 2517 | SubstTemplateTypeParmType(Parm, Replacement); |
| 2518 | Types.push_back(SubstParm); |
| 2519 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2520 | } |
| 2521 | |
| 2522 | return QualType(SubstParm, 0); |
| 2523 | } |
| 2524 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 2525 | /// \brief Retrieve a |
| 2526 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 2527 | const TemplateTypeParmType *Parm, |
| 2528 | const TemplateArgument &ArgPack) { |
| 2529 | #ifndef NDEBUG |
| 2530 | for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(), |
| 2531 | PEnd = ArgPack.pack_end(); |
| 2532 | P != PEnd; ++P) { |
| 2533 | assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 2534 | assert(P->getAsType().isCanonical() && "Pack contains non-canonical type"); |
| 2535 | } |
| 2536 | #endif |
| 2537 | |
| 2538 | llvm::FoldingSetNodeID ID; |
| 2539 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
| 2540 | void *InsertPos = 0; |
| 2541 | if (SubstTemplateTypeParmPackType *SubstParm |
| 2542 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2543 | return QualType(SubstParm, 0); |
| 2544 | |
| 2545 | QualType Canon; |
| 2546 | if (!Parm->isCanonicalUnqualified()) { |
| 2547 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 2548 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 2549 | ArgPack); |
| 2550 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2551 | } |
| 2552 | |
| 2553 | SubstTemplateTypeParmPackType *SubstParm |
| 2554 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 2555 | ArgPack); |
| 2556 | Types.push_back(SubstParm); |
| 2557 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 2558 | return QualType(SubstParm, 0); |
| 2559 | } |
| 2560 | |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2561 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2562 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2563 | /// name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2564 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2565 | bool ParameterPack, |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2566 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2567 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2568 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2569 | void *InsertPos = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2570 | TemplateTypeParmType *TypeParm |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2571 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2572 | |
| 2573 | if (TypeParm) |
| 2574 | return QualType(TypeParm, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2576 | if (TTPDecl) { |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2577 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2578 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2579 | |
| 2580 | TemplateTypeParmType *TypeCheck |
| 2581 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2582 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 2583 | (void)TypeCheck; |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 2584 | } else |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2585 | TypeParm = new (*this, TypeAlignment) |
| 2586 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 2587 | |
| 2588 | Types.push_back(TypeParm); |
| 2589 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 2590 | |
| 2591 | return QualType(TypeParm, 0); |
| 2592 | } |
| 2593 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2594 | TypeSourceInfo * |
| 2595 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 2596 | SourceLocation NameLoc, |
| 2597 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2598 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2599 | assert(!Name.getAsDependentTemplateName() && |
| 2600 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2601 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2602 | |
| 2603 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
| 2604 | TemplateSpecializationTypeLoc TL |
| 2605 | = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc()); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 2606 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2607 | TL.setTemplateNameLoc(NameLoc); |
| 2608 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 2609 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 2610 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 2611 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 2612 | return DI; |
| 2613 | } |
| 2614 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2615 | QualType |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2616 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2617 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2618 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2619 | assert(!Template.getAsDependentTemplateName() && |
| 2620 | "No dependent template names here!"); |
| 2621 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2622 | unsigned NumArgs = Args.size(); |
| 2623 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2624 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2625 | ArgVec.reserve(NumArgs); |
| 2626 | for (unsigned i = 0; i != NumArgs; ++i) |
| 2627 | ArgVec.push_back(Args[i].getArgument()); |
| 2628 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2629 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2630 | Underlying); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2633 | #ifndef NDEBUG |
| 2634 | static bool hasAnyPackExpansions(const TemplateArgument *Args, |
| 2635 | unsigned NumArgs) { |
| 2636 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2637 | if (Args[I].isPackExpansion()) |
| 2638 | return true; |
| 2639 | |
| 2640 | return true; |
| 2641 | } |
| 2642 | #endif |
| 2643 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2644 | QualType |
| 2645 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2646 | const TemplateArgument *Args, |
| 2647 | unsigned NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2648 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2649 | assert(!Template.getAsDependentTemplateName() && |
| 2650 | "No dependent template names here!"); |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2651 | // Look through qualified template names. |
| 2652 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2653 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2654 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2655 | bool IsTypeAlias = |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2656 | Template.getAsTemplateDecl() && |
| 2657 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2658 | QualType CanonType; |
| 2659 | if (!Underlying.isNull()) |
| 2660 | CanonType = getCanonicalType(Underlying); |
| 2661 | else { |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2662 | // We can get here with an alias template when the specialization contains |
| 2663 | // a pack expansion that does not match up with a parameter pack. |
| 2664 | assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) && |
| 2665 | "Caller must compute aliased type"); |
| 2666 | IsTypeAlias = false; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2667 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 2668 | NumArgs); |
| 2669 | } |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 2670 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 2671 | // Allocate the (non-canonical) template specialization type, but don't |
| 2672 | // try to unique it: these types typically have location information that |
| 2673 | // we don't unique and don't want to lose. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2674 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 2675 | sizeof(TemplateArgument) * NumArgs + |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2676 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2677 | TypeAlignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2678 | TemplateSpecializationType *Spec |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 2679 | = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType, |
| 2680 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2681 | |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2682 | Types.push_back(Spec); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2683 | return QualType(Spec, 0); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2686 | QualType |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2687 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 2688 | const TemplateArgument *Args, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2689 | unsigned NumArgs) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2690 | assert(!Template.getAsDependentTemplateName() && |
| 2691 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2692 | |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 2693 | // Look through qualified template names. |
| 2694 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 2695 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 2696 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2697 | // Build the canonical template specialization type. |
| 2698 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2699 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2700 | CanonArgs.reserve(NumArgs); |
| 2701 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2702 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 2703 | |
| 2704 | // Determine whether this canonical template specialization type already |
| 2705 | // exists. |
| 2706 | llvm::FoldingSetNodeID ID; |
| 2707 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 2708 | CanonArgs.data(), NumArgs, *this); |
| 2709 | |
| 2710 | void *InsertPos = 0; |
| 2711 | TemplateSpecializationType *Spec |
| 2712 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2713 | |
| 2714 | if (!Spec) { |
| 2715 | // Allocate a new canonical template specialization type. |
| 2716 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 2717 | sizeof(TemplateArgument) * NumArgs), |
| 2718 | TypeAlignment); |
| 2719 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 2720 | CanonArgs.data(), NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 2721 | QualType(), QualType()); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 2722 | Types.push_back(Spec); |
| 2723 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 2724 | } |
| 2725 | |
| 2726 | assert(Spec->isDependentType() && |
| 2727 | "Non-dependent template-id type must have a canonical type"); |
| 2728 | return QualType(Spec, 0); |
| 2729 | } |
| 2730 | |
| 2731 | QualType |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2732 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 2733 | NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2734 | QualType NamedType) const { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2735 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2736 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2737 | |
| 2738 | void *InsertPos = 0; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2739 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2740 | if (T) |
| 2741 | return QualType(T, 0); |
| 2742 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2743 | QualType Canon = NamedType; |
| 2744 | if (!Canon.isCanonical()) { |
| 2745 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2746 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2747 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2748 | (void)CheckT; |
| 2749 | } |
| 2750 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2751 | T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2752 | Types.push_back(T); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2753 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 2754 | return QualType(T, 0); |
| 2755 | } |
| 2756 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2757 | QualType |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2758 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2759 | llvm::FoldingSetNodeID ID; |
| 2760 | ParenType::Profile(ID, InnerType); |
| 2761 | |
| 2762 | void *InsertPos = 0; |
| 2763 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2764 | if (T) |
| 2765 | return QualType(T, 0); |
| 2766 | |
| 2767 | QualType Canon = InnerType; |
| 2768 | if (!Canon.isCanonical()) { |
| 2769 | Canon = getCanonicalType(InnerType); |
| 2770 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2771 | assert(!CheckT && "Paren canonical type broken"); |
| 2772 | (void)CheckT; |
| 2773 | } |
| 2774 | |
| 2775 | T = new (*this) ParenType(InnerType, Canon); |
| 2776 | Types.push_back(T); |
| 2777 | ParenTypes.InsertNode(T, InsertPos); |
| 2778 | return QualType(T, 0); |
| 2779 | } |
| 2780 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2781 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 2782 | NestedNameSpecifier *NNS, |
| 2783 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2784 | QualType Canon) const { |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2785 | assert(NNS->isDependent() && "nested-name-specifier must be dependent"); |
| 2786 | |
| 2787 | if (Canon.isNull()) { |
| 2788 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2789 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2790 | if (Keyword == ETK_None) |
| 2791 | CanonKeyword = ETK_Typename; |
| 2792 | |
| 2793 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 2794 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2798 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2799 | |
| 2800 | void *InsertPos = 0; |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2801 | DependentNameType *T |
| 2802 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2803 | if (T) |
| 2804 | return QualType(T, 0); |
| 2805 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2806 | T = new (*this) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2807 | Types.push_back(T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2808 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2809 | return QualType(T, 0); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2812 | QualType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2813 | ASTContext::getDependentTemplateSpecializationType( |
| 2814 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 2815 | NestedNameSpecifier *NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2816 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2817 | const TemplateArgumentListInfo &Args) const { |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2818 | // TODO: avoid this copy |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2819 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2820 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 2821 | ArgCopy.push_back(Args[I].getArgument()); |
| 2822 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 2823 | ArgCopy.size(), |
| 2824 | ArgCopy.data()); |
| 2825 | } |
| 2826 | |
| 2827 | QualType |
| 2828 | ASTContext::getDependentTemplateSpecializationType( |
| 2829 | ElaboratedTypeKeyword Keyword, |
| 2830 | NestedNameSpecifier *NNS, |
| 2831 | const IdentifierInfo *Name, |
| 2832 | unsigned NumArgs, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2833 | const TemplateArgument *Args) const { |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 2834 | assert((!NNS || NNS->isDependent()) && |
| 2835 | "nested-name-specifier must be dependent"); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2836 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2837 | llvm::FoldingSetNodeID ID; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2838 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 2839 | Name, NumArgs, Args); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2840 | |
| 2841 | void *InsertPos = 0; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2842 | DependentTemplateSpecializationType *T |
| 2843 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2844 | if (T) |
| 2845 | return QualType(T, 0); |
| 2846 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2847 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2848 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2849 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 2850 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 2851 | |
| 2852 | bool AnyNonCanonArgs = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2853 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2854 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2855 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 2856 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 2857 | AnyNonCanonArgs = true; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2860 | QualType Canon; |
| 2861 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 2862 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 2863 | Name, NumArgs, |
| 2864 | CanonArgs.data()); |
| 2865 | |
| 2866 | // Find the insert position again. |
| 2867 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2868 | } |
| 2869 | |
| 2870 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 2871 | sizeof(TemplateArgument) * NumArgs), |
| 2872 | TypeAlignment); |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 2873 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2874 | Name, NumArgs, Args, Canon); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2875 | Types.push_back(T); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 2876 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2877 | return QualType(T, 0); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2880 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
| 2881 | llvm::Optional<unsigned> NumExpansions) { |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2882 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2883 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2884 | |
| 2885 | assert(Pattern->containsUnexpandedParameterPack() && |
| 2886 | "Pack expansions must expand one or more parameter packs"); |
| 2887 | void *InsertPos = 0; |
| 2888 | PackExpansionType *T |
| 2889 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2890 | if (T) |
| 2891 | return QualType(T, 0); |
| 2892 | |
| 2893 | QualType Canon; |
| 2894 | if (!Pattern.isCanonical()) { |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame^] | 2895 | Canon = getCanonicalType(Pattern); |
| 2896 | // The canonical type might not contain an unexpanded parameter pack, if it |
| 2897 | // contains an alias template specialization which ignores one of its |
| 2898 | // parameters. |
| 2899 | if (Canon->containsUnexpandedParameterPack()) { |
| 2900 | Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2901 | |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame^] | 2902 | // Find the insert position again, in case we inserted an element into |
| 2903 | // PackExpansionTypes and invalidated our insert position. |
| 2904 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2905 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2908 | T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 2909 | Types.push_back(T); |
| 2910 | PackExpansionTypes.InsertNode(T, InsertPos); |
| 2911 | return QualType(T, 0); |
| 2912 | } |
| 2913 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2914 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 2915 | /// alphabetically. |
| 2916 | static bool CmpProtocolNames(const ObjCProtocolDecl *LHS, |
| 2917 | const ObjCProtocolDecl *RHS) { |
Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2918 | return LHS->getDeclName() < RHS->getDeclName(); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2921 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2922 | unsigned NumProtocols) { |
| 2923 | if (NumProtocols == 0) return true; |
| 2924 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2925 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 2926 | return false; |
| 2927 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2928 | for (unsigned i = 1; i != NumProtocols; ++i) |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2929 | if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) || |
| 2930 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2931 | return false; |
| 2932 | return true; |
| 2933 | } |
| 2934 | |
| 2935 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2936 | unsigned &NumProtocols) { |
| 2937 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2938 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2939 | // Sort protocols, keyed by name. |
| 2940 | std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames); |
| 2941 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 2942 | // Canonicalize. |
| 2943 | for (unsigned I = 0, N = NumProtocols; I != N; ++I) |
| 2944 | Protocols[I] = Protocols[I]->getCanonicalDecl(); |
| 2945 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 2946 | // Remove duplicates. |
| 2947 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 2948 | NumProtocols = ProtocolsEnd-Protocols; |
| 2949 | } |
| 2950 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2951 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 2952 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2953 | unsigned NumProtocols) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2954 | // If the base type is an interface and there aren't any protocols |
| 2955 | // to add, then the interface type will do just fine. |
| 2956 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 2957 | return BaseType; |
| 2958 | |
| 2959 | // Look in the folding set for an existing type. |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2960 | llvm::FoldingSetNodeID ID; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2961 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2962 | void *InsertPos = 0; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2963 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2964 | return QualType(QT, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2965 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2966 | // Build the canonical type, which has the canonical base type and |
| 2967 | // a sorted-and-uniqued list of protocols. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2968 | QualType Canonical; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2969 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 2970 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 2971 | if (!ProtocolsSorted) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2972 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 2973 | Protocols + NumProtocols); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2974 | unsigned UniqueCount = NumProtocols; |
| 2975 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2976 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2977 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2978 | &Sorted[0], UniqueCount); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2979 | } else { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2980 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 2981 | Protocols, NumProtocols); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2982 | } |
| 2983 | |
| 2984 | // Regenerate InsertPos. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2985 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2986 | } |
| 2987 | |
| 2988 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 2989 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 2990 | void *Mem = Allocate(Size, TypeAlignment); |
| 2991 | ObjCObjectTypeImpl *T = |
| 2992 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 2993 | |
| 2994 | Types.push_back(T); |
| 2995 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 2996 | return QualType(T, 0); |
| 2997 | } |
| 2998 | |
| 2999 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 3000 | /// the given object type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3001 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3002 | llvm::FoldingSetNodeID ID; |
| 3003 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 3004 | |
| 3005 | void *InsertPos = 0; |
| 3006 | if (ObjCObjectPointerType *QT = |
| 3007 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3008 | return QualType(QT, 0); |
| 3009 | |
| 3010 | // Find the canonical object type. |
| 3011 | QualType Canonical; |
| 3012 | if (!ObjectT.isCanonical()) { |
| 3013 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 3014 | |
| 3015 | // Regenerate InsertPos. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3016 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3017 | } |
| 3018 | |
Douglas Gregor | f85bee6 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 3019 | // No match. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3020 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 3021 | ObjCObjectPointerType *QType = |
| 3022 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3024 | Types.push_back(QType); |
| 3025 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3026 | return QualType(QType, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3027 | } |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3028 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3029 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 3030 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3031 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 3032 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3033 | if (Decl->TypeForDecl) |
| 3034 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3035 | |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3036 | if (PrevDecl) { |
| 3037 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 3038 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3039 | return QualType(PrevDecl->TypeForDecl, 0); |
| 3040 | } |
| 3041 | |
Douglas Gregor | 7671e53 | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 3042 | // Prefer the definition, if there is one. |
| 3043 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 3044 | Decl = Def; |
| 3045 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3046 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 3047 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 3048 | Decl->TypeForDecl = T; |
| 3049 | Types.push_back(T); |
| 3050 | return QualType(T, 0); |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3053 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 3054 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3055 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3056 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3057 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3058 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3059 | TypeOfExprType *toe; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3060 | if (tofExpr->isTypeDependent()) { |
| 3061 | llvm::FoldingSetNodeID ID; |
| 3062 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3063 | |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3064 | void *InsertPos = 0; |
| 3065 | DependentTypeOfExprType *Canon |
| 3066 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3067 | if (Canon) { |
| 3068 | // We already have a "canonical" version of an identical, dependent |
| 3069 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3070 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3071 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3072 | } else { |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3073 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3074 | Canon |
| 3075 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3076 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 3077 | toe = Canon; |
| 3078 | } |
| 3079 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3080 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3081 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3082 | } |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3083 | Types.push_back(toe); |
| 3084 | return QualType(toe, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3087 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
| 3088 | /// TypeOfType AST's. The only motivation to unique these nodes would be |
| 3089 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3090 | /// an issue. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3091 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3092 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3093 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3094 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3095 | Types.push_back(tot); |
| 3096 | return QualType(tot, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 3099 | |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3100 | /// getDecltypeType - Unlike many "get<Type>" functions, we don't unique |
| 3101 | /// DecltypeType AST's. The only motivation to unique these nodes would be |
| 3102 | /// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3103 | /// an issue. This doesn't effect the type checker, since it operates |
David Blaikie | 876657b | 2011-11-06 22:28:03 +0000 | [diff] [blame] | 3104 | /// on canonical types (which are always unique). |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3105 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3106 | DecltypeType *dt; |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3107 | |
| 3108 | // C++0x [temp.type]p2: |
| 3109 | // If an expression e involves a template parameter, decltype(e) denotes a |
| 3110 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 3111 | // type only if their expressions are equivalent (14.5.6.1). |
| 3112 | if (e->isInstantiationDependent()) { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3113 | llvm::FoldingSetNodeID ID; |
| 3114 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3115 | |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3116 | void *InsertPos = 0; |
| 3117 | DependentDecltypeType *Canon |
| 3118 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3119 | if (Canon) { |
| 3120 | // We already have a "canonical" version of an equivalent, dependent |
| 3121 | // decltype type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3122 | dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy, |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3123 | QualType((DecltypeType*)Canon, 0)); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3124 | } else { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3125 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3126 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3127 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
| 3128 | dt = Canon; |
| 3129 | } |
| 3130 | } else { |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3131 | dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType, |
| 3132 | getCanonicalType(UnderlyingType)); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3133 | } |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3134 | Types.push_back(dt); |
| 3135 | return QualType(dt, 0); |
| 3136 | } |
| 3137 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3138 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 3139 | /// savings are minimal and these are rare. |
| 3140 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 3141 | QualType UnderlyingType, |
| 3142 | UnaryTransformType::UTTKind Kind) |
| 3143 | const { |
| 3144 | UnaryTransformType *Ty = |
Douglas Gregor | 6c6e676 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 3145 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 3146 | Kind, |
| 3147 | UnderlyingType->isDependentType() ? |
Peter Collingbourne | 15d48ec | 2012-03-05 16:02:06 +0000 | [diff] [blame] | 3148 | QualType() : getCanonicalType(UnderlyingType)); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3149 | Types.push_back(Ty); |
| 3150 | return QualType(Ty, 0); |
| 3151 | } |
| 3152 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3153 | /// getAutoType - We only unique auto types after they've been deduced. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3154 | QualType ASTContext::getAutoType(QualType DeducedType) const { |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3155 | void *InsertPos = 0; |
| 3156 | if (!DeducedType.isNull()) { |
| 3157 | // Look in the folding set for an existing type. |
| 3158 | llvm::FoldingSetNodeID ID; |
| 3159 | AutoType::Profile(ID, DeducedType); |
| 3160 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3161 | return QualType(AT, 0); |
| 3162 | } |
| 3163 | |
| 3164 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType); |
| 3165 | Types.push_back(AT); |
| 3166 | if (InsertPos) |
| 3167 | AutoTypes.InsertNode(AT, InsertPos); |
| 3168 | return QualType(AT, 0); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3171 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 3172 | /// the given value type. |
| 3173 | QualType ASTContext::getAtomicType(QualType T) const { |
| 3174 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3175 | // structure. |
| 3176 | llvm::FoldingSetNodeID ID; |
| 3177 | AtomicType::Profile(ID, T); |
| 3178 | |
| 3179 | void *InsertPos = 0; |
| 3180 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3181 | return QualType(AT, 0); |
| 3182 | |
| 3183 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 3184 | // either, so fill in the canonical type field. |
| 3185 | QualType Canonical; |
| 3186 | if (!T.isCanonical()) { |
| 3187 | Canonical = getAtomicType(getCanonicalType(T)); |
| 3188 | |
| 3189 | // Get the new insert position for the node we care about. |
| 3190 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3191 | assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP; |
| 3192 | } |
| 3193 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 3194 | Types.push_back(New); |
| 3195 | AtomicTypes.InsertNode(New, InsertPos); |
| 3196 | return QualType(New, 0); |
| 3197 | } |
| 3198 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3199 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 3200 | QualType ASTContext::getAutoDeductType() const { |
| 3201 | if (AutoDeductTy.isNull()) |
| 3202 | AutoDeductTy = getAutoType(QualType()); |
| 3203 | assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern"); |
| 3204 | return AutoDeductTy; |
| 3205 | } |
| 3206 | |
| 3207 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 3208 | QualType ASTContext::getAutoRRefDeductType() const { |
| 3209 | if (AutoRRefDeductTy.isNull()) |
| 3210 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 3211 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 3212 | return AutoRRefDeductTy; |
| 3213 | } |
| 3214 | |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 3215 | /// getTagDeclType - Return the unique reference to the type for the |
| 3216 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3217 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | 2b0ce11 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 3218 | assert (Decl); |
Mike Stump | b93185d | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 3219 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 3220 | // away const? mutable? |
| 3221 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3224 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 3225 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 3226 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 3227 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3228 | return getFromTargetType(Target->getSizeType()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 3229 | } |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 3230 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3231 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 3232 | CanQualType ASTContext::getIntMaxType() const { |
| 3233 | return getFromTargetType(Target->getIntMaxType()); |
| 3234 | } |
| 3235 | |
| 3236 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 3237 | CanQualType ASTContext::getUIntMaxType() const { |
| 3238 | return getFromTargetType(Target->getUIntMaxType()); |
| 3239 | } |
| 3240 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3241 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 3242 | /// Used when in C++, as a GCC extension. |
| 3243 | QualType ASTContext::getSignedWCharType() const { |
| 3244 | // FIXME: derive from "Target" ? |
| 3245 | return WCharTy; |
| 3246 | } |
| 3247 | |
| 3248 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 3249 | /// Used when in C++, as a GCC extension. |
| 3250 | QualType ASTContext::getUnsignedWCharType() const { |
| 3251 | // FIXME: derive from "Target" ? |
| 3252 | return UnsignedIntTy; |
| 3253 | } |
| 3254 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3255 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3256 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 3257 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3258 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3261 | //===----------------------------------------------------------------------===// |
| 3262 | // Type Operators |
| 3263 | //===----------------------------------------------------------------------===// |
| 3264 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3265 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3266 | // Push qualifiers into arrays, and then discard any remaining |
| 3267 | // qualifiers. |
| 3268 | T = getCanonicalType(T); |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3269 | T = getVariableArrayDecayedType(T); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3270 | const Type *Ty = T.getTypePtr(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3271 | QualType Result; |
| 3272 | if (isa<ArrayType>(Ty)) { |
| 3273 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 3274 | } else if (isa<FunctionType>(Ty)) { |
| 3275 | Result = getPointerType(QualType(Ty, 0)); |
| 3276 | } else { |
| 3277 | Result = QualType(Ty, 0); |
| 3278 | } |
| 3279 | |
| 3280 | return CanQualType::CreateUnsafe(Result); |
| 3281 | } |
| 3282 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3283 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 3284 | Qualifiers &quals) { |
| 3285 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 3286 | |
| 3287 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 3288 | // the unqualified desugared type and then drops it on the floor. |
| 3289 | // We then have to strip that sugar back off with |
| 3290 | // getUnqualifiedDesugaredType(), which is silly. |
| 3291 | const ArrayType *AT = |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3292 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3293 | |
| 3294 | // 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] | 3295 | if (!AT) { |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3296 | quals = splitType.Quals; |
| 3297 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3300 | // Otherwise, recurse on the array's element type. |
| 3301 | QualType elementType = AT->getElementType(); |
| 3302 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 3303 | |
| 3304 | // If that didn't change the element type, AT has no qualifiers, so we |
| 3305 | // can just use the results in splitType. |
| 3306 | if (elementType == unqualElementType) { |
| 3307 | assert(quals.empty()); // from the recursive call |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3308 | quals = splitType.Quals; |
| 3309 | return QualType(splitType.Ty, 0); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | // Otherwise, add in the qualifiers from the outermost type, then |
| 3313 | // build the type back up. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3314 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3315 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3316 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3317 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3318 | CAT->getSizeModifier(), 0); |
| 3319 | } |
| 3320 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3321 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3322 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3325 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3326 | return getVariableArrayType(unqualElementType, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3327 | VAT->getSizeExpr(), |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 3328 | VAT->getSizeModifier(), |
| 3329 | VAT->getIndexTypeCVRQualifiers(), |
| 3330 | VAT->getBracketsRange()); |
| 3331 | } |
| 3332 | |
| 3333 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3334 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 3335 | DSAT->getSizeModifier(), 0, |
| 3336 | SourceRange()); |
| 3337 | } |
| 3338 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3339 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 3340 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 3341 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 3342 | /// or pointer-to-member types, or if they are not similar at this |
| 3343 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 3344 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 3345 | /// be called in a loop that successively "unwraps" pointer and |
| 3346 | /// pointer-to-member types to compare them at each level. |
| 3347 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 3348 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 3349 | *T2PtrType = T2->getAs<PointerType>(); |
| 3350 | if (T1PtrType && T2PtrType) { |
| 3351 | T1 = T1PtrType->getPointeeType(); |
| 3352 | T2 = T2PtrType->getPointeeType(); |
| 3353 | return true; |
| 3354 | } |
| 3355 | |
| 3356 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 3357 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 3358 | if (T1MPType && T2MPType && |
| 3359 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 3360 | QualType(T2MPType->getClass(), 0))) { |
| 3361 | T1 = T1MPType->getPointeeType(); |
| 3362 | T2 = T2MPType->getPointeeType(); |
| 3363 | return true; |
| 3364 | } |
| 3365 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3366 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 3367 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 3368 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 3369 | if (T1OPType && T2OPType) { |
| 3370 | T1 = T1OPType->getPointeeType(); |
| 3371 | T2 = T2OPType->getPointeeType(); |
| 3372 | return true; |
| 3373 | } |
| 3374 | } |
| 3375 | |
| 3376 | // FIXME: Block pointers, too? |
| 3377 | |
| 3378 | return false; |
| 3379 | } |
| 3380 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3381 | DeclarationNameInfo |
| 3382 | ASTContext::getNameForTemplate(TemplateName Name, |
| 3383 | SourceLocation NameLoc) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3384 | switch (Name.getKind()) { |
| 3385 | case TemplateName::QualifiedTemplate: |
| 3386 | case TemplateName::Template: |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3387 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3388 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 3389 | NameLoc); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3390 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3391 | case TemplateName::OverloadedTemplate: { |
| 3392 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 3393 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 3394 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 3395 | } |
| 3396 | |
| 3397 | case TemplateName::DependentTemplate: { |
| 3398 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3399 | DeclarationName DName; |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3400 | if (DTN->isIdentifier()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3401 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 3402 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3403 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3404 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 3405 | // DNInfo work in progress: FIXME: source locations? |
| 3406 | DeclarationNameLoc DNLoc; |
| 3407 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 3408 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 3409 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3410 | } |
| 3411 | } |
| 3412 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3413 | case TemplateName::SubstTemplateTemplateParm: { |
| 3414 | SubstTemplateTemplateParmStorage *subst |
| 3415 | = Name.getAsSubstTemplateTemplateParm(); |
| 3416 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 3417 | NameLoc); |
| 3418 | } |
| 3419 | |
| 3420 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3421 | SubstTemplateTemplateParmPackStorage *subst |
| 3422 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3423 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 3424 | NameLoc); |
| 3425 | } |
| 3426 | } |
| 3427 | |
| 3428 | llvm_unreachable("bad template name kind!"); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3431 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3432 | switch (Name.getKind()) { |
| 3433 | case TemplateName::QualifiedTemplate: |
| 3434 | case TemplateName::Template: { |
| 3435 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3436 | if (TemplateTemplateParmDecl *TTP |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3437 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3438 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 3439 | |
| 3440 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 3441 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 3442 | } |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3443 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3444 | case TemplateName::OverloadedTemplate: |
| 3445 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3446 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 3447 | case TemplateName::DependentTemplate: { |
| 3448 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 3449 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 3450 | return DTN->CanonicalTemplateName; |
| 3451 | } |
| 3452 | |
| 3453 | case TemplateName::SubstTemplateTemplateParm: { |
| 3454 | SubstTemplateTemplateParmStorage *subst |
| 3455 | = Name.getAsSubstTemplateTemplateParm(); |
| 3456 | return getCanonicalTemplateName(subst->getReplacement()); |
| 3457 | } |
| 3458 | |
| 3459 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 3460 | SubstTemplateTemplateParmPackStorage *subst |
| 3461 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 3462 | TemplateTemplateParmDecl *canonParameter |
| 3463 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 3464 | TemplateArgument canonArgPack |
| 3465 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 3466 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 3467 | } |
| 3468 | } |
| 3469 | |
| 3470 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 3471 | } |
| 3472 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3473 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 3474 | X = getCanonicalTemplateName(X); |
| 3475 | Y = getCanonicalTemplateName(Y); |
| 3476 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 3477 | } |
| 3478 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3479 | TemplateArgument |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3480 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3481 | switch (Arg.getKind()) { |
| 3482 | case TemplateArgument::Null: |
| 3483 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3484 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3485 | case TemplateArgument::Expression: |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3486 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3487 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 3488 | case TemplateArgument::Declaration: { |
| 3489 | if (Decl *D = Arg.getAsDecl()) |
| 3490 | return TemplateArgument(D->getCanonicalDecl()); |
| 3491 | return TemplateArgument((Decl*)0); |
| 3492 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3493 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3494 | case TemplateArgument::Template: |
| 3495 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3496 | |
| 3497 | case TemplateArgument::TemplateExpansion: |
| 3498 | return TemplateArgument(getCanonicalTemplateName( |
| 3499 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 3500 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3501 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3502 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 3503 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3504 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3505 | case TemplateArgument::Type: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3506 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3507 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3508 | case TemplateArgument::Pack: { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 3509 | if (Arg.pack_size() == 0) |
| 3510 | return Arg; |
| 3511 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3512 | TemplateArgument *CanonArgs |
| 3513 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3514 | unsigned Idx = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3515 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3516 | AEnd = Arg.pack_end(); |
| 3517 | A != AEnd; (void)++A, ++Idx) |
| 3518 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3519 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3520 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3521 | } |
| 3522 | } |
| 3523 | |
| 3524 | // Silence GCC warning |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3525 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3528 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3529 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3530 | if (!NNS) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3531 | return 0; |
| 3532 | |
| 3533 | switch (NNS->getKind()) { |
| 3534 | case NestedNameSpecifier::Identifier: |
| 3535 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3536 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3537 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 3538 | NNS->getAsIdentifier()); |
| 3539 | |
| 3540 | case NestedNameSpecifier::Namespace: |
| 3541 | // A namespace is canonical; build a nested-name-specifier with |
| 3542 | // this namespace and no prefix. |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 3543 | return NestedNameSpecifier::Create(*this, 0, |
| 3544 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 3545 | |
| 3546 | case NestedNameSpecifier::NamespaceAlias: |
| 3547 | // A namespace is canonical; build a nested-name-specifier with |
| 3548 | // this namespace and no prefix. |
| 3549 | return NestedNameSpecifier::Create(*this, 0, |
| 3550 | NNS->getAsNamespaceAlias()->getNamespace() |
| 3551 | ->getOriginalNamespace()); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3552 | |
| 3553 | case NestedNameSpecifier::TypeSpec: |
| 3554 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 3555 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3556 | |
| 3557 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 3558 | // break it apart into its prefix and identifier, then reconsititute those |
| 3559 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 3560 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 3561 | // types, e.g., |
| 3562 | // typedef typename T::type T1; |
| 3563 | // typedef typename T1::type T2; |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3564 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) |
| 3565 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3566 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 3567 | |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 3568 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 3569 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 3570 | // first place? |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3571 | return NestedNameSpecifier::Create(*this, 0, false, |
| 3572 | const_cast<Type*>(T.getTypePtr())); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | case NestedNameSpecifier::Global: |
| 3576 | // The global specifier is canonical and unique. |
| 3577 | return NNS; |
| 3578 | } |
| 3579 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3580 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3583 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3584 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3585 | // Handle the non-qualified case efficiently. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3586 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3587 | // Handle the common positive case fast. |
| 3588 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 3589 | return AT; |
| 3590 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3591 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3592 | // Handle the common negative case fast. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3593 | if (!isa<ArrayType>(T.getCanonicalType())) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3594 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3595 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3596 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3597 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 3598 | // 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] | 3599 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3600 | // If we get here, we either have type qualifiers on the type, or we have |
| 3601 | // 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] | 3602 | // we must propagate them down into the element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3603 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3604 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3605 | Qualifiers qs = split.Quals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3606 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3607 | // If we have a simple case, just return now. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3608 | const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3609 | if (ATy == 0 || qs.empty()) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3610 | return ATy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3611 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3612 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 3613 | // qualifiers into the array element type and return a new array type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3614 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3615 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3616 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 3617 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 3618 | CAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3619 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3620 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 3621 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 3622 | IAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3623 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3624 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3625 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3626 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 3627 | return cast<ArrayType>( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3628 | getDependentSizedArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3629 | DSAT->getSizeExpr(), |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3630 | DSAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3631 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3632 | DSAT->getBracketsRange())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3633 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3634 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3635 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 3636 | VAT->getSizeExpr(), |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3637 | VAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3638 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3639 | VAT->getBracketsRange())); |
Chris Lattner | ed0d079 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 3642 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3643 | // C99 6.7.5.3p7: |
| 3644 | // A declaration of a parameter as "array of type" shall be |
| 3645 | // adjusted to "qualified pointer to type", where the type |
| 3646 | // qualifiers (if any) are those specified within the [ and ] of |
| 3647 | // the array type derivation. |
| 3648 | if (T->isArrayType()) |
| 3649 | return getArrayDecayedType(T); |
| 3650 | |
| 3651 | // C99 6.7.5.3p8: |
| 3652 | // A declaration of a parameter as "function returning type" |
| 3653 | // shall be adjusted to "pointer to function returning type", as |
| 3654 | // in 6.3.2.1. |
| 3655 | if (T->isFunctionType()) |
| 3656 | return getPointerType(T); |
| 3657 | |
| 3658 | return T; |
| 3659 | } |
| 3660 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 3661 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 3662 | T = getVariableArrayDecayedType(T); |
| 3663 | T = getAdjustedParameterType(T); |
| 3664 | return T.getUnqualifiedType(); |
| 3665 | } |
| 3666 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3667 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 3668 | /// specified array type to a pointer. This operation is non-trivial when |
| 3669 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 3670 | /// this returns a pointer to a properly qualified element of the array. |
| 3671 | /// |
| 3672 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3673 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3674 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 3675 | // typedefs in the element type of the array. This also handles propagation |
| 3676 | // of type qualifiers from the array type into the element type if present |
| 3677 | // (C99 6.7.3p8). |
| 3678 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 3679 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3680 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3681 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3682 | |
| 3683 | // int x[restrict 4] -> int *restrict |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3684 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3687 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 3688 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3691 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 3692 | Qualifiers qs; |
| 3693 | while (true) { |
| 3694 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3695 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3696 | if (!array) break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3697 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3698 | type = array->getElementType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3699 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3700 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3702 | return getQualifiedType(type, qs); |
Anders Carlsson | e0808df | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3705 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3706 | uint64_t |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 3707 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 3708 | uint64_t ElementCount = 1; |
| 3709 | do { |
| 3710 | ElementCount *= CA->getSize().getZExtValue(); |
| 3711 | CA = dyn_cast<ConstantArrayType>(CA->getElementType()); |
| 3712 | } while (CA); |
| 3713 | return ElementCount; |
| 3714 | } |
| 3715 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 3716 | /// getFloatingRank - Return a relative rank for floating point types. |
| 3717 | /// 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] | 3718 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3719 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 3720 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3721 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3722 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 3723 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3724 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3725 | case BuiltinType::Half: return HalfRank; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 3726 | case BuiltinType::Float: return FloatRank; |
| 3727 | case BuiltinType::Double: return DoubleRank; |
| 3728 | case BuiltinType::LongDouble: return LongDoubleRank; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3729 | } |
| 3730 | } |
| 3731 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3732 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 3733 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | fc6ffa2 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 3734 | /// 'typeDomain' is a real floating point or complex type. |
| 3735 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3736 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 3737 | QualType Domain) const { |
| 3738 | FloatingRank EltRank = getFloatingRank(Size); |
| 3739 | if (Domain->isComplexType()) { |
| 3740 | switch (EltRank) { |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3741 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3742 | case FloatRank: return FloatComplexTy; |
| 3743 | case DoubleRank: return DoubleComplexTy; |
| 3744 | case LongDoubleRank: return LongDoubleComplexTy; |
| 3745 | } |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 3746 | } |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3747 | |
| 3748 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 3749 | switch (EltRank) { |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3750 | case HalfRank: llvm_unreachable("Half ranks are not valid here"); |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 3751 | case FloatRank: return FloatTy; |
| 3752 | case DoubleRank: return DoubleTy; |
| 3753 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 3754 | } |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 3755 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3756 | } |
| 3757 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3758 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 3759 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 3760 | /// '_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] | 3761 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3762 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3763 | FloatingRank LHSR = getFloatingRank(LHS); |
| 3764 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3765 | |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3766 | if (LHSR == RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3767 | return 0; |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 3768 | if (LHSR > RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 3769 | return 1; |
| 3770 | return -1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3773 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 3774 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 3775 | /// or if it is not canonicalized. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3776 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3777 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3778 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3779 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3780 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3781 | case BuiltinType::Bool: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3782 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3783 | case BuiltinType::Char_S: |
| 3784 | case BuiltinType::Char_U: |
| 3785 | case BuiltinType::SChar: |
| 3786 | case BuiltinType::UChar: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3787 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3788 | case BuiltinType::Short: |
| 3789 | case BuiltinType::UShort: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3790 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3791 | case BuiltinType::Int: |
| 3792 | case BuiltinType::UInt: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3793 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3794 | case BuiltinType::Long: |
| 3795 | case BuiltinType::ULong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3796 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3797 | case BuiltinType::LongLong: |
| 3798 | case BuiltinType::ULongLong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3799 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 3800 | case BuiltinType::Int128: |
| 3801 | case BuiltinType::UInt128: |
| 3802 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3803 | } |
| 3804 | } |
| 3805 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3806 | /// \brief Whether this is a promotable bitfield reference according |
| 3807 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 3808 | /// |
| 3809 | /// \returns the type this bit-field will promote to, or NULL if no |
| 3810 | /// promotion occurs. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3811 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | e05d3cb | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 3812 | if (E->isTypeDependent() || E->isValueDependent()) |
| 3813 | return QualType(); |
| 3814 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3815 | FieldDecl *Field = E->getBitField(); |
| 3816 | if (!Field) |
| 3817 | return QualType(); |
| 3818 | |
| 3819 | QualType FT = Field->getType(); |
| 3820 | |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3821 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 3822 | uint64_t IntSize = getTypeSize(IntTy); |
| 3823 | // GCC extension compatibility: if the bit-field size is less than or equal |
| 3824 | // to the size of int, it gets promoted no matter what its type is. |
| 3825 | // For instance, unsigned long bf : 4 gets promoted to signed int. |
| 3826 | if (BitWidth < IntSize) |
| 3827 | return IntTy; |
| 3828 | |
| 3829 | if (BitWidth == IntSize) |
| 3830 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 3831 | |
| 3832 | // Types bigger than int are not subject to promotions, and therefore act |
| 3833 | // like the base type. |
| 3834 | // FIXME: This doesn't quite match what gcc does, but what gcc does here |
| 3835 | // is ridiculous. |
| 3836 | return QualType(); |
| 3837 | } |
| 3838 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3839 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 3840 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 3841 | /// integer type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3842 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3843 | assert(!Promotable.isNull()); |
| 3844 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 3845 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 3846 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 3847 | |
| 3848 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 3849 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 3850 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 3851 | // types that can represent all the values of its underlying type: |
| 3852 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 3853 | // unsigned long long int [...] |
| 3854 | // FIXME: Is there some better way to compute this? |
| 3855 | if (BT->getKind() == BuiltinType::WChar_S || |
| 3856 | BT->getKind() == BuiltinType::WChar_U || |
| 3857 | BT->getKind() == BuiltinType::Char16 || |
| 3858 | BT->getKind() == BuiltinType::Char32) { |
| 3859 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 3860 | uint64_t FromSize = getTypeSize(BT); |
| 3861 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 3862 | LongLongTy, UnsignedLongLongTy }; |
| 3863 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 3864 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 3865 | if (FromSize < ToSize || |
| 3866 | (FromSize == ToSize && |
| 3867 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 3868 | return PromoteTypes[Idx]; |
| 3869 | } |
| 3870 | llvm_unreachable("char type should fit into long long"); |
| 3871 | } |
| 3872 | } |
| 3873 | |
| 3874 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 3875 | if (Promotable->isSignedIntegerType()) |
| 3876 | return IntTy; |
| 3877 | uint64_t PromotableSize = getTypeSize(Promotable); |
| 3878 | uint64_t IntSize = getTypeSize(IntTy); |
| 3879 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 3880 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 3881 | } |
| 3882 | |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3883 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 3884 | /// type and returns its ownership. |
| 3885 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 3886 | while (!T.isNull()) { |
| 3887 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 3888 | return T.getObjCLifetime(); |
| 3889 | if (T->isArrayType()) |
| 3890 | T = getBaseElementType(T); |
| 3891 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 3892 | T = PT->getPointeeType(); |
| 3893 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 8e25253 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 3894 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 3895 | else |
| 3896 | break; |
| 3897 | } |
| 3898 | |
| 3899 | return Qualifiers::OCL_None; |
| 3900 | } |
| 3901 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3902 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3903 | /// 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] | 3904 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3905 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3906 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 3907 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3908 | if (LHSC == RHSC) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3909 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3910 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 3911 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3912 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3913 | unsigned LHSRank = getIntegerRank(LHSC); |
| 3914 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3915 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3916 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 3917 | if (LHSRank == RHSRank) return 0; |
| 3918 | return LHSRank > RHSRank ? 1 : -1; |
| 3919 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3920 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3921 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 3922 | if (LHSUnsigned) { |
| 3923 | // If the unsigned [LHS] type is larger, return it. |
| 3924 | if (LHSRank >= RHSRank) |
| 3925 | return 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3926 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3927 | // If the signed type can represent all values of the unsigned type, it |
| 3928 | // 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] | 3929 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3930 | return -1; |
| 3931 | } |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3932 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3933 | // If the unsigned [RHS] type is larger, return it. |
| 3934 | if (RHSRank >= LHSRank) |
| 3935 | return -1; |
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 the signed type can represent all values of the unsigned type, it |
| 3938 | // 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] | 3939 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 3940 | return 1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 3941 | } |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3942 | |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3943 | static RecordDecl * |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3944 | CreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK, |
| 3945 | DeclContext *DC, IdentifierInfo *Id) { |
| 3946 | SourceLocation Loc; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3947 | if (Ctx.getLangOpts().CPlusPlus) |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3948 | return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3949 | else |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3950 | return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3951 | } |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3952 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3953 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3954 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3955 | if (!CFConstantStringTypeDecl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3956 | CFConstantStringTypeDecl = |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3957 | CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3958 | &Idents.get("NSConstantString")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3959 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 3960 | |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3961 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3962 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3963 | // const int *isa; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3964 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 3965 | // int flags; |
| 3966 | FieldTypes[1] = IntTy; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3967 | // const char *str; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3968 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3969 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3970 | FieldTypes[3] = LongTy; |
| 3971 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3972 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3973 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3974 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3975 | SourceLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3976 | SourceLocation(), 0, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3977 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3978 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3979 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3980 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 3981 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3982 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3985 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3986 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3987 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 3988 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 412af03 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 3989 | } |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 3990 | |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3991 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3992 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 3993 | assert(Rec && "Invalid CFConstantStringType"); |
| 3994 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 3995 | } |
| 3996 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3997 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 3998 | if (BlockDescriptorType) |
| 3999 | return getTagDeclType(BlockDescriptorType); |
| 4000 | |
| 4001 | RecordDecl *T; |
| 4002 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4003 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4004 | &Idents.get("__block_descriptor")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4005 | T->startDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4006 | |
| 4007 | QualType FieldTypes[] = { |
| 4008 | UnsignedLongTy, |
| 4009 | UnsignedLongTy, |
| 4010 | }; |
| 4011 | |
| 4012 | const char *FieldNames[] = { |
| 4013 | "reserved", |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4014 | "Size" |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4015 | }; |
| 4016 | |
| 4017 | for (size_t i = 0; i < 2; ++i) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4018 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4019 | SourceLocation(), |
| 4020 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4021 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4022 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4023 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4024 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4025 | Field->setAccess(AS_public); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4026 | T->addDecl(Field); |
| 4027 | } |
| 4028 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4029 | T->completeDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4030 | |
| 4031 | BlockDescriptorType = T; |
| 4032 | |
| 4033 | return getTagDeclType(BlockDescriptorType); |
| 4034 | } |
| 4035 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4036 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4037 | if (BlockDescriptorExtendedType) |
| 4038 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4039 | |
| 4040 | RecordDecl *T; |
| 4041 | // FIXME: Needs the FlagAppleBlock bit. |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4042 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4043 | &Idents.get("__block_descriptor_withcopydispose")); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4044 | T->startDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4045 | |
| 4046 | QualType FieldTypes[] = { |
| 4047 | UnsignedLongTy, |
| 4048 | UnsignedLongTy, |
| 4049 | getPointerType(VoidPtrTy), |
| 4050 | getPointerType(VoidPtrTy) |
| 4051 | }; |
| 4052 | |
| 4053 | const char *FieldNames[] = { |
| 4054 | "reserved", |
| 4055 | "Size", |
| 4056 | "CopyFuncPtr", |
| 4057 | "DestroyFuncPtr" |
| 4058 | }; |
| 4059 | |
| 4060 | for (size_t i = 0; i < 4; ++i) { |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4061 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4062 | SourceLocation(), |
| 4063 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4064 | FieldTypes[i], /*TInfo=*/0, |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4065 | /*BitWidth=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4066 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4067 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4068 | Field->setAccess(AS_public); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4069 | T->addDecl(Field); |
| 4070 | } |
| 4071 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4072 | T->completeDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4073 | |
| 4074 | BlockDescriptorExtendedType = T; |
| 4075 | |
| 4076 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4077 | } |
| 4078 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4079 | bool ASTContext::BlockRequiresCopying(QualType Ty) const { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4080 | if (Ty->isObjCRetainableType()) |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4081 | return true; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4082 | if (getLangOpts().CPlusPlus) { |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 4083 | if (const RecordType *RT = Ty->getAs<RecordType>()) { |
| 4084 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 4085 | return RD->hasConstCopyConstructor(); |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 4086 | |
| 4087 | } |
| 4088 | } |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4089 | return false; |
| 4090 | } |
| 4091 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4092 | QualType |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4093 | ASTContext::BuildByRefType(StringRef DeclName, QualType Ty) const { |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4094 | // type = struct __Block_byref_1_X { |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 4095 | // void *__isa; |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4096 | // struct __Block_byref_1_X *__forwarding; |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 4097 | // unsigned int __flags; |
| 4098 | // unsigned int __size; |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 4099 | // void *__copy_helper; // as needed |
| 4100 | // void *__destroy_help // as needed |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4101 | // int X; |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 4102 | // } * |
| 4103 | |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4104 | bool HasCopyAndDispose = BlockRequiresCopying(Ty); |
| 4105 | |
| 4106 | // FIXME: Move up |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4107 | SmallString<36> Name; |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 4108 | llvm::raw_svector_ostream(Name) << "__Block_byref_" << |
| 4109 | ++UniqueBlockByRefTypeID << '_' << DeclName; |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4110 | RecordDecl *T; |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4111 | T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str())); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4112 | T->startDefinition(); |
| 4113 | QualType Int32Ty = IntTy; |
| 4114 | assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported"); |
| 4115 | QualType FieldTypes[] = { |
| 4116 | getPointerType(VoidPtrTy), |
| 4117 | getPointerType(getTagDeclType(T)), |
| 4118 | Int32Ty, |
| 4119 | Int32Ty, |
| 4120 | getPointerType(VoidPtrTy), |
| 4121 | getPointerType(VoidPtrTy), |
| 4122 | Ty |
| 4123 | }; |
| 4124 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4125 | StringRef FieldNames[] = { |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4126 | "__isa", |
| 4127 | "__forwarding", |
| 4128 | "__flags", |
| 4129 | "__size", |
| 4130 | "__copy_helper", |
| 4131 | "__destroy_helper", |
| 4132 | DeclName, |
| 4133 | }; |
| 4134 | |
| 4135 | for (size_t i = 0; i < 7; ++i) { |
| 4136 | if (!HasCopyAndDispose && i >=4 && i <= 5) |
| 4137 | continue; |
| 4138 | FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4139 | SourceLocation(), |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4140 | &Idents.get(FieldNames[i]), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4141 | FieldTypes[i], /*TInfo=*/0, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4142 | /*BitWidth=*/0, /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4143 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4144 | Field->setAccess(AS_public); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4145 | T->addDecl(Field); |
| 4146 | } |
| 4147 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4148 | T->completeDefinition(); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4149 | |
| 4150 | return getPointerType(getTagDeclType(T)); |
Mike Stump | 7fe9cc1 | 2009-10-21 03:49:08 +0000 | [diff] [blame] | 4151 | } |
| 4152 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4153 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 4154 | if (!ObjCInstanceTypeDecl) |
| 4155 | ObjCInstanceTypeDecl = TypedefDecl::Create(*this, |
| 4156 | getTranslationUnitDecl(), |
| 4157 | SourceLocation(), |
| 4158 | SourceLocation(), |
| 4159 | &Idents.get("instancetype"), |
| 4160 | getTrivialTypeSourceInfo(getObjCIdType())); |
| 4161 | return ObjCInstanceTypeDecl; |
| 4162 | } |
| 4163 | |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4164 | // This returns true if a type has been typedefed to BOOL: |
| 4165 | // typedef <type> BOOL; |
Chris Lattner | e021899 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 4166 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4167 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 9b1f279 | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 4168 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 4169 | return II->isStr("BOOL"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4170 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4171 | return false; |
| 4172 | } |
| 4173 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4174 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4175 | /// purpose. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4176 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4177 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 4178 | return CharUnits::Zero(); |
| 4179 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4180 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4181 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4182 | // 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] | 4183 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4184 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4185 | // Treat arrays as pointers, since that's how they're passed in. |
| 4186 | else if (type->isArrayType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4187 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4188 | return sz; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | static inline |
| 4192 | std::string charUnitsToString(const CharUnits &CU) { |
| 4193 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4194 | } |
| 4195 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4196 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4197 | /// declaration. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4198 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 4199 | std::string S; |
| 4200 | |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4201 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 4202 | QualType BlockTy = |
| 4203 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 4204 | // Encode result type. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 4205 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4206 | // Compute size of all parameters. |
| 4207 | // Start with computing size of a pointer in number of bytes. |
| 4208 | // FIXME: There might(should) be a better way of doing this computation! |
| 4209 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4210 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 4211 | CharUnits ParmOffset = PtrSize; |
Fariborz Jahanian | 590c352 | 2010-04-08 18:06:22 +0000 | [diff] [blame] | 4212 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4213 | E = Decl->param_end(); PI != E; ++PI) { |
| 4214 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4215 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | d487941 | 2012-06-30 00:48:59 +0000 | [diff] [blame] | 4216 | if (sz.isZero()) |
| 4217 | continue; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4218 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4219 | ParmOffset += sz; |
| 4220 | } |
| 4221 | // Size of the argument frame |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4222 | S += charUnitsToString(ParmOffset); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4223 | // Block pointer and offset. |
| 4224 | S += "@?0"; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4225 | |
| 4226 | // Argument types. |
| 4227 | ParmOffset = PtrSize; |
| 4228 | for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E = |
| 4229 | Decl->param_end(); PI != E; ++PI) { |
| 4230 | ParmVarDecl *PVDecl = *PI; |
| 4231 | QualType PType = PVDecl->getOriginalType(); |
| 4232 | if (const ArrayType *AT = |
| 4233 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4234 | // Use array's original type only if it has known number of |
| 4235 | // elements. |
| 4236 | if (!isa<ConstantArrayType>(AT)) |
| 4237 | PType = PVDecl->getType(); |
| 4238 | } else if (PType->isFunctionType()) |
| 4239 | PType = PVDecl->getType(); |
| 4240 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4241 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4242 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4243 | } |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4244 | |
| 4245 | return S; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4248 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4249 | std::string& S) { |
| 4250 | // Encode result type. |
| 4251 | getObjCEncodingForType(Decl->getResultType(), S); |
| 4252 | CharUnits ParmOffset; |
| 4253 | // Compute size of all parameters. |
| 4254 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4255 | E = Decl->param_end(); PI != E; ++PI) { |
| 4256 | QualType PType = (*PI)->getType(); |
| 4257 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4258 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4259 | continue; |
| 4260 | |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4261 | assert (sz.isPositive() && |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4262 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4263 | ParmOffset += sz; |
| 4264 | } |
| 4265 | S += charUnitsToString(ParmOffset); |
| 4266 | ParmOffset = CharUnits::Zero(); |
| 4267 | |
| 4268 | // Argument types. |
| 4269 | for (FunctionDecl::param_const_iterator PI = Decl->param_begin(), |
| 4270 | E = Decl->param_end(); PI != E; ++PI) { |
| 4271 | ParmVarDecl *PVDecl = *PI; |
| 4272 | QualType PType = PVDecl->getOriginalType(); |
| 4273 | if (const ArrayType *AT = |
| 4274 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4275 | // Use array's original type only if it has known number of |
| 4276 | // elements. |
| 4277 | if (!isa<ConstantArrayType>(AT)) |
| 4278 | PType = PVDecl->getType(); |
| 4279 | } else if (PType->isFunctionType()) |
| 4280 | PType = PVDecl->getType(); |
| 4281 | getObjCEncodingForType(PType, S); |
| 4282 | S += charUnitsToString(ParmOffset); |
| 4283 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 4284 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4285 | |
| 4286 | return false; |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4287 | } |
| 4288 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4289 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 4290 | /// method parameter or return type. If Extended, include class names and |
| 4291 | /// block object types. |
| 4292 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 4293 | QualType T, std::string& S, |
| 4294 | bool Extended) const { |
| 4295 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 4296 | getObjCEncodingForTypeQualifier(QT, S); |
| 4297 | // Encode parameter type. |
| 4298 | getObjCEncodingForTypeImpl(T, S, true, true, 0, |
| 4299 | true /*OutermostType*/, |
| 4300 | false /*EncodingProperty*/, |
| 4301 | false /*StructField*/, |
| 4302 | Extended /*EncodeBlockParameters*/, |
| 4303 | Extended /*EncodeClassNames*/); |
| 4304 | } |
| 4305 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4306 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4307 | /// declaration. |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4308 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4309 | std::string& S, |
| 4310 | bool Extended) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4311 | // FIXME: This is not very efficient. |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4312 | // Encode return type. |
| 4313 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 4314 | Decl->getResultType(), S, Extended); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4315 | // Compute size of all parameters. |
| 4316 | // Start with computing size of a pointer in number of bytes. |
| 4317 | // FIXME: There might(should) be a better way of doing this computation! |
| 4318 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4319 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4320 | // The first two arguments (self and _cmd) are pointers; account for |
| 4321 | // their size. |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4322 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4323 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4324 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 4325 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4326 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4327 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4328 | continue; |
| 4329 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4330 | assert (sz.isPositive() && |
| 4331 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4332 | ParmOffset += sz; |
| 4333 | } |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4334 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4335 | S += "@0:"; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4336 | S += charUnitsToString(PtrSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4337 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4338 | // Argument types. |
| 4339 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4340 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 4341 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 4342 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4343 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | a0befc0 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 4344 | if (const ArrayType *AT = |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4345 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4346 | // Use array's original type only if it has known number of |
| 4347 | // elements. |
Steve Naroff | 323827e | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 4348 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 4349 | PType = PVDecl->getType(); |
| 4350 | } else if (PType->isFunctionType()) |
| 4351 | PType = PVDecl->getType(); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4352 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 4353 | PType, S, Extended); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4354 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4355 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4356 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4357 | |
| 4358 | return false; |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4361 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4362 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4363 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 4364 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4365 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 4366 | /// attributes readonly and bycopy are encoded as single characters. The |
| 4367 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 4368 | /// encoded as single characters, followed by an identifier. Property types |
| 4369 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4370 | /// these attributes are defined by the following enumeration: |
| 4371 | /// @code |
| 4372 | /// enum PropertyAttributes { |
| 4373 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 4374 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 4375 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 4376 | /// kPropertyDynamic = 'D', // property is dynamic |
| 4377 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 4378 | /// kPropertySetter = 'S', // followed by setter selector name |
| 4379 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 8cf6185 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 4380 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 4381 | /// kPropertyWeak = 'W' // 'weak' property |
| 4382 | /// kPropertyStrong = 'P' // property GC'able |
| 4383 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 4384 | /// }; |
| 4385 | /// @endcode |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4386 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4387 | const Decl *Container, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4388 | std::string& S) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4389 | // Collect information from the property implementation decl(s). |
| 4390 | bool Dynamic = false; |
| 4391 | ObjCPropertyImplDecl *SynthesizePID = 0; |
| 4392 | |
| 4393 | // FIXME: Duplicated code due to poor abstraction. |
| 4394 | if (Container) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4395 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4396 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
| 4397 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4398 | i = CID->propimpl_begin(), e = CID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4399 | i != e; ++i) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4400 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4401 | if (PID->getPropertyDecl() == PD) { |
| 4402 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4403 | Dynamic = true; |
| 4404 | } else { |
| 4405 | SynthesizePID = PID; |
| 4406 | } |
| 4407 | } |
| 4408 | } |
| 4409 | } else { |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 4410 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4411 | for (ObjCCategoryImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4412 | i = OID->propimpl_begin(), e = OID->propimpl_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 4413 | i != e; ++i) { |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4414 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4415 | if (PID->getPropertyDecl() == PD) { |
| 4416 | if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) { |
| 4417 | Dynamic = true; |
| 4418 | } else { |
| 4419 | SynthesizePID = PID; |
| 4420 | } |
| 4421 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4422 | } |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4423 | } |
| 4424 | } |
| 4425 | |
| 4426 | // FIXME: This is not very efficient. |
| 4427 | S = "T"; |
| 4428 | |
| 4429 | // Encode result type. |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4430 | // GCC has some special rules regarding encoding of properties which |
| 4431 | // closely resembles encoding of ivars. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4432 | getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4433 | true /* outermost type */, |
| 4434 | true /* encoding for property */); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4435 | |
| 4436 | if (PD->isReadOnly()) { |
| 4437 | S += ",R"; |
| 4438 | } else { |
| 4439 | switch (PD->getSetterKind()) { |
| 4440 | case ObjCPropertyDecl::Assign: break; |
| 4441 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4442 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 70a315c | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 4443 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4444 | } |
| 4445 | } |
| 4446 | |
| 4447 | // It really isn't clear at all what this means, since properties |
| 4448 | // are "dynamic by default". |
| 4449 | if (Dynamic) |
| 4450 | S += ",D"; |
| 4451 | |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4452 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 4453 | S += ",N"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4454 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4455 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 4456 | S += ",G"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4457 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 4461 | S += ",S"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 4462 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
| 4465 | if (SynthesizePID) { |
| 4466 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 4467 | S += ",V"; |
Chris Lattner | 1cbaacc | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 4468 | S += OID->getNameAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
| 4471 | // FIXME: OBJCGC: weak & strong |
| 4472 | } |
| 4473 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4474 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4475 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 4476 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4477 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 4478 | /// |
| 4479 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4480 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4481 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4482 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4483 | PointeeTy = UnsignedIntTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4484 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4485 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4486 | PointeeTy = IntTy; |
| 4487 | } |
| 4488 | } |
| 4489 | } |
| 4490 | |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4491 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4492 | const FieldDecl *Field) const { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4493 | // We follow the behavior of gcc, expanding structures which are |
| 4494 | // directly pointed to, and expanding embedded structures. Note that |
| 4495 | // these rules are sufficient to prevent recursive encoding of the |
| 4496 | // same type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4497 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | daef00b | 2008-12-22 23:22:27 +0000 | [diff] [blame] | 4498 | true /* outermost type */); |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4501 | static char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) { |
| 4502 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4503 | default: llvm_unreachable("Unhandled builtin type kind"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4504 | case BuiltinType::Void: return 'v'; |
| 4505 | case BuiltinType::Bool: return 'B'; |
| 4506 | case BuiltinType::Char_U: |
| 4507 | case BuiltinType::UChar: return 'C'; |
| 4508 | case BuiltinType::UShort: return 'S'; |
| 4509 | case BuiltinType::UInt: return 'I'; |
| 4510 | case BuiltinType::ULong: |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4511 | return C->getIntWidth(T) == 32 ? 'L' : 'Q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4512 | case BuiltinType::UInt128: return 'T'; |
| 4513 | case BuiltinType::ULongLong: return 'Q'; |
| 4514 | case BuiltinType::Char_S: |
| 4515 | case BuiltinType::SChar: return 'c'; |
| 4516 | case BuiltinType::Short: return 's'; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 4517 | case BuiltinType::WChar_S: |
| 4518 | case BuiltinType::WChar_U: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4519 | case BuiltinType::Int: return 'i'; |
| 4520 | case BuiltinType::Long: |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4521 | return C->getIntWidth(T) == 32 ? 'l' : 'q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4522 | case BuiltinType::LongLong: return 'q'; |
| 4523 | case BuiltinType::Int128: return 't'; |
| 4524 | case BuiltinType::Float: return 'f'; |
| 4525 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 7cba5a7 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 4526 | case BuiltinType::LongDouble: return 'D'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4527 | } |
| 4528 | } |
| 4529 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4530 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 4531 | EnumDecl *Enum = ET->getDecl(); |
| 4532 | |
| 4533 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 4534 | if (!Enum->isFixed()) |
| 4535 | return 'i'; |
| 4536 | |
| 4537 | // The encoding of a fixed enum type matches its fixed underlying type. |
| 4538 | return ObjCEncodingForPrimitiveKind(C, Enum->getIntegerType()); |
| 4539 | } |
| 4540 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4541 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4542 | QualType T, const FieldDecl *FD) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4543 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4544 | S += 'b'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4545 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 4546 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 4547 | // then the offset (in bits) of the first element, then the type of the |
| 4548 | // bitfield, then the size in bits. For example, in this structure: |
| 4549 | // |
| 4550 | // struct |
| 4551 | // { |
| 4552 | // int integer; |
| 4553 | // int flags:2; |
| 4554 | // }; |
| 4555 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 4556 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 4557 | // information is not especially sensible, but we're stuck with it for |
| 4558 | // compatibility with GCC, although providing it breaks anything that |
| 4559 | // actually uses runtime introspection and wants to work on both runtimes... |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4560 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4561 | const RecordDecl *RD = FD->getParent(); |
| 4562 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | a3c122d | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 4563 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4564 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 4565 | S += ObjCEncodingForEnumType(Ctx, ET); |
David Chisnall | 6f0a7d2 | 2010-12-26 20:12:30 +0000 | [diff] [blame] | 4566 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4567 | S += ObjCEncodingForPrimitiveKind(Ctx, T); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4568 | } |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4569 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4570 | } |
| 4571 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4572 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4573 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 4574 | bool ExpandPointedToStructures, |
| 4575 | bool ExpandStructures, |
Daniel Dunbar | c040ce4 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 4576 | const FieldDecl *FD, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 4577 | bool OutermostType, |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4578 | bool EncodingProperty, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4579 | bool StructField, |
| 4580 | bool EncodeBlockParameters, |
| 4581 | bool EncodeClassNames) const { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4582 | if (T->getAs<BuiltinType>()) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4583 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4584 | return EncodeBitField(this, S, T, FD); |
| 4585 | S += ObjCEncodingForPrimitiveKind(this, T); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4586 | return; |
| 4587 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4588 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4589 | if (const ComplexType *CT = T->getAs<ComplexType>()) { |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4590 | S += 'j'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4591 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false, |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 4592 | false); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4593 | return; |
| 4594 | } |
Fariborz Jahanian | d25c219 | 2009-11-23 20:40:50 +0000 | [diff] [blame] | 4595 | |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4596 | // encoding for pointer or r3eference types. |
| 4597 | QualType PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4598 | if (const PointerType *PT = T->getAs<PointerType>()) { |
Fariborz Jahanian | 89b660c | 2009-11-30 18:43:52 +0000 | [diff] [blame] | 4599 | if (PT->isObjCSelType()) { |
| 4600 | S += ':'; |
| 4601 | return; |
| 4602 | } |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4603 | PointeeTy = PT->getPointeeType(); |
| 4604 | } |
| 4605 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 4606 | PointeeTy = RT->getPointeeType(); |
| 4607 | if (!PointeeTy.isNull()) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4608 | bool isReadOnly = false; |
| 4609 | // For historical/compatibility reasons, the read-only qualifier of the |
| 4610 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 4611 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4612 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 4613 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4614 | if (OutermostType && T.isConstQualified()) { |
| 4615 | isReadOnly = true; |
| 4616 | S += 'r'; |
| 4617 | } |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 4618 | } else if (OutermostType) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4619 | QualType P = PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4620 | while (P->getAs<PointerType>()) |
| 4621 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4622 | if (P.isConstQualified()) { |
| 4623 | isReadOnly = true; |
| 4624 | S += 'r'; |
| 4625 | } |
| 4626 | } |
| 4627 | if (isReadOnly) { |
| 4628 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 4629 | // combinations need to be rearranged. |
| 4630 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4631 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 4632 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4633 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4634 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4635 | if (PointeeTy->isCharType()) { |
| 4636 | // char pointer types should be encoded as '*' unless it is a |
| 4637 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4638 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4639 | S += '*'; |
| 4640 | return; |
| 4641 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4642 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 3de6b70 | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 4643 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 4644 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 4645 | S += '#'; |
| 4646 | return; |
| 4647 | } |
| 4648 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 4649 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 4650 | S += '@'; |
| 4651 | return; |
| 4652 | } |
| 4653 | // fall through... |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4654 | } |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4655 | S += '^'; |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 4656 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 4657 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4658 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4659 | NULL); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4660 | return; |
| 4661 | } |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 4662 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4663 | if (const ArrayType *AT = |
| 4664 | // Ignore type qualifiers etc. |
| 4665 | dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4666 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4667 | // Incomplete arrays are encoded as a pointer to the array element. |
| 4668 | S += '^'; |
| 4669 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4670 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4671 | false, ExpandStructures, FD); |
| 4672 | } else { |
| 4673 | S += '['; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4674 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4675 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
| 4676 | if (getTypeSize(CAT->getElementType()) == 0) |
| 4677 | S += '0'; |
| 4678 | else |
| 4679 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 4680 | } else { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4681 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4682 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 4683 | "Unknown array type!"); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4684 | S += '0'; |
| 4685 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4686 | |
| 4687 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 4688 | false, ExpandStructures, FD); |
| 4689 | S += ']'; |
| 4690 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4691 | return; |
| 4692 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4693 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4694 | if (T->getAs<FunctionType>()) { |
Anders Carlsson | df4cc61 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 4695 | S += '?'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4696 | return; |
| 4697 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4698 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4699 | if (const RecordType *RTy = T->getAs<RecordType>()) { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 4700 | RecordDecl *RDecl = RTy->getDecl(); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4701 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4702 | // Anonymous structures print as '?' |
| 4703 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 4704 | S += II->getName(); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4705 | if (ClassTemplateSpecializationDecl *Spec |
| 4706 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 4707 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 4708 | std::string TemplateArgsStr |
| 4709 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4710 | TemplateArgs.data(), |
| 4711 | TemplateArgs.size(), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 4712 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 4713 | |
| 4714 | S += TemplateArgsStr; |
| 4715 | } |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 4716 | } else { |
| 4717 | S += '?'; |
| 4718 | } |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 4719 | if (ExpandStructures) { |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4720 | S += '='; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4721 | if (!RDecl->isUnion()) { |
| 4722 | getObjCEncodingForStructureImpl(RDecl, S, FD); |
| 4723 | } else { |
| 4724 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4725 | FieldEnd = RDecl->field_end(); |
| 4726 | Field != FieldEnd; ++Field) { |
| 4727 | if (FD) { |
| 4728 | S += '"'; |
| 4729 | S += Field->getNameAsString(); |
| 4730 | S += '"'; |
| 4731 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4732 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4733 | // Special case bit-fields. |
| 4734 | if (Field->isBitField()) { |
| 4735 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4736 | *Field); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4737 | } else { |
| 4738 | QualType qt = Field->getType(); |
| 4739 | getLegacyIntegralTypeEncoding(qt); |
| 4740 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 4741 | FD, /*OutermostType*/false, |
| 4742 | /*EncodingProperty*/false, |
| 4743 | /*StructField*/true); |
| 4744 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4745 | } |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 4746 | } |
Fariborz Jahanian | bc92fd7 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 4747 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 4748 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4749 | return; |
| 4750 | } |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4751 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4752 | if (const EnumType *ET = T->getAs<EnumType>()) { |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4753 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 4754 | EncodeBitField(this, S, T, FD); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 4755 | else |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 4756 | S += ObjCEncodingForEnumType(this, ET); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4757 | return; |
| 4758 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4759 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4760 | if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) { |
Steve Naroff | 49140cb | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 4761 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4762 | if (EncodeBlockParameters) { |
| 4763 | const FunctionType *FT = BT->getPointeeType()->getAs<FunctionType>(); |
| 4764 | |
| 4765 | S += '<'; |
| 4766 | // Block return type |
| 4767 | getObjCEncodingForTypeImpl(FT->getResultType(), S, |
| 4768 | ExpandPointedToStructures, ExpandStructures, |
| 4769 | FD, |
| 4770 | false /* OutermostType */, |
| 4771 | EncodingProperty, |
| 4772 | false /* StructField */, |
| 4773 | EncodeBlockParameters, |
| 4774 | EncodeClassNames); |
| 4775 | // Block self |
| 4776 | S += "@?"; |
| 4777 | // Block parameters |
| 4778 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
| 4779 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(), |
| 4780 | E = FPT->arg_type_end(); I && (I != E); ++I) { |
| 4781 | getObjCEncodingForTypeImpl(*I, S, |
| 4782 | ExpandPointedToStructures, |
| 4783 | ExpandStructures, |
| 4784 | FD, |
| 4785 | false /* OutermostType */, |
| 4786 | EncodingProperty, |
| 4787 | false /* StructField */, |
| 4788 | EncodeBlockParameters, |
| 4789 | EncodeClassNames); |
| 4790 | } |
| 4791 | } |
| 4792 | S += '>'; |
| 4793 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4794 | return; |
| 4795 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4796 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4797 | // Ignore protocol qualifiers when mangling at this level. |
| 4798 | if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>()) |
| 4799 | T = OT->getBaseType(); |
| 4800 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4801 | if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4802 | // @encode(class_name) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4803 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4804 | S += '{'; |
| 4805 | const IdentifierInfo *II = OI->getIdentifier(); |
| 4806 | S += II->getName(); |
| 4807 | S += '='; |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4808 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4809 | DeepCollectObjCIvars(OI, true, Ivars); |
| 4810 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4811 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4812 | if (Field->isBitField()) |
| 4813 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4814 | else |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 4815 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4816 | } |
| 4817 | S += '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4818 | return; |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 4819 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4820 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4821 | if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4822 | if (OPT->isObjCIdType()) { |
| 4823 | S += '@'; |
| 4824 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4825 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4826 | |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4827 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 4828 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 4829 | // Since this is a binary compatibility issue, need to consult with runtime |
| 4830 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4831 | S += '#'; |
| 4832 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4833 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4834 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4835 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4836 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4837 | ExpandPointedToStructures, |
| 4838 | ExpandStructures, FD); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4839 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4840 | // Note that we do extended encoding of protocol qualifer list |
| 4841 | // Only when doing ivar or property encoding. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4842 | S += '"'; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4843 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4844 | E = OPT->qual_end(); I != E; ++I) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4845 | S += '<'; |
| 4846 | S += (*I)->getNameAsString(); |
| 4847 | S += '>'; |
| 4848 | } |
| 4849 | S += '"'; |
| 4850 | } |
| 4851 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4852 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4853 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4854 | QualType PointeeTy = OPT->getPointeeType(); |
| 4855 | if (!EncodingProperty && |
| 4856 | isa<TypedefType>(PointeeTy.getTypePtr())) { |
| 4857 | // Another historical/compatibility reason. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4858 | // We encode the underlying type which comes out as |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4859 | // {...}; |
| 4860 | S += '^'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4861 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 4862 | false, ExpandPointedToStructures, |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4863 | NULL); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4864 | return; |
| 4865 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4866 | |
| 4867 | S += '@'; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4868 | if (OPT->getInterfaceDecl() && |
| 4869 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4870 | S += '"'; |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 4871 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4872 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 4873 | E = OPT->qual_end(); I != E; ++I) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4874 | S += '<'; |
| 4875 | S += (*I)->getNameAsString(); |
| 4876 | S += '>'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4877 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 4878 | S += '"'; |
| 4879 | } |
| 4880 | return; |
| 4881 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4882 | |
John McCall | a9e6e8d | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 4883 | // gcc just blithely ignores member pointers. |
| 4884 | // TODO: maybe there should be a mangling for these |
| 4885 | if (T->getAs<MemberPointerType>()) |
| 4886 | return; |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 4887 | |
| 4888 | if (T->isVectorType()) { |
| 4889 | // This matches gcc's encoding, even though technically it is |
| 4890 | // insufficient. |
| 4891 | // FIXME. We should do a better job than gcc. |
| 4892 | return; |
| 4893 | } |
| 4894 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4895 | llvm_unreachable("@encode for type not implemented!"); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4896 | } |
| 4897 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4898 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 4899 | std::string &S, |
| 4900 | const FieldDecl *FD, |
| 4901 | bool includeVBases) const { |
| 4902 | assert(RDecl && "Expected non-null RecordDecl"); |
| 4903 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 4904 | if (!RDecl->getDefinition()) |
| 4905 | return; |
| 4906 | |
| 4907 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 4908 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 4909 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 4910 | |
| 4911 | if (CXXRec) { |
| 4912 | for (CXXRecordDecl::base_class_iterator |
| 4913 | BI = CXXRec->bases_begin(), |
| 4914 | BE = CXXRec->bases_end(); BI != BE; ++BI) { |
| 4915 | if (!BI->isVirtual()) { |
| 4916 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4917 | if (base->isEmpty()) |
| 4918 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 4919 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4920 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4921 | std::make_pair(offs, base)); |
| 4922 | } |
| 4923 | } |
| 4924 | } |
| 4925 | |
| 4926 | unsigned i = 0; |
| 4927 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 4928 | FieldEnd = RDecl->field_end(); |
| 4929 | Field != FieldEnd; ++Field, ++i) { |
| 4930 | uint64_t offs = layout.getFieldOffset(i); |
| 4931 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4932 | std::make_pair(offs, *Field)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | if (CXXRec && includeVBases) { |
| 4936 | for (CXXRecordDecl::base_class_iterator |
| 4937 | BI = CXXRec->vbases_begin(), |
| 4938 | BE = CXXRec->vbases_end(); BI != BE; ++BI) { |
| 4939 | CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 4940 | if (base->isEmpty()) |
| 4941 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 4942 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); |
Argyrios Kyrtzidis | 81c0b5c | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 4943 | if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
| 4944 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 4945 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4946 | } |
| 4947 | } |
| 4948 | |
| 4949 | CharUnits size; |
| 4950 | if (CXXRec) { |
| 4951 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 4952 | } else { |
| 4953 | size = layout.getSize(); |
| 4954 | } |
| 4955 | |
| 4956 | uint64_t CurOffs = 0; |
| 4957 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 4958 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 4959 | |
Douglas Gregor | f5d6c74 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 4960 | if (CXXRec && CXXRec->isDynamicClass() && |
| 4961 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 4962 | if (FD) { |
| 4963 | S += "\"_vptr$"; |
| 4964 | std::string recname = CXXRec->getNameAsString(); |
| 4965 | if (recname.empty()) recname = "?"; |
| 4966 | S += recname; |
| 4967 | S += '"'; |
| 4968 | } |
| 4969 | S += "^^?"; |
| 4970 | CurOffs += getTypeSize(VoidPtrTy); |
| 4971 | } |
| 4972 | |
| 4973 | if (!RDecl->hasFlexibleArrayMember()) { |
| 4974 | // Mark the end of the structure. |
| 4975 | uint64_t offs = toBits(size); |
| 4976 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 4977 | std::make_pair(offs, (NamedDecl*)0)); |
| 4978 | } |
| 4979 | |
| 4980 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
| 4981 | assert(CurOffs <= CurLayObj->first); |
| 4982 | |
| 4983 | if (CurOffs < CurLayObj->first) { |
| 4984 | uint64_t padding = CurLayObj->first - CurOffs; |
| 4985 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 4986 | // packing/alignment of members is different that normal, in which case |
| 4987 | // the encoding will be out-of-sync with the real layout. |
| 4988 | // If the runtime switches to just consider the size of types without |
| 4989 | // taking into account alignment, we could make padding explicit in the |
| 4990 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 4991 | // longer then though. |
| 4992 | CurOffs += padding; |
| 4993 | } |
| 4994 | |
| 4995 | NamedDecl *dcl = CurLayObj->second; |
| 4996 | if (dcl == 0) |
| 4997 | break; // reached end of structure. |
| 4998 | |
| 4999 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 5000 | // We expand the bases without their virtual bases since those are going |
| 5001 | // in the initial structure. Note that this differs from gcc which |
| 5002 | // expands virtual bases each time one is encountered in the hierarchy, |
| 5003 | // making the encoding type bigger than it really is. |
| 5004 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5005 | assert(!base->isEmpty()); |
| 5006 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5007 | } else { |
| 5008 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 5009 | if (FD) { |
| 5010 | S += '"'; |
| 5011 | S += field->getNameAsString(); |
| 5012 | S += '"'; |
| 5013 | } |
| 5014 | |
| 5015 | if (field->isBitField()) { |
| 5016 | EncodeBitField(this, S, field->getType(), field); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5017 | CurOffs += field->getBitWidthValue(*this); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5018 | } else { |
| 5019 | QualType qt = field->getType(); |
| 5020 | getLegacyIntegralTypeEncoding(qt); |
| 5021 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 5022 | /*OutermostType*/false, |
| 5023 | /*EncodingProperty*/false, |
| 5024 | /*StructField*/true); |
| 5025 | CurOffs += getTypeSize(field->getType()); |
| 5026 | } |
| 5027 | } |
| 5028 | } |
| 5029 | } |
| 5030 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5031 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 5032 | std::string& S) const { |
| 5033 | if (QT & Decl::OBJC_TQ_In) |
| 5034 | S += 'n'; |
| 5035 | if (QT & Decl::OBJC_TQ_Inout) |
| 5036 | S += 'N'; |
| 5037 | if (QT & Decl::OBJC_TQ_Out) |
| 5038 | S += 'o'; |
| 5039 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 5040 | S += 'O'; |
| 5041 | if (QT & Decl::OBJC_TQ_Byref) |
| 5042 | S += 'R'; |
| 5043 | if (QT & Decl::OBJC_TQ_Oneway) |
| 5044 | S += 'V'; |
| 5045 | } |
| 5046 | |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 5047 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 5048 | if (!ObjCIdDecl) { |
| 5049 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0); |
| 5050 | T = getObjCObjectPointerType(T); |
| 5051 | TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T); |
| 5052 | ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5053 | getTranslationUnitDecl(), |
| 5054 | SourceLocation(), SourceLocation(), |
| 5055 | &Idents.get("id"), IdInfo); |
| 5056 | } |
| 5057 | |
| 5058 | return ObjCIdDecl; |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 5059 | } |
| 5060 | |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 5061 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 5062 | if (!ObjCSelDecl) { |
| 5063 | QualType SelT = getPointerType(ObjCBuiltinSelTy); |
| 5064 | TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT); |
| 5065 | ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5066 | getTranslationUnitDecl(), |
| 5067 | SourceLocation(), SourceLocation(), |
| 5068 | &Idents.get("SEL"), SelInfo); |
| 5069 | } |
| 5070 | return ObjCSelDecl; |
Fariborz Jahanian | 4bef462 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 5071 | } |
| 5072 | |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 5073 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 5074 | if (!ObjCClassDecl) { |
| 5075 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0); |
| 5076 | T = getObjCObjectPointerType(T); |
| 5077 | TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T); |
| 5078 | ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this), |
| 5079 | getTranslationUnitDecl(), |
| 5080 | SourceLocation(), SourceLocation(), |
| 5081 | &Idents.get("Class"), ClassInfo); |
| 5082 | } |
| 5083 | |
| 5084 | return ObjCClassDecl; |
Anders Carlsson | f56a7ae | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 5085 | } |
| 5086 | |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 5087 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 5088 | if (!ObjCProtocolClassDecl) { |
| 5089 | ObjCProtocolClassDecl |
| 5090 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 5091 | SourceLocation(), |
| 5092 | &Idents.get("Protocol"), |
| 5093 | /*PrevDecl=*/0, |
| 5094 | SourceLocation(), true); |
| 5095 | } |
| 5096 | |
| 5097 | return ObjCProtocolClassDecl; |
| 5098 | } |
| 5099 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5100 | //===----------------------------------------------------------------------===// |
| 5101 | // __builtin_va_list Construction Functions |
| 5102 | //===----------------------------------------------------------------------===// |
| 5103 | |
| 5104 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5105 | // typedef char* __builtin_va_list; |
| 5106 | QualType CharPtrType = Context->getPointerType(Context->CharTy); |
| 5107 | TypeSourceInfo *TInfo |
| 5108 | = Context->getTrivialTypeSourceInfo(CharPtrType); |
| 5109 | |
| 5110 | TypedefDecl *VaListTypeDecl |
| 5111 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5112 | Context->getTranslationUnitDecl(), |
| 5113 | SourceLocation(), SourceLocation(), |
| 5114 | &Context->Idents.get("__builtin_va_list"), |
| 5115 | TInfo); |
| 5116 | return VaListTypeDecl; |
| 5117 | } |
| 5118 | |
| 5119 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5120 | // typedef void* __builtin_va_list; |
| 5121 | QualType VoidPtrType = Context->getPointerType(Context->VoidTy); |
| 5122 | TypeSourceInfo *TInfo |
| 5123 | = Context->getTrivialTypeSourceInfo(VoidPtrType); |
| 5124 | |
| 5125 | TypedefDecl *VaListTypeDecl |
| 5126 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5127 | Context->getTranslationUnitDecl(), |
| 5128 | SourceLocation(), SourceLocation(), |
| 5129 | &Context->Idents.get("__builtin_va_list"), |
| 5130 | TInfo); |
| 5131 | return VaListTypeDecl; |
| 5132 | } |
| 5133 | |
| 5134 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5135 | // typedef struct __va_list_tag { |
| 5136 | RecordDecl *VaListTagDecl; |
| 5137 | |
| 5138 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5139 | Context->getTranslationUnitDecl(), |
| 5140 | &Context->Idents.get("__va_list_tag")); |
| 5141 | VaListTagDecl->startDefinition(); |
| 5142 | |
| 5143 | const size_t NumFields = 5; |
| 5144 | QualType FieldTypes[NumFields]; |
| 5145 | const char *FieldNames[NumFields]; |
| 5146 | |
| 5147 | // unsigned char gpr; |
| 5148 | FieldTypes[0] = Context->UnsignedCharTy; |
| 5149 | FieldNames[0] = "gpr"; |
| 5150 | |
| 5151 | // unsigned char fpr; |
| 5152 | FieldTypes[1] = Context->UnsignedCharTy; |
| 5153 | FieldNames[1] = "fpr"; |
| 5154 | |
| 5155 | // unsigned short reserved; |
| 5156 | FieldTypes[2] = Context->UnsignedShortTy; |
| 5157 | FieldNames[2] = "reserved"; |
| 5158 | |
| 5159 | // void* overflow_arg_area; |
| 5160 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5161 | FieldNames[3] = "overflow_arg_area"; |
| 5162 | |
| 5163 | // void* reg_save_area; |
| 5164 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
| 5165 | FieldNames[4] = "reg_save_area"; |
| 5166 | |
| 5167 | // Create fields |
| 5168 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5169 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
| 5170 | SourceLocation(), |
| 5171 | SourceLocation(), |
| 5172 | &Context->Idents.get(FieldNames[i]), |
| 5173 | FieldTypes[i], /*TInfo=*/0, |
| 5174 | /*BitWidth=*/0, |
| 5175 | /*Mutable=*/false, |
| 5176 | ICIS_NoInit); |
| 5177 | Field->setAccess(AS_public); |
| 5178 | VaListTagDecl->addDecl(Field); |
| 5179 | } |
| 5180 | VaListTagDecl->completeDefinition(); |
| 5181 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5182 | Context->VaListTagTy = VaListTagType; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5183 | |
| 5184 | // } __va_list_tag; |
| 5185 | TypedefDecl *VaListTagTypedefDecl |
| 5186 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5187 | Context->getTranslationUnitDecl(), |
| 5188 | SourceLocation(), SourceLocation(), |
| 5189 | &Context->Idents.get("__va_list_tag"), |
| 5190 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5191 | QualType VaListTagTypedefType = |
| 5192 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5193 | |
| 5194 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5195 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5196 | QualType VaListTagArrayType |
| 5197 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5198 | Size, ArrayType::Normal, 0); |
| 5199 | TypeSourceInfo *TInfo |
| 5200 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5201 | TypedefDecl *VaListTypedefDecl |
| 5202 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5203 | Context->getTranslationUnitDecl(), |
| 5204 | SourceLocation(), SourceLocation(), |
| 5205 | &Context->Idents.get("__builtin_va_list"), |
| 5206 | TInfo); |
| 5207 | |
| 5208 | return VaListTypedefDecl; |
| 5209 | } |
| 5210 | |
| 5211 | static TypedefDecl * |
| 5212 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5213 | // typedef struct __va_list_tag { |
| 5214 | RecordDecl *VaListTagDecl; |
| 5215 | VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct, |
| 5216 | Context->getTranslationUnitDecl(), |
| 5217 | &Context->Idents.get("__va_list_tag")); |
| 5218 | VaListTagDecl->startDefinition(); |
| 5219 | |
| 5220 | const size_t NumFields = 4; |
| 5221 | QualType FieldTypes[NumFields]; |
| 5222 | const char *FieldNames[NumFields]; |
| 5223 | |
| 5224 | // unsigned gp_offset; |
| 5225 | FieldTypes[0] = Context->UnsignedIntTy; |
| 5226 | FieldNames[0] = "gp_offset"; |
| 5227 | |
| 5228 | // unsigned fp_offset; |
| 5229 | FieldTypes[1] = Context->UnsignedIntTy; |
| 5230 | FieldNames[1] = "fp_offset"; |
| 5231 | |
| 5232 | // void* overflow_arg_area; |
| 5233 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 5234 | FieldNames[2] = "overflow_arg_area"; |
| 5235 | |
| 5236 | // void* reg_save_area; |
| 5237 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 5238 | FieldNames[3] = "reg_save_area"; |
| 5239 | |
| 5240 | // Create fields |
| 5241 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5242 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5243 | VaListTagDecl, |
| 5244 | SourceLocation(), |
| 5245 | SourceLocation(), |
| 5246 | &Context->Idents.get(FieldNames[i]), |
| 5247 | FieldTypes[i], /*TInfo=*/0, |
| 5248 | /*BitWidth=*/0, |
| 5249 | /*Mutable=*/false, |
| 5250 | ICIS_NoInit); |
| 5251 | Field->setAccess(AS_public); |
| 5252 | VaListTagDecl->addDecl(Field); |
| 5253 | } |
| 5254 | VaListTagDecl->completeDefinition(); |
| 5255 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5256 | Context->VaListTagTy = VaListTagType; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5257 | |
| 5258 | // } __va_list_tag; |
| 5259 | TypedefDecl *VaListTagTypedefDecl |
| 5260 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5261 | Context->getTranslationUnitDecl(), |
| 5262 | SourceLocation(), SourceLocation(), |
| 5263 | &Context->Idents.get("__va_list_tag"), |
| 5264 | Context->getTrivialTypeSourceInfo(VaListTagType)); |
| 5265 | QualType VaListTagTypedefType = |
| 5266 | Context->getTypedefType(VaListTagTypedefDecl); |
| 5267 | |
| 5268 | // typedef __va_list_tag __builtin_va_list[1]; |
| 5269 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 5270 | QualType VaListTagArrayType |
| 5271 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 5272 | Size, ArrayType::Normal,0); |
| 5273 | TypeSourceInfo *TInfo |
| 5274 | = Context->getTrivialTypeSourceInfo(VaListTagArrayType); |
| 5275 | TypedefDecl *VaListTypedefDecl |
| 5276 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5277 | Context->getTranslationUnitDecl(), |
| 5278 | SourceLocation(), SourceLocation(), |
| 5279 | &Context->Idents.get("__builtin_va_list"), |
| 5280 | TInfo); |
| 5281 | |
| 5282 | return VaListTypedefDecl; |
| 5283 | } |
| 5284 | |
| 5285 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5286 | // typedef int __builtin_va_list[4]; |
| 5287 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); |
| 5288 | QualType IntArrayType |
| 5289 | = Context->getConstantArrayType(Context->IntTy, |
| 5290 | Size, ArrayType::Normal, 0); |
| 5291 | TypedefDecl *VaListTypedefDecl |
| 5292 | = TypedefDecl::Create(const_cast<ASTContext &>(*Context), |
| 5293 | Context->getTranslationUnitDecl(), |
| 5294 | SourceLocation(), SourceLocation(), |
| 5295 | &Context->Idents.get("__builtin_va_list"), |
| 5296 | Context->getTrivialTypeSourceInfo(IntArrayType)); |
| 5297 | |
| 5298 | return VaListTypedefDecl; |
| 5299 | } |
| 5300 | |
| 5301 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
| 5302 | TargetInfo::BuiltinVaListKind Kind) { |
| 5303 | switch (Kind) { |
| 5304 | case TargetInfo::CharPtrBuiltinVaList: |
| 5305 | return CreateCharPtrBuiltinVaListDecl(Context); |
| 5306 | case TargetInfo::VoidPtrBuiltinVaList: |
| 5307 | return CreateVoidPtrBuiltinVaListDecl(Context); |
| 5308 | case TargetInfo::PowerABIBuiltinVaList: |
| 5309 | return CreatePowerABIBuiltinVaListDecl(Context); |
| 5310 | case TargetInfo::X86_64ABIBuiltinVaList: |
| 5311 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
| 5312 | case TargetInfo::PNaClABIBuiltinVaList: |
| 5313 | return CreatePNaClABIBuiltinVaListDecl(Context); |
| 5314 | } |
| 5315 | |
| 5316 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
| 5317 | } |
| 5318 | |
| 5319 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
| 5320 | if (!BuiltinVaListDecl) |
| 5321 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); |
| 5322 | |
| 5323 | return BuiltinVaListDecl; |
| 5324 | } |
| 5325 | |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 5326 | QualType ASTContext::getVaListTagType() const { |
| 5327 | // Force the creation of VaListTagTy by building the __builtin_va_list |
| 5328 | // declaration. |
| 5329 | if (VaListTagTy.isNull()) |
| 5330 | (void) getBuiltinVaListDecl(); |
| 5331 | |
| 5332 | return VaListTagTy; |
| 5333 | } |
| 5334 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5335 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5336 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 5337 | "'NSConstantString' type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5338 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5339 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 5340 | } |
| 5341 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5342 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 5343 | /// lookup. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5344 | TemplateName |
| 5345 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 5346 | UnresolvedSetIterator End) const { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5347 | unsigned size = End - Begin; |
| 5348 | assert(size > 1 && "set is not overloaded!"); |
| 5349 | |
| 5350 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 5351 | size * sizeof(FunctionTemplateDecl*)); |
| 5352 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 5353 | |
| 5354 | NamedDecl **Storage = OT->getStorage(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 5355 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 5356 | NamedDecl *D = *I; |
| 5357 | assert(isa<FunctionTemplateDecl>(D) || |
| 5358 | (isa<UsingShadowDecl>(D) && |
| 5359 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 5360 | *Storage++ = D; |
| 5361 | } |
| 5362 | |
| 5363 | return TemplateName(OT); |
| 5364 | } |
| 5365 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5366 | /// \brief Retrieve the template name that represents a qualified |
| 5367 | /// template name such as \c std::vector. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5368 | TemplateName |
| 5369 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 5370 | bool TemplateKeyword, |
| 5371 | TemplateDecl *Template) const { |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 5372 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 5373 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5374 | // FIXME: Canonicalization? |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5375 | llvm::FoldingSetNodeID ID; |
| 5376 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 5377 | |
| 5378 | void *InsertPos = 0; |
| 5379 | QualifiedTemplateName *QTN = |
| 5380 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5381 | if (!QTN) { |
| 5382 | QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template); |
| 5383 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 5384 | } |
| 5385 | |
| 5386 | return TemplateName(QTN); |
| 5387 | } |
| 5388 | |
| 5389 | /// \brief Retrieve the template name that represents a dependent |
| 5390 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5391 | TemplateName |
| 5392 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 5393 | const IdentifierInfo *Name) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5394 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 5395 | "Nested name specifier must be dependent"); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5396 | |
| 5397 | llvm::FoldingSetNodeID ID; |
| 5398 | DependentTemplateName::Profile(ID, NNS, Name); |
| 5399 | |
| 5400 | void *InsertPos = 0; |
| 5401 | DependentTemplateName *QTN = |
| 5402 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5403 | |
| 5404 | if (QTN) |
| 5405 | return TemplateName(QTN); |
| 5406 | |
| 5407 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5408 | if (CanonNNS == NNS) { |
| 5409 | QTN = new (*this,4) DependentTemplateName(NNS, Name); |
| 5410 | } else { |
| 5411 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
| 5412 | QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5413 | DependentTemplateName *CheckQTN = |
| 5414 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5415 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 5416 | (void)CheckQTN; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 5417 | } |
| 5418 | |
| 5419 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5420 | return TemplateName(QTN); |
| 5421 | } |
| 5422 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5423 | /// \brief Retrieve the template name that represents a dependent |
| 5424 | /// template name such as \c MetaFun::template operator+. |
| 5425 | TemplateName |
| 5426 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5427 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5428 | assert((!NNS || NNS->isDependent()) && |
| 5429 | "Nested name specifier must be dependent"); |
| 5430 | |
| 5431 | llvm::FoldingSetNodeID ID; |
| 5432 | DependentTemplateName::Profile(ID, NNS, Operator); |
| 5433 | |
| 5434 | void *InsertPos = 0; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5435 | DependentTemplateName *QTN |
| 5436 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5437 | |
| 5438 | if (QTN) |
| 5439 | return TemplateName(QTN); |
| 5440 | |
| 5441 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 5442 | if (CanonNNS == NNS) { |
| 5443 | QTN = new (*this,4) DependentTemplateName(NNS, Operator); |
| 5444 | } else { |
| 5445 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
| 5446 | QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 5447 | |
| 5448 | DependentTemplateName *CheckQTN |
| 5449 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 5450 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 5451 | (void)CheckQTN; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
| 5454 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 5455 | return TemplateName(QTN); |
| 5456 | } |
| 5457 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5458 | TemplateName |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5459 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 5460 | TemplateName replacement) const { |
| 5461 | llvm::FoldingSetNodeID ID; |
| 5462 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
| 5463 | |
| 5464 | void *insertPos = 0; |
| 5465 | SubstTemplateTemplateParmStorage *subst |
| 5466 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 5467 | |
| 5468 | if (!subst) { |
| 5469 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 5470 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 5471 | } |
| 5472 | |
| 5473 | return TemplateName(subst); |
| 5474 | } |
| 5475 | |
| 5476 | TemplateName |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5477 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 5478 | const TemplateArgument &ArgPack) const { |
| 5479 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 5480 | llvm::FoldingSetNodeID ID; |
| 5481 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
| 5482 | |
| 5483 | void *InsertPos = 0; |
| 5484 | SubstTemplateTemplateParmPackStorage *Subst |
| 5485 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 5486 | |
| 5487 | if (!Subst) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5488 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 5489 | ArgPack.pack_size(), |
| 5490 | ArgPack.pack_begin()); |
| 5491 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 5492 | } |
| 5493 | |
| 5494 | return TemplateName(Subst); |
| 5495 | } |
| 5496 | |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5497 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | ab13857 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 5498 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 5499 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5500 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5501 | switch (Type) { |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 5502 | case TargetInfo::NoInt: return CanQualType(); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5503 | case TargetInfo::SignedShort: return ShortTy; |
| 5504 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 5505 | case TargetInfo::SignedInt: return IntTy; |
| 5506 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 5507 | case TargetInfo::SignedLong: return LongTy; |
| 5508 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 5509 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 5510 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 5511 | } |
| 5512 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5513 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 5514 | } |
Ted Kremenek | 77c51b2 | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 5515 | |
| 5516 | //===----------------------------------------------------------------------===// |
| 5517 | // Type Predicates. |
| 5518 | //===----------------------------------------------------------------------===// |
| 5519 | |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5520 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 5521 | /// garbage collection attribute. |
| 5522 | /// |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5523 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5524 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5525 | return Qualifiers::GCNone; |
| 5526 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5527 | assert(getLangOpts().ObjC1); |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 5528 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 5529 | |
| 5530 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 5531 | // (or pointers to them) be treated as though they were declared |
| 5532 | // as __strong. |
| 5533 | if (GCAttrs == Qualifiers::GCNone) { |
| 5534 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 5535 | return Qualifiers::Strong; |
| 5536 | else if (Ty->isPointerType()) |
| 5537 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 5538 | } else { |
| 5539 | // It's not valid to set GC attributes on anything that isn't a |
| 5540 | // pointer. |
| 5541 | #ifndef NDEBUG |
| 5542 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 5543 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 5544 | CT = AT->getElementType(); |
| 5545 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 5546 | #endif |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5547 | } |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 5548 | return GCAttrs; |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 5549 | } |
| 5550 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5551 | //===----------------------------------------------------------------------===// |
| 5552 | // Type Compatibility Testing |
| 5553 | //===----------------------------------------------------------------------===// |
Chris Lattner | b338a6b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 5554 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5555 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5556 | /// compatible. |
| 5557 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 5558 | const VectorType *RHS) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 5559 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5560 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 5561 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5562 | } |
| 5563 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5564 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 5565 | QualType SecondVec) { |
| 5566 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 5567 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 5568 | |
| 5569 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 5570 | return true; |
| 5571 | |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5572 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 5573 | // equivalent GCC vector types. |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5574 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 5575 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5576 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5577 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 5578 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 5579 | First->getVectorKind() != VectorType::AltiVecBool && |
| 5580 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 5581 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5582 | return true; |
| 5583 | |
| 5584 | return false; |
| 5585 | } |
| 5586 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5587 | //===----------------------------------------------------------------------===// |
| 5588 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 5589 | //===----------------------------------------------------------------------===// |
| 5590 | |
| 5591 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 5592 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5593 | bool |
| 5594 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 5595 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 5596 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5597 | return true; |
| 5598 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 5599 | E = rProto->protocol_end(); PI != E; ++PI) |
| 5600 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
| 5601 | return true; |
| 5602 | return false; |
| 5603 | } |
| 5604 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5605 | /// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...> |
| 5606 | /// return true if lhs's protocols conform to rhs's protocol; false |
| 5607 | /// otherwise. |
| 5608 | bool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) { |
| 5609 | if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType()) |
| 5610 | return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false); |
| 5611 | return false; |
| 5612 | } |
| 5613 | |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5614 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<p,...> and |
| 5615 | /// Class<p1, ...>. |
| 5616 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 5617 | QualType rhs) { |
| 5618 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 5619 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 5620 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 5621 | |
| 5622 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5623 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5624 | bool match = false; |
| 5625 | ObjCProtocolDecl *lhsProto = *I; |
| 5626 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5627 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5628 | ObjCProtocolDecl *rhsProto = *J; |
| 5629 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 5630 | match = true; |
| 5631 | break; |
| 5632 | } |
| 5633 | } |
| 5634 | if (!match) |
| 5635 | return false; |
| 5636 | } |
| 5637 | return true; |
| 5638 | } |
| 5639 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5640 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 5641 | /// ObjCQualifiedIDType. |
| 5642 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 5643 | bool compare) { |
| 5644 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5645 | if (lhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5646 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 5647 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5648 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5649 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 5650 | return true; |
| 5651 | |
| 5652 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5653 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5654 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5655 | if (!rhsOPT) return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5656 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5657 | if (rhsOPT->qual_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5658 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5659 | // make sure we check the class hierarchy. |
| 5660 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5661 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5662 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5663 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5664 | // see if static class implements all of id's protocols, directly or |
| 5665 | // through its super class and categories. |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5666 | if (!rhsID->ClassImplementsProtocol(*I, true)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5667 | return false; |
| 5668 | } |
| 5669 | } |
| 5670 | // If there are no qualifiers and no interface, we have an 'id'. |
| 5671 | return true; |
| 5672 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5673 | // Both the right and left sides have qualifiers. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5674 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5675 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5676 | ObjCProtocolDecl *lhsProto = *I; |
| 5677 | bool match = false; |
| 5678 | |
| 5679 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5680 | // see if static class implements all of id's protocols, directly or |
| 5681 | // through its super class and categories. |
| 5682 | for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(), |
| 5683 | E = rhsOPT->qual_end(); J != E; ++J) { |
| 5684 | ObjCProtocolDecl *rhsProto = *J; |
| 5685 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5686 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5687 | match = true; |
| 5688 | break; |
| 5689 | } |
| 5690 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5691 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5692 | // make sure we check the class hierarchy. |
| 5693 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
| 5694 | for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(), |
| 5695 | E = lhsQID->qual_end(); I != E; ++I) { |
| 5696 | // when comparing an id<P> on lhs with a static type on rhs, |
| 5697 | // see if static class implements all of id's protocols, directly or |
| 5698 | // through its super class and categories. |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 5699 | if (rhsID->ClassImplementsProtocol(*I, true)) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5700 | match = true; |
| 5701 | break; |
| 5702 | } |
| 5703 | } |
| 5704 | } |
| 5705 | if (!match) |
| 5706 | return false; |
| 5707 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5708 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5709 | return true; |
| 5710 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5711 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5712 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 5713 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 5714 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5715 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5716 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5717 | // If both the right and left sides have qualifiers. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5718 | for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(), |
| 5719 | E = lhsOPT->qual_end(); I != E; ++I) { |
| 5720 | ObjCProtocolDecl *lhsProto = *I; |
| 5721 | bool match = false; |
| 5722 | |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5723 | // 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] | 5724 | // see if static class implements all of id's protocols, directly or |
| 5725 | // through its super class and categories. |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5726 | // First, lhs protocols in the qualifier list must be found, direct |
| 5727 | // or indirect in rhs's qualifier list or it is a mismatch. |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5728 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5729 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5730 | ObjCProtocolDecl *rhsProto = *J; |
| 5731 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5732 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5733 | match = true; |
| 5734 | break; |
| 5735 | } |
| 5736 | } |
| 5737 | if (!match) |
| 5738 | return false; |
| 5739 | } |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 5740 | |
| 5741 | // Static class's protocols, or its super class or category protocols |
| 5742 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 5743 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 5744 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 5745 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 5746 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 5747 | // no type qualifier and its class has no static protocol(s) |
| 5748 | // assume that it is mismatch. |
| 5749 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 5750 | return false; |
| 5751 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5752 | LHSInheritedProtocols.begin(), |
| 5753 | E = LHSInheritedProtocols.end(); I != E; ++I) { |
| 5754 | bool match = false; |
| 5755 | ObjCProtocolDecl *lhsProto = (*I); |
| 5756 | for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(), |
| 5757 | E = rhsQID->qual_end(); J != E; ++J) { |
| 5758 | ObjCProtocolDecl *rhsProto = *J; |
| 5759 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 5760 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 5761 | match = true; |
| 5762 | break; |
| 5763 | } |
| 5764 | } |
| 5765 | if (!match) |
| 5766 | return false; |
| 5767 | } |
| 5768 | } |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5769 | return true; |
| 5770 | } |
| 5771 | return false; |
| 5772 | } |
| 5773 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 5774 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5775 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 5776 | /// protocol qualifiers on the LHS or RHS. |
| 5777 | /// |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5778 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 5779 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5780 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5781 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5782 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 5783 | // 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] | 5784 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 5785 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5786 | return true; |
| 5787 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5788 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5789 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5790 | QualType(RHSOPT,0), |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5791 | false); |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 5792 | |
| 5793 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 5794 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 5795 | QualType(RHSOPT,0)); |
| 5796 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5797 | // If we have 2 user-defined types, fall into that path. |
| 5798 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5799 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5800 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 5801 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5802 | } |
| 5803 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5804 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 5805 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5806 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 5807 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 5808 | /// not OK. For the return type, the opposite is not OK. |
| 5809 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 5810 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5811 | const ObjCObjectPointerType *RHSOPT, |
| 5812 | bool BlockReturnType) { |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5813 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5814 | return true; |
| 5815 | |
| 5816 | if (LHSOPT->isObjCBuiltinType()) { |
| 5817 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 5818 | } |
| 5819 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 5820 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5821 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 5822 | QualType(RHSOPT,0), |
| 5823 | false); |
| 5824 | |
| 5825 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 5826 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 5827 | if (LHS && RHS) { // We have 2 user-defined types. |
| 5828 | if (LHS != RHS) { |
| 5829 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5830 | return BlockReturnType; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5831 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 5832 | return !BlockReturnType; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5833 | } |
| 5834 | else |
| 5835 | return true; |
| 5836 | } |
| 5837 | return false; |
| 5838 | } |
| 5839 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5840 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 5841 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 5842 | /// It is used to build composite qualifier list of the composite type of |
| 5843 | /// the conditional expression involving two objective-c pointer objects. |
| 5844 | static |
| 5845 | void getIntersectionOfProtocols(ASTContext &Context, |
| 5846 | const ObjCObjectPointerType *LHSOPT, |
| 5847 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5848 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5849 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5850 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 5851 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 5852 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 5853 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5854 | |
| 5855 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 5856 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 5857 | if (LHSNumProtocols > 0) |
| 5858 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 5859 | else { |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5860 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5861 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 5862 | LHSInheritedProtocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5863 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 5864 | LHSInheritedProtocols.end()); |
| 5865 | } |
| 5866 | |
| 5867 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 5868 | if (RHSNumProtocols > 0) { |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 5869 | ObjCProtocolDecl **RHSProtocols = |
| 5870 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5871 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 5872 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 5873 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 5874 | } else { |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5875 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5876 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 5877 | RHSInheritedProtocols); |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 5878 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5879 | RHSInheritedProtocols.begin(), |
| 5880 | E = RHSInheritedProtocols.end(); I != E; ++I) |
| 5881 | if (InheritedProtocolSet.count((*I))) |
| 5882 | IntersectionOfProtocols.push_back((*I)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5883 | } |
| 5884 | } |
| 5885 | |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5886 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 5887 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 5888 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 5889 | /// warning is issued. So, its invokation is extremely rare. |
| 5890 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5891 | const ObjCObjectPointerType *Lptr, |
| 5892 | const ObjCObjectPointerType *Rptr) { |
| 5893 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 5894 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 5895 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 5896 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 5897 | if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl))) |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5898 | return QualType(); |
| 5899 | |
Fariborz Jahanian | b107143 | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5900 | do { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5901 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5902 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5903 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5904 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 5905 | |
| 5906 | QualType Result = QualType(LHS, 0); |
| 5907 | if (!Protocols.empty()) |
| 5908 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 5909 | Result = getObjCObjectPointerType(Result); |
| 5910 | return Result; |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 5911 | } |
Fariborz Jahanian | b107143 | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 5912 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 5913 | |
| 5914 | return QualType(); |
| 5915 | } |
| 5916 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5917 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 5918 | const ObjCObjectType *RHS) { |
| 5919 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 5920 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 5921 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5922 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 5923 | // the LHS. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5924 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5925 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5926 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5927 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 5928 | // protocol qualified at all, then we are good. |
Steve Naroff | c277ad1 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 5929 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5930 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5931 | |
Fariborz Jahanian | 4806ff8 | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5932 | // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, |
| 5933 | // more detailed analysis is required. |
| 5934 | if (RHS->getNumProtocols() == 0) { |
| 5935 | // OK, if LHS is a superclass of RHS *and* |
| 5936 | // this superclass is assignment compatible with LHS. |
| 5937 | // false otherwise. |
Fariborz Jahanian | 240400b | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5938 | bool IsSuperClass = |
| 5939 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 5940 | if (IsSuperClass) { |
Fariborz Jahanian | 4806ff8 | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5941 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 5942 | // ; i.e., SuperClass may implement at least one of the protocols |
| 5943 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 5944 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 5945 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
Fariborz Jahanian | 240400b | 2011-04-12 16:34:14 +0000 | [diff] [blame] | 5946 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
Fariborz Jahanian | 4806ff8 | 2011-04-08 18:25:29 +0000 | [diff] [blame] | 5947 | // If super class has no protocols, it is not a match. |
| 5948 | if (SuperClassInheritedProtocols.empty()) |
| 5949 | return false; |
| 5950 | |
| 5951 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5952 | LHSPE = LHS->qual_end(); |
| 5953 | LHSPI != LHSPE; LHSPI++) { |
| 5954 | bool SuperImplementsProtocol = false; |
| 5955 | ObjCProtocolDecl *LHSProto = (*LHSPI); |
| 5956 | |
| 5957 | for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I = |
| 5958 | SuperClassInheritedProtocols.begin(), |
| 5959 | E = SuperClassInheritedProtocols.end(); I != E; ++I) { |
| 5960 | ObjCProtocolDecl *SuperClassProto = (*I); |
| 5961 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 5962 | SuperImplementsProtocol = true; |
| 5963 | break; |
| 5964 | } |
| 5965 | } |
| 5966 | if (!SuperImplementsProtocol) |
| 5967 | return false; |
| 5968 | } |
| 5969 | return true; |
| 5970 | } |
| 5971 | return false; |
| 5972 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5973 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5974 | for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(), |
| 5975 | LHSPE = LHS->qual_end(); |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5976 | LHSPI != LHSPE; LHSPI++) { |
| 5977 | bool RHSImplementsProtocol = false; |
| 5978 | |
| 5979 | // If the RHS doesn't implement the protocol on the left, the types |
| 5980 | // are incompatible. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5981 | for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(), |
| 5982 | RHSPE = RHS->qual_end(); |
Steve Naroff | fac5bc9 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5983 | RHSPI != RHSPE; RHSPI++) { |
| 5984 | if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) { |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5985 | RHSImplementsProtocol = true; |
Steve Naroff | fac5bc9 | 2009-07-16 16:21:02 +0000 | [diff] [blame] | 5986 | break; |
| 5987 | } |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5988 | } |
| 5989 | // FIXME: For better diagnostics, consider passing back the protocol name. |
| 5990 | if (!RHSImplementsProtocol) |
| 5991 | return false; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5992 | } |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 5993 | // The RHS implements all protocols listed on the LHS. |
| 5994 | return true; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 5995 | } |
| 5996 | |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 5997 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 5998 | // get the "pointed to" types |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5999 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 6000 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6001 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6002 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6003 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6004 | |
| 6005 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 6006 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6007 | } |
| 6008 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6009 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 6010 | return canAssignObjCInterfaces( |
| 6011 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 6012 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 6013 | } |
| 6014 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6015 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6016 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6017 | /// 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] | 6018 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6019 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 6020 | bool CompareUnqualified) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6021 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6022 | return hasSameType(LHS, RHS); |
| 6023 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6024 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6025 | } |
| 6026 | |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6027 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | c87c879 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 6028 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6029 | } |
| 6030 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6031 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 6032 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 6033 | } |
| 6034 | |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6035 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 6036 | /// of T is compatible with SubType, return the merged type, else return |
| 6037 | /// QualType() |
| 6038 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 6039 | bool OfBlockPointer, |
| 6040 | bool Unqualified) { |
| 6041 | if (const RecordType *UT = T->getAsUnionType()) { |
| 6042 | RecordDecl *UD = UT->getDecl(); |
| 6043 | if (UD->hasAttr<TransparentUnionAttr>()) { |
| 6044 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6045 | itend = UD->field_end(); it != itend; ++it) { |
Peter Collingbourne | 19b961d | 2010-12-02 21:00:06 +0000 | [diff] [blame] | 6046 | QualType ET = it->getType().getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6047 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 6048 | if (!MT.isNull()) |
| 6049 | return MT; |
| 6050 | } |
| 6051 | } |
| 6052 | } |
| 6053 | |
| 6054 | return QualType(); |
| 6055 | } |
| 6056 | |
| 6057 | /// mergeFunctionArgumentTypes - merge two types which appear as function |
| 6058 | /// argument types |
| 6059 | QualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs, |
| 6060 | bool OfBlockPointer, |
| 6061 | bool Unqualified) { |
| 6062 | // GNU extension: two types are compatible if they appear as a function |
| 6063 | // argument, one of the types is a transparent union type and the other |
| 6064 | // type is compatible with a union member |
| 6065 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 6066 | Unqualified); |
| 6067 | if (!lmerge.isNull()) |
| 6068 | return lmerge; |
| 6069 | |
| 6070 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 6071 | Unqualified); |
| 6072 | if (!rmerge.isNull()) |
| 6073 | return rmerge; |
| 6074 | |
| 6075 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 6076 | } |
| 6077 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6078 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6079 | bool OfBlockPointer, |
| 6080 | bool Unqualified) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6081 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 6082 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6083 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 6084 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6085 | bool allLTypes = true; |
| 6086 | bool allRTypes = true; |
| 6087 | |
| 6088 | // Check return type |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6089 | QualType retType; |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6090 | if (OfBlockPointer) { |
| 6091 | QualType RHS = rbase->getResultType(); |
| 6092 | QualType LHS = lbase->getResultType(); |
| 6093 | bool UnqualifiedResult = Unqualified; |
| 6094 | if (!UnqualifiedResult) |
| 6095 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6096 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6097 | } |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6098 | else |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6099 | retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false, |
| 6100 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6101 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6102 | |
| 6103 | if (Unqualified) |
| 6104 | retType = retType.getUnqualifiedType(); |
| 6105 | |
| 6106 | CanQualType LRetType = getCanonicalType(lbase->getResultType()); |
| 6107 | CanQualType RRetType = getCanonicalType(rbase->getResultType()); |
| 6108 | if (Unqualified) { |
| 6109 | LRetType = LRetType.getUnqualifiedType(); |
| 6110 | RRetType = RRetType.getUnqualifiedType(); |
| 6111 | } |
| 6112 | |
| 6113 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6114 | allLTypes = false; |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6115 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6116 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6117 | |
Daniel Dunbar | edd5bae | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 6118 | // FIXME: double check this |
| 6119 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 6120 | // rbase->getRegParmAttr() != 0 && |
| 6121 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 6122 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 6123 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6124 | |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6125 | // Compatible functions must have compatible calling conventions |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6126 | if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC())) |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 6127 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6128 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6129 | // Regparm is part of the calling convention. |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 6130 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 6131 | return QualType(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6132 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 6133 | return QualType(); |
| 6134 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6135 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 6136 | return QualType(); |
| 6137 | |
Fariborz Jahanian | 48c6910 | 2011-10-05 00:05:34 +0000 | [diff] [blame] | 6138 | // functypes which return are preferred over those that do not. |
| 6139 | if (lbaseInfo.getNoReturn() && !rbaseInfo.getNoReturn()) |
| 6140 | allLTypes = false; |
| 6141 | else if (!lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn()) |
| 6142 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6143 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 6144 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6145 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6146 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6147 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6148 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6149 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 6150 | "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6151 | unsigned lproto_nargs = lproto->getNumArgs(); |
| 6152 | unsigned rproto_nargs = rproto->getNumArgs(); |
| 6153 | |
| 6154 | // Compatible functions must have the same number of arguments |
| 6155 | if (lproto_nargs != rproto_nargs) |
| 6156 | return QualType(); |
| 6157 | |
| 6158 | // Variadic and non-variadic functions aren't compatible |
| 6159 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 6160 | return QualType(); |
| 6161 | |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 6162 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 6163 | return QualType(); |
| 6164 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6165 | if (LangOpts.ObjCAutoRefCount && |
| 6166 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 6167 | return QualType(); |
| 6168 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6169 | // Check argument compatibility |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6170 | SmallVector<QualType, 10> types; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6171 | for (unsigned i = 0; i < lproto_nargs; i++) { |
| 6172 | QualType largtype = lproto->getArgType(i).getUnqualifiedType(); |
| 6173 | QualType rargtype = rproto->getArgType(i).getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6174 | QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype, |
| 6175 | OfBlockPointer, |
| 6176 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6177 | if (argtype.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6178 | |
| 6179 | if (Unqualified) |
| 6180 | argtype = argtype.getUnqualifiedType(); |
| 6181 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6182 | types.push_back(argtype); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6183 | if (Unqualified) { |
| 6184 | largtype = largtype.getUnqualifiedType(); |
| 6185 | rargtype = rargtype.getUnqualifiedType(); |
| 6186 | } |
| 6187 | |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6188 | if (getCanonicalType(argtype) != getCanonicalType(largtype)) |
| 6189 | allLTypes = false; |
| 6190 | if (getCanonicalType(argtype) != getCanonicalType(rargtype)) |
| 6191 | allRTypes = false; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6192 | } |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6193 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6194 | if (allLTypes) return lhs; |
| 6195 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6196 | |
| 6197 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 6198 | EPI.ExtInfo = einfo; |
| 6199 | return getFunctionType(retType, types.begin(), types.size(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6200 | } |
| 6201 | |
| 6202 | if (lproto) allRTypes = false; |
| 6203 | if (rproto) allLTypes = false; |
| 6204 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6205 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6206 | if (proto) { |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 6207 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6208 | if (proto->isVariadic()) return QualType(); |
| 6209 | // Check that the types are compatible with the types that |
| 6210 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 6211 | // The only types actually affected are promotable integer |
| 6212 | // types and floats, which would be passed as a different |
| 6213 | // type depending on whether the prototype is visible. |
| 6214 | unsigned proto_nargs = proto->getNumArgs(); |
| 6215 | for (unsigned i = 0; i < proto_nargs; ++i) { |
| 6216 | QualType argTy = proto->getArgType(i); |
Douglas Gregor | 2973d40 | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 6217 | |
| 6218 | // Look at the promotion type of enum types, since that is the type used |
| 6219 | // to pass enum values. |
| 6220 | if (const EnumType *Enum = argTy->getAs<EnumType>()) |
| 6221 | argTy = Enum->getDecl()->getPromotionType(); |
| 6222 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6223 | if (argTy->isPromotableIntegerType() || |
| 6224 | getCanonicalType(argTy).getUnqualifiedType() == FloatTy) |
| 6225 | return QualType(); |
| 6226 | } |
| 6227 | |
| 6228 | if (allLTypes) return lhs; |
| 6229 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6230 | |
| 6231 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 6232 | EPI.ExtInfo = einfo; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6233 | return getFunctionType(retType, proto->arg_type_begin(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6234 | proto->getNumArgs(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6235 | } |
| 6236 | |
| 6237 | if (allLTypes) return lhs; |
| 6238 | if (allRTypes) return rhs; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6239 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6240 | } |
| 6241 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6242 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6243 | bool OfBlockPointer, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6244 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | db4e349 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 6245 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 6246 | // type is adjusted to "T" prior to any further analysis, the expression |
| 6247 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 6248 | // expression is an lvalue unless the reference is an rvalue reference and |
| 6249 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6250 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 6251 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6252 | |
| 6253 | if (Unqualified) { |
| 6254 | LHS = LHS.getUnqualifiedType(); |
| 6255 | RHS = RHS.getUnqualifiedType(); |
| 6256 | } |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6257 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6258 | QualType LHSCan = getCanonicalType(LHS), |
| 6259 | RHSCan = getCanonicalType(RHS); |
| 6260 | |
| 6261 | // If two types are identical, they are compatible. |
| 6262 | if (LHSCan == RHSCan) |
| 6263 | return LHS; |
| 6264 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6265 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 6266 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 6267 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6268 | if (LQuals != RQuals) { |
| 6269 | // If any of these qualifiers are different, we have a type |
| 6270 | // mismatch. |
| 6271 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6272 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 6273 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6274 | return QualType(); |
| 6275 | |
| 6276 | // Exactly one GC qualifier difference is allowed: __strong is |
| 6277 | // okay if the other type has no GC qualifier but is an Objective |
| 6278 | // C object pointer (i.e. implicitly strong by default). We fix |
| 6279 | // this by pretending that the unqualified type was actually |
| 6280 | // qualified __strong. |
| 6281 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 6282 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 6283 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 6284 | |
| 6285 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 6286 | return QualType(); |
| 6287 | |
| 6288 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 6289 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 6290 | } |
| 6291 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 6292 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 6293 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6294 | return QualType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6295 | } |
| 6296 | |
| 6297 | // Okay, qualifiers are equal. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6298 | |
Eli Friedman | dcca633 | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 6299 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 6300 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6301 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6302 | // We want to consider the two function types to be the same for these |
| 6303 | // comparisons, just force one to the other. |
| 6304 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 6305 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 16f9096 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 6306 | |
| 6307 | // Same as above for arrays |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 6308 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 6309 | LHSClass = Type::ConstantArray; |
| 6310 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 6311 | RHSClass = Type::ConstantArray; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6312 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6313 | // ObjCInterfaces are just specialized ObjCObjects. |
| 6314 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 6315 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 6316 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 6317 | // Canonicalize ExtVector -> Vector. |
| 6318 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 6319 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6320 | |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 6321 | // If the canonical type classes don't match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6322 | if (LHSClass != RHSClass) { |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6323 | // 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] | 6324 | // a signed integer type, or an unsigned integer type. |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 6325 | // Compatibility is based on the underlying type, not the promotion |
| 6326 | // type. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6327 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
Fariborz Jahanian | adfe905 | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 6328 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 6329 | if (!TINT.isNull() && hasSameType(TINT, RHSCan.getUnqualifiedType())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6330 | return RHS; |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 6331 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6332 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
Fariborz Jahanian | adfe905 | 2012-02-06 19:06:20 +0000 | [diff] [blame] | 6333 | QualType TINT = ETy->getDecl()->getIntegerType(); |
| 6334 | if (!TINT.isNull() && hasSameType(TINT, LHSCan.getUnqualifiedType())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6335 | return LHS; |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 6336 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 6337 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 194904e | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 6338 | if (OfBlockPointer && !BlockReturnType) { |
| 6339 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 6340 | return LHS; |
| 6341 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 6342 | return RHS; |
| 6343 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 6344 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6345 | return QualType(); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6346 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6347 | |
Steve Naroff | c6edcbd | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 6348 | // The canonical type classes match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6349 | switch (LHSClass) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6350 | #define TYPE(Class, Base) |
| 6351 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 6352 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6353 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 6354 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 6355 | #include "clang/AST/TypeNodes.def" |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6356 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6357 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 6358 | case Type::LValueReference: |
| 6359 | case Type::RValueReference: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6360 | case Type::MemberPointer: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6361 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6362 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6363 | case Type::ObjCInterface: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6364 | case Type::IncompleteArray: |
| 6365 | case Type::VariableArray: |
| 6366 | case Type::FunctionProto: |
| 6367 | case Type::ExtVector: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6368 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6369 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6370 | case Type::Pointer: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6371 | { |
| 6372 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6373 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 6374 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6375 | if (Unqualified) { |
| 6376 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 6377 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 6378 | } |
| 6379 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 6380 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6381 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 6382 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6383 | return LHS; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 6384 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6385 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6386 | return getPointerType(ResultType); |
| 6387 | } |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 6388 | case Type::BlockPointer: |
| 6389 | { |
| 6390 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6391 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 6392 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6393 | if (Unqualified) { |
| 6394 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 6395 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 6396 | } |
| 6397 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 6398 | Unqualified); |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 6399 | if (ResultType.isNull()) return QualType(); |
| 6400 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 6401 | return LHS; |
| 6402 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 6403 | return RHS; |
| 6404 | return getBlockPointerType(ResultType); |
| 6405 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 6406 | case Type::Atomic: |
| 6407 | { |
| 6408 | // Merge two pointer types, while trying to preserve typedef info |
| 6409 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 6410 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 6411 | if (Unqualified) { |
| 6412 | LHSValue = LHSValue.getUnqualifiedType(); |
| 6413 | RHSValue = RHSValue.getUnqualifiedType(); |
| 6414 | } |
| 6415 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 6416 | Unqualified); |
| 6417 | if (ResultType.isNull()) return QualType(); |
| 6418 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 6419 | return LHS; |
| 6420 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 6421 | return RHS; |
| 6422 | return getAtomicType(ResultType); |
| 6423 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6424 | case Type::ConstantArray: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6425 | { |
| 6426 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 6427 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 6428 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 6429 | return QualType(); |
| 6430 | |
| 6431 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 6432 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6433 | if (Unqualified) { |
| 6434 | LHSElem = LHSElem.getUnqualifiedType(); |
| 6435 | RHSElem = RHSElem.getUnqualifiedType(); |
| 6436 | } |
| 6437 | |
| 6438 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6439 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6440 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6441 | return LHS; |
| 6442 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6443 | return RHS; |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6444 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 6445 | ArrayType::ArraySizeModifier(), 0); |
| 6446 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 6447 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6448 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 6449 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6450 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 6451 | return LHS; |
| 6452 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 6453 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6454 | if (LVAT) { |
| 6455 | // FIXME: This isn't correct! But tricky to implement because |
| 6456 | // the array's size has to be the size of LHS, but the type |
| 6457 | // has to be different. |
| 6458 | return LHS; |
| 6459 | } |
| 6460 | if (RVAT) { |
| 6461 | // FIXME: This isn't correct! But tricky to implement because |
| 6462 | // the array's size has to be the size of RHS, but the type |
| 6463 | // has to be different. |
| 6464 | return RHS; |
| 6465 | } |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 6466 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 6467 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 6468 | return getIncompleteArrayType(ResultType, |
| 6469 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6470 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6471 | case Type::FunctionNoProto: |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6472 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6473 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6474 | case Type::Enum: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6475 | return QualType(); |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 6476 | case Type::Builtin: |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6477 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6478 | return QualType(); |
Daniel Dunbar | 804c044 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 6479 | case Type::Complex: |
| 6480 | // Distinct complex types are incompatible. |
| 6481 | return QualType(); |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 6482 | case Type::Vector: |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6483 | // FIXME: The merged type should be an ExtVector! |
John McCall | 44c064b | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 6484 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 6485 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6486 | return LHS; |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6487 | return QualType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6488 | case Type::ObjCObject: { |
| 6489 | // Check if the types are assignment compatible. |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 6490 | // FIXME: This should be type compatibility, e.g. whether |
| 6491 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6492 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 6493 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 6494 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 7a7814c | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 6495 | return LHS; |
| 6496 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6497 | return QualType(); |
Cedric Venet | 4fc88b7 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 6498 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6499 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6500 | if (OfBlockPointer) { |
| 6501 | if (canAssignObjCInterfacesInBlockPointer( |
| 6502 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6503 | RHS->getAs<ObjCObjectPointerType>(), |
| 6504 | BlockReturnType)) |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6505 | return LHS; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6506 | return QualType(); |
| 6507 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6508 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 6509 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6510 | return LHS; |
| 6511 | |
Steve Naroff | c68cfcf | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 6512 | return QualType(); |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6513 | } |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6514 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6515 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 6516 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6517 | } |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 6518 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 6519 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 6520 | const FunctionProtoType *FromFunctionType, |
| 6521 | const FunctionProtoType *ToFunctionType) { |
| 6522 | if (FromFunctionType->hasAnyConsumedArgs() != |
| 6523 | ToFunctionType->hasAnyConsumedArgs()) |
| 6524 | return false; |
| 6525 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 6526 | FromFunctionType->getExtProtoInfo(); |
| 6527 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 6528 | ToFunctionType->getExtProtoInfo(); |
| 6529 | if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments) |
| 6530 | for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs(); |
| 6531 | ArgIdx != NumArgs; ++ArgIdx) { |
| 6532 | if (FromEPI.ConsumedArguments[ArgIdx] != |
| 6533 | ToEPI.ConsumedArguments[ArgIdx]) |
| 6534 | return false; |
| 6535 | } |
| 6536 | return true; |
| 6537 | } |
| 6538 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6539 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 6540 | /// 'RHS' attributes and returns the merged version; including for function |
| 6541 | /// return types. |
| 6542 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 6543 | QualType LHSCan = getCanonicalType(LHS), |
| 6544 | RHSCan = getCanonicalType(RHS); |
| 6545 | // If two types are identical, they are compatible. |
| 6546 | if (LHSCan == RHSCan) |
| 6547 | return LHS; |
| 6548 | if (RHSCan->isFunctionType()) { |
| 6549 | if (!LHSCan->isFunctionType()) |
| 6550 | return QualType(); |
| 6551 | QualType OldReturnType = |
| 6552 | cast<FunctionType>(RHSCan.getTypePtr())->getResultType(); |
| 6553 | QualType NewReturnType = |
| 6554 | cast<FunctionType>(LHSCan.getTypePtr())->getResultType(); |
| 6555 | QualType ResReturnType = |
| 6556 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 6557 | if (ResReturnType.isNull()) |
| 6558 | return QualType(); |
| 6559 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 6560 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 6561 | // In either case, use OldReturnType to build the new function type. |
| 6562 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 6563 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6564 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 6565 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6566 | QualType ResultType |
| 6567 | = getFunctionType(OldReturnType, FPT->arg_type_begin(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6568 | FPT->getNumArgs(), EPI); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 6569 | return ResultType; |
| 6570 | } |
| 6571 | } |
| 6572 | return QualType(); |
| 6573 | } |
| 6574 | |
| 6575 | // If the qualifiers are different, the types can still be merged. |
| 6576 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 6577 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 6578 | if (LQuals != RQuals) { |
| 6579 | // If any of these qualifiers are different, we have a type mismatch. |
| 6580 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 6581 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 6582 | return QualType(); |
| 6583 | |
| 6584 | // Exactly one GC qualifier difference is allowed: __strong is |
| 6585 | // okay if the other type has no GC qualifier but is an Objective |
| 6586 | // C object pointer (i.e. implicitly strong by default). We fix |
| 6587 | // this by pretending that the unqualified type was actually |
| 6588 | // qualified __strong. |
| 6589 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 6590 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 6591 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 6592 | |
| 6593 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 6594 | return QualType(); |
| 6595 | |
| 6596 | if (GC_L == Qualifiers::Strong) |
| 6597 | return LHS; |
| 6598 | if (GC_R == Qualifiers::Strong) |
| 6599 | return RHS; |
| 6600 | return QualType(); |
| 6601 | } |
| 6602 | |
| 6603 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 6604 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6605 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6606 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 6607 | if (ResQT == LHSBaseQT) |
| 6608 | return LHS; |
| 6609 | if (ResQT == RHSBaseQT) |
| 6610 | return RHS; |
| 6611 | } |
| 6612 | return QualType(); |
| 6613 | } |
| 6614 | |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 6615 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6616 | // Integer Predicates |
| 6617 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3c91971 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 6618 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6619 | unsigned ASTContext::getIntWidth(QualType T) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 6620 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
Eli Friedman | ee275c8 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 6621 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6622 | if (T->isBooleanType()) |
| 6623 | return 1; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 6624 | // For builtin types, just use the standard type sizing method |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6625 | return (unsigned)getTypeSize(T); |
| 6626 | } |
| 6627 | |
| 6628 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) { |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6629 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6630 | |
| 6631 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 6632 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 6633 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6634 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6635 | |
| 6636 | // For enums, we return the unsigned version of the base type. |
| 6637 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6638 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6639 | |
| 6640 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 6641 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6642 | switch (BTy->getKind()) { |
| 6643 | case BuiltinType::Char_S: |
| 6644 | case BuiltinType::SChar: |
| 6645 | return UnsignedCharTy; |
| 6646 | case BuiltinType::Short: |
| 6647 | return UnsignedShortTy; |
| 6648 | case BuiltinType::Int: |
| 6649 | return UnsignedIntTy; |
| 6650 | case BuiltinType::Long: |
| 6651 | return UnsignedLongTy; |
| 6652 | case BuiltinType::LongLong: |
| 6653 | return UnsignedLongLongTy; |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 6654 | case BuiltinType::Int128: |
| 6655 | return UnsignedInt128Ty; |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6656 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6657 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 6658 | } |
| 6659 | } |
| 6660 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 6661 | ASTMutationListener::~ASTMutationListener() { } |
| 6662 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6663 | |
| 6664 | //===----------------------------------------------------------------------===// |
| 6665 | // Builtin Type Computation |
| 6666 | //===----------------------------------------------------------------------===// |
| 6667 | |
| 6668 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6669 | /// pointer over the consumed characters. This returns the resultant type. If |
| 6670 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 6671 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 6672 | /// a vector of "i*". |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6673 | /// |
| 6674 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 6675 | /// to be an Integer Constant Expression. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6676 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6677 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6678 | bool &RequiresICE, |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6679 | bool AllowTypeModifiers) { |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6680 | // Modifiers. |
| 6681 | int HowLong = 0; |
| 6682 | bool Signed = false, Unsigned = false; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6683 | RequiresICE = false; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6684 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6685 | // Read the prefixed modifiers first. |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6686 | bool Done = false; |
| 6687 | while (!Done) { |
| 6688 | switch (*Str++) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6689 | default: Done = true; --Str; break; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6690 | case 'I': |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6691 | RequiresICE = true; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6692 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6693 | case 'S': |
| 6694 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 6695 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 6696 | Signed = true; |
| 6697 | break; |
| 6698 | case 'U': |
| 6699 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
| 6700 | assert(!Unsigned && "Can't use 'S' modifier multiple times!"); |
| 6701 | Unsigned = true; |
| 6702 | break; |
| 6703 | case 'L': |
| 6704 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 6705 | ++HowLong; |
| 6706 | break; |
| 6707 | } |
| 6708 | } |
| 6709 | |
| 6710 | QualType Type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6711 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6712 | // Read the base type. |
| 6713 | switch (*Str++) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6714 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6715 | case 'v': |
| 6716 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6717 | "Bad modifiers used with 'v'!"); |
| 6718 | Type = Context.VoidTy; |
| 6719 | break; |
| 6720 | case 'f': |
| 6721 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 6722 | "Bad modifiers used with 'f'!"); |
| 6723 | Type = Context.FloatTy; |
| 6724 | break; |
| 6725 | case 'd': |
| 6726 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 6727 | "Bad modifiers used with 'd'!"); |
| 6728 | if (HowLong) |
| 6729 | Type = Context.LongDoubleTy; |
| 6730 | else |
| 6731 | Type = Context.DoubleTy; |
| 6732 | break; |
| 6733 | case 's': |
| 6734 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 6735 | if (Unsigned) |
| 6736 | Type = Context.UnsignedShortTy; |
| 6737 | else |
| 6738 | Type = Context.ShortTy; |
| 6739 | break; |
| 6740 | case 'i': |
| 6741 | if (HowLong == 3) |
| 6742 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 6743 | else if (HowLong == 2) |
| 6744 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 6745 | else if (HowLong == 1) |
| 6746 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 6747 | else |
| 6748 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 6749 | break; |
| 6750 | case 'c': |
| 6751 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 6752 | if (Signed) |
| 6753 | Type = Context.SignedCharTy; |
| 6754 | else if (Unsigned) |
| 6755 | Type = Context.UnsignedCharTy; |
| 6756 | else |
| 6757 | Type = Context.CharTy; |
| 6758 | break; |
| 6759 | case 'b': // boolean |
| 6760 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 6761 | Type = Context.BoolTy; |
| 6762 | break; |
| 6763 | case 'z': // size_t. |
| 6764 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 6765 | Type = Context.getSizeType(); |
| 6766 | break; |
| 6767 | case 'F': |
| 6768 | Type = Context.getCFConstantStringType(); |
| 6769 | break; |
Fariborz Jahanian | d11da7e | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 6770 | case 'G': |
| 6771 | Type = Context.getObjCIdType(); |
| 6772 | break; |
| 6773 | case 'H': |
| 6774 | Type = Context.getObjCSelType(); |
| 6775 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6776 | case 'a': |
| 6777 | Type = Context.getBuiltinVaListType(); |
| 6778 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 6779 | break; |
| 6780 | case 'A': |
| 6781 | // This is a "reference" to a va_list; however, what exactly |
| 6782 | // this means depends on how va_list is defined. There are two |
| 6783 | // different kinds of va_list: ones passed by value, and ones |
| 6784 | // passed by reference. An example of a by-value va_list is |
| 6785 | // x86, where va_list is a char*. An example of by-ref va_list |
| 6786 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 6787 | // we want this argument to be a char*&; for x86-64, we want |
| 6788 | // it to be a __va_list_tag*. |
| 6789 | Type = Context.getBuiltinVaListType(); |
| 6790 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6791 | if (Type->isArrayType()) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6792 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6793 | else |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6794 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6795 | break; |
| 6796 | case 'V': { |
| 6797 | char *End; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6798 | unsigned NumElements = strtoul(Str, &End, 10); |
| 6799 | assert(End != Str && "Missing vector size"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6800 | Str = End; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6801 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6802 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 6803 | RequiresICE, false); |
| 6804 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6805 | |
| 6806 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 6807 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 6808 | VectorType::GenericVector); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6809 | break; |
| 6810 | } |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 6811 | case 'E': { |
| 6812 | char *End; |
| 6813 | |
| 6814 | unsigned NumElements = strtoul(Str, &End, 10); |
| 6815 | assert(End != Str && "Missing vector size"); |
| 6816 | |
| 6817 | Str = End; |
| 6818 | |
| 6819 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 6820 | false); |
| 6821 | Type = Context.getExtVectorType(ElementType, NumElements); |
| 6822 | break; |
| 6823 | } |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6824 | case 'X': { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6825 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 6826 | false); |
| 6827 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 6828 | Type = Context.getComplexType(ElementType); |
| 6829 | break; |
Fariborz Jahanian | 73952fc | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 6830 | } |
| 6831 | case 'Y' : { |
| 6832 | Type = Context.getPointerDiffType(); |
| 6833 | break; |
| 6834 | } |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6835 | case 'P': |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 6836 | Type = Context.getFILEType(); |
| 6837 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6838 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6839 | return QualType(); |
| 6840 | } |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6841 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 6842 | case 'J': |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6843 | if (Signed) |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6844 | Type = Context.getsigjmp_bufType(); |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6845 | else |
| 6846 | Type = Context.getjmp_bufType(); |
| 6847 | |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6848 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 6849 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 6850 | return QualType(); |
| 6851 | } |
| 6852 | break; |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 6853 | case 'K': |
| 6854 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 6855 | Type = Context.getucontext_tType(); |
| 6856 | |
| 6857 | if (Type.isNull()) { |
| 6858 | Error = ASTContext::GE_Missing_ucontext; |
| 6859 | return QualType(); |
| 6860 | } |
| 6861 | break; |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 6862 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6863 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6864 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 6865 | Done = !AllowTypeModifiers; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6866 | while (!Done) { |
John McCall | b8b9466 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 6867 | switch (char c = *Str++) { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6868 | default: Done = true; --Str; break; |
| 6869 | case '*': |
| 6870 | case '&': { |
| 6871 | // Both pointers and references can have their pointee types |
| 6872 | // qualified with an address space. |
| 6873 | char *End; |
| 6874 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 6875 | if (End != Str && AddrSpace != 0) { |
| 6876 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 6877 | Str = End; |
| 6878 | } |
| 6879 | if (c == '*') |
| 6880 | Type = Context.getPointerType(Type); |
| 6881 | else |
| 6882 | Type = Context.getLValueReferenceType(Type); |
| 6883 | break; |
| 6884 | } |
| 6885 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 6886 | case 'C': |
| 6887 | Type = Type.withConst(); |
| 6888 | break; |
| 6889 | case 'D': |
| 6890 | Type = Context.getVolatileType(Type); |
| 6891 | break; |
Ted Kremenek | f2a2f5f | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 6892 | case 'R': |
| 6893 | Type = Type.withRestrict(); |
| 6894 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6895 | } |
| 6896 | } |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6897 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6898 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 6899 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6900 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6901 | return Type; |
| 6902 | } |
| 6903 | |
| 6904 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6905 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6906 | GetBuiltinTypeError &Error, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6907 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 6908 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6909 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6910 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6911 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6912 | bool RequiresICE = false; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6913 | Error = GE_None; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6914 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 6915 | RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6916 | if (Error != GE_None) |
| 6917 | return QualType(); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6918 | |
| 6919 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 6920 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6921 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6922 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6923 | if (Error != GE_None) |
| 6924 | return QualType(); |
| 6925 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 6926 | // If this argument is required to be an IntegerConstantExpression and the |
| 6927 | // caller cares, fill in the bitmask we return. |
| 6928 | if (RequiresICE && IntegerConstantArgs) |
| 6929 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 6930 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6931 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 6932 | if (Ty->isArrayType()) |
| 6933 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6934 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6935 | ArgTypes.push_back(Ty); |
| 6936 | } |
| 6937 | |
| 6938 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 6939 | "'.' should only occur at end of builtin type list!"); |
| 6940 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6941 | FunctionType::ExtInfo EI; |
| 6942 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 6943 | |
| 6944 | bool Variadic = (TypeStr[0] == '.'); |
| 6945 | |
| 6946 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 6947 | if (ArgTypes.empty() && Variadic) |
| 6948 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 6949 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6950 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 6951 | EPI.ExtInfo = EI; |
| 6952 | EPI.Variadic = Variadic; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 6953 | |
| 6954 | return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 6955 | } |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 6956 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6957 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) { |
| 6958 | GVALinkage External = GVA_StrongExternal; |
| 6959 | |
| 6960 | Linkage L = FD->getLinkage(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6961 | switch (L) { |
| 6962 | case NoLinkage: |
| 6963 | case InternalLinkage: |
| 6964 | case UniqueExternalLinkage: |
| 6965 | return GVA_Internal; |
| 6966 | |
| 6967 | case ExternalLinkage: |
| 6968 | switch (FD->getTemplateSpecializationKind()) { |
| 6969 | case TSK_Undeclared: |
| 6970 | case TSK_ExplicitSpecialization: |
| 6971 | External = GVA_StrongExternal; |
| 6972 | break; |
| 6973 | |
| 6974 | case TSK_ExplicitInstantiationDefinition: |
| 6975 | return GVA_ExplicitTemplateInstantiation; |
| 6976 | |
| 6977 | case TSK_ExplicitInstantiationDeclaration: |
| 6978 | case TSK_ImplicitInstantiation: |
| 6979 | External = GVA_TemplateInstantiation; |
| 6980 | break; |
| 6981 | } |
| 6982 | } |
| 6983 | |
| 6984 | if (!FD->isInlined()) |
| 6985 | return External; |
| 6986 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6987 | if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 6988 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 6989 | // externally visible. |
| 6990 | if (FD->isInlineDefinitionExternallyVisible()) |
| 6991 | return External; |
| 6992 | |
| 6993 | // C99 inline semantics, where the symbol is not externally visible. |
| 6994 | return GVA_C99Inline; |
| 6995 | } |
| 6996 | |
| 6997 | // C++0x [temp.explicit]p9: |
| 6998 | // [ Note: The intent is that an inline function that is the subject of |
| 6999 | // an explicit instantiation declaration will still be implicitly |
| 7000 | // instantiated when used so that the body can be considered for |
| 7001 | // inlining, but that no out-of-line copy of the inline function would be |
| 7002 | // generated in the translation unit. -- end note ] |
| 7003 | if (FD->getTemplateSpecializationKind() |
| 7004 | == TSK_ExplicitInstantiationDeclaration) |
| 7005 | return GVA_C99Inline; |
| 7006 | |
| 7007 | return GVA_CXXInline; |
| 7008 | } |
| 7009 | |
| 7010 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 7011 | // If this is a static data member, compute the kind of template |
| 7012 | // specialization. Otherwise, this variable is not part of a |
| 7013 | // template. |
| 7014 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 7015 | if (VD->isStaticDataMember()) |
| 7016 | TSK = VD->getTemplateSpecializationKind(); |
| 7017 | |
| 7018 | Linkage L = VD->getLinkage(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7019 | if (L == ExternalLinkage && getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7020 | VD->getType()->getLinkage() == UniqueExternalLinkage) |
| 7021 | L = UniqueExternalLinkage; |
| 7022 | |
| 7023 | switch (L) { |
| 7024 | case NoLinkage: |
| 7025 | case InternalLinkage: |
| 7026 | case UniqueExternalLinkage: |
| 7027 | return GVA_Internal; |
| 7028 | |
| 7029 | case ExternalLinkage: |
| 7030 | switch (TSK) { |
| 7031 | case TSK_Undeclared: |
| 7032 | case TSK_ExplicitSpecialization: |
| 7033 | return GVA_StrongExternal; |
| 7034 | |
| 7035 | case TSK_ExplicitInstantiationDeclaration: |
| 7036 | llvm_unreachable("Variable should not be instantiated"); |
| 7037 | // Fall through to treat this like any other instantiation. |
| 7038 | |
| 7039 | case TSK_ExplicitInstantiationDefinition: |
| 7040 | return GVA_ExplicitTemplateInstantiation; |
| 7041 | |
| 7042 | case TSK_ImplicitInstantiation: |
| 7043 | return GVA_TemplateInstantiation; |
| 7044 | } |
| 7045 | } |
| 7046 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7047 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7048 | } |
| 7049 | |
Argyrios Kyrtzidis | c904933 | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 7050 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7051 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 7052 | if (!VD->isFileVarDecl()) |
| 7053 | return false; |
Argyrios Kyrtzidis | a98e861 | 2011-09-13 21:35:00 +0000 | [diff] [blame] | 7054 | } else if (!isa<FunctionDecl>(D)) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7055 | return false; |
| 7056 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7057 | // Weak references don't produce any output by themselves. |
| 7058 | if (D->hasAttr<WeakRefAttr>()) |
| 7059 | return false; |
| 7060 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7061 | // Aliases and used decls are required. |
| 7062 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 7063 | return true; |
| 7064 | |
| 7065 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 7066 | // Forward declarations aren't required. |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 7067 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 7068 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7069 | |
| 7070 | // Constructors and destructors are required. |
| 7071 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 7072 | return true; |
| 7073 | |
| 7074 | // The key function for a class is required. |
| 7075 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 7076 | const CXXRecordDecl *RD = MD->getParent(); |
| 7077 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 7078 | const CXXMethodDecl *KeyFunc = getKeyFunction(RD); |
| 7079 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 7080 | return true; |
| 7081 | } |
| 7082 | } |
| 7083 | |
| 7084 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 7085 | |
| 7086 | // static, static inline, always_inline, and extern inline functions can |
| 7087 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 7088 | // Implicit template instantiations can also be deferred in C++. |
| 7089 | if (Linkage == GVA_Internal || Linkage == GVA_C99Inline || |
Anton Yartsev | 79de4d4 | 2012-02-02 06:06:34 +0000 | [diff] [blame] | 7090 | Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7091 | return false; |
| 7092 | return true; |
| 7093 | } |
Douglas Gregor | 87d8124 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 7094 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7095 | const VarDecl *VD = cast<VarDecl>(D); |
| 7096 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 7097 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 7098 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly) |
| 7099 | return false; |
| 7100 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7101 | // Structs that have non-trivial constructors or destructors are required. |
| 7102 | |
| 7103 | // FIXME: Handle references. |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 7104 | // FIXME: Be more selective about which constructors we care about. |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7105 | if (const RecordType *RT = VD->getType()->getAs<RecordType>()) { |
| 7106 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 7107 | if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() && |
| 7108 | RD->hasTrivialCopyConstructor() && |
| 7109 | RD->hasTrivialMoveConstructor() && |
| 7110 | RD->hasTrivialDestructor())) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7111 | return true; |
| 7112 | } |
| 7113 | } |
| 7114 | |
| 7115 | GVALinkage L = GetGVALinkageForVariable(VD); |
| 7116 | if (L == GVA_Internal || L == GVA_TemplateInstantiation) { |
| 7117 | if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this))) |
| 7118 | return false; |
| 7119 | } |
| 7120 | |
| 7121 | return true; |
| 7122 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7123 | |
Timur Iskhodzhanov | c5098ad | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7124 | CallingConv ASTContext::getDefaultCXXMethodCallConv(bool isVariadic) { |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7125 | // Pass through to the C++ ABI object |
Timur Iskhodzhanov | c5098ad | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 7126 | return ABI->getDefaultMethodCallConv(isVariadic); |
| 7127 | } |
| 7128 | |
| 7129 | CallingConv ASTContext::getCanonicalCallConv(CallingConv CC) const { |
| 7130 | if (CC == CC_C && !LangOpts.MRTD && getTargetInfo().getCXXABI() != CXXABI_Microsoft) |
| 7131 | return CC_Default; |
| 7132 | return CC; |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 7133 | } |
| 7134 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7135 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 7136 | // Pass through to the C++ ABI object |
| 7137 | return ABI->isNearlyEmpty(RD); |
| 7138 | } |
| 7139 | |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7140 | MangleContext *ASTContext::createMangleContext() { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 7141 | switch (Target->getCXXABI()) { |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7142 | case CXXABI_ARM: |
| 7143 | case CXXABI_Itanium: |
| 7144 | return createItaniumMangleContext(*this, getDiagnostics()); |
| 7145 | case CXXABI_Microsoft: |
| 7146 | return createMicrosoftMangleContext(*this, getDiagnostics()); |
| 7147 | } |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7148 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 7149 | } |
| 7150 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 7151 | CXXABI::~CXXABI() {} |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 7152 | |
| 7153 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Ted Kremenek | 7d39c9a | 2011-07-27 18:41:12 +0000 | [diff] [blame] | 7154 | return ASTRecordLayouts.getMemorySize() |
| 7155 | + llvm::capacity_in_bytes(ObjCLayouts) |
| 7156 | + llvm::capacity_in_bytes(KeyFunctions) |
| 7157 | + llvm::capacity_in_bytes(ObjCImpls) |
| 7158 | + llvm::capacity_in_bytes(BlockVarCopyInits) |
| 7159 | + llvm::capacity_in_bytes(DeclAttrs) |
| 7160 | + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember) |
| 7161 | + llvm::capacity_in_bytes(InstantiatedFromUsingDecl) |
| 7162 | + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) |
| 7163 | + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) |
| 7164 | + llvm::capacity_in_bytes(OverriddenMethods) |
| 7165 | + llvm::capacity_in_bytes(Types) |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 7166 | + llvm::capacity_in_bytes(VariableArrayTypes) |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 7167 | + llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 7168 | } |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 7169 | |
Douglas Gregor | 6379854 | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 7170 | unsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) { |
| 7171 | CXXRecordDecl *Lambda = CallOperator->getParent(); |
| 7172 | return LambdaMangleContexts[Lambda->getDeclContext()] |
| 7173 | .getManglingNumber(CallOperator); |
| 7174 | } |
| 7175 | |
| 7176 | |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 7177 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 7178 | ParamIndices[D] = index; |
| 7179 | } |
| 7180 | |
| 7181 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 7182 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 7183 | assert(I != ParamIndices.end() && |
| 7184 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 7185 | return I->second; |
| 7186 | } |