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" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "CXXABI.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTMutationListener.h" |
| 17 | #include "clang/AST/Attr.h" |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 18 | #include "clang/AST/CharUnits.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/Comment.h" |
Dmitri Gribenko | ca7f80a | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 20 | #include "clang/AST/CommentCommandTraits.h" |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 67391b8 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExternalASTSource.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 27 | #include "clang/AST/Mangle.h" |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 28 | #include "clang/AST/MangleNumberingContext.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 29 | #include "clang/AST/RecordLayout.h" |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 30 | #include "clang/AST/RecursiveASTVisitor.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 31 | #include "clang/AST/TypeLoc.h" |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 32 | #include "clang/AST/VTableBuilder.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 33 | #include "clang/Basic/Builtins.h" |
Chris Lattner | d286851 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 34 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 35 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/StringExtras.h" |
Robert Lytton | eaf6f36 | 2013-11-12 10:09:34 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/Triple.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Capacity.h" |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 40 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 42 | #include <map> |
Anders Carlsson | a4267a6 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 43 | |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 44 | using namespace clang; |
| 45 | |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 46 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 47 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 48 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 49 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 50 | unsigned ASTContext::NumImplicitMoveConstructors; |
| 51 | unsigned ASTContext::NumImplicitMoveConstructorsDeclared; |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 52 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 53 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 54 | unsigned ASTContext::NumImplicitMoveAssignmentOperators; |
| 55 | unsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 56 | unsigned ASTContext::NumImplicitDestructors; |
| 57 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 58 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 59 | enum FloatingRank { |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 60 | HalfRank, FloatRank, DoubleRank, LongDoubleRank |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 63 | RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 64 | if (!CommentsLoaded && ExternalSource) { |
| 65 | ExternalSource->ReadComments(); |
Dmitri Gribenko | 9ee0e30 | 2014-03-27 15:40:39 +0000 | [diff] [blame] | 66 | |
| 67 | #ifndef NDEBUG |
| 68 | ArrayRef<RawComment *> RawComments = Comments.getComments(); |
| 69 | assert(std::is_sorted(RawComments.begin(), RawComments.end(), |
| 70 | BeforeThanCompare<RawComment>(SourceMgr))); |
| 71 | #endif |
| 72 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 73 | CommentsLoaded = true; |
| 74 | } |
| 75 | |
| 76 | assert(D); |
| 77 | |
Dmitri Gribenko | df17d64 | 2012-06-28 16:19:39 +0000 | [diff] [blame] | 78 | // User can not attach documentation to implicit declarations. |
| 79 | if (D->isImplicit()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 80 | return nullptr; |
Dmitri Gribenko | df17d64 | 2012-06-28 16:19:39 +0000 | [diff] [blame] | 81 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 82 | // User can not attach documentation to implicit instantiations. |
| 83 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 84 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 85 | return nullptr; |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 88 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 89 | if (VD->isStaticDataMember() && |
| 90 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 91 | return nullptr; |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) { |
| 95 | if (CRD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 96 | return nullptr; |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 99 | if (const ClassTemplateSpecializationDecl *CTSD = |
| 100 | dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 101 | TemplateSpecializationKind TSK = CTSD->getSpecializationKind(); |
| 102 | if (TSK == TSK_ImplicitInstantiation || |
| 103 | TSK == TSK_Undeclared) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 104 | return nullptr; |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 107 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 108 | if (ED->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 109 | return nullptr; |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 110 | } |
Fariborz Jahanian | 799a403 | 2013-04-17 21:05:20 +0000 | [diff] [blame] | 111 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 112 | // When tag declaration (but not definition!) is part of the |
| 113 | // decl-specifier-seq of some other declaration, it doesn't get comment |
| 114 | if (TD->isEmbeddedInDeclarator() && !TD->isCompleteDefinition()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 115 | return nullptr; |
Fariborz Jahanian | 799a403 | 2013-04-17 21:05:20 +0000 | [diff] [blame] | 116 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 117 | // TODO: handle comments for function parameters properly. |
| 118 | if (isa<ParmVarDecl>(D)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 119 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 120 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 121 | // TODO: we could look up template parameter documentation in the template |
| 122 | // documentation. |
| 123 | if (isa<TemplateTypeParmDecl>(D) || |
| 124 | isa<NonTypeTemplateParmDecl>(D) || |
| 125 | isa<TemplateTemplateParmDecl>(D)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 126 | return nullptr; |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 127 | |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 128 | ArrayRef<RawComment *> RawComments = Comments.getComments(); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 129 | |
| 130 | // If there are no comments anywhere, we won't find anything. |
| 131 | if (RawComments.empty()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 132 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 133 | |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 134 | // Find declaration location. |
| 135 | // For Objective-C declarations we generally don't expect to have multiple |
| 136 | // declarators, thus use declaration starting location as the "declaration |
| 137 | // location". |
| 138 | // For all other declarations multiple declarators are used quite frequently, |
| 139 | // so we use the location of the identifier as the "declaration location". |
| 140 | SourceLocation DeclLoc; |
| 141 | if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) || |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 142 | isa<ObjCPropertyDecl>(D) || |
Dmitri Gribenko | 7f4b377 | 2012-08-02 20:49:51 +0000 | [diff] [blame] | 143 | isa<RedeclarableTemplateDecl>(D) || |
| 144 | isa<ClassTemplateSpecializationDecl>(D)) |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 145 | DeclLoc = D->getLocStart(); |
Fariborz Jahanian | b64e95f | 2013-07-24 22:58:51 +0000 | [diff] [blame] | 146 | else { |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 147 | DeclLoc = D->getLocation(); |
Dmitri Gribenko | ef099dc | 2014-03-27 16:40:51 +0000 | [diff] [blame] | 148 | if (DeclLoc.isMacroID()) { |
| 149 | if (isa<TypedefDecl>(D)) { |
| 150 | // If location of the typedef name is in a macro, it is because being |
| 151 | // declared via a macro. Try using declaration's starting location as |
| 152 | // the "declaration location". |
| 153 | DeclLoc = D->getLocStart(); |
| 154 | } else if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 155 | // If location of the tag decl is inside a macro, but the spelling of |
| 156 | // the tag name comes from a macro argument, it looks like a special |
| 157 | // macro like NS_ENUM is being used to define the tag decl. In that |
| 158 | // case, adjust the source location to the expansion loc so that we can |
| 159 | // attach the comment to the tag decl. |
| 160 | if (SourceMgr.isMacroArgExpansion(DeclLoc) && |
| 161 | TD->isCompleteDefinition()) |
| 162 | DeclLoc = SourceMgr.getExpansionLoc(DeclLoc); |
| 163 | } |
| 164 | } |
Fariborz Jahanian | b64e95f | 2013-07-24 22:58:51 +0000 | [diff] [blame] | 165 | } |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 166 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 167 | // If the declaration doesn't map directly to a location in a file, we |
| 168 | // can't find the comment. |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 169 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 170 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 171 | |
| 172 | // Find the comment that occurs just after this declaration. |
Dmitri Gribenko | 82ea947 | 2012-07-17 22:01:09 +0000 | [diff] [blame] | 173 | ArrayRef<RawComment *>::iterator Comment; |
| 174 | { |
| 175 | // When searching for comments during parsing, the comment we are looking |
| 176 | // for is usually among the last two comments we parsed -- check them |
| 177 | // first. |
Dmitri Gribenko | a7d16ce | 2013-04-10 15:35:17 +0000 | [diff] [blame] | 178 | RawComment CommentAtDeclLoc( |
| 179 | SourceMgr, SourceRange(DeclLoc), false, |
| 180 | LangOpts.CommentOpts.ParseAllComments); |
Dmitri Gribenko | 82ea947 | 2012-07-17 22:01:09 +0000 | [diff] [blame] | 181 | BeforeThanCompare<RawComment> Compare(SourceMgr); |
| 182 | ArrayRef<RawComment *>::iterator MaybeBeforeDecl = RawComments.end() - 1; |
| 183 | bool Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); |
| 184 | if (!Found && RawComments.size() >= 2) { |
| 185 | MaybeBeforeDecl--; |
| 186 | Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); |
| 187 | } |
| 188 | |
| 189 | if (Found) { |
| 190 | Comment = MaybeBeforeDecl + 1; |
| 191 | assert(Comment == std::lower_bound(RawComments.begin(), RawComments.end(), |
| 192 | &CommentAtDeclLoc, Compare)); |
| 193 | } else { |
| 194 | // Slow path. |
| 195 | Comment = std::lower_bound(RawComments.begin(), RawComments.end(), |
| 196 | &CommentAtDeclLoc, Compare); |
| 197 | } |
| 198 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 199 | |
| 200 | // Decompose the location for the declaration and find the beginning of the |
| 201 | // file buffer. |
| 202 | std::pair<FileID, unsigned> DeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLoc); |
| 203 | |
| 204 | // First check whether we have a trailing comment. |
| 205 | if (Comment != RawComments.end() && |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 206 | (*Comment)->isDocumentation() && (*Comment)->isTrailingComment() && |
Fariborz Jahanian | fad2854 | 2013-08-06 23:29:00 +0000 | [diff] [blame] | 207 | (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D) || |
Fariborz Jahanian | 3ab6222 | 2013-08-07 16:40:29 +0000 | [diff] [blame] | 208 | isa<ObjCMethodDecl>(D) || isa<ObjCPropertyDecl>(D))) { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 209 | std::pair<FileID, unsigned> CommentBeginDecomp |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 210 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getBegin()); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 211 | // Check that Doxygen trailing comment comes after the declaration, starts |
| 212 | // on the same line and in the same file as the declaration. |
| 213 | if (DeclLocDecomp.first == CommentBeginDecomp.first && |
| 214 | SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second) |
| 215 | == SourceMgr.getLineNumber(CommentBeginDecomp.first, |
| 216 | CommentBeginDecomp.second)) { |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 217 | return *Comment; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | // The comment just after the declaration was not a trailing comment. |
| 222 | // Let's look at the previous comment. |
| 223 | if (Comment == RawComments.begin()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 224 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 225 | --Comment; |
| 226 | |
| 227 | // Check that we actually have a non-member Doxygen comment. |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 228 | if (!(*Comment)->isDocumentation() || (*Comment)->isTrailingComment()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 229 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 230 | |
| 231 | // Decompose the end of the comment. |
| 232 | std::pair<FileID, unsigned> CommentEndDecomp |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 233 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getEnd()); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 234 | |
| 235 | // If the comment and the declaration aren't in the same file, then they |
| 236 | // aren't related. |
| 237 | if (DeclLocDecomp.first != CommentEndDecomp.first) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 238 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 239 | |
| 240 | // Get the corresponding buffer. |
| 241 | bool Invalid = false; |
| 242 | const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first, |
| 243 | &Invalid).data(); |
| 244 | if (Invalid) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 245 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 246 | |
| 247 | // Extract text between the comment and declaration. |
| 248 | StringRef Text(Buffer + CommentEndDecomp.second, |
| 249 | DeclLocDecomp.second - CommentEndDecomp.second); |
| 250 | |
Dmitri Gribenko | 7e8729b | 2012-06-27 23:43:37 +0000 | [diff] [blame] | 251 | // There should be no other declarations or preprocessor directives between |
| 252 | // comment and declaration. |
Argyrios Kyrtzidis | b534d3a | 2013-07-26 18:38:12 +0000 | [diff] [blame] | 253 | if (Text.find_first_of(";{}#@") != StringRef::npos) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 254 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 255 | |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 256 | return *Comment; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 259 | namespace { |
| 260 | /// If we have a 'templated' declaration for a template, adjust 'D' to |
| 261 | /// refer to the actual template. |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 262 | /// If we have an implicit instantiation, adjust 'D' to refer to template. |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 263 | const Decl *adjustDeclToTemplate(const Decl *D) { |
Douglas Gregor | 35ceb27 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 264 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 265 | // Is this function declaration part of a function template? |
Douglas Gregor | 35ceb27 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 266 | if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate()) |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 267 | return FTD; |
| 268 | |
| 269 | // Nothing to do if function is not an implicit instantiation. |
| 270 | if (FD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
| 271 | return D; |
| 272 | |
| 273 | // Function is an implicit instantiation of a function template? |
| 274 | if (const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate()) |
| 275 | return FTD; |
| 276 | |
| 277 | // Function is instantiated from a member definition of a class template? |
| 278 | if (const FunctionDecl *MemberDecl = |
| 279 | FD->getInstantiatedFromMemberFunction()) |
| 280 | return MemberDecl; |
| 281 | |
| 282 | return D; |
Douglas Gregor | 35ceb27 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 283 | } |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 284 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 285 | // Static data member is instantiated from a member definition of a class |
| 286 | // template? |
| 287 | if (VD->isStaticDataMember()) |
| 288 | if (const VarDecl *MemberDecl = VD->getInstantiatedFromStaticDataMember()) |
| 289 | return MemberDecl; |
| 290 | |
| 291 | return D; |
| 292 | } |
| 293 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) { |
| 294 | // Is this class declaration part of a class template? |
| 295 | if (const ClassTemplateDecl *CTD = CRD->getDescribedClassTemplate()) |
| 296 | return CTD; |
| 297 | |
| 298 | // Class is an implicit instantiation of a class template or partial |
| 299 | // specialization? |
| 300 | if (const ClassTemplateSpecializationDecl *CTSD = |
| 301 | dyn_cast<ClassTemplateSpecializationDecl>(CRD)) { |
| 302 | if (CTSD->getSpecializationKind() != TSK_ImplicitInstantiation) |
| 303 | return D; |
| 304 | llvm::PointerUnion<ClassTemplateDecl *, |
| 305 | ClassTemplatePartialSpecializationDecl *> |
| 306 | PU = CTSD->getSpecializedTemplateOrPartial(); |
| 307 | return PU.is<ClassTemplateDecl*>() ? |
| 308 | static_cast<const Decl*>(PU.get<ClassTemplateDecl *>()) : |
| 309 | static_cast<const Decl*>( |
| 310 | PU.get<ClassTemplatePartialSpecializationDecl *>()); |
| 311 | } |
| 312 | |
| 313 | // Class is instantiated from a member definition of a class template? |
| 314 | if (const MemberSpecializationInfo *Info = |
| 315 | CRD->getMemberSpecializationInfo()) |
| 316 | return Info->getInstantiatedFrom(); |
| 317 | |
| 318 | return D; |
| 319 | } |
| 320 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 321 | // Enum is instantiated from a member definition of a class template? |
| 322 | if (const EnumDecl *MemberDecl = ED->getInstantiatedFromMemberEnum()) |
| 323 | return MemberDecl; |
| 324 | |
| 325 | return D; |
| 326 | } |
| 327 | // FIXME: Adjust alias templates? |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 328 | return D; |
| 329 | } |
| 330 | } // unnamed namespace |
| 331 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 332 | const RawComment *ASTContext::getRawCommentForAnyRedecl( |
| 333 | const Decl *D, |
| 334 | const Decl **OriginalDecl) const { |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 335 | D = adjustDeclToTemplate(D); |
Douglas Gregor | 35ceb27 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 336 | |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 337 | // Check whether we have cached a comment for this declaration already. |
| 338 | { |
| 339 | llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos = |
| 340 | RedeclComments.find(D); |
| 341 | if (Pos != RedeclComments.end()) { |
| 342 | const RawCommentAndCacheFlags &Raw = Pos->second; |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 343 | if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) { |
| 344 | if (OriginalDecl) |
| 345 | *OriginalDecl = Raw.getOriginalDecl(); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 346 | return Raw.getRaw(); |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 347 | } |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 348 | } |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 349 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 350 | |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 351 | // Search for comments attached to declarations in the redeclaration chain. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 352 | const RawComment *RC = nullptr; |
| 353 | const Decl *OriginalDeclForRC = nullptr; |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 354 | for (auto I : D->redecls()) { |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 355 | llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos = |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 356 | RedeclComments.find(I); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 357 | if (Pos != RedeclComments.end()) { |
| 358 | const RawCommentAndCacheFlags &Raw = Pos->second; |
| 359 | if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) { |
| 360 | RC = Raw.getRaw(); |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 361 | OriginalDeclForRC = Raw.getOriginalDecl(); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 362 | break; |
| 363 | } |
| 364 | } else { |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 365 | RC = getRawCommentForDeclNoCache(I); |
| 366 | OriginalDeclForRC = I; |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 367 | RawCommentAndCacheFlags Raw; |
| 368 | if (RC) { |
Will Wilson | f9de536 | 2015-10-27 17:01:10 +0000 | [diff] [blame] | 369 | // Call order swapped to work around ICE in VS2015 RTM (Release Win32) |
| 370 | // https://connect.microsoft.com/VisualStudio/feedback/details/1741530 |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 371 | Raw.setKind(RawCommentAndCacheFlags::FromDecl); |
Will Wilson | f9de536 | 2015-10-27 17:01:10 +0000 | [diff] [blame] | 372 | Raw.setRaw(RC); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 373 | } else |
| 374 | Raw.setKind(RawCommentAndCacheFlags::NoCommentInDecl); |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 375 | Raw.setOriginalDecl(I); |
| 376 | RedeclComments[I] = Raw; |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 377 | if (RC) |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | |
Dmitri Gribenko | 5c8897d | 2012-06-28 16:25:36 +0000 | [diff] [blame] | 382 | // If we found a comment, it should be a documentation comment. |
| 383 | assert(!RC || RC->isDocumentation()); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 384 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 385 | if (OriginalDecl) |
| 386 | *OriginalDecl = OriginalDeclForRC; |
| 387 | |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 388 | // Update cache for every declaration in the redeclaration chain. |
| 389 | RawCommentAndCacheFlags Raw; |
| 390 | Raw.setRaw(RC); |
| 391 | Raw.setKind(RawCommentAndCacheFlags::FromRedecl); |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 392 | Raw.setOriginalDecl(OriginalDeclForRC); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 393 | |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 394 | for (auto I : D->redecls()) { |
| 395 | RawCommentAndCacheFlags &R = RedeclComments[I]; |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 396 | if (R.getKind() == RawCommentAndCacheFlags::NoCommentInDecl) |
| 397 | R = Raw; |
| 398 | } |
| 399 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 400 | return RC; |
| 401 | } |
| 402 | |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 403 | static void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod, |
| 404 | SmallVectorImpl<const NamedDecl *> &Redeclared) { |
| 405 | const DeclContext *DC = ObjCMethod->getDeclContext(); |
| 406 | if (const ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(DC)) { |
| 407 | const ObjCInterfaceDecl *ID = IMD->getClassInterface(); |
| 408 | if (!ID) |
| 409 | return; |
| 410 | // Add redeclared method here. |
Aaron Ballman | b4a5345 | 2014-03-13 21:57:01 +0000 | [diff] [blame] | 411 | for (const auto *Ext : ID->known_extensions()) { |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 412 | if (ObjCMethodDecl *RedeclaredMethod = |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 413 | Ext->getMethod(ObjCMethod->getSelector(), |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 414 | ObjCMethod->isInstanceMethod())) |
| 415 | Redeclared.push_back(RedeclaredMethod); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 420 | comments::FullComment *ASTContext::cloneFullComment(comments::FullComment *FC, |
| 421 | const Decl *D) const { |
| 422 | comments::DeclInfo *ThisDeclInfo = new (*this) comments::DeclInfo; |
| 423 | ThisDeclInfo->CommentDecl = D; |
| 424 | ThisDeclInfo->IsFilled = false; |
| 425 | ThisDeclInfo->fill(); |
| 426 | ThisDeclInfo->CommentDecl = FC->getDecl(); |
Argyrios Kyrtzidis | 7daabbd | 2014-04-27 22:53:03 +0000 | [diff] [blame] | 427 | if (!ThisDeclInfo->TemplateParameters) |
| 428 | ThisDeclInfo->TemplateParameters = FC->getDeclInfo()->TemplateParameters; |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 429 | comments::FullComment *CFC = |
| 430 | new (*this) comments::FullComment(FC->getBlocks(), |
| 431 | ThisDeclInfo); |
| 432 | return CFC; |
| 433 | |
| 434 | } |
| 435 | |
Richard Smith | b39b9d5 | 2013-05-21 05:24:00 +0000 | [diff] [blame] | 436 | comments::FullComment *ASTContext::getLocalCommentForDeclUncached(const Decl *D) const { |
| 437 | const RawComment *RC = getRawCommentForDeclNoCache(D); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 438 | return RC ? RC->parse(*this, nullptr, D) : nullptr; |
Richard Smith | b39b9d5 | 2013-05-21 05:24:00 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 441 | comments::FullComment *ASTContext::getCommentForDecl( |
| 442 | const Decl *D, |
| 443 | const Preprocessor *PP) const { |
Fariborz Jahanian | 096f7c1 | 2013-05-13 17:27:00 +0000 | [diff] [blame] | 444 | if (D->isInvalidDecl()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 445 | return nullptr; |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 446 | D = adjustDeclToTemplate(D); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 447 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 448 | const Decl *Canonical = D->getCanonicalDecl(); |
| 449 | llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos = |
| 450 | ParsedComments.find(Canonical); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 451 | |
| 452 | if (Pos != ParsedComments.end()) { |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 453 | if (Canonical != D) { |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 454 | comments::FullComment *FC = Pos->second; |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 455 | comments::FullComment *CFC = cloneFullComment(FC, D); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 456 | return CFC; |
| 457 | } |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 458 | return Pos->second; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 461 | const Decl *OriginalDecl; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 462 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 463 | const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 464 | if (!RC) { |
| 465 | if (isa<ObjCMethodDecl>(D) || isa<FunctionDecl>(D)) { |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 466 | SmallVector<const NamedDecl*, 8> Overridden; |
Fariborz Jahanian | 37494a1 | 2013-01-12 00:28:34 +0000 | [diff] [blame] | 467 | const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 468 | if (OMD && OMD->isPropertyAccessor()) |
| 469 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) |
| 470 | if (comments::FullComment *FC = getCommentForDecl(PDecl, PP)) |
| 471 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 37494a1 | 2013-01-12 00:28:34 +0000 | [diff] [blame] | 472 | if (OMD) |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 473 | addRedeclaredMethods(OMD, Overridden); |
| 474 | getOverriddenMethods(dyn_cast<NamedDecl>(D), Overridden); |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 475 | for (unsigned i = 0, e = Overridden.size(); i < e; i++) |
| 476 | if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP)) |
| 477 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 478 | } |
Fariborz Jahanian | 6384fbb | 2013-05-02 15:44:16 +0000 | [diff] [blame] | 479 | else if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 480 | // Attach any tag type's documentation to its typedef if latter |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 481 | // does not have one of its own. |
Fariborz Jahanian | 40abf34 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 482 | QualType QT = TD->getUnderlyingType(); |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 483 | if (const TagType *TT = QT->getAs<TagType>()) |
| 484 | if (const Decl *TD = TT->getDecl()) |
| 485 | if (comments::FullComment *FC = getCommentForDecl(TD, PP)) |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 486 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 40abf34 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 487 | } |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 488 | else if (const ObjCInterfaceDecl *IC = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 489 | while (IC->getSuperClass()) { |
| 490 | IC = IC->getSuperClass(); |
| 491 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
| 492 | return cloneFullComment(FC, D); |
| 493 | } |
| 494 | } |
| 495 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
| 496 | if (const ObjCInterfaceDecl *IC = CD->getClassInterface()) |
| 497 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
| 498 | return cloneFullComment(FC, D); |
| 499 | } |
| 500 | else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 501 | if (!(RD = RD->getDefinition())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 502 | return nullptr; |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 503 | // Check non-virtual bases. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 504 | for (const auto &I : RD->bases()) { |
| 505 | if (I.isVirtual() || (I.getAccessSpecifier() != AS_public)) |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 506 | continue; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 507 | QualType Ty = I.getType(); |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 508 | if (Ty.isNull()) |
| 509 | continue; |
| 510 | if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) { |
| 511 | if (!(NonVirtualBase= NonVirtualBase->getDefinition())) |
| 512 | continue; |
| 513 | |
| 514 | if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP)) |
| 515 | return cloneFullComment(FC, D); |
| 516 | } |
| 517 | } |
| 518 | // Check virtual bases. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 519 | for (const auto &I : RD->vbases()) { |
| 520 | if (I.getAccessSpecifier() != AS_public) |
Fariborz Jahanian | 5a2e4a2 | 2013-04-26 23:34:36 +0000 | [diff] [blame] | 521 | continue; |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 522 | QualType Ty = I.getType(); |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 523 | if (Ty.isNull()) |
| 524 | continue; |
| 525 | if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) { |
| 526 | if (!(VirtualBase= VirtualBase->getDefinition())) |
| 527 | continue; |
| 528 | if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP)) |
| 529 | return cloneFullComment(FC, D); |
| 530 | } |
| 531 | } |
| 532 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 533 | return nullptr; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Dmitri Gribenko | bfda9f7 | 2012-08-22 18:12:19 +0000 | [diff] [blame] | 536 | // If the RawComment was attached to other redeclaration of this Decl, we |
| 537 | // should parse the comment in context of that other Decl. This is important |
| 538 | // because comments can contain references to parameter names which can be |
| 539 | // different across redeclarations. |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 540 | if (D != OriginalDecl) |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 541 | return getCommentForDecl(OriginalDecl, PP); |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 542 | |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 543 | comments::FullComment *FC = RC->parse(*this, PP, D); |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 544 | ParsedComments[Canonical] = FC; |
| 545 | return FC; |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 548 | void |
| 549 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 550 | TemplateTemplateParmDecl *Parm) { |
| 551 | ID.AddInteger(Parm->getDepth()); |
| 552 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 553 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 554 | |
| 555 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 556 | ID.AddInteger(Params->size()); |
| 557 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 558 | PEnd = Params->end(); |
| 559 | P != PEnd; ++P) { |
| 560 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 561 | ID.AddInteger(0); |
| 562 | ID.AddBoolean(TTP->isParameterPack()); |
| 563 | continue; |
| 564 | } |
| 565 | |
| 566 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 567 | ID.AddInteger(1); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 568 | ID.AddBoolean(NTTP->isParameterPack()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 569 | ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 570 | if (NTTP->isExpandedParameterPack()) { |
| 571 | ID.AddBoolean(true); |
| 572 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 573 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 574 | QualType T = NTTP->getExpansionType(I); |
| 575 | ID.AddPointer(T.getCanonicalType().getAsOpaquePtr()); |
| 576 | } |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 577 | } else |
| 578 | ID.AddBoolean(false); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 579 | continue; |
| 580 | } |
| 581 | |
| 582 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 583 | ID.AddInteger(2); |
| 584 | Profile(ID, TTP); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | TemplateTemplateParmDecl * |
| 589 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 590 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 591 | // Check if we already have a canonical template template parameter. |
| 592 | llvm::FoldingSetNodeID ID; |
| 593 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 594 | void *InsertPos = nullptr; |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 595 | CanonicalTemplateTemplateParm *Canonical |
| 596 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 597 | if (Canonical) |
| 598 | return Canonical->getParam(); |
| 599 | |
| 600 | // Build a canonical template parameter list. |
| 601 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 602 | SmallVector<NamedDecl *, 4> CanonParams; |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 603 | CanonParams.reserve(Params->size()); |
| 604 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 605 | PEnd = Params->end(); |
| 606 | P != PEnd; ++P) { |
| 607 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 608 | CanonParams.push_back( |
| 609 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 610 | SourceLocation(), |
| 611 | SourceLocation(), |
| 612 | TTP->getDepth(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 613 | TTP->getIndex(), nullptr, false, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 614 | TTP->isParameterPack())); |
| 615 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 616 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 617 | QualType T = getCanonicalType(NTTP->getType()); |
| 618 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 619 | NonTypeTemplateParmDecl *Param; |
| 620 | if (NTTP->isExpandedParameterPack()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 621 | SmallVector<QualType, 2> ExpandedTypes; |
| 622 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 623 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 624 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 625 | ExpandedTInfos.push_back( |
| 626 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 627 | } |
| 628 | |
| 629 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 630 | SourceLocation(), |
| 631 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 632 | NTTP->getDepth(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 633 | NTTP->getPosition(), nullptr, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 634 | T, |
| 635 | TInfo, |
| 636 | ExpandedTypes.data(), |
| 637 | ExpandedTypes.size(), |
| 638 | ExpandedTInfos.data()); |
| 639 | } else { |
| 640 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 641 | SourceLocation(), |
| 642 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 643 | NTTP->getDepth(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 644 | NTTP->getPosition(), nullptr, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 645 | T, |
| 646 | NTTP->isParameterPack(), |
| 647 | TInfo); |
| 648 | } |
| 649 | CanonParams.push_back(Param); |
| 650 | |
| 651 | } else |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 652 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 653 | cast<TemplateTemplateParmDecl>(*P))); |
| 654 | } |
| 655 | |
| 656 | TemplateTemplateParmDecl *CanonTTP |
| 657 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 658 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 659 | TTP->getPosition(), |
| 660 | TTP->isParameterPack(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 661 | nullptr, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 662 | TemplateParameterList::Create(*this, SourceLocation(), |
| 663 | SourceLocation(), |
| 664 | CanonParams.data(), |
| 665 | CanonParams.size(), |
| 666 | SourceLocation())); |
| 667 | |
| 668 | // Get the new insert position for the node we care about. |
| 669 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 670 | assert(!Canonical && "Shouldn't be in the map!"); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 671 | (void)Canonical; |
| 672 | |
| 673 | // Create the canonical template template parameter entry. |
| 674 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 675 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 676 | return CanonTTP; |
| 677 | } |
| 678 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 679 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 680 | if (!LangOpts.CPlusPlus) return nullptr; |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 681 | |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 682 | switch (T.getCXXABI().getKind()) { |
Joerg Sonnenberger | daa13aa | 2014-05-13 11:20:16 +0000 | [diff] [blame] | 683 | case TargetCXXABI::GenericARM: // Same as Itanium at this level |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 684 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 685 | case TargetCXXABI::iOS64: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 686 | case TargetCXXABI::WatchOS: |
Joerg Sonnenberger | daa13aa | 2014-05-13 11:20:16 +0000 | [diff] [blame] | 687 | case TargetCXXABI::GenericAArch64: |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 688 | case TargetCXXABI::GenericMIPS: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 689 | case TargetCXXABI::GenericItanium: |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 690 | case TargetCXXABI::WebAssembly: |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 691 | return CreateItaniumCXXABI(*this); |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 692 | case TargetCXXABI::Microsoft: |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 693 | return CreateMicrosoftCXXABI(*this); |
| 694 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 695 | llvm_unreachable("Invalid CXXABI type!"); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 698 | static const LangAS::Map *getAddressSpaceMap(const TargetInfo &T, |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 699 | const LangOptions &LOpts) { |
| 700 | if (LOpts.FakeAddressSpaceMap) { |
| 701 | // The fake address space map must have a distinct entry for each |
| 702 | // language-specific address space. |
| 703 | static const unsigned FakeAddrSpaceMap[] = { |
| 704 | 1, // opencl_global |
| 705 | 2, // opencl_local |
Peter Collingbourne | f44bdf9 | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 706 | 3, // opencl_constant |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 707 | 4, // opencl_generic |
| 708 | 5, // cuda_device |
| 709 | 6, // cuda_constant |
| 710 | 7 // cuda_shared |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 711 | }; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 712 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 713 | } else { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 714 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 718 | static bool isAddrSpaceMapManglingEnabled(const TargetInfo &TI, |
| 719 | const LangOptions &LangOpts) { |
| 720 | switch (LangOpts.getAddressSpaceMapMangling()) { |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 721 | case LangOptions::ASMM_Target: |
| 722 | return TI.useAddressSpaceMapMangling(); |
| 723 | case LangOptions::ASMM_On: |
| 724 | return true; |
| 725 | case LangOptions::ASMM_Off: |
| 726 | return false; |
| 727 | } |
NAKAMURA Takumi | 5c81ca4 | 2013-09-13 17:12:09 +0000 | [diff] [blame] | 728 | llvm_unreachable("getAddressSpaceMapMangling() doesn't cover anything."); |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Alexey Samsonov | 0b15e34 | 2014-10-15 22:17:27 +0000 | [diff] [blame] | 731 | ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM, |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 732 | IdentifierTable &idents, SelectorTable &sels, |
Alp Toker | 0804343 | 2014-05-03 03:46:04 +0000 | [diff] [blame] | 733 | Builtin::Context &builtins) |
Alexey Samsonov | 0b15e34 | 2014-10-15 22:17:27 +0000 | [diff] [blame] | 734 | : FunctionProtoTypes(this_()), TemplateSpecializationTypes(this_()), |
| 735 | DependentTemplateSpecializationTypes(this_()), |
| 736 | SubstTemplateTemplateParmPacks(this_()), |
| 737 | GlobalNestedNameSpecifier(nullptr), Int128Decl(nullptr), |
| 738 | UInt128Decl(nullptr), Float128StubDecl(nullptr), |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 739 | BuiltinVaListDecl(nullptr), BuiltinMSVaListDecl(nullptr), |
| 740 | ObjCIdDecl(nullptr), ObjCSelDecl(nullptr), ObjCClassDecl(nullptr), |
| 741 | ObjCProtocolClassDecl(nullptr), BOOLDecl(nullptr), |
Alexey Samsonov | 0b15e34 | 2014-10-15 22:17:27 +0000 | [diff] [blame] | 742 | CFConstantStringTypeDecl(nullptr), ObjCInstanceTypeDecl(nullptr), |
| 743 | FILEDecl(nullptr), jmp_bufDecl(nullptr), sigjmp_bufDecl(nullptr), |
| 744 | ucontext_tDecl(nullptr), BlockDescriptorType(nullptr), |
| 745 | BlockDescriptorExtendedType(nullptr), cudaConfigureCallDecl(nullptr), |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 746 | FirstLocalImport(), LastLocalImport(), ExternCContext(nullptr), |
| 747 | SourceMgr(SM), LangOpts(LOpts), |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 748 | SanitizerBL(new SanitizerBlacklist(LangOpts.SanitizerBlacklistFiles, SM)), |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 749 | AddrSpaceMap(nullptr), Target(nullptr), AuxTarget(nullptr), |
| 750 | PrintingPolicy(LOpts), Idents(idents), Selectors(sels), |
| 751 | BuiltinInfo(builtins), DeclarationNames(*this), ExternalSource(nullptr), |
| 752 | Listener(nullptr), Comments(SM), CommentsLoaded(false), |
Alexey Samsonov | 0b15e34 | 2014-10-15 22:17:27 +0000 | [diff] [blame] | 753 | CommentCommandTraits(BumpAlloc, LOpts.CommentOpts), LastSDM(nullptr, 0) { |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 754 | TUDecl = TranslationUnitDecl::Create(*this); |
| 755 | } |
| 756 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 757 | ASTContext::~ASTContext() { |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 758 | ReleaseParentMapEntries(); |
| 759 | |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 760 | // Release the DenseMaps associated with DeclContext objects. |
| 761 | // FIXME: Is this the ideal solution? |
| 762 | ReleaseDeclContextMaps(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 763 | |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 764 | // Call all of the deallocation functions on all of their targets. |
| 765 | for (DeallocationMap::const_iterator I = Deallocations.begin(), |
| 766 | E = Deallocations.end(); I != E; ++I) |
| 767 | for (unsigned J = 0, N = I->second.size(); J != N; ++J) |
| 768 | (I->first)((I->second)[J]); |
| 769 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 770 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 771 | // because they can contain DenseMaps. |
| 772 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 773 | const ASTRecordLayout*>::iterator |
| 774 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 775 | // Increment in loop to prevent using deallocated memory. |
| 776 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 777 | R->Destroy(*this); |
| 778 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 779 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 780 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 781 | // Increment in loop to prevent using deallocated memory. |
| 782 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 783 | R->Destroy(*this); |
| 784 | } |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 785 | |
| 786 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 787 | AEnd = DeclAttrs.end(); |
| 788 | A != AEnd; ++A) |
| 789 | A->second->~AttrVec(); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 790 | |
David Majnemer | e694f3e | 2015-08-14 14:43:50 +0000 | [diff] [blame] | 791 | for (std::pair<const MaterializeTemporaryExpr *, APValue *> &MTVPair : |
| 792 | MaterializedTemporaryValues) |
| 793 | MTVPair.second->~APValue(); |
| 794 | |
Reid Kleckner | 588c937 | 2014-02-19 23:44:52 +0000 | [diff] [blame] | 795 | llvm::DeleteContainerSeconds(MangleNumberingContexts); |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 796 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 797 | |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 798 | void ASTContext::ReleaseParentMapEntries() { |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 799 | if (!PointerParents) return; |
| 800 | for (const auto &Entry : *PointerParents) { |
| 801 | if (Entry.second.is<ast_type_traits::DynTypedNode *>()) { |
| 802 | delete Entry.second.get<ast_type_traits::DynTypedNode *>(); |
| 803 | } else if (Entry.second.is<ParentVector *>()) { |
| 804 | delete Entry.second.get<ParentVector *>(); |
| 805 | } |
| 806 | } |
| 807 | for (const auto &Entry : *OtherParents) { |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 808 | if (Entry.second.is<ast_type_traits::DynTypedNode *>()) { |
| 809 | delete Entry.second.get<ast_type_traits::DynTypedNode *>(); |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 810 | } else if (Entry.second.is<ParentVector *>()) { |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 811 | delete Entry.second.get<ParentVector *>(); |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 816 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 817 | Deallocations[Callback].push_back(Data); |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 820 | void |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 821 | ASTContext::setExternalSource(IntrusiveRefCntPtr<ExternalASTSource> Source) { |
| 822 | ExternalSource = Source; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 825 | void ASTContext::PrintStats() const { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 826 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 827 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 828 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 829 | unsigned counts[] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 830 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 831 | #define ABSTRACT_TYPE(Name, Parent) |
| 832 | #include "clang/AST/TypeNodes.def" |
| 833 | 0 // Extra |
| 834 | }; |
Douglas Gregor | b1fe2c9 | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 835 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 836 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 837 | Type *T = Types[i]; |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 838 | counts[(unsigned)T->getTypeClass()]++; |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 841 | unsigned Idx = 0; |
| 842 | unsigned TotalBytes = 0; |
| 843 | #define TYPE(Name, Parent) \ |
| 844 | if (counts[Idx]) \ |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 845 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 846 | << " types\n"; \ |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 847 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 848 | ++Idx; |
| 849 | #define ABSTRACT_TYPE(Name, Parent) |
| 850 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 851 | |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 852 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 853 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 854 | // Implicit special member functions. |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 855 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 856 | << NumImplicitDefaultConstructors |
| 857 | << " implicit default constructors created\n"; |
| 858 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 859 | << NumImplicitCopyConstructors |
| 860 | << " implicit copy constructors created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 861 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 862 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 863 | << NumImplicitMoveConstructors |
| 864 | << " implicit move constructors created\n"; |
| 865 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 866 | << NumImplicitCopyAssignmentOperators |
| 867 | << " implicit copy assignment operators created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 868 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 869 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 870 | << NumImplicitMoveAssignmentOperators |
| 871 | << " implicit move assignment operators created\n"; |
| 872 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 873 | << NumImplicitDestructors |
| 874 | << " implicit destructors created\n"; |
| 875 | |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 876 | if (ExternalSource) { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 877 | llvm::errs() << "\n"; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 878 | ExternalSource->PrintStats(); |
| 879 | } |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 880 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 881 | BumpAlloc.PrintStats(); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 884 | void ASTContext::mergeDefinitionIntoModule(NamedDecl *ND, Module *M, |
| 885 | bool NotifyListeners) { |
| 886 | if (NotifyListeners) |
| 887 | if (auto *Listener = getASTMutationListener()) |
| 888 | Listener->RedefinedHiddenDefinition(ND, M); |
| 889 | |
| 890 | if (getLangOpts().ModulesLocalVisibility) |
| 891 | MergedDefModules[ND].push_back(M); |
| 892 | else |
| 893 | ND->setHidden(false); |
| 894 | } |
| 895 | |
| 896 | void ASTContext::deduplicateMergedDefinitonsFor(NamedDecl *ND) { |
| 897 | auto It = MergedDefModules.find(ND); |
| 898 | if (It == MergedDefModules.end()) |
| 899 | return; |
| 900 | |
| 901 | auto &Merged = It->second; |
| 902 | llvm::DenseSet<Module*> Found; |
| 903 | for (Module *&M : Merged) |
| 904 | if (!Found.insert(M).second) |
| 905 | M = nullptr; |
| 906 | Merged.erase(std::remove(Merged.begin(), Merged.end(), nullptr), Merged.end()); |
| 907 | } |
| 908 | |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 909 | ExternCContextDecl *ASTContext::getExternCContextDecl() const { |
| 910 | if (!ExternCContext) |
| 911 | ExternCContext = ExternCContextDecl::Create(*this, getTranslationUnitDecl()); |
| 912 | |
| 913 | return ExternCContext; |
| 914 | } |
| 915 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 916 | RecordDecl *ASTContext::buildImplicitRecord(StringRef Name, |
| 917 | RecordDecl::TagKind TK) const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 918 | SourceLocation Loc; |
| 919 | RecordDecl *NewDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 920 | if (getLangOpts().CPlusPlus) |
| 921 | NewDecl = CXXRecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, |
| 922 | Loc, &Idents.get(Name)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 923 | else |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 924 | NewDecl = RecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, Loc, |
| 925 | &Idents.get(Name)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 926 | NewDecl->setImplicit(); |
David Majnemer | f863736 | 2015-01-15 08:41:25 +0000 | [diff] [blame] | 927 | NewDecl->addAttr(TypeVisibilityAttr::CreateImplicit( |
| 928 | const_cast<ASTContext &>(*this), TypeVisibilityAttr::Default)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 929 | return NewDecl; |
| 930 | } |
| 931 | |
| 932 | TypedefDecl *ASTContext::buildImplicitTypedef(QualType T, |
| 933 | StringRef Name) const { |
| 934 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 935 | TypedefDecl *NewDecl = TypedefDecl::Create( |
| 936 | const_cast<ASTContext &>(*this), getTranslationUnitDecl(), |
| 937 | SourceLocation(), SourceLocation(), &Idents.get(Name), TInfo); |
| 938 | NewDecl->setImplicit(); |
| 939 | return NewDecl; |
| 940 | } |
| 941 | |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 942 | TypedefDecl *ASTContext::getInt128Decl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 943 | if (!Int128Decl) |
| 944 | Int128Decl = buildImplicitTypedef(Int128Ty, "__int128_t"); |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 945 | return Int128Decl; |
| 946 | } |
| 947 | |
| 948 | TypedefDecl *ASTContext::getUInt128Decl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 949 | if (!UInt128Decl) |
| 950 | UInt128Decl = buildImplicitTypedef(UnsignedInt128Ty, "__uint128_t"); |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 951 | return UInt128Decl; |
| 952 | } |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 953 | |
Nico Weber | e1687c5 | 2013-06-20 21:44:55 +0000 | [diff] [blame] | 954 | TypeDecl *ASTContext::getFloat128StubType() const { |
Nico Weber | 5b0b46f | 2013-06-21 01:29:36 +0000 | [diff] [blame] | 955 | assert(LangOpts.CPlusPlus && "should only be called for c++"); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 956 | if (!Float128StubDecl) |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 957 | Float128StubDecl = buildImplicitRecord("__float128"); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 958 | |
Nico Weber | e1687c5 | 2013-06-20 21:44:55 +0000 | [diff] [blame] | 959 | return Float128StubDecl; |
| 960 | } |
| 961 | |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 962 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 963 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 964 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 965 | Types.push_back(Ty); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 968 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target, |
| 969 | const TargetInfo *AuxTarget) { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 970 | assert((!this->Target || this->Target == &Target) && |
| 971 | "Incorrect target reinitialization"); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 972 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 973 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 974 | this->Target = &Target; |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 975 | this->AuxTarget = AuxTarget; |
| 976 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 977 | ABI.reset(createCXXABI(Target)); |
| 978 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 979 | AddrSpaceMapMangling = isAddrSpaceMapManglingEnabled(Target, LangOpts); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 980 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 981 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 982 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 983 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 984 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 985 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 986 | // C99 6.2.5p3. |
Eli Friedman | 9ffd4a9 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 987 | if (LangOpts.CharIsSigned) |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 988 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 989 | else |
| 990 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 991 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 992 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 993 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 994 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 995 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 996 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 998 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 999 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 1000 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 1001 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 1002 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 1003 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1005 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1006 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 1007 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 1008 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 1010 | // GNU extension, 128-bit integers. |
| 1011 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 1012 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 1013 | |
Hans Wennborg | 0d81e01 | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 1014 | // C++ 3.9.1p5 |
| 1015 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
| 1016 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 1017 | else // -fshort-wchar makes wchar_t be unsigned. |
| 1018 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 1019 | if (LangOpts.CPlusPlus && LangOpts.WChar) |
| 1020 | WideCharTy = WCharTy; |
| 1021 | else { |
| 1022 | // C99 (or C++ using -fno-wchar). |
| 1023 | WideCharTy = getFromTargetType(Target.getWCharType()); |
| 1024 | } |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1025 | |
James Molloy | 3636554 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 1026 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 1027 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1028 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 1029 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 1030 | else // C99 |
| 1031 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 1032 | |
| 1033 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 1034 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 1035 | else // C99 |
| 1036 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 1037 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1038 | // Placeholder type for type-dependent expressions whose type is |
| 1039 | // completely unknown. No code should ever check a type against |
| 1040 | // DependentTy and users should never see it; however, it is here to |
| 1041 | // help diagnose failures to properly check for type-dependent |
| 1042 | // expressions. |
| 1043 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1044 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 1045 | // Placeholder type for functions. |
| 1046 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 1047 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1048 | // Placeholder type for bound members. |
| 1049 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 1050 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1051 | // Placeholder type for pseudo-objects. |
| 1052 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 1053 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 1054 | // "any" type; useful for debugger-like clients. |
| 1055 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 1056 | |
John McCall | 8a6b59a | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 1057 | // Placeholder type for unbridged ARC casts. |
| 1058 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 1059 | |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 1060 | // Placeholder type for builtin functions. |
| 1061 | InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn); |
| 1062 | |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 1063 | // Placeholder type for OMP array sections. |
| 1064 | if (LangOpts.OpenMP) |
| 1065 | InitBuiltinType(OMPArraySectionTy, BuiltinType::OMPArraySection); |
| 1066 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1067 | // C99 6.2.5p11. |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1068 | FloatComplexTy = getComplexType(FloatTy); |
| 1069 | DoubleComplexTy = getComplexType(DoubleTy); |
| 1070 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1071 | |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1072 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1073 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 1074 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1075 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1076 | |
| 1077 | if (LangOpts.OpenCL) { |
| 1078 | InitBuiltinType(OCLImage1dTy, BuiltinType::OCLImage1d); |
| 1079 | InitBuiltinType(OCLImage1dArrayTy, BuiltinType::OCLImage1dArray); |
| 1080 | InitBuiltinType(OCLImage1dBufferTy, BuiltinType::OCLImage1dBuffer); |
| 1081 | InitBuiltinType(OCLImage2dTy, BuiltinType::OCLImage2d); |
| 1082 | InitBuiltinType(OCLImage2dArrayTy, BuiltinType::OCLImage2dArray); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1083 | InitBuiltinType(OCLImage2dDepthTy, BuiltinType::OCLImage2dDepth); |
| 1084 | InitBuiltinType(OCLImage2dArrayDepthTy, BuiltinType::OCLImage2dArrayDepth); |
| 1085 | InitBuiltinType(OCLImage2dMSAATy, BuiltinType::OCLImage2dMSAA); |
| 1086 | InitBuiltinType(OCLImage2dArrayMSAATy, BuiltinType::OCLImage2dArrayMSAA); |
| 1087 | InitBuiltinType(OCLImage2dMSAADepthTy, BuiltinType::OCLImage2dMSAADepth); |
| 1088 | InitBuiltinType(OCLImage2dArrayMSAADepthTy, |
| 1089 | BuiltinType::OCLImage2dArrayMSAADepth); |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1090 | InitBuiltinType(OCLImage3dTy, BuiltinType::OCLImage3d); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1091 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1092 | InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1093 | InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1094 | InitBuiltinType(OCLClkEventTy, BuiltinType::OCLClkEvent); |
| 1095 | InitBuiltinType(OCLQueueTy, BuiltinType::OCLQueue); |
| 1096 | InitBuiltinType(OCLNDRangeTy, BuiltinType::OCLNDRange); |
| 1097 | InitBuiltinType(OCLReserveIDTy, BuiltinType::OCLReserveID); |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1098 | } |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1099 | |
| 1100 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 29898f4 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 1101 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 1102 | SignedCharTy : BoolTy); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1103 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1104 | ObjCConstantStringType = QualType(); |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1105 | |
| 1106 | ObjCSuperType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1108 | // void * type |
| 1109 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1110 | |
| 1111 | // nullptr type (C++0x 2.14.7) |
| 1112 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1113 | |
| 1114 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 1115 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 1116 | |
| 1117 | // Builtin type used to help define __builtin_va_list. |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 1118 | VaListTagDecl = nullptr; |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1121 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1122 | return SourceMgr.getDiagnostics(); |
| 1123 | } |
| 1124 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 1125 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 1126 | AttrVec *&Result = DeclAttrs[D]; |
| 1127 | if (!Result) { |
| 1128 | void *Mem = Allocate(sizeof(AttrVec)); |
| 1129 | Result = new (Mem) AttrVec; |
| 1130 | } |
| 1131 | |
| 1132 | return *Result; |
| 1133 | } |
| 1134 | |
| 1135 | /// \brief Erase the attributes corresponding to the given declaration. |
| 1136 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 1137 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 1138 | if (Pos != DeclAttrs.end()) { |
| 1139 | Pos->second->~AttrVec(); |
| 1140 | DeclAttrs.erase(Pos); |
| 1141 | } |
| 1142 | } |
| 1143 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1144 | // FIXME: Remove ? |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1145 | MemberSpecializationInfo * |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1146 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1147 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1148 | return getTemplateOrSpecializationInfo(Var) |
| 1149 | .dyn_cast<MemberSpecializationInfo *>(); |
| 1150 | } |
| 1151 | |
| 1152 | ASTContext::TemplateOrSpecializationInfo |
| 1153 | ASTContext::getTemplateOrSpecializationInfo(const VarDecl *Var) { |
| 1154 | llvm::DenseMap<const VarDecl *, TemplateOrSpecializationInfo>::iterator Pos = |
| 1155 | TemplateOrInstantiation.find(Var); |
| 1156 | if (Pos == TemplateOrInstantiation.end()) |
| 1157 | return TemplateOrSpecializationInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1158 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1159 | return Pos->second; |
| 1160 | } |
| 1161 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | void |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1163 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 1164 | TemplateSpecializationKind TSK, |
| 1165 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1166 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 1167 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1168 | setTemplateOrSpecializationInfo(Inst, new (*this) MemberSpecializationInfo( |
| 1169 | Tmpl, TSK, PointOfInstantiation)); |
| 1170 | } |
| 1171 | |
| 1172 | void |
| 1173 | ASTContext::setTemplateOrSpecializationInfo(VarDecl *Inst, |
| 1174 | TemplateOrSpecializationInfo TSI) { |
| 1175 | assert(!TemplateOrInstantiation[Inst] && |
| 1176 | "Already noted what the variable was instantiated from"); |
| 1177 | TemplateOrInstantiation[Inst] = TSI; |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1180 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 1181 | const FunctionDecl *FD){ |
| 1182 | assert(FD && "Specialization is 0"); |
| 1183 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1184 | = ClassScopeSpecializationPattern.find(FD); |
| 1185 | if (Pos == ClassScopeSpecializationPattern.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1186 | return nullptr; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1187 | |
| 1188 | return Pos->second; |
| 1189 | } |
| 1190 | |
| 1191 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 1192 | FunctionDecl *Pattern) { |
| 1193 | assert(FD && "Specialization is 0"); |
| 1194 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1195 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1198 | NamedDecl * |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1199 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1200 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1201 | = InstantiatedFromUsingDecl.find(UUD); |
| 1202 | if (Pos == InstantiatedFromUsingDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1203 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1205 | return Pos->second; |
| 1206 | } |
| 1207 | |
| 1208 | void |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1209 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 1210 | assert((isa<UsingDecl>(Pattern) || |
| 1211 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 1212 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 1213 | "pattern decl is not a using decl"); |
| 1214 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 1215 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 1216 | } |
| 1217 | |
| 1218 | UsingShadowDecl * |
| 1219 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 1220 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 1221 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 1222 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1223 | return nullptr; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1224 | |
| 1225 | return Pos->second; |
| 1226 | } |
| 1227 | |
| 1228 | void |
| 1229 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 1230 | UsingShadowDecl *Pattern) { |
| 1231 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 1232 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1235 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 1236 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 1237 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 1238 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1239 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1240 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1241 | return Pos->second; |
| 1242 | } |
| 1243 | |
| 1244 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 1245 | FieldDecl *Tmpl) { |
| 1246 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 1247 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 1248 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 1249 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1250 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1251 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 1252 | } |
| 1253 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1254 | ASTContext::overridden_cxx_method_iterator |
| 1255 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 1256 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | cc4ca0a | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1257 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1258 | if (Pos == OverriddenMethods.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1259 | return nullptr; |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1260 | |
| 1261 | return Pos->second.begin(); |
| 1262 | } |
| 1263 | |
| 1264 | ASTContext::overridden_cxx_method_iterator |
| 1265 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 1266 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | cc4ca0a | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1267 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1268 | if (Pos == OverriddenMethods.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1269 | return nullptr; |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1270 | |
| 1271 | return Pos->second.end(); |
| 1272 | } |
| 1273 | |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1274 | unsigned |
| 1275 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 1276 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | cc4ca0a | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1277 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1278 | if (Pos == OverriddenMethods.end()) |
| 1279 | return 0; |
| 1280 | |
| 1281 | return Pos->second.size(); |
| 1282 | } |
| 1283 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1284 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 1285 | const CXXMethodDecl *Overridden) { |
Argyrios Kyrtzidis | cc4ca0a | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1286 | assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl()); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1287 | OverriddenMethods[Method].push_back(Overridden); |
| 1288 | } |
| 1289 | |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 1290 | void ASTContext::getOverriddenMethods( |
| 1291 | const NamedDecl *D, |
| 1292 | SmallVectorImpl<const NamedDecl *> &Overridden) const { |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1293 | assert(D); |
| 1294 | |
| 1295 | if (const CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | c8e7008 | 2013-04-17 00:09:03 +0000 | [diff] [blame] | 1296 | Overridden.append(overridden_methods_begin(CXXMethod), |
| 1297 | overridden_methods_end(CXXMethod)); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1298 | return; |
| 1299 | } |
| 1300 | |
| 1301 | const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D); |
| 1302 | if (!Method) |
| 1303 | return; |
| 1304 | |
Argyrios Kyrtzidis | 353f6a4 | 2012-10-09 18:19:01 +0000 | [diff] [blame] | 1305 | SmallVector<const ObjCMethodDecl *, 8> OverDecls; |
| 1306 | Method->getOverriddenMethods(OverDecls); |
Argyrios Kyrtzidis | a7a1081 | 2012-10-09 20:08:43 +0000 | [diff] [blame] | 1307 | Overridden.append(OverDecls.begin(), OverDecls.end()); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 1310 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 1311 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 1312 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 1313 | if (!FirstLocalImport) { |
| 1314 | FirstLocalImport = Import; |
| 1315 | LastLocalImport = Import; |
| 1316 | return; |
| 1317 | } |
| 1318 | |
| 1319 | LastLocalImport->NextLocalImport = Import; |
| 1320 | LastLocalImport = Import; |
| 1321 | } |
| 1322 | |
Chris Lattner | 53cfe80 | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1323 | //===----------------------------------------------------------------------===// |
| 1324 | // Type Sizing and Analysis |
| 1325 | //===----------------------------------------------------------------------===// |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1326 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1327 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 1328 | /// scalar floating point type. |
| 1329 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1330 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1331 | assert(BT && "Not a floating point type!"); |
| 1332 | switch (BT->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1333 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1334 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1335 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 1336 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 1337 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1341 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1342 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1343 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1344 | bool UseAlignAttrOnly = false; |
| 1345 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 1346 | Align = AlignFromAttr; |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1347 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1348 | // __attribute__((aligned)) can increase or decrease alignment |
| 1349 | // *except* on a struct or struct member, where it only increases |
| 1350 | // alignment unless 'packed' is also specified. |
| 1351 | // |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1352 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1353 | // ignore that possibility; Sema should diagnose it. |
| 1354 | if (isa<FieldDecl>(D)) { |
| 1355 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 1356 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 1357 | } else { |
| 1358 | UseAlignAttrOnly = true; |
| 1359 | } |
| 1360 | } |
Fariborz Jahanian | 9f10718 | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 1361 | else if (isa<FieldDecl>(D)) |
| 1362 | UseAlignAttrOnly = |
| 1363 | D->hasAttr<PackedAttr>() || |
| 1364 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1365 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1366 | // If we're using the align attribute only, just ignore everything |
| 1367 | // else about the declaration and its type. |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1368 | if (UseAlignAttrOnly) { |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1369 | // do nothing |
| 1370 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1371 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1372 | QualType T = VD->getType(); |
Richard Smith | f6d7030 | 2014-06-10 23:34:28 +0000 | [diff] [blame] | 1373 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1374 | if (ForAlignof) |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1375 | T = RT->getPointeeType(); |
| 1376 | else |
| 1377 | T = getPointerType(RT->getPointeeType()); |
| 1378 | } |
Richard Smith | f6d7030 | 2014-06-10 23:34:28 +0000 | [diff] [blame] | 1379 | QualType BaseT = getBaseElementType(T); |
| 1380 | if (!BaseT->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1381 | // Adjust alignments of declarations with array type by the |
| 1382 | // large-array alignment on the target. |
Rafael Espindola | 8857275 | 2013-08-07 18:08:19 +0000 | [diff] [blame] | 1383 | if (const ArrayType *arrayType = getAsArrayType(T)) { |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1384 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
| 1385 | if (!ForAlignof && MinWidth) { |
Rafael Espindola | 8857275 | 2013-08-07 18:08:19 +0000 | [diff] [blame] | 1386 | if (isa<VariableArrayType>(arrayType)) |
| 1387 | Align = std::max(Align, Target->getLargeArrayAlign()); |
| 1388 | else if (isa<ConstantArrayType>(arrayType) && |
| 1389 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
| 1390 | Align = std::max(Align, Target->getLargeArrayAlign()); |
| 1391 | } |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1392 | } |
Chad Rosier | 99ee782 | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 1393 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1394 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Ulrich Weigand | b63f779 | 2015-04-21 17:26:18 +0000 | [diff] [blame] | 1395 | if (VD->hasGlobalStorage() && !ForAlignof) |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1396 | Align = std::max(Align, getTargetInfo().getMinGlobalAlign()); |
| 1397 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1398 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1399 | |
| 1400 | // Fields can be subject to extra alignment constraints, like if |
| 1401 | // the field is packed, the struct is packed, or the struct has a |
| 1402 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 1403 | // the actual alignment of the field within the struct, and then |
| 1404 | // (as we're expected to) constrain that by the alignment of the type. |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1405 | if (const FieldDecl *Field = dyn_cast<FieldDecl>(VD)) { |
| 1406 | const RecordDecl *Parent = Field->getParent(); |
| 1407 | // We can only produce a sensible answer if the record is valid. |
| 1408 | if (!Parent->isInvalidDecl()) { |
| 1409 | const ASTRecordLayout &Layout = getASTRecordLayout(Parent); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1410 | |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1411 | // Start with the record's overall alignment. |
| 1412 | unsigned FieldAlign = toBits(Layout.getAlignment()); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1413 | |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1414 | // Use the GCD of that and the offset within the record. |
| 1415 | uint64_t Offset = Layout.getFieldOffset(Field->getFieldIndex()); |
| 1416 | if (Offset > 0) { |
| 1417 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 1418 | // which means we get to do this crazy thing instead of Euclid's. |
| 1419 | uint64_t LowBitOfOffset = Offset & (~Offset + 1); |
| 1420 | if (LowBitOfOffset < FieldAlign) |
| 1421 | FieldAlign = static_cast<unsigned>(LowBitOfOffset); |
| 1422 | } |
| 1423 | |
| 1424 | Align = std::min(Align, FieldAlign); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1425 | } |
Charles Davis | 3fc5107 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 1426 | } |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1427 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1428 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1429 | return toCharUnitsFromBits(Align); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1430 | } |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1431 | |
John McCall | f124992 | 2012-08-21 04:10:00 +0000 | [diff] [blame] | 1432 | // getTypeInfoDataSizeInChars - Return the size of a type, in |
| 1433 | // chars. If the type is a record, its data size is returned. This is |
| 1434 | // the size of the memcpy that's performed when assigning this type |
| 1435 | // using a trivial copy/move assignment operator. |
| 1436 | std::pair<CharUnits, CharUnits> |
| 1437 | ASTContext::getTypeInfoDataSizeInChars(QualType T) const { |
| 1438 | std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T); |
| 1439 | |
| 1440 | // In C++, objects can sometimes be allocated into the tail padding |
| 1441 | // of a base-class subobject. We decide whether that's possible |
| 1442 | // during class layout, so here we can just trust the layout results. |
| 1443 | if (getLangOpts().CPlusPlus) { |
| 1444 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 1445 | const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl()); |
| 1446 | sizeAndAlign.first = layout.getDataSize(); |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | return sizeAndAlign; |
| 1451 | } |
| 1452 | |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1453 | /// getConstantArrayInfoInChars - Performing the computation in CharUnits |
| 1454 | /// instead of in bits prevents overflowing the uint64_t for some large arrays. |
| 1455 | std::pair<CharUnits, CharUnits> |
| 1456 | static getConstantArrayInfoInChars(const ASTContext &Context, |
| 1457 | const ConstantArrayType *CAT) { |
| 1458 | std::pair<CharUnits, CharUnits> EltInfo = |
| 1459 | Context.getTypeInfoInChars(CAT->getElementType()); |
| 1460 | uint64_t Size = CAT->getSize().getZExtValue(); |
Richard Trieu | c750907 | 2013-05-14 23:41:50 +0000 | [diff] [blame] | 1461 | assert((Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity()) <= |
| 1462 | (uint64_t)(-1)/Size) && |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1463 | "Overflow in array type char size evaluation"); |
| 1464 | uint64_t Width = EltInfo.first.getQuantity() * Size; |
| 1465 | unsigned Align = EltInfo.second.getQuantity(); |
Warren Hunt | 5ae586a | 2013-11-01 23:59:41 +0000 | [diff] [blame] | 1466 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 1467 | Context.getTargetInfo().getPointerWidth(0) == 64) |
| 1468 | Width = llvm::RoundUpToAlignment(Width, Align); |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1469 | return std::make_pair(CharUnits::fromQuantity(Width), |
| 1470 | CharUnits::fromQuantity(Align)); |
| 1471 | } |
| 1472 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1473 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1474 | ASTContext::getTypeInfoInChars(const Type *T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1475 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) |
| 1476 | return getConstantArrayInfoInChars(*this, CAT); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1477 | TypeInfo Info = getTypeInfo(T); |
| 1478 | return std::make_pair(toCharUnitsFromBits(Info.Width), |
| 1479 | toCharUnitsFromBits(Info.Align)); |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1483 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1484 | return getTypeInfoInChars(T.getTypePtr()); |
| 1485 | } |
| 1486 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1487 | bool ASTContext::isAlignmentRequired(const Type *T) const { |
| 1488 | return getTypeInfo(T).AlignIsRequired; |
| 1489 | } |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1490 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1491 | bool ASTContext::isAlignmentRequired(QualType T) const { |
| 1492 | return isAlignmentRequired(T.getTypePtr()); |
| 1493 | } |
| 1494 | |
| 1495 | TypeInfo ASTContext::getTypeInfo(const Type *T) const { |
David Majnemer | f8d3864 | 2014-07-30 08:42:33 +0000 | [diff] [blame] | 1496 | TypeInfoMap::iterator I = MemoizedTypeInfo.find(T); |
| 1497 | if (I != MemoizedTypeInfo.end()) |
| 1498 | return I->second; |
| 1499 | |
| 1500 | // This call can invalidate MemoizedTypeInfo[T], so we need a second lookup. |
| 1501 | TypeInfo TI = getTypeInfoImpl(T); |
| 1502 | MemoizedTypeInfo[T] = TI; |
Rafael Espindola | eaa88c1 | 2014-07-30 04:40:23 +0000 | [diff] [blame] | 1503 | return TI; |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 1507 | /// method does not work on incomplete types. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1508 | /// |
| 1509 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 1510 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 1511 | /// should take a QualType, &c. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1512 | TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const { |
| 1513 | uint64_t Width = 0; |
| 1514 | unsigned Align = 8; |
| 1515 | bool AlignIsRequired = false; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1516 | switch (T->getTypeClass()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1517 | #define TYPE(Class, Base) |
| 1518 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1519 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1520 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
David Blaikie | ab277d6 | 2013-07-13 21:08:03 +0000 | [diff] [blame] | 1521 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) \ |
| 1522 | case Type::Class: \ |
| 1523 | assert(!T->isDependentType() && "should not see dependent types here"); \ |
| 1524 | return getTypeInfo(cast<Class##Type>(T)->desugar().getTypePtr()); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1525 | #include "clang/AST/TypeNodes.def" |
John McCall | 15547bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 1526 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1527 | |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1528 | case Type::FunctionNoProto: |
| 1529 | case Type::FunctionProto: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1530 | // GCC extension: alignof(function) = 32 bits |
| 1531 | Width = 0; |
| 1532 | Align = 32; |
| 1533 | break; |
| 1534 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1535 | case Type::IncompleteArray: |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1536 | case Type::VariableArray: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1537 | Width = 0; |
| 1538 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 1539 | break; |
| 1540 | |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1541 | case Type::ConstantArray: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1542 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1544 | TypeInfo EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | d541a4c | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1545 | uint64_t Size = CAT->getSize().getZExtValue(); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1546 | assert((Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) && |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1547 | "Overflow in array type bit size evaluation"); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1548 | Width = EltInfo.Width * Size; |
| 1549 | Align = EltInfo.Align; |
Warren Hunt | 5ae586a | 2013-11-01 23:59:41 +0000 | [diff] [blame] | 1550 | if (!getTargetInfo().getCXXABI().isMicrosoft() || |
| 1551 | getTargetInfo().getPointerWidth(0) == 64) |
| 1552 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1553 | break; |
Christopher Lamb | c5fafa2 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 1554 | } |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1555 | case Type::ExtVector: |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1556 | case Type::Vector: { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1557 | const VectorType *VT = cast<VectorType>(T); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1558 | TypeInfo EltInfo = getTypeInfo(VT->getElementType()); |
| 1559 | Width = EltInfo.Width * VT->getNumElements(); |
Eli Friedman | 3df5efe | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1560 | Align = Width; |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1561 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 1562 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | ef78c8e | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 1563 | if (Align & (Align-1)) { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1564 | Align = llvm::NextPowerOf2(Align); |
| 1565 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 1566 | } |
Chad Rosier | cc40ea7 | 2012-07-13 23:57:43 +0000 | [diff] [blame] | 1567 | // Adjust the alignment based on the target max. |
| 1568 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
| 1569 | if (TargetVectorAlign && TargetVectorAlign < Align) |
| 1570 | Align = TargetVectorAlign; |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1571 | break; |
| 1572 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1573 | |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1574 | case Type::Builtin: |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1575 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1576 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1577 | case BuiltinType::Void: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1578 | // GCC extension: alignof(void) = 8 bits. |
| 1579 | Width = 0; |
| 1580 | Align = 8; |
| 1581 | break; |
| 1582 | |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1583 | case BuiltinType::Bool: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1584 | Width = Target->getBoolWidth(); |
| 1585 | Align = Target->getBoolAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1586 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1587 | case BuiltinType::Char_S: |
| 1588 | case BuiltinType::Char_U: |
| 1589 | case BuiltinType::UChar: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1590 | case BuiltinType::SChar: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1591 | Width = Target->getCharWidth(); |
| 1592 | Align = Target->getCharAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1593 | break; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1594 | case BuiltinType::WChar_S: |
| 1595 | case BuiltinType::WChar_U: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1596 | Width = Target->getWCharWidth(); |
| 1597 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1598 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1599 | case BuiltinType::Char16: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1600 | Width = Target->getChar16Width(); |
| 1601 | Align = Target->getChar16Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1602 | break; |
| 1603 | case BuiltinType::Char32: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1604 | Width = Target->getChar32Width(); |
| 1605 | Align = Target->getChar32Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1606 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1607 | case BuiltinType::UShort: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1608 | case BuiltinType::Short: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1609 | Width = Target->getShortWidth(); |
| 1610 | Align = Target->getShortAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1611 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1612 | case BuiltinType::UInt: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1613 | case BuiltinType::Int: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1614 | Width = Target->getIntWidth(); |
| 1615 | Align = Target->getIntAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1616 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1617 | case BuiltinType::ULong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1618 | case BuiltinType::Long: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1619 | Width = Target->getLongWidth(); |
| 1620 | Align = Target->getLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1621 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1622 | case BuiltinType::ULongLong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1623 | case BuiltinType::LongLong: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1624 | Width = Target->getLongLongWidth(); |
| 1625 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1626 | break; |
Chris Lattner | 0a415ec | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 1627 | case BuiltinType::Int128: |
| 1628 | case BuiltinType::UInt128: |
| 1629 | Width = 128; |
| 1630 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 1631 | break; |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1632 | case BuiltinType::Half: |
| 1633 | Width = Target->getHalfWidth(); |
| 1634 | Align = Target->getHalfAlign(); |
| 1635 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1636 | case BuiltinType::Float: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1637 | Width = Target->getFloatWidth(); |
| 1638 | Align = Target->getFloatAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1639 | break; |
| 1640 | case BuiltinType::Double: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1641 | Width = Target->getDoubleWidth(); |
| 1642 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1643 | break; |
| 1644 | case BuiltinType::LongDouble: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1645 | Width = Target->getLongDoubleWidth(); |
| 1646 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1647 | break; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1648 | case BuiltinType::NullPtr: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1649 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 1650 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | a81b0b7 | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 1651 | break; |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1652 | case BuiltinType::ObjCId: |
| 1653 | case BuiltinType::ObjCClass: |
| 1654 | case BuiltinType::ObjCSel: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1655 | Width = Target->getPointerWidth(0); |
| 1656 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1657 | break; |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1658 | case BuiltinType::OCLSampler: |
| 1659 | // Samplers are modeled as integers. |
| 1660 | Width = Target->getIntWidth(); |
| 1661 | Align = Target->getIntAlign(); |
| 1662 | break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1663 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1664 | case BuiltinType::OCLClkEvent: |
| 1665 | case BuiltinType::OCLQueue: |
| 1666 | case BuiltinType::OCLNDRange: |
| 1667 | case BuiltinType::OCLReserveID: |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1668 | case BuiltinType::OCLImage1d: |
| 1669 | case BuiltinType::OCLImage1dArray: |
| 1670 | case BuiltinType::OCLImage1dBuffer: |
| 1671 | case BuiltinType::OCLImage2d: |
| 1672 | case BuiltinType::OCLImage2dArray: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1673 | case BuiltinType::OCLImage2dDepth: |
| 1674 | case BuiltinType::OCLImage2dArrayDepth: |
| 1675 | case BuiltinType::OCLImage2dMSAA: |
| 1676 | case BuiltinType::OCLImage2dArrayMSAA: |
| 1677 | case BuiltinType::OCLImage2dMSAADepth: |
| 1678 | case BuiltinType::OCLImage2dArrayMSAADepth: |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1679 | case BuiltinType::OCLImage3d: |
| 1680 | // Currently these types are pointers to opaque types. |
| 1681 | Width = Target->getPointerWidth(0); |
| 1682 | Align = Target->getPointerAlign(0); |
| 1683 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1684 | } |
Chris Lattner | 48f84b8 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 1685 | break; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1686 | case Type::ObjCObjectPointer: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1687 | Width = Target->getPointerWidth(0); |
| 1688 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1689 | break; |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1690 | case Type::BlockPointer: { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1691 | unsigned AS = getTargetAddressSpace( |
| 1692 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1693 | Width = Target->getPointerWidth(AS); |
| 1694 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1695 | break; |
| 1696 | } |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1697 | case Type::LValueReference: |
| 1698 | case Type::RValueReference: { |
| 1699 | // alignof and sizeof should never enter this code path here, so we go |
| 1700 | // the pointer route. |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1701 | unsigned AS = getTargetAddressSpace( |
| 1702 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1703 | Width = Target->getPointerWidth(AS); |
| 1704 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1705 | break; |
| 1706 | } |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1707 | case Type::Pointer: { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1708 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1709 | Width = Target->getPointerWidth(AS); |
| 1710 | Align = Target->getPointerAlign(AS); |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1711 | break; |
| 1712 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1713 | case Type::MemberPointer: { |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1714 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1715 | std::tie(Width, Align) = ABI->getMemberPointerWidthAndAlign(MPT); |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1716 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1717 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1718 | case Type::Complex: { |
| 1719 | // Complex types have the same alignment as their elements, but twice the |
| 1720 | // size. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1721 | TypeInfo EltInfo = getTypeInfo(cast<ComplexType>(T)->getElementType()); |
| 1722 | Width = EltInfo.Width * 2; |
| 1723 | Align = EltInfo.Align; |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1724 | break; |
| 1725 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1726 | case Type::ObjCObject: |
| 1727 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1728 | case Type::Adjusted: |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 1729 | case Type::Decayed: |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1730 | return getTypeInfo(cast<AdjustedType>(T)->getAdjustedType().getTypePtr()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1731 | case Type::ObjCInterface: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1732 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1733 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1734 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1735 | Align = toBits(Layout.getAlignment()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1736 | break; |
| 1737 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1738 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1739 | case Type::Enum: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1740 | const TagType *TT = cast<TagType>(T); |
| 1741 | |
| 1742 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 7f97189 | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1743 | Width = 8; |
| 1744 | Align = 8; |
Chris Lattner | 572100b | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1745 | break; |
| 1746 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 1748 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) { |
| 1749 | const EnumDecl *ED = ET->getDecl(); |
| 1750 | TypeInfo Info = |
| 1751 | getTypeInfo(ED->getIntegerType()->getUnqualifiedDesugaredType()); |
| 1752 | if (unsigned AttrAlign = ED->getMaxAlignment()) { |
| 1753 | Info.Align = AttrAlign; |
| 1754 | Info.AlignIsRequired = true; |
| 1755 | } |
| 1756 | return Info; |
| 1757 | } |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1758 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1759 | const RecordType *RT = cast<RecordType>(TT); |
David Majnemer | 5821ff7 | 2015-02-03 08:49:29 +0000 | [diff] [blame] | 1760 | const RecordDecl *RD = RT->getDecl(); |
| 1761 | const ASTRecordLayout &Layout = getASTRecordLayout(RD); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1762 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1763 | Align = toBits(Layout.getAlignment()); |
David Majnemer | 5821ff7 | 2015-02-03 08:49:29 +0000 | [diff] [blame] | 1764 | AlignIsRequired = RD->hasAttr<AlignedAttr>(); |
Chris Lattner | 49a953a | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1765 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1766 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1767 | |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1768 | case Type::SubstTemplateTypeParm: |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1769 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1770 | getReplacementType().getTypePtr()); |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1771 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1772 | case Type::Auto: { |
| 1773 | const AutoType *A = cast<AutoType>(T); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 1774 | assert(!A->getDeducedType().isNull() && |
| 1775 | "cannot request the size of an undeduced or dependent auto type"); |
Matt Beaumont-Gay | 7a24210e | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1776 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1779 | case Type::Paren: |
| 1780 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1781 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1782 | case Type::Typedef: { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1783 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1784 | TypeInfo Info = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1785 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1786 | // alignment we have. This violates the GCC documentation (which says that |
| 1787 | // attribute(aligned) can only round up) but matches its implementation. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1788 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) { |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1789 | Align = AttrAlign; |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1790 | AlignIsRequired = true; |
David Majnemer | 37bffb6 | 2014-08-04 05:11:01 +0000 | [diff] [blame] | 1791 | } else { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1792 | Align = Info.Align; |
David Majnemer | 37bffb6 | 2014-08-04 05:11:01 +0000 | [diff] [blame] | 1793 | AlignIsRequired = Info.AlignIsRequired; |
| 1794 | } |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1795 | Width = Info.Width; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1796 | break; |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1797 | } |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1798 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1799 | case Type::Elaborated: |
| 1800 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1801 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1802 | case Type::Attributed: |
| 1803 | return getTypeInfo( |
| 1804 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1805 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1806 | case Type::Atomic: { |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1807 | // Start with the base type information. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1808 | TypeInfo Info = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1809 | Width = Info.Width; |
| 1810 | Align = Info.Align; |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1811 | |
| 1812 | // If the size of the type doesn't exceed the platform's max |
| 1813 | // atomic promotion width, make the size and alignment more |
| 1814 | // favorable to atomic operations: |
| 1815 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth()) { |
| 1816 | // Round the size up to a power of 2. |
| 1817 | if (!llvm::isPowerOf2_64(Width)) |
| 1818 | Width = llvm::NextPowerOf2(Width); |
| 1819 | |
| 1820 | // Set the alignment equal to the size. |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1821 | Align = static_cast<unsigned>(Width); |
| 1822 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1825 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1827 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1828 | return TypeInfo(Width, Align, AlignIsRequired); |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1831 | unsigned ASTContext::getOpenMPDefaultSimdAlign(QualType T) const { |
| 1832 | unsigned SimdAlign = getTargetInfo().getSimdDefaultAlign(); |
| 1833 | // Target ppc64 with QPX: simd default alignment for pointer to double is 32. |
| 1834 | if ((getTargetInfo().getTriple().getArch() == llvm::Triple::ppc64 || |
| 1835 | getTargetInfo().getTriple().getArch() == llvm::Triple::ppc64le) && |
| 1836 | getTargetInfo().getABI() == "elfv1-qpx" && |
| 1837 | T->isSpecificBuiltinType(BuiltinType::Double)) |
| 1838 | SimdAlign = 256; |
| 1839 | return SimdAlign; |
| 1840 | } |
| 1841 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1842 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1843 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1844 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1845 | } |
| 1846 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1847 | /// toBits - Convert a size in characters to a size in characters. |
| 1848 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1849 | return CharSize.getQuantity() * getCharWidth(); |
| 1850 | } |
| 1851 | |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1852 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1853 | /// This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1854 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1855 | return getTypeInfoInChars(T).first; |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1856 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1857 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1858 | return getTypeInfoInChars(T).first; |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Ken Dyck | a6046ab | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1861 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1862 | /// characters. This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1863 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1864 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1865 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1866 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1867 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1870 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1871 | /// type for the current target in bits. This can be different than the ABI |
| 1872 | /// alignment in cases where it is beneficial for performance to overalign |
| 1873 | /// a data type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1874 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1875 | TypeInfo TI = getTypeInfo(T); |
| 1876 | unsigned ABIAlign = TI.Align; |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1877 | |
David Majnemer | e154456 | 2015-04-24 01:25:05 +0000 | [diff] [blame] | 1878 | T = T->getBaseElementTypeUnsafe(); |
| 1879 | |
| 1880 | // The preferred alignment of member pointers is that of a pointer. |
| 1881 | if (T->isMemberPointerType()) |
| 1882 | return getPreferredTypeAlign(getPointerDiffType().getTypePtr()); |
| 1883 | |
Robert Lytton | eaf6f36 | 2013-11-12 10:09:34 +0000 | [diff] [blame] | 1884 | if (Target->getTriple().getArch() == llvm::Triple::xcore) |
| 1885 | return ABIAlign; // Never overalign on XCore. |
| 1886 | |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1887 | // Double and long long should be naturally aligned if possible. |
David Majnemer | 1d4db8f | 2014-02-24 23:43:27 +0000 | [diff] [blame] | 1888 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1889 | T = CT->getElementType().getTypePtr(); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 1890 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 1891 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1892 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | b57321a | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 1893 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 1894 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
David Majnemer | 8b6bd57 | 2014-02-24 23:34:17 +0000 | [diff] [blame] | 1895 | // Don't increase the alignment if an alignment attribute was specified on a |
| 1896 | // typedef declaration. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1897 | if (!TI.AlignIsRequired) |
David Majnemer | 8b6bd57 | 2014-02-24 23:34:17 +0000 | [diff] [blame] | 1898 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1899 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1900 | return ABIAlign; |
| 1901 | } |
| 1902 | |
Ulrich Weigand | ca3cb7f | 2015-04-21 17:29:35 +0000 | [diff] [blame] | 1903 | /// getTargetDefaultAlignForAttributeAligned - Return the default alignment |
| 1904 | /// for __attribute__((aligned)) on this target, to be used if no alignment |
| 1905 | /// value is specified. |
| 1906 | unsigned ASTContext::getTargetDefaultAlignForAttributeAligned(void) const { |
| 1907 | return getTargetInfo().getDefaultAlignForAttributeAligned(); |
| 1908 | } |
| 1909 | |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1910 | /// getAlignOfGlobalVar - Return the alignment in bits that should be given |
| 1911 | /// to a global variable of the specified type. |
| 1912 | unsigned ASTContext::getAlignOfGlobalVar(QualType T) const { |
| 1913 | return std::max(getTypeAlign(T), getTargetInfo().getMinGlobalAlign()); |
| 1914 | } |
| 1915 | |
| 1916 | /// getAlignOfGlobalVarInChars - Return the alignment in characters that |
| 1917 | /// should be given to a global variable of the specified type. |
| 1918 | CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T) const { |
| 1919 | return toCharUnitsFromBits(getAlignOfGlobalVar(T)); |
| 1920 | } |
| 1921 | |
David Majnemer | 08ef2ba | 2015-06-23 20:34:18 +0000 | [diff] [blame] | 1922 | CharUnits ASTContext::getOffsetOfBaseWithVBPtr(const CXXRecordDecl *RD) const { |
| 1923 | CharUnits Offset = CharUnits::Zero(); |
| 1924 | const ASTRecordLayout *Layout = &getASTRecordLayout(RD); |
| 1925 | while (const CXXRecordDecl *Base = Layout->getBaseSharingVBPtr()) { |
| 1926 | Offset += Layout->getBaseClassOffset(Base); |
| 1927 | Layout = &getASTRecordLayout(Base); |
| 1928 | } |
| 1929 | return Offset; |
| 1930 | } |
| 1931 | |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1932 | /// DeepCollectObjCIvars - |
| 1933 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1934 | /// super class and then collects all ivars, including those synthesized for |
| 1935 | /// current class. This routine is used for implementation of current class |
| 1936 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1937 | /// |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1938 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1939 | bool leafClass, |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1940 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1941 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1942 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1943 | if (!leafClass) { |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 1944 | for (const auto *I : OI->ivars()) |
| 1945 | Ivars.push_back(I); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1946 | } else { |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1947 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1948 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1949 | Iv= Iv->getNextIvar()) |
| 1950 | Ivars.push_back(Iv); |
| 1951 | } |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1954 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1955 | /// those inherited by it. |
| 1956 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1957 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1958 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1959 | // We can use protocol_iterator here instead of |
| 1960 | // all_referenced_protocol_iterator since we are walking all categories. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 1961 | for (auto *Proto : OI->all_referenced_protocols()) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 1962 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1963 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1964 | |
| 1965 | // Categories of this Interface. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 1966 | for (const auto *Cat : OI->visible_categories()) |
| 1967 | CollectInheritedProtocols(Cat, Protocols); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1968 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1969 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1970 | while (SD) { |
| 1971 | CollectInheritedProtocols(SD, Protocols); |
| 1972 | SD = SD->getSuperClass(); |
| 1973 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1974 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 1975 | for (auto *Proto : OC->protocols()) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 1976 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1977 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1978 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 1979 | // Insert the protocol. |
| 1980 | if (!Protocols.insert( |
| 1981 | const_cast<ObjCProtocolDecl *>(OP->getCanonicalDecl())).second) |
| 1982 | return; |
| 1983 | |
| 1984 | for (auto *Proto : OP->protocols()) |
| 1985 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1986 | } |
| 1987 | } |
| 1988 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1989 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1990 | unsigned count = 0; |
| 1991 | // Count ivars declared in class extension. |
Aaron Ballman | b4a5345 | 2014-03-13 21:57:01 +0000 | [diff] [blame] | 1992 | for (const auto *Ext : OI->known_extensions()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1993 | count += Ext->ivar_size(); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1994 | |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1995 | // Count ivar defined in this class's implementation. This |
| 1996 | // includes synthesized ivars. |
| 1997 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1998 | count += ImplDecl->ivar_size(); |
| 1999 | |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2000 | return count; |
| 2001 | } |
| 2002 | |
Argyrios Kyrtzidis | 2e809ce | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 2003 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 2004 | if (!E) |
| 2005 | return false; |
| 2006 | |
| 2007 | // nullptr_t is always treated as null. |
| 2008 | if (E->getType()->isNullPtrType()) return true; |
| 2009 | |
| 2010 | if (E->getType()->isAnyPointerType() && |
| 2011 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 2012 | Expr::NPC_ValueDependentIsNull)) |
| 2013 | return true; |
| 2014 | |
| 2015 | // Unfortunately, __null has type 'int'. |
| 2016 | if (isa<GNUNullExpr>(E)) return true; |
| 2017 | |
| 2018 | return false; |
| 2019 | } |
| 2020 | |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2021 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 2022 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 2023 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 2024 | I = ObjCImpls.find(D); |
| 2025 | if (I != ObjCImpls.end()) |
| 2026 | return cast<ObjCImplementationDecl>(I->second); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2027 | return nullptr; |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2028 | } |
| 2029 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 2030 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 2031 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 2032 | I = ObjCImpls.find(D); |
| 2033 | if (I != ObjCImpls.end()) |
| 2034 | return cast<ObjCCategoryImplDecl>(I->second); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2035 | return nullptr; |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 2039 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 2040 | ObjCImplementationDecl *ImplD) { |
| 2041 | assert(IFaceD && ImplD && "Passed null params"); |
| 2042 | ObjCImpls[IFaceD] = ImplD; |
| 2043 | } |
| 2044 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 2045 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 2046 | ObjCCategoryImplDecl *ImplD) { |
| 2047 | assert(CatD && ImplD && "Passed null params"); |
| 2048 | ObjCImpls[CatD] = ImplD; |
| 2049 | } |
| 2050 | |
Dmitri Gribenko | 37527c2 | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 2051 | const ObjCInterfaceDecl *ASTContext::getObjContainingInterface( |
| 2052 | const NamedDecl *ND) const { |
| 2053 | if (const ObjCInterfaceDecl *ID = |
| 2054 | dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2055 | return ID; |
Dmitri Gribenko | 37527c2 | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 2056 | if (const ObjCCategoryDecl *CD = |
| 2057 | dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2058 | return CD->getClassInterface(); |
Dmitri Gribenko | 37527c2 | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 2059 | if (const ObjCImplDecl *IMD = |
| 2060 | dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2061 | return IMD->getClassInterface(); |
| 2062 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2063 | return nullptr; |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2066 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 2067 | /// none exists. |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 2068 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2069 | assert(VD && "Passed null params"); |
| 2070 | assert(VD->hasAttr<BlocksAttr>() && |
| 2071 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 2072 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2073 | I = BlockVarCopyInits.find(VD); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2074 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : nullptr; |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | /// \brief Set the copy inialization expression of a block var decl. |
| 2078 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 2079 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2080 | assert(VD->hasAttr<BlocksAttr>() && |
| 2081 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2082 | BlockVarCopyInits[VD] = Init; |
| 2083 | } |
| 2084 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2085 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2086 | unsigned DataSize) const { |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 2087 | if (!DataSize) |
| 2088 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 2089 | else |
| 2090 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2091 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 2092 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2093 | TypeSourceInfo *TInfo = |
| 2094 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 2095 | new (TInfo) TypeSourceInfo(T); |
| 2096 | return TInfo; |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2099 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2100 | SourceLocation L) const { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2101 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | 2d525f0 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2102 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | 3665e00 | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 2103 | return DI; |
| 2104 | } |
| 2105 | |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2106 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2107 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2108 | return getObjCLayout(D, nullptr); |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2112 | ASTContext::getASTObjCImplementationLayout( |
| 2113 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2114 | return getObjCLayout(D->getClassInterface(), D); |
| 2115 | } |
| 2116 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2117 | //===----------------------------------------------------------------------===// |
| 2118 | // Type creation/memoization methods |
| 2119 | //===----------------------------------------------------------------------===// |
| 2120 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2121 | QualType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2122 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 2123 | unsigned fastQuals = quals.getFastQualifiers(); |
| 2124 | quals.removeFastQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2125 | |
| 2126 | // Check if we've already instantiated this type. |
| 2127 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2128 | ExtQuals::Profile(ID, baseType, quals); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2129 | void *insertPos = nullptr; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2130 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 2131 | assert(eq->getQualifiers() == quals); |
| 2132 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2135 | // If the base type is not canonical, make the appropriate canonical type. |
| 2136 | QualType canon; |
| 2137 | if (!baseType->isCanonicalUnqualified()) { |
| 2138 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2139 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 2140 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2141 | |
| 2142 | // Re-find the insert position. |
| 2143 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 2144 | } |
| 2145 | |
| 2146 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 2147 | ExtQualNodes.InsertNode(eq, insertPos); |
| 2148 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2151 | QualType |
| 2152 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2153 | QualType CanT = getCanonicalType(T); |
| 2154 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | 445fcab | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2155 | return T; |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2156 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2157 | // If we are composing extended qualifiers together, merge together |
| 2158 | // into one ExtQuals node. |
| 2159 | QualifierCollector Quals; |
| 2160 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2161 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2162 | // If this type already has an address space specified, it cannot get |
| 2163 | // another one. |
| 2164 | assert(!Quals.hasAddressSpace() && |
| 2165 | "Type cannot be in multiple addr spaces!"); |
| 2166 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2167 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2168 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | 025b5fb | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2171 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2172 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2173 | QualType CanT = getCanonicalType(T); |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2174 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2175 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2176 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2177 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 2178 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 2179 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | b68215c | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 2180 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 2181 | return getPointerType(ResultType); |
| 2182 | } |
| 2183 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2184 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2185 | // If we are composing extended qualifiers together, merge together |
| 2186 | // into one ExtQuals node. |
| 2187 | QualifierCollector Quals; |
| 2188 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2189 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2190 | // If this type already has an ObjCGC specified, it cannot get |
| 2191 | // another one. |
| 2192 | assert(!Quals.hasObjCGCAttr() && |
| 2193 | "Type cannot have multiple ObjCGCs!"); |
| 2194 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2195 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2196 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2197 | } |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2198 | |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2199 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 2200 | FunctionType::ExtInfo Info) { |
| 2201 | if (T->getExtInfo() == Info) |
| 2202 | return T; |
| 2203 | |
| 2204 | QualType Result; |
| 2205 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2206 | Result = getFunctionNoProtoType(FNPT->getReturnType(), Info); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2207 | } else { |
| 2208 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 2209 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 2210 | EPI.ExtInfo = Info; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2211 | Result = getFunctionType(FPT->getReturnType(), FPT->getParamTypes(), EPI); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | return cast<FunctionType>(Result.getTypePtr()); |
| 2215 | } |
| 2216 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2217 | void ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD, |
| 2218 | QualType ResultType) { |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2219 | FD = FD->getMostRecentDecl(); |
| 2220 | while (true) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2221 | const FunctionProtoType *FPT = FD->getType()->castAs<FunctionProtoType>(); |
| 2222 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2223 | FD->setType(getFunctionType(ResultType, FPT->getParamTypes(), EPI)); |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2224 | if (FunctionDecl *Next = FD->getPreviousDecl()) |
| 2225 | FD = Next; |
| 2226 | else |
| 2227 | break; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2228 | } |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2229 | if (ASTMutationListener *L = getASTMutationListener()) |
| 2230 | L->DeducedReturnType(FD, ResultType); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2233 | /// Get a function type and produce the equivalent function type with the |
| 2234 | /// specified exception specification. Type sugar that can be present on a |
| 2235 | /// declaration of a function with an exception specification is permitted |
| 2236 | /// and preserved. Other type sugar (for instance, typedefs) is not. |
| 2237 | static QualType getFunctionTypeWithExceptionSpec( |
| 2238 | ASTContext &Context, QualType Orig, |
| 2239 | const FunctionProtoType::ExceptionSpecInfo &ESI) { |
| 2240 | // Might have some parens. |
| 2241 | if (auto *PT = dyn_cast<ParenType>(Orig)) |
| 2242 | return Context.getParenType( |
| 2243 | getFunctionTypeWithExceptionSpec(Context, PT->getInnerType(), ESI)); |
| 2244 | |
| 2245 | // Might have a calling-convention attribute. |
| 2246 | if (auto *AT = dyn_cast<AttributedType>(Orig)) |
| 2247 | return Context.getAttributedType( |
| 2248 | AT->getAttrKind(), |
| 2249 | getFunctionTypeWithExceptionSpec(Context, AT->getModifiedType(), ESI), |
| 2250 | getFunctionTypeWithExceptionSpec(Context, AT->getEquivalentType(), |
| 2251 | ESI)); |
| 2252 | |
| 2253 | // Anything else must be a function type. Rebuild it with the new exception |
| 2254 | // specification. |
| 2255 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Orig); |
| 2256 | return Context.getFunctionType( |
| 2257 | Proto->getReturnType(), Proto->getParamTypes(), |
| 2258 | Proto->getExtProtoInfo().withExceptionSpec(ESI)); |
| 2259 | } |
| 2260 | |
| 2261 | void ASTContext::adjustExceptionSpec( |
| 2262 | FunctionDecl *FD, const FunctionProtoType::ExceptionSpecInfo &ESI, |
| 2263 | bool AsWritten) { |
| 2264 | // Update the type. |
| 2265 | QualType Updated = |
| 2266 | getFunctionTypeWithExceptionSpec(*this, FD->getType(), ESI); |
| 2267 | FD->setType(Updated); |
| 2268 | |
| 2269 | if (!AsWritten) |
| 2270 | return; |
| 2271 | |
| 2272 | // Update the type in the type source information too. |
| 2273 | if (TypeSourceInfo *TSInfo = FD->getTypeSourceInfo()) { |
| 2274 | // If the type and the type-as-written differ, we may need to update |
| 2275 | // the type-as-written too. |
| 2276 | if (TSInfo->getType() != FD->getType()) |
| 2277 | Updated = getFunctionTypeWithExceptionSpec(*this, TSInfo->getType(), ESI); |
| 2278 | |
| 2279 | // FIXME: When we get proper type location information for exceptions, |
| 2280 | // we'll also have to rebuild the TypeSourceInfo. For now, we just patch |
| 2281 | // up the TypeSourceInfo; |
| 2282 | assert(TypeLoc::getFullDataSizeForType(Updated) == |
| 2283 | TypeLoc::getFullDataSizeForType(TSInfo->getType()) && |
| 2284 | "TypeLoc size mismatch from updating exception specification"); |
| 2285 | TSInfo->overrideType(Updated); |
| 2286 | } |
| 2287 | } |
| 2288 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2289 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 2290 | /// number with the specified element type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2291 | QualType ASTContext::getComplexType(QualType T) const { |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2292 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2293 | // structure. |
| 2294 | llvm::FoldingSetNodeID ID; |
| 2295 | ComplexType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2296 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2297 | void *InsertPos = nullptr; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2298 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2299 | return QualType(CT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2300 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2301 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2302 | // so fill in the canonical type field. |
| 2303 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2304 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2305 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2306 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2307 | // Get the new insert position for the node we care about. |
| 2308 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2309 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2310 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2311 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2312 | Types.push_back(New); |
| 2313 | ComplexTypes.InsertNode(New, InsertPos); |
| 2314 | return QualType(New, 0); |
| 2315 | } |
| 2316 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 2317 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 2318 | /// the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2319 | QualType ASTContext::getPointerType(QualType T) const { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 2320 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2321 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2322 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2323 | PointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2325 | void *InsertPos = nullptr; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2326 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2327 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2328 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 2329 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2330 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2331 | QualType Canonical; |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2332 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2333 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2335 | // Get the new insert position for the node we care about. |
| 2336 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2337 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2338 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2339 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2340 | Types.push_back(New); |
| 2341 | PointerTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2342 | return QualType(New, 0); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2345 | QualType ASTContext::getAdjustedType(QualType Orig, QualType New) const { |
| 2346 | llvm::FoldingSetNodeID ID; |
| 2347 | AdjustedType::Profile(ID, Orig, New); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2348 | void *InsertPos = nullptr; |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2349 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2350 | if (AT) |
| 2351 | return QualType(AT, 0); |
| 2352 | |
| 2353 | QualType Canonical = getCanonicalType(New); |
| 2354 | |
| 2355 | // Get the new insert position for the node we care about. |
| 2356 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2357 | assert(!AT && "Shouldn't be in the map!"); |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2358 | |
| 2359 | AT = new (*this, TypeAlignment) |
| 2360 | AdjustedType(Type::Adjusted, Orig, New, Canonical); |
| 2361 | Types.push_back(AT); |
| 2362 | AdjustedTypes.InsertNode(AT, InsertPos); |
| 2363 | return QualType(AT, 0); |
| 2364 | } |
| 2365 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2366 | QualType ASTContext::getDecayedType(QualType T) const { |
| 2367 | assert((T->isArrayType() || T->isFunctionType()) && "T does not decay"); |
| 2368 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2369 | QualType Decayed; |
| 2370 | |
| 2371 | // C99 6.7.5.3p7: |
| 2372 | // A declaration of a parameter as "array of type" shall be |
| 2373 | // adjusted to "qualified pointer to type", where the type |
| 2374 | // qualifiers (if any) are those specified within the [ and ] of |
| 2375 | // the array type derivation. |
| 2376 | if (T->isArrayType()) |
| 2377 | Decayed = getArrayDecayedType(T); |
| 2378 | |
| 2379 | // C99 6.7.5.3p8: |
| 2380 | // A declaration of a parameter as "function returning type" |
| 2381 | // shall be adjusted to "pointer to function returning type", as |
| 2382 | // in 6.3.2.1. |
| 2383 | if (T->isFunctionType()) |
| 2384 | Decayed = getPointerType(T); |
| 2385 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2386 | llvm::FoldingSetNodeID ID; |
| 2387 | AdjustedType::Profile(ID, T, Decayed); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2388 | void *InsertPos = nullptr; |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2389 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2390 | if (AT) |
| 2391 | return QualType(AT, 0); |
| 2392 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2393 | QualType Canonical = getCanonicalType(Decayed); |
| 2394 | |
| 2395 | // Get the new insert position for the node we care about. |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2396 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2397 | assert(!AT && "Shouldn't be in the map!"); |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2398 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2399 | AT = new (*this, TypeAlignment) DecayedType(T, Decayed, Canonical); |
| 2400 | Types.push_back(AT); |
| 2401 | AdjustedTypes.InsertNode(AT, InsertPos); |
| 2402 | return QualType(AT, 0); |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2405 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2406 | /// a pointer to the specified block. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2407 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 0ac01283 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2408 | assert(T->isFunctionType() && "block of function types only"); |
| 2409 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2410 | // structure. |
| 2411 | llvm::FoldingSetNodeID ID; |
| 2412 | BlockPointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2414 | void *InsertPos = nullptr; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2415 | if (BlockPointerType *PT = |
| 2416 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2417 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2418 | |
| 2419 | // 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] | 2420 | // type either so fill in the canonical type field. |
| 2421 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2422 | if (!T.isCanonical()) { |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2423 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2424 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2425 | // Get the new insert position for the node we care about. |
| 2426 | BlockPointerType *NewIP = |
| 2427 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2428 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2429 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2430 | BlockPointerType *New |
| 2431 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2432 | Types.push_back(New); |
| 2433 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 2434 | return QualType(New, 0); |
| 2435 | } |
| 2436 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2437 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 2438 | /// lvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2439 | QualType |
| 2440 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 291e8ee | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 2441 | assert(getCanonicalType(T) != OverloadTy && |
| 2442 | "Unresolved overloaded function type"); |
| 2443 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2444 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2445 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2446 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2447 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2448 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2449 | void *InsertPos = nullptr; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2450 | if (LValueReferenceType *RT = |
| 2451 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2452 | return QualType(RT, 0); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2453 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2454 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2455 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2456 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2457 | // either, so fill in the canonical type field. |
| 2458 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2459 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 2460 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2461 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2462 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2463 | // Get the new insert position for the node we care about. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2464 | LValueReferenceType *NewIP = |
| 2465 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2466 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2469 | LValueReferenceType *New |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2470 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 2471 | SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2472 | Types.push_back(New); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2473 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2474 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2475 | return QualType(New, 0); |
| 2476 | } |
| 2477 | |
| 2478 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 2479 | /// rvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2480 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2481 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2482 | // structure. |
| 2483 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2484 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2485 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2486 | void *InsertPos = nullptr; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2487 | if (RValueReferenceType *RT = |
| 2488 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2489 | return QualType(RT, 0); |
| 2490 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2491 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2492 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2493 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2494 | // either, so fill in the canonical type field. |
| 2495 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2496 | if (InnerRef || !T.isCanonical()) { |
| 2497 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2498 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2499 | |
| 2500 | // Get the new insert position for the node we care about. |
| 2501 | RValueReferenceType *NewIP = |
| 2502 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2503 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2506 | RValueReferenceType *New |
| 2507 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2508 | Types.push_back(New); |
| 2509 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2510 | return QualType(New, 0); |
| 2511 | } |
| 2512 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2513 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 2514 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2515 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2516 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2517 | // structure. |
| 2518 | llvm::FoldingSetNodeID ID; |
| 2519 | MemberPointerType::Profile(ID, T, Cls); |
| 2520 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2521 | void *InsertPos = nullptr; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2522 | if (MemberPointerType *PT = |
| 2523 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2524 | return QualType(PT, 0); |
| 2525 | |
| 2526 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 2527 | // type either, so fill in the canonical type field. |
| 2528 | QualType Canonical; |
Douglas Gregor | 615ac67 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2529 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2530 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 2531 | |
| 2532 | // Get the new insert position for the node we care about. |
| 2533 | MemberPointerType *NewIP = |
| 2534 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2535 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2536 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2537 | MemberPointerType *New |
| 2538 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2539 | Types.push_back(New); |
| 2540 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 2541 | return QualType(New, 0); |
| 2542 | } |
| 2543 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2544 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2545 | /// array of the specified element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2546 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2547 | const llvm::APInt &ArySizeIn, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2548 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2549 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 2dfdb82 | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 2550 | assert((EltTy->isDependentType() || |
| 2551 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 2552 | "Constant array of VLAs is illegal!"); |
| 2553 | |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2554 | // Convert the array size into a canonical width matching the pointer size for |
| 2555 | // the target. |
| 2556 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2557 | ArySize = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2558 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2559 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2560 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2561 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2562 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2563 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2564 | if (ConstantArrayType *ATP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2565 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2566 | return QualType(ATP, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2567 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2568 | // If the element type isn't canonical or has qualifiers, this won't |
| 2569 | // be a canonical type either, so fill in the canonical type field. |
| 2570 | QualType Canon; |
| 2571 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2572 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2573 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2574 | ASM, IndexTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2575 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2576 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 2577 | // Get the new insert position for the node we care about. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2578 | ConstantArrayType *NewIP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2579 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2580 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 2581 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2582 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2583 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2584 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2585 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 2586 | Types.push_back(New); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2587 | return QualType(New, 0); |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2590 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 2591 | /// variably-modified, into the corresponding type with all the known |
| 2592 | /// sizes replaced with [*]. |
| 2593 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 2594 | // Vastly most common case. |
| 2595 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2596 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2597 | QualType result; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2598 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2599 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2600 | const Type *ty = split.Ty; |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2601 | switch (ty->getTypeClass()) { |
| 2602 | #define TYPE(Class, Base) |
| 2603 | #define ABSTRACT_TYPE(Class, Base) |
| 2604 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2605 | #include "clang/AST/TypeNodes.def" |
| 2606 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 2607 | |
| 2608 | // These types should never be variably-modified. |
| 2609 | case Type::Builtin: |
| 2610 | case Type::Complex: |
| 2611 | case Type::Vector: |
| 2612 | case Type::ExtVector: |
| 2613 | case Type::DependentSizedExtVector: |
| 2614 | case Type::ObjCObject: |
| 2615 | case Type::ObjCInterface: |
| 2616 | case Type::ObjCObjectPointer: |
| 2617 | case Type::Record: |
| 2618 | case Type::Enum: |
| 2619 | case Type::UnresolvedUsing: |
| 2620 | case Type::TypeOfExpr: |
| 2621 | case Type::TypeOf: |
| 2622 | case Type::Decltype: |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2623 | case Type::UnaryTransform: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2624 | case Type::DependentName: |
| 2625 | case Type::InjectedClassName: |
| 2626 | case Type::TemplateSpecialization: |
| 2627 | case Type::DependentTemplateSpecialization: |
| 2628 | case Type::TemplateTypeParm: |
| 2629 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2630 | case Type::Auto: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2631 | case Type::PackExpansion: |
| 2632 | llvm_unreachable("type should never be variably-modified"); |
| 2633 | |
| 2634 | // These types can be variably-modified but should never need to |
| 2635 | // further decay. |
| 2636 | case Type::FunctionNoProto: |
| 2637 | case Type::FunctionProto: |
| 2638 | case Type::BlockPointer: |
| 2639 | case Type::MemberPointer: |
| 2640 | return type; |
| 2641 | |
| 2642 | // These types can be variably-modified. All these modifications |
| 2643 | // preserve structure except as noted by comments. |
| 2644 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 2645 | // optimizations available here. |
| 2646 | case Type::Pointer: |
| 2647 | result = getPointerType(getVariableArrayDecayedType( |
| 2648 | cast<PointerType>(ty)->getPointeeType())); |
| 2649 | break; |
| 2650 | |
| 2651 | case Type::LValueReference: { |
| 2652 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 2653 | result = getLValueReferenceType( |
| 2654 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 2655 | lv->isSpelledAsLValue()); |
| 2656 | break; |
| 2657 | } |
| 2658 | |
| 2659 | case Type::RValueReference: { |
| 2660 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 2661 | result = getRValueReferenceType( |
| 2662 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 2663 | break; |
| 2664 | } |
| 2665 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2666 | case Type::Atomic: { |
| 2667 | const AtomicType *at = cast<AtomicType>(ty); |
| 2668 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 2669 | break; |
| 2670 | } |
| 2671 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2672 | case Type::ConstantArray: { |
| 2673 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 2674 | result = getConstantArrayType( |
| 2675 | getVariableArrayDecayedType(cat->getElementType()), |
| 2676 | cat->getSize(), |
| 2677 | cat->getSizeModifier(), |
| 2678 | cat->getIndexTypeCVRQualifiers()); |
| 2679 | break; |
| 2680 | } |
| 2681 | |
| 2682 | case Type::DependentSizedArray: { |
| 2683 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 2684 | result = getDependentSizedArrayType( |
| 2685 | getVariableArrayDecayedType(dat->getElementType()), |
| 2686 | dat->getSizeExpr(), |
| 2687 | dat->getSizeModifier(), |
| 2688 | dat->getIndexTypeCVRQualifiers(), |
| 2689 | dat->getBracketsRange()); |
| 2690 | break; |
| 2691 | } |
| 2692 | |
| 2693 | // Turn incomplete types into [*] types. |
| 2694 | case Type::IncompleteArray: { |
| 2695 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 2696 | result = getVariableArrayType( |
| 2697 | getVariableArrayDecayedType(iat->getElementType()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2698 | /*size*/ nullptr, |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2699 | ArrayType::Normal, |
| 2700 | iat->getIndexTypeCVRQualifiers(), |
| 2701 | SourceRange()); |
| 2702 | break; |
| 2703 | } |
| 2704 | |
| 2705 | // Turn VLA types into [*] types. |
| 2706 | case Type::VariableArray: { |
| 2707 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 2708 | result = getVariableArrayType( |
| 2709 | getVariableArrayDecayedType(vat->getElementType()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2710 | /*size*/ nullptr, |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2711 | ArrayType::Star, |
| 2712 | vat->getIndexTypeCVRQualifiers(), |
| 2713 | vat->getBracketsRange()); |
| 2714 | break; |
| 2715 | } |
| 2716 | } |
| 2717 | |
| 2718 | // Apply the top-level qualifiers from the original. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2719 | return getQualifiedType(result, split.Quals); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2720 | } |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2721 | |
Steve Naroff | cadebd0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 2722 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 2723 | /// variable array of the specified element type. |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2724 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 2725 | Expr *NumElts, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2726 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2727 | unsigned IndexTypeQuals, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2728 | SourceRange Brackets) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2729 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 2730 | // that have an expression provided for their size. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2731 | QualType Canon; |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2732 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2733 | // Be sure to pull qualifiers off the element type. |
| 2734 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2735 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2736 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2737 | IndexTypeQuals, Brackets); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2738 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2741 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2742 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2743 | |
| 2744 | VariableArrayTypes.push_back(New); |
| 2745 | Types.push_back(New); |
| 2746 | return QualType(New, 0); |
| 2747 | } |
| 2748 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2749 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 2750 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 2751 | /// type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2752 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 2753 | Expr *numElements, |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2754 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2755 | unsigned elementTypeQuals, |
| 2756 | SourceRange brackets) const { |
| 2757 | assert((!numElements || numElements->isTypeDependent() || |
| 2758 | numElements->isValueDependent()) && |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2759 | "Size must be type- or value-dependent!"); |
| 2760 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2761 | // Dependently-sized array types that do not have a specified number |
| 2762 | // of elements will have their sizes deduced from a dependent |
| 2763 | // initializer. We do no canonicalization here at all, which is okay |
| 2764 | // because they can't be used in most locations. |
| 2765 | if (!numElements) { |
| 2766 | DependentSizedArrayType *newType |
| 2767 | = new (*this, TypeAlignment) |
| 2768 | DependentSizedArrayType(*this, elementType, QualType(), |
| 2769 | numElements, ASM, elementTypeQuals, |
| 2770 | brackets); |
| 2771 | Types.push_back(newType); |
| 2772 | return QualType(newType, 0); |
| 2773 | } |
| 2774 | |
| 2775 | // Otherwise, we actually build a new type every time, but we |
| 2776 | // also build a canonical type. |
| 2777 | |
| 2778 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 2779 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2780 | void *insertPos = nullptr; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2781 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2782 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2783 | QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2784 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2785 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2786 | // Look for an existing type with these properties. |
| 2787 | DependentSizedArrayType *canonTy = |
| 2788 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2789 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2790 | // If we don't have one, build one. |
| 2791 | if (!canonTy) { |
| 2792 | canonTy = new (*this, TypeAlignment) |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2793 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2794 | QualType(), numElements, ASM, elementTypeQuals, |
| 2795 | brackets); |
| 2796 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 2797 | Types.push_back(canonTy); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2798 | } |
| 2799 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2800 | // Apply qualifiers from the element type to the array. |
| 2801 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2802 | canonElementType.Quals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2803 | |
David Majnemer | 16a7470 | 2015-07-24 05:54:19 +0000 | [diff] [blame] | 2804 | // If we didn't need extra canonicalization for the element type or the size |
| 2805 | // expression, then just use that as our result. |
| 2806 | if (QualType(canonElementType.Ty, 0) == elementType && |
| 2807 | canonTy->getSizeExpr() == numElements) |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2808 | return canon; |
| 2809 | |
| 2810 | // Otherwise, we need to build a type which follows the spelling |
| 2811 | // of the element type. |
| 2812 | DependentSizedArrayType *sugaredType |
| 2813 | = new (*this, TypeAlignment) |
| 2814 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 2815 | ASM, elementTypeQuals, brackets); |
| 2816 | Types.push_back(sugaredType); |
| 2817 | return QualType(sugaredType, 0); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2818 | } |
| 2819 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2820 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2821 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2822 | unsigned elementTypeQuals) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2823 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2824 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2825 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2826 | void *insertPos = nullptr; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2827 | if (IncompleteArrayType *iat = |
| 2828 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 2829 | return QualType(iat, 0); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2830 | |
| 2831 | // 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] | 2832 | // either, so fill in the canonical type field. We also have to pull |
| 2833 | // qualifiers off the element type. |
| 2834 | QualType canon; |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2835 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2836 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 2837 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2838 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2839 | ASM, elementTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2840 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2841 | |
| 2842 | // Get the new insert position for the node we care about. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2843 | IncompleteArrayType *existing = |
| 2844 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 2845 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 843ebedd | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 2846 | } |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2847 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2848 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 2849 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2850 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2851 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 2852 | Types.push_back(newType); |
| 2853 | return QualType(newType, 0); |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2856 | /// getVectorType - Return the unique reference to a vector type of |
| 2857 | /// 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] | 2858 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2859 | VectorType::VectorKind VecKind) const { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2860 | assert(vecType->isBuiltinType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2862 | // Check if we've already instantiated a vector of this type. |
| 2863 | llvm::FoldingSetNodeID ID; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2864 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2865 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2866 | void *InsertPos = nullptr; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2867 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2868 | return QualType(VTP, 0); |
| 2869 | |
| 2870 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2871 | // so fill in the canonical type field. |
| 2872 | QualType Canonical; |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 2873 | if (!vecType.isCanonical()) { |
Bob Wilson | 7795480 | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 2874 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2875 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2876 | // Get the new insert position for the node we care about. |
| 2877 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2878 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2879 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2880 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2881 | VectorType(vecType, NumElts, Canonical, VecKind); |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2882 | VectorTypes.InsertNode(New, InsertPos); |
| 2883 | Types.push_back(New); |
| 2884 | return QualType(New, 0); |
| 2885 | } |
| 2886 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2887 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2888 | /// 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] | 2889 | QualType |
| 2890 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2891 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2892 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2893 | // Check if we've already instantiated a vector of this type. |
| 2894 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2895 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2896 | VectorType::GenericVector); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2897 | void *InsertPos = nullptr; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2898 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2899 | return QualType(VTP, 0); |
| 2900 | |
| 2901 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2902 | // so fill in the canonical type field. |
| 2903 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2904 | if (!vecType.isCanonical()) { |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2905 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2906 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2907 | // Get the new insert position for the node we care about. |
| 2908 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2909 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2910 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2911 | ExtVectorType *New = new (*this, TypeAlignment) |
| 2912 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2913 | VectorTypes.InsertNode(New, InsertPos); |
| 2914 | Types.push_back(New); |
| 2915 | return QualType(New, 0); |
| 2916 | } |
| 2917 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2918 | QualType |
| 2919 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 2920 | Expr *SizeExpr, |
| 2921 | SourceLocation AttrLoc) const { |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2922 | llvm::FoldingSetNodeID ID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2923 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2924 | SizeExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2925 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2926 | void *InsertPos = nullptr; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2927 | DependentSizedExtVectorType *Canon |
| 2928 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2929 | DependentSizedExtVectorType *New; |
| 2930 | if (Canon) { |
| 2931 | // We already have a canonical version of this array type; use it as |
| 2932 | // the canonical type for a newly-built type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2933 | New = new (*this, TypeAlignment) |
| 2934 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2935 | SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2936 | } else { |
| 2937 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2938 | if (CanonVecTy == vecType) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2939 | New = new (*this, TypeAlignment) |
| 2940 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2941 | AttrLoc); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2942 | |
| 2943 | DependentSizedExtVectorType *CanonCheck |
| 2944 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2945 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2946 | (void)CanonCheck; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2947 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2948 | } else { |
| 2949 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2950 | SourceLocation()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2951 | New = new (*this, TypeAlignment) |
| 2952 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2953 | } |
| 2954 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2955 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2956 | Types.push_back(New); |
| 2957 | return QualType(New, 0); |
| 2958 | } |
| 2959 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2960 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2961 | /// |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2962 | QualType |
| 2963 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2964 | const FunctionType::ExtInfo &Info) const { |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2965 | const CallingConv CallConv = Info.getCC(); |
| 2966 | |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2967 | // Unique functions, to guarantee there is only one function of a particular |
| 2968 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2969 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2970 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2971 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2972 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2973 | if (FunctionNoProtoType *FT = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2974 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2975 | return QualType(FT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2976 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2977 | QualType Canonical; |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2978 | if (!ResultTy.isCanonical()) { |
| 2979 | Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2980 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2981 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2982 | FunctionNoProtoType *NewIP = |
| 2983 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2984 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2985 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2986 | |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2987 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2988 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2989 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2990 | Types.push_back(New); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2991 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2992 | return QualType(New, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Douglas Gregor | cd78037 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2995 | /// \brief Determine whether \p T is canonical as the result type of a function. |
| 2996 | static bool isCanonicalResultType(QualType T) { |
| 2997 | return T.isCanonical() && |
| 2998 | (T.getObjCLifetime() == Qualifiers::OCL_None || |
| 2999 | T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone); |
| 3000 | } |
| 3001 | |
Douglas Gregor | a602a15 | 2015-10-01 20:20:47 +0000 | [diff] [blame] | 3002 | CanQualType |
| 3003 | ASTContext::getCanonicalFunctionResultType(QualType ResultType) const { |
| 3004 | CanQualType CanResultType = getCanonicalType(ResultType); |
| 3005 | |
| 3006 | // Canonical result types do not have ARC lifetime qualifiers. |
| 3007 | if (CanResultType.getQualifiers().hasObjCLifetime()) { |
| 3008 | Qualifiers Qs = CanResultType.getQualifiers(); |
| 3009 | Qs.removeObjCLifetime(); |
| 3010 | return CanQualType::CreateUnsafe( |
| 3011 | getQualifiedType(CanResultType.getUnqualifiedType(), Qs)); |
| 3012 | } |
| 3013 | |
| 3014 | return CanResultType; |
| 3015 | } |
| 3016 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3017 | QualType |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3018 | ASTContext::getFunctionType(QualType ResultTy, ArrayRef<QualType> ArgArray, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3019 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3020 | size_t NumArgs = ArgArray.size(); |
| 3021 | |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3022 | // Unique functions, to guarantee there is only one function of a particular |
| 3023 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 3024 | llvm::FoldingSetNodeID ID; |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3025 | FunctionProtoType::Profile(ID, ResultTy, ArgArray.begin(), NumArgs, EPI, |
| 3026 | *this); |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 3027 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3028 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3029 | if (FunctionProtoType *FTP = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3030 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3031 | return QualType(FTP, 0); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3032 | |
| 3033 | // Determine whether the type being created is already canonical or not. |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 3034 | bool isCanonical = |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3035 | EPI.ExceptionSpec.Type == EST_None && isCanonicalResultType(ResultTy) && |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 3036 | !EPI.HasTrailingReturn; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3037 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3038 | if (!ArgArray[i].isCanonicalAsParam()) |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3039 | isCanonical = false; |
| 3040 | |
| 3041 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3042 | // The exception spec is not part of the canonical type. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3043 | QualType Canonical; |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 3044 | if (!isCanonical) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3045 | SmallVector<QualType, 16> CanonicalArgs; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3046 | CanonicalArgs.reserve(NumArgs); |
| 3047 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3048 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3049 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3050 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 3051 | CanonicalEPI.HasTrailingReturn = false; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3052 | CanonicalEPI.ExceptionSpec = FunctionProtoType::ExceptionSpecInfo(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3053 | |
Douglas Gregor | a602a15 | 2015-10-01 20:20:47 +0000 | [diff] [blame] | 3054 | // Adjust the canonical function result type. |
| 3055 | CanQualType CanResultTy = getCanonicalFunctionResultType(ResultTy); |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3056 | Canonical = getFunctionType(CanResultTy, CanonicalArgs, CanonicalEPI); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3057 | |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 3058 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3059 | FunctionProtoType *NewIP = |
| 3060 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3061 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3062 | } |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3063 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3064 | // FunctionProtoType objects are allocated with extra bytes after |
| 3065 | // them for three variable size arrays at the end: |
| 3066 | // - parameter types |
| 3067 | // - exception types |
| 3068 | // - consumed-arguments flags |
| 3069 | // Instead of the exception types, there could be a noexcept |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3070 | // expression, or information used to resolve the exception |
| 3071 | // specification. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3072 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3073 | NumArgs * sizeof(QualType); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3074 | if (EPI.ExceptionSpec.Type == EST_Dynamic) { |
| 3075 | Size += EPI.ExceptionSpec.Exceptions.size() * sizeof(QualType); |
| 3076 | } else if (EPI.ExceptionSpec.Type == EST_ComputedNoexcept) { |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 3077 | Size += sizeof(Expr*); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3078 | } else if (EPI.ExceptionSpec.Type == EST_Uninstantiated) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3079 | Size += 2 * sizeof(FunctionDecl*); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3080 | } else if (EPI.ExceptionSpec.Type == EST_Unevaluated) { |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3081 | Size += sizeof(FunctionDecl*); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3082 | } |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3083 | if (EPI.ConsumedParameters) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3084 | Size += NumArgs * sizeof(bool); |
| 3085 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3086 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 3087 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3088 | new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3089 | Types.push_back(FTP); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3090 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3091 | return QualType(FTP, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3092 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 3093 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3094 | #ifndef NDEBUG |
| 3095 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 3096 | if (!isa<CXXRecordDecl>(D)) return false; |
| 3097 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 3098 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 3099 | return true; |
| 3100 | if (RD->getDescribedClassTemplate() && |
| 3101 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 3102 | return true; |
| 3103 | return false; |
| 3104 | } |
| 3105 | #endif |
| 3106 | |
| 3107 | /// getInjectedClassNameType - Return the unique reference to the |
| 3108 | /// injected class name type for the specified templated declaration. |
| 3109 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3110 | QualType TST) const { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3111 | assert(NeedsInjectedClassNameType(Decl)); |
| 3112 | if (Decl->TypeForDecl) { |
| 3113 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3114 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3115 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 3116 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3117 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 3118 | } else { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3119 | Type *newType = |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3120 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3121 | Decl->TypeForDecl = newType; |
| 3122 | Types.push_back(newType); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3123 | } |
| 3124 | return QualType(Decl->TypeForDecl, 0); |
| 3125 | } |
| 3126 | |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3127 | /// getTypeDeclType - Return the unique reference to the type for the |
| 3128 | /// specified type declaration. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3129 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 89656d2 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 3130 | assert(Decl && "Passed null for Decl param"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3131 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3132 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3133 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3134 | return getTypedefType(Typedef); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3135 | |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3136 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 3137 | "Template type parameter types are always available."); |
| 3138 | |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 3139 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 3140 | assert(Record->isFirstDecl() && "struct/union has previous declaration"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3141 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3142 | return getRecordType(Record); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 3143 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 3144 | assert(Enum->isFirstDecl() && "enum has previous declaration"); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3145 | return getEnumType(Enum); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 3146 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3147 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3148 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 3149 | Decl->TypeForDecl = newType; |
| 3150 | Types.push_back(newType); |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3151 | } else |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3152 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 3153 | |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 3154 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 3157 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3158 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3159 | QualType |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3160 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 3161 | QualType Canonical) const { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3162 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3163 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3164 | if (Canonical.isNull()) |
| 3165 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3166 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3167 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3168 | Decl->TypeForDecl = newType; |
| 3169 | Types.push_back(newType); |
| 3170 | return QualType(newType, 0); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3173 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3174 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 3175 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3176 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3177 | if (PrevDecl->TypeForDecl) |
| 3178 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 3179 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3180 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 3181 | Decl->TypeForDecl = newType; |
| 3182 | Types.push_back(newType); |
| 3183 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3186 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3187 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 3188 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3189 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3190 | if (PrevDecl->TypeForDecl) |
| 3191 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 3192 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3193 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 3194 | Decl->TypeForDecl = newType; |
| 3195 | Types.push_back(newType); |
| 3196 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3197 | } |
| 3198 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 3199 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 3200 | QualType modifiedType, |
| 3201 | QualType equivalentType) { |
| 3202 | llvm::FoldingSetNodeID id; |
| 3203 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 3204 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3205 | void *insertPos = nullptr; |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 3206 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 3207 | if (type) return QualType(type, 0); |
| 3208 | |
| 3209 | QualType canon = getCanonicalType(equivalentType); |
| 3210 | type = new (*this, TypeAlignment) |
| 3211 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 3212 | |
| 3213 | Types.push_back(type); |
| 3214 | AttributedTypes.InsertNode(type, insertPos); |
| 3215 | |
| 3216 | return QualType(type, 0); |
| 3217 | } |
| 3218 | |
| 3219 | |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3220 | /// \brief Retrieve a substitution-result type. |
| 3221 | QualType |
| 3222 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3223 | QualType Replacement) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3224 | assert(Replacement.isCanonical() |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3225 | && "replacement types must always be canonical"); |
| 3226 | |
| 3227 | llvm::FoldingSetNodeID ID; |
| 3228 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3229 | void *InsertPos = nullptr; |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3230 | SubstTemplateTypeParmType *SubstParm |
| 3231 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3232 | |
| 3233 | if (!SubstParm) { |
| 3234 | SubstParm = new (*this, TypeAlignment) |
| 3235 | SubstTemplateTypeParmType(Parm, Replacement); |
| 3236 | Types.push_back(SubstParm); |
| 3237 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 3238 | } |
| 3239 | |
| 3240 | return QualType(SubstParm, 0); |
| 3241 | } |
| 3242 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3243 | /// \brief Retrieve a |
| 3244 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 3245 | const TemplateTypeParmType *Parm, |
| 3246 | const TemplateArgument &ArgPack) { |
| 3247 | #ifndef NDEBUG |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 3248 | for (const auto &P : ArgPack.pack_elements()) { |
| 3249 | assert(P.getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 3250 | assert(P.getAsType().isCanonical() && "Pack contains non-canonical type"); |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3251 | } |
| 3252 | #endif |
| 3253 | |
| 3254 | llvm::FoldingSetNodeID ID; |
| 3255 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3256 | void *InsertPos = nullptr; |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3257 | if (SubstTemplateTypeParmPackType *SubstParm |
| 3258 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3259 | return QualType(SubstParm, 0); |
| 3260 | |
| 3261 | QualType Canon; |
| 3262 | if (!Parm->isCanonicalUnqualified()) { |
| 3263 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 3264 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 3265 | ArgPack); |
| 3266 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3267 | } |
| 3268 | |
| 3269 | SubstTemplateTypeParmPackType *SubstParm |
| 3270 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 3271 | ArgPack); |
| 3272 | Types.push_back(SubstParm); |
| 3273 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 3274 | return QualType(SubstParm, 0); |
| 3275 | } |
| 3276 | |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3277 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3278 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3279 | /// name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3280 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3281 | bool ParameterPack, |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3282 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3283 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3284 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3285 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3286 | TemplateTypeParmType *TypeParm |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3287 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3288 | |
| 3289 | if (TypeParm) |
| 3290 | return QualType(TypeParm, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3291 | |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3292 | if (TTPDecl) { |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3293 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3294 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3295 | |
| 3296 | TemplateTypeParmType *TypeCheck |
| 3297 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3298 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 3299 | (void)TypeCheck; |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3300 | } else |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3301 | TypeParm = new (*this, TypeAlignment) |
| 3302 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3303 | |
| 3304 | Types.push_back(TypeParm); |
| 3305 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 3306 | |
| 3307 | return QualType(TypeParm, 0); |
| 3308 | } |
| 3309 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3310 | TypeSourceInfo * |
| 3311 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 3312 | SourceLocation NameLoc, |
| 3313 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3314 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3315 | assert(!Name.getAsDependentTemplateName() && |
| 3316 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3317 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3318 | |
| 3319 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3320 | TemplateSpecializationTypeLoc TL = |
| 3321 | DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>(); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3322 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3323 | TL.setTemplateNameLoc(NameLoc); |
| 3324 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 3325 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 3326 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 3327 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 3328 | return DI; |
| 3329 | } |
| 3330 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3331 | QualType |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3332 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3333 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3334 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3335 | assert(!Template.getAsDependentTemplateName() && |
| 3336 | "No dependent template names here!"); |
| 3337 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3338 | unsigned NumArgs = Args.size(); |
| 3339 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3340 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3341 | ArgVec.reserve(NumArgs); |
| 3342 | for (unsigned i = 0; i != NumArgs; ++i) |
| 3343 | ArgVec.push_back(Args[i].getArgument()); |
| 3344 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3345 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3346 | Underlying); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3349 | #ifndef NDEBUG |
| 3350 | static bool hasAnyPackExpansions(const TemplateArgument *Args, |
| 3351 | unsigned NumArgs) { |
| 3352 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3353 | if (Args[I].isPackExpansion()) |
| 3354 | return true; |
| 3355 | |
| 3356 | return true; |
| 3357 | } |
| 3358 | #endif |
| 3359 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3360 | QualType |
| 3361 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3362 | const TemplateArgument *Args, |
| 3363 | unsigned NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3364 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3365 | assert(!Template.getAsDependentTemplateName() && |
| 3366 | "No dependent template names here!"); |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3367 | // Look through qualified template names. |
| 3368 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3369 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3370 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3371 | bool IsTypeAlias = |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3372 | Template.getAsTemplateDecl() && |
| 3373 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3374 | QualType CanonType; |
| 3375 | if (!Underlying.isNull()) |
| 3376 | CanonType = getCanonicalType(Underlying); |
| 3377 | else { |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3378 | // We can get here with an alias template when the specialization contains |
| 3379 | // a pack expansion that does not match up with a parameter pack. |
| 3380 | assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) && |
| 3381 | "Caller must compute aliased type"); |
| 3382 | IsTypeAlias = false; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3383 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 3384 | NumArgs); |
| 3385 | } |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3386 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3387 | // Allocate the (non-canonical) template specialization type, but don't |
| 3388 | // try to unique it: these types typically have location information that |
| 3389 | // we don't unique and don't want to lose. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3390 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 3391 | sizeof(TemplateArgument) * NumArgs + |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3392 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3393 | TypeAlignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3394 | TemplateSpecializationType *Spec |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3395 | = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType, |
| 3396 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3398 | Types.push_back(Spec); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3399 | return QualType(Spec, 0); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3402 | QualType |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3403 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 3404 | const TemplateArgument *Args, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3405 | unsigned NumArgs) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3406 | assert(!Template.getAsDependentTemplateName() && |
| 3407 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3408 | |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3409 | // Look through qualified template names. |
| 3410 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3411 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3412 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3413 | // Build the canonical template specialization type. |
| 3414 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3415 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3416 | CanonArgs.reserve(NumArgs); |
| 3417 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3418 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 3419 | |
| 3420 | // Determine whether this canonical template specialization type already |
| 3421 | // exists. |
| 3422 | llvm::FoldingSetNodeID ID; |
| 3423 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 3424 | CanonArgs.data(), NumArgs, *this); |
| 3425 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3426 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3427 | TemplateSpecializationType *Spec |
| 3428 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3429 | |
| 3430 | if (!Spec) { |
| 3431 | // Allocate a new canonical template specialization type. |
| 3432 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 3433 | sizeof(TemplateArgument) * NumArgs), |
| 3434 | TypeAlignment); |
| 3435 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 3436 | CanonArgs.data(), NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3437 | QualType(), QualType()); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3438 | Types.push_back(Spec); |
| 3439 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 3440 | } |
| 3441 | |
| 3442 | assert(Spec->isDependentType() && |
| 3443 | "Non-dependent template-id type must have a canonical type"); |
| 3444 | return QualType(Spec, 0); |
| 3445 | } |
| 3446 | |
| 3447 | QualType |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3448 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 3449 | NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3450 | QualType NamedType) const { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3451 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3452 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3453 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3454 | void *InsertPos = nullptr; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3455 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3456 | if (T) |
| 3457 | return QualType(T, 0); |
| 3458 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3459 | QualType Canon = NamedType; |
| 3460 | if (!Canon.isCanonical()) { |
| 3461 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3462 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3463 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3464 | (void)CheckT; |
| 3465 | } |
| 3466 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 3467 | T = new (*this, TypeAlignment) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3468 | Types.push_back(T); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3469 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3470 | return QualType(T, 0); |
| 3471 | } |
| 3472 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3473 | QualType |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3474 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3475 | llvm::FoldingSetNodeID ID; |
| 3476 | ParenType::Profile(ID, InnerType); |
| 3477 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3478 | void *InsertPos = nullptr; |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3479 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3480 | if (T) |
| 3481 | return QualType(T, 0); |
| 3482 | |
| 3483 | QualType Canon = InnerType; |
| 3484 | if (!Canon.isCanonical()) { |
| 3485 | Canon = getCanonicalType(InnerType); |
| 3486 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3487 | assert(!CheckT && "Paren canonical type broken"); |
| 3488 | (void)CheckT; |
| 3489 | } |
| 3490 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 3491 | T = new (*this, TypeAlignment) ParenType(InnerType, Canon); |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3492 | Types.push_back(T); |
| 3493 | ParenTypes.InsertNode(T, InsertPos); |
| 3494 | return QualType(T, 0); |
| 3495 | } |
| 3496 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3497 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 3498 | NestedNameSpecifier *NNS, |
| 3499 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3500 | QualType Canon) const { |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3501 | if (Canon.isNull()) { |
| 3502 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3503 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3504 | if (Keyword == ETK_None) |
| 3505 | CanonKeyword = ETK_Typename; |
| 3506 | |
| 3507 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 3508 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
| 3511 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3512 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3513 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3514 | void *InsertPos = nullptr; |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3515 | DependentNameType *T |
| 3516 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3517 | if (T) |
| 3518 | return QualType(T, 0); |
| 3519 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 3520 | T = new (*this, TypeAlignment) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3521 | Types.push_back(T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3522 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3523 | return QualType(T, 0); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3524 | } |
| 3525 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3526 | QualType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3527 | ASTContext::getDependentTemplateSpecializationType( |
| 3528 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3529 | NestedNameSpecifier *NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3530 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3531 | const TemplateArgumentListInfo &Args) const { |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3532 | // TODO: avoid this copy |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3533 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3534 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 3535 | ArgCopy.push_back(Args[I].getArgument()); |
| 3536 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 3537 | ArgCopy.size(), |
| 3538 | ArgCopy.data()); |
| 3539 | } |
| 3540 | |
| 3541 | QualType |
| 3542 | ASTContext::getDependentTemplateSpecializationType( |
| 3543 | ElaboratedTypeKeyword Keyword, |
| 3544 | NestedNameSpecifier *NNS, |
| 3545 | const IdentifierInfo *Name, |
| 3546 | unsigned NumArgs, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3547 | const TemplateArgument *Args) const { |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 3548 | assert((!NNS || NNS->isDependent()) && |
| 3549 | "nested-name-specifier must be dependent"); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3550 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3551 | llvm::FoldingSetNodeID ID; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3552 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 3553 | Name, NumArgs, Args); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3554 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3555 | void *InsertPos = nullptr; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3556 | DependentTemplateSpecializationType *T |
| 3557 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3558 | if (T) |
| 3559 | return QualType(T, 0); |
| 3560 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3561 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3562 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3563 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3564 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 3565 | |
| 3566 | bool AnyNonCanonArgs = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3567 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3568 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 3569 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 3570 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 3571 | AnyNonCanonArgs = true; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3572 | } |
| 3573 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3574 | QualType Canon; |
| 3575 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 3576 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 3577 | Name, NumArgs, |
| 3578 | CanonArgs.data()); |
| 3579 | |
| 3580 | // Find the insert position again. |
| 3581 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3582 | } |
| 3583 | |
| 3584 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 3585 | sizeof(TemplateArgument) * NumArgs), |
| 3586 | TypeAlignment); |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 3587 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3588 | Name, NumArgs, Args, Canon); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3589 | Types.push_back(T); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3590 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3591 | return QualType(T, 0); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3594 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3595 | Optional<unsigned> NumExpansions) { |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3596 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3597 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3598 | |
| 3599 | assert(Pattern->containsUnexpandedParameterPack() && |
| 3600 | "Pack expansions must expand one or more parameter packs"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3601 | void *InsertPos = nullptr; |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3602 | PackExpansionType *T |
| 3603 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3604 | if (T) |
| 3605 | return QualType(T, 0); |
| 3606 | |
| 3607 | QualType Canon; |
| 3608 | if (!Pattern.isCanonical()) { |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3609 | Canon = getCanonicalType(Pattern); |
| 3610 | // The canonical type might not contain an unexpanded parameter pack, if it |
| 3611 | // contains an alias template specialization which ignores one of its |
| 3612 | // parameters. |
| 3613 | if (Canon->containsUnexpandedParameterPack()) { |
Richard Smith | 8b4e1e2 | 2014-07-10 01:20:17 +0000 | [diff] [blame] | 3614 | Canon = getPackExpansionType(Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3615 | |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3616 | // Find the insert position again, in case we inserted an element into |
| 3617 | // PackExpansionTypes and invalidated our insert position. |
| 3618 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3619 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3620 | } |
| 3621 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 3622 | T = new (*this, TypeAlignment) |
| 3623 | PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3624 | Types.push_back(T); |
| 3625 | PackExpansionTypes.InsertNode(T, InsertPos); |
Richard Smith | 8b4e1e2 | 2014-07-10 01:20:17 +0000 | [diff] [blame] | 3626 | return QualType(T, 0); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3629 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 3630 | /// alphabetically. |
Benjamin Kramer | 0eb262f | 2015-03-14 13:32:49 +0000 | [diff] [blame] | 3631 | static int CmpProtocolNames(ObjCProtocolDecl *const *LHS, |
| 3632 | ObjCProtocolDecl *const *RHS) { |
| 3633 | return DeclarationName::compare((*LHS)->getDeclName(), (*RHS)->getDeclName()); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3634 | } |
| 3635 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3636 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3637 | unsigned NumProtocols) { |
| 3638 | if (NumProtocols == 0) return true; |
| 3639 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3640 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 3641 | return false; |
| 3642 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3643 | for (unsigned i = 1; i != NumProtocols; ++i) |
Benjamin Kramer | 0eb262f | 2015-03-14 13:32:49 +0000 | [diff] [blame] | 3644 | if (CmpProtocolNames(&Protocols[i - 1], &Protocols[i]) >= 0 || |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3645 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3646 | return false; |
| 3647 | return true; |
| 3648 | } |
| 3649 | |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 3650 | static void |
| 3651 | SortAndUniqueProtocols(SmallVectorImpl<ObjCProtocolDecl *> &Protocols) { |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3652 | // Sort protocols, keyed by name. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 3653 | llvm::array_pod_sort(Protocols.begin(), Protocols.end(), CmpProtocolNames); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3654 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3655 | // Canonicalize. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 3656 | for (ObjCProtocolDecl *&P : Protocols) |
| 3657 | P = P->getCanonicalDecl(); |
| 3658 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3659 | // Remove duplicates. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 3660 | auto ProtocolsEnd = std::unique(Protocols.begin(), Protocols.end()); |
| 3661 | Protocols.erase(ProtocolsEnd, Protocols.end()); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3664 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 3665 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3666 | unsigned NumProtocols) const { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3667 | return getObjCObjectType(BaseType, { }, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3668 | llvm::makeArrayRef(Protocols, NumProtocols), |
| 3669 | /*isKindOf=*/false); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | QualType ASTContext::getObjCObjectType( |
| 3673 | QualType baseType, |
| 3674 | ArrayRef<QualType> typeArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3675 | ArrayRef<ObjCProtocolDecl *> protocols, |
| 3676 | bool isKindOf) const { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3677 | // If the base type is an interface and there aren't any protocols or |
| 3678 | // type arguments to add, then the interface type will do just fine. |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3679 | if (typeArgs.empty() && protocols.empty() && !isKindOf && |
| 3680 | isa<ObjCInterfaceType>(baseType)) |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3681 | return baseType; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3682 | |
| 3683 | // Look in the folding set for an existing type. |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3684 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3685 | ObjCObjectTypeImpl::Profile(ID, baseType, typeArgs, protocols, isKindOf); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3686 | void *InsertPos = nullptr; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3687 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3688 | return QualType(QT, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3689 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3690 | // Determine the type arguments to be used for canonicalization, |
| 3691 | // which may be explicitly specified here or written on the base |
| 3692 | // type. |
| 3693 | ArrayRef<QualType> effectiveTypeArgs = typeArgs; |
| 3694 | if (effectiveTypeArgs.empty()) { |
| 3695 | if (auto baseObject = baseType->getAs<ObjCObjectType>()) |
| 3696 | effectiveTypeArgs = baseObject->getTypeArgs(); |
| 3697 | } |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3698 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3699 | // Build the canonical type, which has the canonical base type and a |
| 3700 | // sorted-and-uniqued list of protocols and the type arguments |
| 3701 | // canonicalized. |
| 3702 | QualType canonical; |
| 3703 | bool typeArgsAreCanonical = std::all_of(effectiveTypeArgs.begin(), |
| 3704 | effectiveTypeArgs.end(), |
| 3705 | [&](QualType type) { |
| 3706 | return type.isCanonical(); |
| 3707 | }); |
| 3708 | bool protocolsSorted = areSortedAndUniqued(protocols.data(), |
| 3709 | protocols.size()); |
| 3710 | if (!typeArgsAreCanonical || !protocolsSorted || !baseType.isCanonical()) { |
| 3711 | // Determine the canonical type arguments. |
| 3712 | ArrayRef<QualType> canonTypeArgs; |
| 3713 | SmallVector<QualType, 4> canonTypeArgsVec; |
| 3714 | if (!typeArgsAreCanonical) { |
| 3715 | canonTypeArgsVec.reserve(effectiveTypeArgs.size()); |
| 3716 | for (auto typeArg : effectiveTypeArgs) |
| 3717 | canonTypeArgsVec.push_back(getCanonicalType(typeArg)); |
| 3718 | canonTypeArgs = canonTypeArgsVec; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3719 | } else { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3720 | canonTypeArgs = effectiveTypeArgs; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3723 | ArrayRef<ObjCProtocolDecl *> canonProtocols; |
| 3724 | SmallVector<ObjCProtocolDecl*, 8> canonProtocolsVec; |
| 3725 | if (!protocolsSorted) { |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 3726 | canonProtocolsVec.append(protocols.begin(), protocols.end()); |
| 3727 | SortAndUniqueProtocols(canonProtocolsVec); |
| 3728 | canonProtocols = canonProtocolsVec; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3729 | } else { |
| 3730 | canonProtocols = protocols; |
| 3731 | } |
| 3732 | |
| 3733 | canonical = getObjCObjectType(getCanonicalType(baseType), canonTypeArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3734 | canonProtocols, isKindOf); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3735 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3736 | // Regenerate InsertPos. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3737 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3738 | } |
| 3739 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3740 | unsigned size = sizeof(ObjCObjectTypeImpl); |
| 3741 | size += typeArgs.size() * sizeof(QualType); |
| 3742 | size += protocols.size() * sizeof(ObjCProtocolDecl *); |
| 3743 | void *mem = Allocate(size, TypeAlignment); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3744 | ObjCObjectTypeImpl *T = |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3745 | new (mem) ObjCObjectTypeImpl(canonical, baseType, typeArgs, protocols, |
| 3746 | isKindOf); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3747 | |
| 3748 | Types.push_back(T); |
| 3749 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 3750 | return QualType(T, 0); |
| 3751 | } |
| 3752 | |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3753 | /// ObjCObjectAdoptsQTypeProtocols - Checks that protocols in IC's |
| 3754 | /// protocol list adopt all protocols in QT's qualified-id protocol |
| 3755 | /// list. |
| 3756 | bool ASTContext::ObjCObjectAdoptsQTypeProtocols(QualType QT, |
| 3757 | ObjCInterfaceDecl *IC) { |
| 3758 | if (!QT->isObjCQualifiedIdType()) |
| 3759 | return false; |
| 3760 | |
| 3761 | if (const ObjCObjectPointerType *OPT = QT->getAs<ObjCObjectPointerType>()) { |
| 3762 | // If both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 3763 | for (auto *Proto : OPT->quals()) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3764 | if (!IC->ClassImplementsProtocol(Proto, false)) |
| 3765 | return false; |
| 3766 | } |
| 3767 | return true; |
| 3768 | } |
| 3769 | return false; |
| 3770 | } |
| 3771 | |
| 3772 | /// QIdProtocolsAdoptObjCObjectProtocols - Checks that protocols in |
| 3773 | /// QT's qualified-id protocol list adopt all protocols in IDecl's list |
| 3774 | /// of protocols. |
| 3775 | bool ASTContext::QIdProtocolsAdoptObjCObjectProtocols(QualType QT, |
| 3776 | ObjCInterfaceDecl *IDecl) { |
| 3777 | if (!QT->isObjCQualifiedIdType()) |
| 3778 | return false; |
| 3779 | const ObjCObjectPointerType *OPT = QT->getAs<ObjCObjectPointerType>(); |
| 3780 | if (!OPT) |
| 3781 | return false; |
| 3782 | if (!IDecl->hasDefinition()) |
| 3783 | return false; |
Fariborz Jahanian | 91fb0be | 2013-11-20 19:01:50 +0000 | [diff] [blame] | 3784 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocols; |
| 3785 | CollectInheritedProtocols(IDecl, InheritedProtocols); |
| 3786 | if (InheritedProtocols.empty()) |
| 3787 | return false; |
Fariborz Jahanian | 48a01cb | 2014-04-04 23:53:45 +0000 | [diff] [blame] | 3788 | // Check that if every protocol in list of id<plist> conforms to a protcol |
| 3789 | // of IDecl's, then bridge casting is ok. |
| 3790 | bool Conforms = false; |
| 3791 | for (auto *Proto : OPT->quals()) { |
| 3792 | Conforms = false; |
| 3793 | for (auto *PI : InheritedProtocols) { |
| 3794 | if (ProtocolCompatibleWithProtocol(Proto, PI)) { |
| 3795 | Conforms = true; |
| 3796 | break; |
| 3797 | } |
| 3798 | } |
| 3799 | if (!Conforms) |
| 3800 | break; |
| 3801 | } |
| 3802 | if (Conforms) |
| 3803 | return true; |
| 3804 | |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 3805 | for (auto *PI : InheritedProtocols) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3806 | // If both the right and left sides have qualifiers. |
| 3807 | bool Adopts = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 3808 | for (auto *Proto : OPT->quals()) { |
Fariborz Jahanian | 48a01cb | 2014-04-04 23:53:45 +0000 | [diff] [blame] | 3809 | // return 'true' if 'PI' is in the inheritance hierarchy of Proto |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 3810 | if ((Adopts = ProtocolCompatibleWithProtocol(PI, Proto))) |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3811 | break; |
| 3812 | } |
| 3813 | if (!Adopts) |
Fariborz Jahanian | 91fb0be | 2013-11-20 19:01:50 +0000 | [diff] [blame] | 3814 | return false; |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3815 | } |
| 3816 | return true; |
| 3817 | } |
| 3818 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3819 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 3820 | /// the given object type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3821 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3822 | llvm::FoldingSetNodeID ID; |
| 3823 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 3824 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3825 | void *InsertPos = nullptr; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3826 | if (ObjCObjectPointerType *QT = |
| 3827 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3828 | return QualType(QT, 0); |
| 3829 | |
| 3830 | // Find the canonical object type. |
| 3831 | QualType Canonical; |
| 3832 | if (!ObjectT.isCanonical()) { |
| 3833 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 3834 | |
| 3835 | // Regenerate InsertPos. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3836 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3837 | } |
| 3838 | |
Douglas Gregor | f85bee6 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 3839 | // No match. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3840 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 3841 | ObjCObjectPointerType *QType = |
| 3842 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3843 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3844 | Types.push_back(QType); |
| 3845 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3846 | return QualType(QType, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3847 | } |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3848 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3849 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 3850 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3851 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 3852 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3853 | if (Decl->TypeForDecl) |
| 3854 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3855 | |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3856 | if (PrevDecl) { |
| 3857 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 3858 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3859 | return QualType(PrevDecl->TypeForDecl, 0); |
| 3860 | } |
| 3861 | |
Douglas Gregor | 7671e53 | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 3862 | // Prefer the definition, if there is one. |
| 3863 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 3864 | Decl = Def; |
| 3865 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3866 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 3867 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 3868 | Decl->TypeForDecl = T; |
| 3869 | Types.push_back(T); |
| 3870 | return QualType(T, 0); |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3873 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 3874 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3875 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3876 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3877 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3878 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3879 | TypeOfExprType *toe; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3880 | if (tofExpr->isTypeDependent()) { |
| 3881 | llvm::FoldingSetNodeID ID; |
| 3882 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3883 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3884 | void *InsertPos = nullptr; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3885 | DependentTypeOfExprType *Canon |
| 3886 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3887 | if (Canon) { |
| 3888 | // We already have a "canonical" version of an identical, dependent |
| 3889 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3890 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3891 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3892 | } else { |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3893 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3894 | Canon |
| 3895 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3896 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 3897 | toe = Canon; |
| 3898 | } |
| 3899 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3900 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3901 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3902 | } |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3903 | Types.push_back(toe); |
| 3904 | return QualType(toe, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3907 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3908 | /// TypeOfType nodes. The only motivation to unique these nodes would be |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3909 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3910 | /// an issue. This doesn't affect the type checker, since it operates |
| 3911 | /// on canonical types (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3912 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3913 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3914 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3915 | Types.push_back(tot); |
| 3916 | return QualType(tot, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3917 | } |
| 3918 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 3919 | |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3920 | /// \brief Unlike many "get<Type>" functions, we don't unique DecltypeType |
| 3921 | /// nodes. This would never be helpful, since each such type has its own |
| 3922 | /// expression, and would not give a significant memory saving, since there |
| 3923 | /// is an Expr tree under each such type. |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3924 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3925 | DecltypeType *dt; |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3926 | |
| 3927 | // C++11 [temp.type]p2: |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3928 | // If an expression e involves a template parameter, decltype(e) denotes a |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3929 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 3930 | // type only if their expressions are equivalent (14.5.6.1). |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3931 | if (e->isInstantiationDependent()) { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3932 | llvm::FoldingSetNodeID ID; |
| 3933 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3935 | void *InsertPos = nullptr; |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3936 | DependentDecltypeType *Canon |
| 3937 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3938 | if (!Canon) { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3939 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3940 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3941 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3942 | } |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3943 | dt = new (*this, TypeAlignment) |
| 3944 | DecltypeType(e, UnderlyingType, QualType((DecltypeType *)Canon, 0)); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3945 | } else { |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3946 | dt = new (*this, TypeAlignment) |
| 3947 | DecltypeType(e, UnderlyingType, getCanonicalType(UnderlyingType)); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3948 | } |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3949 | Types.push_back(dt); |
| 3950 | return QualType(dt, 0); |
| 3951 | } |
| 3952 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3953 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 3954 | /// savings are minimal and these are rare. |
| 3955 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 3956 | QualType UnderlyingType, |
| 3957 | UnaryTransformType::UTTKind Kind) |
| 3958 | const { |
| 3959 | UnaryTransformType *Ty = |
Douglas Gregor | 6c6e676 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 3960 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 3961 | Kind, |
| 3962 | UnderlyingType->isDependentType() ? |
Peter Collingbourne | 15d48ec | 2012-03-05 16:02:06 +0000 | [diff] [blame] | 3963 | QualType() : getCanonicalType(UnderlyingType)); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3964 | Types.push_back(Ty); |
| 3965 | return QualType(Ty, 0); |
| 3966 | } |
| 3967 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3968 | /// getAutoType - Return the uniqued reference to the 'auto' type which has been |
| 3969 | /// deduced to the given type, or to the canonical undeduced 'auto' type, or the |
| 3970 | /// canonical deduced-but-dependent 'auto' type. |
| 3971 | QualType ASTContext::getAutoType(QualType DeducedType, bool IsDecltypeAuto, |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3972 | bool IsDependent) const { |
| 3973 | if (DeducedType.isNull() && !IsDecltypeAuto && !IsDependent) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3974 | return getAutoDeductType(); |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3975 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3976 | // Look in the folding set for an existing type. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3977 | void *InsertPos = nullptr; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3978 | llvm::FoldingSetNodeID ID; |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3979 | AutoType::Profile(ID, DeducedType, IsDecltypeAuto, IsDependent); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3980 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3981 | return QualType(AT, 0); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3982 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3983 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType, |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3984 | IsDecltypeAuto, |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3985 | IsDependent); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3986 | Types.push_back(AT); |
| 3987 | if (InsertPos) |
| 3988 | AutoTypes.InsertNode(AT, InsertPos); |
| 3989 | return QualType(AT, 0); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3990 | } |
| 3991 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3992 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 3993 | /// the given value type. |
| 3994 | QualType ASTContext::getAtomicType(QualType T) const { |
| 3995 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3996 | // structure. |
| 3997 | llvm::FoldingSetNodeID ID; |
| 3998 | AtomicType::Profile(ID, T); |
| 3999 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4000 | void *InsertPos = nullptr; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4001 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4002 | return QualType(AT, 0); |
| 4003 | |
| 4004 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 4005 | // either, so fill in the canonical type field. |
| 4006 | QualType Canonical; |
| 4007 | if (!T.isCanonical()) { |
| 4008 | Canonical = getAtomicType(getCanonicalType(T)); |
| 4009 | |
| 4010 | // Get the new insert position for the node we care about. |
| 4011 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4012 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4013 | } |
| 4014 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 4015 | Types.push_back(New); |
| 4016 | AtomicTypes.InsertNode(New, InsertPos); |
| 4017 | return QualType(New, 0); |
| 4018 | } |
| 4019 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 4020 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 4021 | QualType ASTContext::getAutoDeductType() const { |
| 4022 | if (AutoDeductTy.isNull()) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4023 | AutoDeductTy = QualType( |
| 4024 | new (*this, TypeAlignment) AutoType(QualType(), /*decltype(auto)*/false, |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4025 | /*dependent*/false), |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4026 | 0); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 4027 | return AutoDeductTy; |
| 4028 | } |
| 4029 | |
| 4030 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 4031 | QualType ASTContext::getAutoRRefDeductType() const { |
| 4032 | if (AutoRRefDeductTy.isNull()) |
| 4033 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 4034 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 4035 | return AutoRRefDeductTy; |
| 4036 | } |
| 4037 | |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 4038 | /// getTagDeclType - Return the unique reference to the type for the |
| 4039 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4040 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | 2b0ce11 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 4041 | assert (Decl); |
Mike Stump | b93185d | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 4042 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 4043 | // away const? mutable? |
| 4044 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4047 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 4048 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 4049 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4050 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4051 | return getFromTargetType(Target->getSizeType()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 4052 | } |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 4053 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 4054 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 4055 | CanQualType ASTContext::getIntMaxType() const { |
| 4056 | return getFromTargetType(Target->getIntMaxType()); |
| 4057 | } |
| 4058 | |
| 4059 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 4060 | CanQualType ASTContext::getUIntMaxType() const { |
| 4061 | return getFromTargetType(Target->getUIntMaxType()); |
| 4062 | } |
| 4063 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4064 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 4065 | /// Used when in C++, as a GCC extension. |
| 4066 | QualType ASTContext::getSignedWCharType() const { |
| 4067 | // FIXME: derive from "Target" ? |
| 4068 | return WCharTy; |
| 4069 | } |
| 4070 | |
| 4071 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 4072 | /// Used when in C++, as a GCC extension. |
| 4073 | QualType ASTContext::getUnsignedWCharType() const { |
| 4074 | // FIXME: derive from "Target" ? |
| 4075 | return UnsignedIntTy; |
| 4076 | } |
| 4077 | |
Enea Zaffanella | f11ceb6 | 2013-01-26 17:08:37 +0000 | [diff] [blame] | 4078 | QualType ASTContext::getIntPtrType() const { |
| 4079 | return getFromTargetType(Target->getIntPtrType()); |
| 4080 | } |
| 4081 | |
| 4082 | QualType ASTContext::getUIntPtrType() const { |
| 4083 | return getCorrespondingUnsignedType(getIntPtrType()); |
| 4084 | } |
| 4085 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 4086 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 4087 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 4088 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4089 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
Eli Friedman | 4e91899e | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 4092 | /// \brief Return the unique type for "pid_t" defined in |
| 4093 | /// <sys/types.h>. We need this to compute the correct type for vfork(). |
| 4094 | QualType ASTContext::getProcessIDType() const { |
| 4095 | return getFromTargetType(Target->getProcessIDType()); |
| 4096 | } |
| 4097 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4098 | //===----------------------------------------------------------------------===// |
| 4099 | // Type Operators |
| 4100 | //===----------------------------------------------------------------------===// |
| 4101 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4102 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4103 | // Push qualifiers into arrays, and then discard any remaining |
| 4104 | // qualifiers. |
| 4105 | T = getCanonicalType(T); |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4106 | T = getVariableArrayDecayedType(T); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4107 | const Type *Ty = T.getTypePtr(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4108 | QualType Result; |
| 4109 | if (isa<ArrayType>(Ty)) { |
| 4110 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 4111 | } else if (isa<FunctionType>(Ty)) { |
| 4112 | Result = getPointerType(QualType(Ty, 0)); |
| 4113 | } else { |
| 4114 | Result = QualType(Ty, 0); |
| 4115 | } |
| 4116 | |
| 4117 | return CanQualType::CreateUnsafe(Result); |
| 4118 | } |
| 4119 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4120 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 4121 | Qualifiers &quals) { |
| 4122 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 4123 | |
| 4124 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 4125 | // the unqualified desugared type and then drops it on the floor. |
| 4126 | // We then have to strip that sugar back off with |
| 4127 | // getUnqualifiedDesugaredType(), which is silly. |
| 4128 | const ArrayType *AT = |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4129 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4130 | |
| 4131 | // 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] | 4132 | if (!AT) { |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4133 | quals = splitType.Quals; |
| 4134 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4137 | // Otherwise, recurse on the array's element type. |
| 4138 | QualType elementType = AT->getElementType(); |
| 4139 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 4140 | |
| 4141 | // If that didn't change the element type, AT has no qualifiers, so we |
| 4142 | // can just use the results in splitType. |
| 4143 | if (elementType == unqualElementType) { |
| 4144 | assert(quals.empty()); // from the recursive call |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4145 | quals = splitType.Quals; |
| 4146 | return QualType(splitType.Ty, 0); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | // Otherwise, add in the qualifiers from the outermost type, then |
| 4150 | // build the type back up. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4151 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4152 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 4153 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4154 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4155 | CAT->getSizeModifier(), 0); |
| 4156 | } |
| 4157 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 4158 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4159 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 4162 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4163 | return getVariableArrayType(unqualElementType, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4164 | VAT->getSizeExpr(), |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 4165 | VAT->getSizeModifier(), |
| 4166 | VAT->getIndexTypeCVRQualifiers(), |
| 4167 | VAT->getBracketsRange()); |
| 4168 | } |
| 4169 | |
| 4170 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4171 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4172 | DSAT->getSizeModifier(), 0, |
| 4173 | SourceRange()); |
| 4174 | } |
| 4175 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 4176 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 4177 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 4178 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 4179 | /// or pointer-to-member types, or if they are not similar at this |
| 4180 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 4181 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 4182 | /// be called in a loop that successively "unwraps" pointer and |
| 4183 | /// pointer-to-member types to compare them at each level. |
| 4184 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 4185 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 4186 | *T2PtrType = T2->getAs<PointerType>(); |
| 4187 | if (T1PtrType && T2PtrType) { |
| 4188 | T1 = T1PtrType->getPointeeType(); |
| 4189 | T2 = T2PtrType->getPointeeType(); |
| 4190 | return true; |
| 4191 | } |
| 4192 | |
| 4193 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 4194 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 4195 | if (T1MPType && T2MPType && |
| 4196 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 4197 | QualType(T2MPType->getClass(), 0))) { |
| 4198 | T1 = T1MPType->getPointeeType(); |
| 4199 | T2 = T2MPType->getPointeeType(); |
| 4200 | return true; |
| 4201 | } |
| 4202 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4203 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 4204 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 4205 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 4206 | if (T1OPType && T2OPType) { |
| 4207 | T1 = T1OPType->getPointeeType(); |
| 4208 | T2 = T2OPType->getPointeeType(); |
| 4209 | return true; |
| 4210 | } |
| 4211 | } |
| 4212 | |
| 4213 | // FIXME: Block pointers, too? |
| 4214 | |
| 4215 | return false; |
| 4216 | } |
| 4217 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4218 | DeclarationNameInfo |
| 4219 | ASTContext::getNameForTemplate(TemplateName Name, |
| 4220 | SourceLocation NameLoc) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4221 | switch (Name.getKind()) { |
| 4222 | case TemplateName::QualifiedTemplate: |
| 4223 | case TemplateName::Template: |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4224 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4225 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 4226 | NameLoc); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4227 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4228 | case TemplateName::OverloadedTemplate: { |
| 4229 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 4230 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 4231 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 4232 | } |
| 4233 | |
| 4234 | case TemplateName::DependentTemplate: { |
| 4235 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4236 | DeclarationName DName; |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 4237 | if (DTN->isIdentifier()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4238 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 4239 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 4240 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4241 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 4242 | // DNInfo work in progress: FIXME: source locations? |
| 4243 | DeclarationNameLoc DNLoc; |
| 4244 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 4245 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 4246 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 4247 | } |
| 4248 | } |
| 4249 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4250 | case TemplateName::SubstTemplateTemplateParm: { |
| 4251 | SubstTemplateTemplateParmStorage *subst |
| 4252 | = Name.getAsSubstTemplateTemplateParm(); |
| 4253 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 4254 | NameLoc); |
| 4255 | } |
| 4256 | |
| 4257 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 4258 | SubstTemplateTemplateParmPackStorage *subst |
| 4259 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 4260 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 4261 | NameLoc); |
| 4262 | } |
| 4263 | } |
| 4264 | |
| 4265 | llvm_unreachable("bad template name kind!"); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4268 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4269 | switch (Name.getKind()) { |
| 4270 | case TemplateName::QualifiedTemplate: |
| 4271 | case TemplateName::Template: { |
| 4272 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 4273 | if (TemplateTemplateParmDecl *TTP |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4274 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 4275 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 4276 | |
| 4277 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4278 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 4279 | } |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 4280 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4281 | case TemplateName::OverloadedTemplate: |
| 4282 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4283 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4284 | case TemplateName::DependentTemplate: { |
| 4285 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 4286 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 4287 | return DTN->CanonicalTemplateName; |
| 4288 | } |
| 4289 | |
| 4290 | case TemplateName::SubstTemplateTemplateParm: { |
| 4291 | SubstTemplateTemplateParmStorage *subst |
| 4292 | = Name.getAsSubstTemplateTemplateParm(); |
| 4293 | return getCanonicalTemplateName(subst->getReplacement()); |
| 4294 | } |
| 4295 | |
| 4296 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 4297 | SubstTemplateTemplateParmPackStorage *subst |
| 4298 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 4299 | TemplateTemplateParmDecl *canonParameter |
| 4300 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 4301 | TemplateArgument canonArgPack |
| 4302 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 4303 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 4304 | } |
| 4305 | } |
| 4306 | |
| 4307 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 4310 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 4311 | X = getCanonicalTemplateName(X); |
| 4312 | Y = getCanonicalTemplateName(Y); |
| 4313 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 4314 | } |
| 4315 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4316 | TemplateArgument |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4317 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4318 | switch (Arg.getKind()) { |
| 4319 | case TemplateArgument::Null: |
| 4320 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4321 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4322 | case TemplateArgument::Expression: |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4323 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4324 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4325 | case TemplateArgument::Declaration: { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4326 | ValueDecl *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl()); |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 4327 | return TemplateArgument(D, Arg.getParamTypeForDecl()); |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4328 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4329 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4330 | case TemplateArgument::NullPtr: |
| 4331 | return TemplateArgument(getCanonicalType(Arg.getNullPtrType()), |
| 4332 | /*isNullPtr*/true); |
| 4333 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4334 | case TemplateArgument::Template: |
| 4335 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4336 | |
| 4337 | case TemplateArgument::TemplateExpansion: |
| 4338 | return TemplateArgument(getCanonicalTemplateName( |
| 4339 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 4340 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4341 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4342 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4343 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4344 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4345 | case TemplateArgument::Type: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4346 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4347 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4348 | case TemplateArgument::Pack: { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 4349 | if (Arg.pack_size() == 0) |
| 4350 | return Arg; |
| 4351 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4352 | TemplateArgument *CanonArgs |
| 4353 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4354 | unsigned Idx = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4355 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4356 | AEnd = Arg.pack_end(); |
| 4357 | A != AEnd; (void)++A, ++Idx) |
| 4358 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4359 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4360 | return TemplateArgument(llvm::makeArrayRef(CanonArgs, Arg.pack_size())); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4361 | } |
| 4362 | } |
| 4363 | |
| 4364 | // Silence GCC warning |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4365 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4368 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4369 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4370 | if (!NNS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4371 | return nullptr; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4372 | |
| 4373 | switch (NNS->getKind()) { |
| 4374 | case NestedNameSpecifier::Identifier: |
| 4375 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4376 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4377 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 4378 | NNS->getAsIdentifier()); |
| 4379 | |
| 4380 | case NestedNameSpecifier::Namespace: |
| 4381 | // A namespace is canonical; build a nested-name-specifier with |
| 4382 | // this namespace and no prefix. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4383 | return NestedNameSpecifier::Create(*this, nullptr, |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4384 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 4385 | |
| 4386 | case NestedNameSpecifier::NamespaceAlias: |
| 4387 | // A namespace is canonical; build a nested-name-specifier with |
| 4388 | // this namespace and no prefix. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4389 | return NestedNameSpecifier::Create(*this, nullptr, |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4390 | NNS->getAsNamespaceAlias()->getNamespace() |
| 4391 | ->getOriginalNamespace()); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4392 | |
| 4393 | case NestedNameSpecifier::TypeSpec: |
| 4394 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 4395 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4396 | |
| 4397 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 4398 | // break it apart into its prefix and identifier, then reconsititute those |
| 4399 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 4400 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 4401 | // types, e.g., |
| 4402 | // typedef typename T::type T1; |
| 4403 | // typedef typename T1::type T2; |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 4404 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) |
| 4405 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4406 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4407 | |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 4408 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 4409 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 4410 | // first place? |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4411 | return NestedNameSpecifier::Create(*this, nullptr, false, |
| 4412 | const_cast<Type *>(T.getTypePtr())); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4416 | case NestedNameSpecifier::Super: |
| 4417 | // The global specifier and __super specifer are canonical and unique. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4418 | return NNS; |
| 4419 | } |
| 4420 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4421 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4422 | } |
| 4423 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4424 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4425 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4426 | // Handle the non-qualified case efficiently. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4427 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4428 | // Handle the common positive case fast. |
| 4429 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 4430 | return AT; |
| 4431 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4432 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4433 | // Handle the common negative case fast. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4434 | if (!isa<ArrayType>(T.getCanonicalType())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4435 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4436 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4437 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4438 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 4439 | // 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] | 4440 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4441 | // If we get here, we either have type qualifiers on the type, or we have |
| 4442 | // 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] | 4443 | // we must propagate them down into the element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4444 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4445 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4446 | Qualifiers qs = split.Quals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4447 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4448 | // If we have a simple case, just return now. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4449 | const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4450 | if (!ATy || qs.empty()) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4451 | return ATy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4452 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4453 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 4454 | // qualifiers into the array element type and return a new array type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4455 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4456 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4457 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 4458 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 4459 | CAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4460 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4461 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 4462 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 4463 | IAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4464 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4465 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4466 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4467 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 4468 | return cast<ArrayType>( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4469 | getDependentSizedArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4470 | DSAT->getSizeExpr(), |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4471 | DSAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4472 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4473 | DSAT->getBracketsRange())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4474 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4475 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4476 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4477 | VAT->getSizeExpr(), |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4478 | VAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4479 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4480 | VAT->getBracketsRange())); |
Chris Lattner | ed0d079 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4483 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 4484 | if (T->isArrayType() || T->isFunctionType()) |
| 4485 | return getDecayedType(T); |
| 4486 | return T; |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4487 | } |
| 4488 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4489 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4490 | T = getVariableArrayDecayedType(T); |
| 4491 | T = getAdjustedParameterType(T); |
| 4492 | return T.getUnqualifiedType(); |
| 4493 | } |
| 4494 | |
David Majnemer | d09a51c | 2015-03-03 01:50:05 +0000 | [diff] [blame] | 4495 | QualType ASTContext::getExceptionObjectType(QualType T) const { |
| 4496 | // C++ [except.throw]p3: |
| 4497 | // A throw-expression initializes a temporary object, called the exception |
| 4498 | // object, the type of which is determined by removing any top-level |
| 4499 | // cv-qualifiers from the static type of the operand of throw and adjusting |
| 4500 | // the type from "array of T" or "function returning T" to "pointer to T" |
| 4501 | // or "pointer to function returning T", [...] |
| 4502 | T = getVariableArrayDecayedType(T); |
| 4503 | if (T->isArrayType() || T->isFunctionType()) |
| 4504 | T = getDecayedType(T); |
| 4505 | return T.getUnqualifiedType(); |
| 4506 | } |
| 4507 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4508 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 4509 | /// specified array type to a pointer. This operation is non-trivial when |
| 4510 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 4511 | /// this returns a pointer to a properly qualified element of the array. |
| 4512 | /// |
| 4513 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4514 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4515 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 4516 | // typedefs in the element type of the array. This also handles propagation |
| 4517 | // of type qualifiers from the array type into the element type if present |
| 4518 | // (C99 6.7.3p8). |
| 4519 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 4520 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4521 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4522 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4523 | |
| 4524 | // int x[restrict 4] -> int *restrict |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4525 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4528 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 4529 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4532 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 4533 | Qualifiers qs; |
| 4534 | while (true) { |
| 4535 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4536 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4537 | if (!array) break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4538 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4539 | type = array->getElementType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4540 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4541 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4542 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4543 | return getQualifiedType(type, qs); |
Anders Carlsson | e0808df | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4546 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4547 | uint64_t |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4548 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 4549 | uint64_t ElementCount = 1; |
| 4550 | do { |
| 4551 | ElementCount *= CA->getSize().getZExtValue(); |
Richard Smith | 7808c6a | 2012-12-06 03:04:50 +0000 | [diff] [blame] | 4552 | CA = dyn_cast_or_null<ConstantArrayType>( |
| 4553 | CA->getElementType()->getAsArrayTypeUnsafe()); |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4554 | } while (CA); |
| 4555 | return ElementCount; |
| 4556 | } |
| 4557 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 4558 | /// getFloatingRank - Return a relative rank for floating point types. |
| 4559 | /// 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] | 4560 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4561 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 4562 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4563 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4564 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 4565 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4566 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4567 | case BuiltinType::Half: return HalfRank; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 4568 | case BuiltinType::Float: return FloatRank; |
| 4569 | case BuiltinType::Double: return DoubleRank; |
| 4570 | case BuiltinType::LongDouble: return LongDoubleRank; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 4571 | } |
| 4572 | } |
| 4573 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4574 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 4575 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | fc6ffa2 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 4576 | /// 'typeDomain' is a real floating point or complex type. |
| 4577 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4578 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 4579 | QualType Domain) const { |
| 4580 | FloatingRank EltRank = getFloatingRank(Size); |
| 4581 | if (Domain->isComplexType()) { |
| 4582 | switch (EltRank) { |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 4583 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 4584 | case FloatRank: return FloatComplexTy; |
| 4585 | case DoubleRank: return DoubleComplexTy; |
| 4586 | case LongDoubleRank: return LongDoubleComplexTy; |
| 4587 | } |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 4588 | } |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4589 | |
| 4590 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 4591 | switch (EltRank) { |
Joey Gouly | dd7f456 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 4592 | case HalfRank: return HalfTy; |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4593 | case FloatRank: return FloatTy; |
| 4594 | case DoubleRank: return DoubleTy; |
| 4595 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 4596 | } |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 4597 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 4598 | } |
| 4599 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4600 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 4601 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 4602 | /// '_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] | 4603 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4604 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4605 | FloatingRank LHSR = getFloatingRank(LHS); |
| 4606 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4607 | |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4608 | if (LHSR == RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 4609 | return 0; |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4610 | if (LHSR > RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 4611 | return 1; |
| 4612 | return -1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4615 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 4616 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 4617 | /// or if it is not canonicalized. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4618 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4619 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4620 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4621 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4622 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4623 | case BuiltinType::Bool: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4624 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4625 | case BuiltinType::Char_S: |
| 4626 | case BuiltinType::Char_U: |
| 4627 | case BuiltinType::SChar: |
| 4628 | case BuiltinType::UChar: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4629 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4630 | case BuiltinType::Short: |
| 4631 | case BuiltinType::UShort: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4632 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4633 | case BuiltinType::Int: |
| 4634 | case BuiltinType::UInt: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4635 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4636 | case BuiltinType::Long: |
| 4637 | case BuiltinType::ULong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4638 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4639 | case BuiltinType::LongLong: |
| 4640 | case BuiltinType::ULongLong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4641 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4642 | case BuiltinType::Int128: |
| 4643 | case BuiltinType::UInt128: |
| 4644 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4645 | } |
| 4646 | } |
| 4647 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4648 | /// \brief Whether this is a promotable bitfield reference according |
| 4649 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 4650 | /// |
| 4651 | /// \returns the type this bit-field will promote to, or NULL if no |
| 4652 | /// promotion occurs. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4653 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | e05d3cb | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 4654 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4655 | return QualType(); |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 4656 | |
| 4657 | // FIXME: We should not do this unless E->refersToBitField() is true. This |
| 4658 | // matters in C where getSourceBitField() will find bit-fields for various |
| 4659 | // cases where the source expression is not a bit-field designator. |
| 4660 | |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 4661 | FieldDecl *Field = E->getSourceBitField(); // FIXME: conditional bit-fields? |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4662 | if (!Field) |
| 4663 | return QualType(); |
| 4664 | |
| 4665 | QualType FT = Field->getType(); |
| 4666 | |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4667 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4668 | uint64_t IntSize = getTypeSize(IntTy); |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 4669 | // C++ [conv.prom]p5: |
| 4670 | // A prvalue for an integral bit-field can be converted to a prvalue of type |
| 4671 | // int if int can represent all the values of the bit-field; otherwise, it |
| 4672 | // can be converted to unsigned int if unsigned int can represent all the |
| 4673 | // values of the bit-field. If the bit-field is larger yet, no integral |
| 4674 | // promotion applies to it. |
| 4675 | // C11 6.3.1.1/2: |
| 4676 | // [For a bit-field of type _Bool, int, signed int, or unsigned int:] |
| 4677 | // If an int can represent all values of the original type (as restricted by |
| 4678 | // the width, for a bit-field), the value is converted to an int; otherwise, |
| 4679 | // it is converted to an unsigned int. |
| 4680 | // |
| 4681 | // FIXME: C does not permit promotion of a 'long : 3' bitfield to int. |
| 4682 | // We perform that promotion here to match GCC and C++. |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4683 | if (BitWidth < IntSize) |
| 4684 | return IntTy; |
| 4685 | |
| 4686 | if (BitWidth == IntSize) |
| 4687 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 4688 | |
| 4689 | // Types bigger than int are not subject to promotions, and therefore act |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 4690 | // like the base type. GCC has some weird bugs in this area that we |
| 4691 | // deliberately do not follow (GCC follows a pre-standard resolution to |
| 4692 | // C's DR315 which treats bit-width as being part of the type, and this leaks |
| 4693 | // into their semantics in some cases). |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4694 | return QualType(); |
| 4695 | } |
| 4696 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4697 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 4698 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 4699 | /// integer type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4700 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4701 | assert(!Promotable.isNull()); |
| 4702 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4703 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 4704 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 4705 | |
| 4706 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 4707 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 4708 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 4709 | // types that can represent all the values of its underlying type: |
| 4710 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 4711 | // unsigned long long int [...] |
| 4712 | // FIXME: Is there some better way to compute this? |
| 4713 | if (BT->getKind() == BuiltinType::WChar_S || |
| 4714 | BT->getKind() == BuiltinType::WChar_U || |
| 4715 | BT->getKind() == BuiltinType::Char16 || |
| 4716 | BT->getKind() == BuiltinType::Char32) { |
| 4717 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 4718 | uint64_t FromSize = getTypeSize(BT); |
| 4719 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 4720 | LongLongTy, UnsignedLongLongTy }; |
| 4721 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 4722 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 4723 | if (FromSize < ToSize || |
| 4724 | (FromSize == ToSize && |
| 4725 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 4726 | return PromoteTypes[Idx]; |
| 4727 | } |
| 4728 | llvm_unreachable("char type should fit into long long"); |
| 4729 | } |
| 4730 | } |
| 4731 | |
| 4732 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4733 | if (Promotable->isSignedIntegerType()) |
| 4734 | return IntTy; |
Eli Friedman | 6745c3b | 2012-11-15 01:21:59 +0000 | [diff] [blame] | 4735 | uint64_t PromotableSize = getIntWidth(Promotable); |
| 4736 | uint64_t IntSize = getIntWidth(IntTy); |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4737 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 4738 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 4739 | } |
| 4740 | |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 4741 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 4742 | /// type and returns its ownership. |
| 4743 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 4744 | while (!T.isNull()) { |
| 4745 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 4746 | return T.getObjCLifetime(); |
| 4747 | if (T->isArrayType()) |
| 4748 | T = getBaseElementType(T); |
| 4749 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 4750 | T = PT->getPointeeType(); |
| 4751 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 8e25253 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 4752 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 4753 | else |
| 4754 | break; |
| 4755 | } |
| 4756 | |
| 4757 | return Qualifiers::OCL_None; |
| 4758 | } |
| 4759 | |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 4760 | static const Type *getIntegerTypeForEnum(const EnumType *ET) { |
| 4761 | // Incomplete enum types are not treated as integer types. |
| 4762 | // FIXME: In C++, enum types are never integer types. |
| 4763 | if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped()) |
| 4764 | return ET->getDecl()->getIntegerType().getTypePtr(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4765 | return nullptr; |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 4766 | } |
| 4767 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4768 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4769 | /// 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] | 4770 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4771 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4772 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 4773 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 4774 | |
| 4775 | // Unwrap enums to their underlying type. |
| 4776 | if (const EnumType *ET = dyn_cast<EnumType>(LHSC)) |
| 4777 | LHSC = getIntegerTypeForEnum(ET); |
| 4778 | if (const EnumType *ET = dyn_cast<EnumType>(RHSC)) |
| 4779 | RHSC = getIntegerTypeForEnum(ET); |
| 4780 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4781 | if (LHSC == RHSC) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4782 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4783 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 4784 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4785 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4786 | unsigned LHSRank = getIntegerRank(LHSC); |
| 4787 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4788 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4789 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 4790 | if (LHSRank == RHSRank) return 0; |
| 4791 | return LHSRank > RHSRank ? 1 : -1; |
| 4792 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4793 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4794 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 4795 | if (LHSUnsigned) { |
| 4796 | // If the unsigned [LHS] type is larger, return it. |
| 4797 | if (LHSRank >= RHSRank) |
| 4798 | return 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4799 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4800 | // If the signed type can represent all values of the unsigned type, it |
| 4801 | // 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] | 4802 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4803 | return -1; |
| 4804 | } |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4805 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4806 | // If the unsigned [RHS] type is larger, return it. |
| 4807 | if (RHSRank >= LHSRank) |
| 4808 | return -1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4809 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4810 | // If the signed type can represent all values of the unsigned type, it |
| 4811 | // 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] | 4812 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4813 | return 1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 4814 | } |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4815 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4816 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4817 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4818 | if (!CFConstantStringTypeDecl) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4819 | CFConstantStringTypeDecl = buildImplicitRecord("NSConstantString"); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4820 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4821 | |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 4822 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4823 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4824 | // const int *isa; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4825 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 4826 | // int flags; |
| 4827 | FieldTypes[1] = IntTy; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4828 | // const char *str; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4829 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4830 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4831 | FieldTypes[3] = LongTy; |
| 4832 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4833 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4834 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4835 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4836 | SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4837 | SourceLocation(), nullptr, |
| 4838 | FieldTypes[i], /*TInfo=*/nullptr, |
| 4839 | /*BitWidth=*/nullptr, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4840 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4841 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4842 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4843 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4844 | } |
| 4845 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4846 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4847 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4848 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4849 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 412af03 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 4850 | } |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4851 | |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 4852 | QualType ASTContext::getObjCSuperType() const { |
| 4853 | if (ObjCSuperType.isNull()) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4854 | RecordDecl *ObjCSuperTypeDecl = buildImplicitRecord("objc_super"); |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 4855 | TUDecl->addDecl(ObjCSuperTypeDecl); |
| 4856 | ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); |
| 4857 | } |
| 4858 | return ObjCSuperType; |
| 4859 | } |
| 4860 | |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4861 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4862 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4863 | assert(Rec && "Invalid CFConstantStringType"); |
| 4864 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 4865 | } |
| 4866 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4867 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4868 | if (BlockDescriptorType) |
| 4869 | return getTagDeclType(BlockDescriptorType); |
| 4870 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4871 | RecordDecl *RD; |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4872 | // FIXME: Needs the FlagAppleBlock bit. |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4873 | RD = buildImplicitRecord("__block_descriptor"); |
| 4874 | RD->startDefinition(); |
| 4875 | |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4876 | QualType FieldTypes[] = { |
| 4877 | UnsignedLongTy, |
| 4878 | UnsignedLongTy, |
| 4879 | }; |
| 4880 | |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 4881 | static const char *const FieldNames[] = { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4882 | "reserved", |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4883 | "Size" |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4884 | }; |
| 4885 | |
| 4886 | for (size_t i = 0; i < 2; ++i) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4887 | FieldDecl *Field = FieldDecl::Create( |
| 4888 | *this, RD, SourceLocation(), SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4889 | &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
| 4890 | /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4891 | Field->setAccess(AS_public); |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4892 | RD->addDecl(Field); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4893 | } |
| 4894 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4895 | RD->completeDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4896 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4897 | BlockDescriptorType = RD; |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4898 | |
| 4899 | return getTagDeclType(BlockDescriptorType); |
| 4900 | } |
| 4901 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4902 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4903 | if (BlockDescriptorExtendedType) |
| 4904 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4905 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4906 | RecordDecl *RD; |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4907 | // FIXME: Needs the FlagAppleBlock bit. |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4908 | RD = buildImplicitRecord("__block_descriptor_withcopydispose"); |
| 4909 | RD->startDefinition(); |
| 4910 | |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4911 | QualType FieldTypes[] = { |
| 4912 | UnsignedLongTy, |
| 4913 | UnsignedLongTy, |
| 4914 | getPointerType(VoidPtrTy), |
| 4915 | getPointerType(VoidPtrTy) |
| 4916 | }; |
| 4917 | |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 4918 | static const char *const FieldNames[] = { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4919 | "reserved", |
| 4920 | "Size", |
| 4921 | "CopyFuncPtr", |
| 4922 | "DestroyFuncPtr" |
| 4923 | }; |
| 4924 | |
| 4925 | for (size_t i = 0; i < 4; ++i) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4926 | FieldDecl *Field = FieldDecl::Create( |
| 4927 | *this, RD, SourceLocation(), SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4928 | &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
| 4929 | /*BitWidth=*/nullptr, |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4930 | /*Mutable=*/false, ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4931 | Field->setAccess(AS_public); |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4932 | RD->addDecl(Field); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4935 | RD->completeDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4936 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4937 | BlockDescriptorExtendedType = RD; |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4938 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4939 | } |
| 4940 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4941 | /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty" |
| 4942 | /// requires copy/dispose. Note that this must match the logic |
| 4943 | /// in buildByrefHelpers. |
| 4944 | bool ASTContext::BlockRequiresCopying(QualType Ty, |
| 4945 | const VarDecl *D) { |
| 4946 | if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) { |
| 4947 | const Expr *copyExpr = getBlockVarCopyInits(D); |
| 4948 | if (!copyExpr && record->hasTrivialDestructor()) return false; |
| 4949 | |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4950 | return true; |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 4951 | } |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4952 | |
| 4953 | if (!Ty->isObjCRetainableType()) return false; |
| 4954 | |
| 4955 | Qualifiers qs = Ty.getQualifiers(); |
| 4956 | |
| 4957 | // If we have lifetime, that dominates. |
| 4958 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4959 | switch (lifetime) { |
| 4960 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
| 4961 | |
| 4962 | // These are just bits as far as the runtime is concerned. |
| 4963 | case Qualifiers::OCL_ExplicitNone: |
| 4964 | case Qualifiers::OCL_Autoreleasing: |
| 4965 | return false; |
| 4966 | |
| 4967 | // Tell the runtime that this is ARC __weak, called by the |
| 4968 | // byref routines. |
| 4969 | case Qualifiers::OCL_Weak: |
| 4970 | // ARC __strong __block variables need to be retained. |
| 4971 | case Qualifiers::OCL_Strong: |
| 4972 | return true; |
| 4973 | } |
| 4974 | llvm_unreachable("fell out of lifetime switch!"); |
| 4975 | } |
| 4976 | return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) || |
| 4977 | Ty->isObjCObjectPointerType()); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4978 | } |
| 4979 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4980 | bool ASTContext::getByrefLifetime(QualType Ty, |
| 4981 | Qualifiers::ObjCLifetime &LifeTime, |
| 4982 | bool &HasByrefExtendedLayout) const { |
| 4983 | |
| 4984 | if (!getLangOpts().ObjC1 || |
| 4985 | getLangOpts().getGC() != LangOptions::NonGC) |
| 4986 | return false; |
| 4987 | |
| 4988 | HasByrefExtendedLayout = false; |
Fariborz Jahanian | f762b72 | 2012-12-11 19:58:01 +0000 | [diff] [blame] | 4989 | if (Ty->isRecordType()) { |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4990 | HasByrefExtendedLayout = true; |
| 4991 | LifeTime = Qualifiers::OCL_None; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 4992 | } else if ((LifeTime = Ty.getObjCLifetime())) { |
| 4993 | // Honor the ARC qualifiers. |
| 4994 | } else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) { |
| 4995 | // The MRR rule. |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4996 | LifeTime = Qualifiers::OCL_ExplicitNone; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 4997 | } else { |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4998 | LifeTime = Qualifiers::OCL_None; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 4999 | } |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 5000 | return true; |
| 5001 | } |
| 5002 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 5003 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 5004 | if (!ObjCInstanceTypeDecl) |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 5005 | ObjCInstanceTypeDecl = |
| 5006 | buildImplicitTypedef(getObjCIdType(), "instancetype"); |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 5007 | return ObjCInstanceTypeDecl; |
| 5008 | } |
| 5009 | |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 5010 | // This returns true if a type has been typedefed to BOOL: |
| 5011 | // typedef <type> BOOL; |
Chris Lattner | e021899 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 5012 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 5013 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 9b1f279 | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 5014 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 5015 | return II->isStr("BOOL"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5016 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5017 | return false; |
| 5018 | } |
| 5019 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5020 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5021 | /// purpose. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5022 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5023 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 5024 | return CharUnits::Zero(); |
| 5025 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5026 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5027 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5028 | // 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] | 5029 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5030 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5031 | // Treat arrays as pointers, since that's how they're passed in. |
| 5032 | else if (type->isArrayType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5033 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5034 | return sz; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5035 | } |
| 5036 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 5037 | bool ASTContext::isMSStaticDataMemberInlineDefinition(const VarDecl *VD) const { |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 5038 | return getTargetInfo().getCXXABI().isMicrosoft() && |
| 5039 | VD->isStaticDataMember() && |
David Majnemer | fac5243 | 2015-10-19 23:22:49 +0000 | [diff] [blame] | 5040 | VD->getType()->isIntegralOrEnumerationType() && |
| 5041 | !VD->getFirstDecl()->isOutOfLine() && VD->getFirstDecl()->hasInit(); |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 5042 | } |
| 5043 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5044 | static inline |
| 5045 | std::string charUnitsToString(const CharUnits &CU) { |
| 5046 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5047 | } |
| 5048 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 5049 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5050 | /// declaration. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 5051 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 5052 | std::string S; |
| 5053 | |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5054 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 5055 | QualType BlockTy = |
| 5056 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 5057 | // Encode result type. |
Fariborz Jahanian | 0e3043b | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 5058 | if (getLangOpts().EncodeExtendedBlockSig) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5059 | getObjCEncodingForMethodParameter( |
| 5060 | Decl::OBJC_TQ_None, BlockTy->getAs<FunctionType>()->getReturnType(), S, |
| 5061 | true /*Extended*/); |
Fariborz Jahanian | 64223e6 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 5062 | else |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5063 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getReturnType(), S); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5064 | // Compute size of all parameters. |
| 5065 | // Start with computing size of a pointer in number of bytes. |
| 5066 | // FIXME: There might(should) be a better way of doing this computation! |
| 5067 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5068 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 5069 | CharUnits ParmOffset = PtrSize; |
Aaron Ballman | b2b8b1d | 2014-03-07 16:09:59 +0000 | [diff] [blame] | 5070 | for (auto PI : Decl->params()) { |
| 5071 | QualType PType = PI->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5072 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | d487941 | 2012-06-30 00:48:59 +0000 | [diff] [blame] | 5073 | if (sz.isZero()) |
| 5074 | continue; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5075 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5076 | ParmOffset += sz; |
| 5077 | } |
| 5078 | // Size of the argument frame |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5079 | S += charUnitsToString(ParmOffset); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5080 | // Block pointer and offset. |
| 5081 | S += "@?0"; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5082 | |
| 5083 | // Argument types. |
| 5084 | ParmOffset = PtrSize; |
Aaron Ballman | b2b8b1d | 2014-03-07 16:09:59 +0000 | [diff] [blame] | 5085 | for (auto PVDecl : Decl->params()) { |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5086 | QualType PType = PVDecl->getOriginalType(); |
| 5087 | if (const ArrayType *AT = |
| 5088 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 5089 | // Use array's original type only if it has known number of |
| 5090 | // elements. |
| 5091 | if (!isa<ConstantArrayType>(AT)) |
| 5092 | PType = PVDecl->getType(); |
| 5093 | } else if (PType->isFunctionType()) |
| 5094 | PType = PVDecl->getType(); |
Fariborz Jahanian | 0e3043b | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 5095 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 64223e6 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 5096 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType, |
| 5097 | S, true /*Extended*/); |
| 5098 | else |
| 5099 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5100 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5101 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5102 | } |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 5103 | |
| 5104 | return S; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5105 | } |
| 5106 | |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5107 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 5108 | std::string& S) { |
| 5109 | // Encode result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5110 | getObjCEncodingForType(Decl->getReturnType(), S); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 5111 | CharUnits ParmOffset; |
| 5112 | // Compute size of all parameters. |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 5113 | for (auto PI : Decl->params()) { |
| 5114 | QualType PType = PI->getType(); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 5115 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5116 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 5117 | continue; |
| 5118 | |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 5119 | assert (sz.isPositive() && |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5120 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 5121 | ParmOffset += sz; |
| 5122 | } |
| 5123 | S += charUnitsToString(ParmOffset); |
| 5124 | ParmOffset = CharUnits::Zero(); |
| 5125 | |
| 5126 | // Argument types. |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 5127 | for (auto PVDecl : Decl->params()) { |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 5128 | QualType PType = PVDecl->getOriginalType(); |
| 5129 | if (const ArrayType *AT = |
| 5130 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 5131 | // Use array's original type only if it has known number of |
| 5132 | // elements. |
| 5133 | if (!isa<ConstantArrayType>(AT)) |
| 5134 | PType = PVDecl->getType(); |
| 5135 | } else if (PType->isFunctionType()) |
| 5136 | PType = PVDecl->getType(); |
| 5137 | getObjCEncodingForType(PType, S); |
| 5138 | S += charUnitsToString(ParmOffset); |
| 5139 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 5140 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5141 | |
| 5142 | return false; |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 5143 | } |
| 5144 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5145 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 5146 | /// method parameter or return type. If Extended, include class names and |
| 5147 | /// block object types. |
| 5148 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 5149 | QualType T, std::string& S, |
| 5150 | bool Extended) const { |
| 5151 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 5152 | getObjCEncodingForTypeQualifier(QT, S); |
| 5153 | // Encode parameter type. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5154 | getObjCEncodingForTypeImpl(T, S, true, true, nullptr, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5155 | true /*OutermostType*/, |
| 5156 | false /*EncodingProperty*/, |
| 5157 | false /*StructField*/, |
| 5158 | Extended /*EncodeBlockParameters*/, |
| 5159 | Extended /*EncodeClassNames*/); |
| 5160 | } |
| 5161 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5162 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5163 | /// declaration. |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5164 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5165 | std::string& S, |
| 5166 | bool Extended) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5167 | // FIXME: This is not very efficient. |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5168 | // Encode return type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5169 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 5170 | Decl->getReturnType(), S, Extended); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5171 | // Compute size of all parameters. |
| 5172 | // Start with computing size of a pointer in number of bytes. |
| 5173 | // FIXME: There might(should) be a better way of doing this computation! |
| 5174 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5175 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5176 | // The first two arguments (self and _cmd) are pointers; account for |
| 5177 | // their size. |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5178 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 5179 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 5180 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 5181 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5182 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5183 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 5184 | continue; |
| 5185 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5186 | assert (sz.isPositive() && |
| 5187 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5188 | ParmOffset += sz; |
| 5189 | } |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5190 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5191 | S += "@0:"; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5192 | S += charUnitsToString(PtrSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5193 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5194 | // Argument types. |
| 5195 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 5196 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 5197 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 5198 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5199 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | a0befc0 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 5200 | if (const ArrayType *AT = |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 5201 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 5202 | // Use array's original type only if it has known number of |
| 5203 | // elements. |
Steve Naroff | 323827e | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 5204 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 5205 | PType = PVDecl->getType(); |
| 5206 | } else if (PType->isFunctionType()) |
| 5207 | PType = PVDecl->getType(); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5208 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 5209 | PType, S, Extended); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5210 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5211 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5212 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5213 | |
| 5214 | return false; |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5215 | } |
| 5216 | |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 5217 | ObjCPropertyImplDecl * |
| 5218 | ASTContext::getObjCPropertyImplDeclForPropertyDecl( |
| 5219 | const ObjCPropertyDecl *PD, |
| 5220 | const Decl *Container) const { |
| 5221 | if (!Container) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5222 | return nullptr; |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 5223 | if (const ObjCCategoryImplDecl *CID = |
| 5224 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 5225 | for (auto *PID : CID->property_impls()) |
| 5226 | if (PID->getPropertyDecl() == PD) |
| 5227 | return PID; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5228 | } else { |
| 5229 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
| 5230 | for (auto *PID : OID->property_impls()) |
| 5231 | if (PID->getPropertyDecl() == PD) |
| 5232 | return PID; |
| 5233 | } |
| 5234 | return nullptr; |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 5235 | } |
| 5236 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5237 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 5238 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5239 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 5240 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5241 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 5242 | /// attributes readonly and bycopy are encoded as single characters. The |
| 5243 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 5244 | /// encoded as single characters, followed by an identifier. Property types |
| 5245 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 5246 | /// these attributes are defined by the following enumeration: |
| 5247 | /// @code |
| 5248 | /// enum PropertyAttributes { |
| 5249 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 5250 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 5251 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 5252 | /// kPropertyDynamic = 'D', // property is dynamic |
| 5253 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 5254 | /// kPropertySetter = 'S', // followed by setter selector name |
| 5255 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 8cf6185 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 5256 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 5257 | /// kPropertyWeak = 'W' // 'weak' property |
| 5258 | /// kPropertyStrong = 'P' // property GC'able |
| 5259 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 5260 | /// }; |
| 5261 | /// @endcode |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5262 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5263 | const Decl *Container, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5264 | std::string& S) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5265 | // Collect information from the property implementation decl(s). |
| 5266 | bool Dynamic = false; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5267 | ObjCPropertyImplDecl *SynthesizePID = nullptr; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5268 | |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 5269 | if (ObjCPropertyImplDecl *PropertyImpDecl = |
| 5270 | getObjCPropertyImplDeclForPropertyDecl(PD, Container)) { |
| 5271 | if (PropertyImpDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 5272 | Dynamic = true; |
| 5273 | else |
| 5274 | SynthesizePID = PropertyImpDecl; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5275 | } |
| 5276 | |
| 5277 | // FIXME: This is not very efficient. |
| 5278 | S = "T"; |
| 5279 | |
| 5280 | // Encode result type. |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 5281 | // GCC has some special rules regarding encoding of properties which |
| 5282 | // closely resembles encoding of ivars. |
Joe Groff | 98ac7d2 | 2014-07-07 22:25:15 +0000 | [diff] [blame] | 5283 | getObjCEncodingForPropertyType(PD->getType(), S); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5284 | |
| 5285 | if (PD->isReadOnly()) { |
| 5286 | S += ",R"; |
Nico Weber | 4e8626f | 2013-05-08 23:47:40 +0000 | [diff] [blame] | 5287 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) |
| 5288 | S += ",C"; |
| 5289 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) |
| 5290 | S += ",&"; |
Fariborz Jahanian | 33079ee | 2014-04-02 22:49:42 +0000 | [diff] [blame] | 5291 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) |
| 5292 | S += ",W"; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5293 | } else { |
| 5294 | switch (PD->getSetterKind()) { |
| 5295 | case ObjCPropertyDecl::Assign: break; |
| 5296 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5297 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 70a315c | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 5298 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5299 | } |
| 5300 | } |
| 5301 | |
| 5302 | // It really isn't clear at all what this means, since properties |
| 5303 | // are "dynamic by default". |
| 5304 | if (Dynamic) |
| 5305 | S += ",D"; |
| 5306 | |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 5307 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 5308 | S += ",N"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5309 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5310 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 5311 | S += ",G"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5312 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5313 | } |
| 5314 | |
| 5315 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 5316 | S += ",S"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5317 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5318 | } |
| 5319 | |
| 5320 | if (SynthesizePID) { |
| 5321 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 5322 | S += ",V"; |
Chris Lattner | 1cbaacc | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 5323 | S += OID->getNameAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5324 | } |
| 5325 | |
| 5326 | // FIXME: OBJCGC: weak & strong |
| 5327 | } |
| 5328 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5329 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5330 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 5331 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5332 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 5333 | /// |
| 5334 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 5335 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5336 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5337 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5338 | PointeeTy = UnsignedIntTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5339 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5340 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5341 | PointeeTy = IntTy; |
| 5342 | } |
| 5343 | } |
| 5344 | } |
| 5345 | |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5346 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5347 | const FieldDecl *Field, |
| 5348 | QualType *NotEncodedT) const { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 5349 | // We follow the behavior of gcc, expanding structures which are |
| 5350 | // directly pointed to, and expanding embedded structures. Note that |
| 5351 | // these rules are sufficient to prevent recursive encoding of the |
| 5352 | // same type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5353 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5354 | true /* outermost type */, false, false, |
| 5355 | false, false, false, NotEncodedT); |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 5356 | } |
| 5357 | |
Joe Groff | 98ac7d2 | 2014-07-07 22:25:15 +0000 | [diff] [blame] | 5358 | void ASTContext::getObjCEncodingForPropertyType(QualType T, |
| 5359 | std::string& S) const { |
| 5360 | // Encode result type. |
| 5361 | // GCC has some special rules regarding encoding of properties which |
| 5362 | // closely resembles encoding of ivars. |
| 5363 | getObjCEncodingForTypeImpl(T, S, true, true, nullptr, |
| 5364 | true /* outermost type */, |
| 5365 | true /* encoding property */); |
| 5366 | } |
| 5367 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5368 | static char getObjCEncodingForPrimitiveKind(const ASTContext *C, |
| 5369 | BuiltinType::Kind kind) { |
| 5370 | switch (kind) { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5371 | case BuiltinType::Void: return 'v'; |
| 5372 | case BuiltinType::Bool: return 'B'; |
| 5373 | case BuiltinType::Char_U: |
| 5374 | case BuiltinType::UChar: return 'C'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5375 | case BuiltinType::Char16: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5376 | case BuiltinType::UShort: return 'S'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5377 | case BuiltinType::Char32: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5378 | case BuiltinType::UInt: return 'I'; |
| 5379 | case BuiltinType::ULong: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5380 | return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5381 | case BuiltinType::UInt128: return 'T'; |
| 5382 | case BuiltinType::ULongLong: return 'Q'; |
| 5383 | case BuiltinType::Char_S: |
| 5384 | case BuiltinType::SChar: return 'c'; |
| 5385 | case BuiltinType::Short: return 's'; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 5386 | case BuiltinType::WChar_S: |
| 5387 | case BuiltinType::WChar_U: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5388 | case BuiltinType::Int: return 'i'; |
| 5389 | case BuiltinType::Long: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5390 | return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5391 | case BuiltinType::LongLong: return 'q'; |
| 5392 | case BuiltinType::Int128: return 't'; |
| 5393 | case BuiltinType::Float: return 'f'; |
| 5394 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 7cba5a7 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 5395 | case BuiltinType::LongDouble: return 'D'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5396 | case BuiltinType::NullPtr: return '*'; // like char* |
| 5397 | |
| 5398 | case BuiltinType::Half: |
| 5399 | // FIXME: potentially need @encodes for these! |
| 5400 | return ' '; |
| 5401 | |
| 5402 | case BuiltinType::ObjCId: |
| 5403 | case BuiltinType::ObjCClass: |
| 5404 | case BuiltinType::ObjCSel: |
| 5405 | llvm_unreachable("@encoding ObjC primitive type"); |
| 5406 | |
| 5407 | // OpenCL and placeholder types don't need @encodings. |
| 5408 | case BuiltinType::OCLImage1d: |
| 5409 | case BuiltinType::OCLImage1dArray: |
| 5410 | case BuiltinType::OCLImage1dBuffer: |
| 5411 | case BuiltinType::OCLImage2d: |
| 5412 | case BuiltinType::OCLImage2dArray: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 5413 | case BuiltinType::OCLImage2dDepth: |
| 5414 | case BuiltinType::OCLImage2dArrayDepth: |
| 5415 | case BuiltinType::OCLImage2dMSAA: |
| 5416 | case BuiltinType::OCLImage2dArrayMSAA: |
| 5417 | case BuiltinType::OCLImage2dMSAADepth: |
| 5418 | case BuiltinType::OCLImage2dArrayMSAADepth: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5419 | case BuiltinType::OCLImage3d: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5420 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 5421 | case BuiltinType::OCLClkEvent: |
| 5422 | case BuiltinType::OCLQueue: |
| 5423 | case BuiltinType::OCLNDRange: |
| 5424 | case BuiltinType::OCLReserveID: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5425 | case BuiltinType::OCLSampler: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5426 | case BuiltinType::Dependent: |
| 5427 | #define BUILTIN_TYPE(KIND, ID) |
| 5428 | #define PLACEHOLDER_TYPE(KIND, ID) \ |
| 5429 | case BuiltinType::KIND: |
| 5430 | #include "clang/AST/BuiltinTypes.def" |
| 5431 | llvm_unreachable("invalid builtin type for @encode"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5432 | } |
David Blaikie | 5a6a020 | 2013-01-09 17:48:41 +0000 | [diff] [blame] | 5433 | llvm_unreachable("invalid BuiltinType::Kind value"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5434 | } |
| 5435 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5436 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 5437 | EnumDecl *Enum = ET->getDecl(); |
| 5438 | |
| 5439 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 5440 | if (!Enum->isFixed()) |
| 5441 | return 'i'; |
| 5442 | |
| 5443 | // The encoding of a fixed enum type matches its fixed underlying type. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5444 | const BuiltinType *BT = Enum->getIntegerType()->castAs<BuiltinType>(); |
| 5445 | return getObjCEncodingForPrimitiveKind(C, BT->getKind()); |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5448 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5449 | QualType T, const FieldDecl *FD) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5450 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 5451 | S += 'b'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5452 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 5453 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 5454 | // then the offset (in bits) of the first element, then the type of the |
| 5455 | // bitfield, then the size in bits. For example, in this structure: |
| 5456 | // |
| 5457 | // struct |
| 5458 | // { |
| 5459 | // int integer; |
| 5460 | // int flags:2; |
| 5461 | // }; |
| 5462 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 5463 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 5464 | // information is not especially sensible, but we're stuck with it for |
| 5465 | // compatibility with GCC, although providing it breaks anything that |
| 5466 | // actually uses runtime introspection and wants to work on both runtimes... |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 5467 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5468 | const RecordDecl *RD = FD->getParent(); |
| 5469 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | a3c122d | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 5470 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5471 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 5472 | S += ObjCEncodingForEnumType(Ctx, ET); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5473 | else { |
| 5474 | const BuiltinType *BT = T->castAs<BuiltinType>(); |
| 5475 | S += getObjCEncodingForPrimitiveKind(Ctx, BT->getKind()); |
| 5476 | } |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5477 | } |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5478 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 5479 | } |
| 5480 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 5481 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 5482 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 5483 | bool ExpandPointedToStructures, |
| 5484 | bool ExpandStructures, |
Daniel Dunbar | c040ce4 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 5485 | const FieldDecl *FD, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 5486 | bool OutermostType, |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5487 | bool EncodingProperty, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5488 | bool StructField, |
| 5489 | bool EncodeBlockParameters, |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5490 | bool EncodeClassNames, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5491 | bool EncodePointerToObjCTypedef, |
| 5492 | QualType *NotEncodedT) const { |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5493 | CanQualType CT = getCanonicalType(T); |
| 5494 | switch (CT->getTypeClass()) { |
| 5495 | case Type::Builtin: |
| 5496 | case Type::Enum: |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5497 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5498 | return EncodeBitField(this, S, T, FD); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5499 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CT)) |
| 5500 | S += getObjCEncodingForPrimitiveKind(this, BT->getKind()); |
| 5501 | else |
| 5502 | S += ObjCEncodingForEnumType(this, cast<EnumType>(CT)); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5503 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5504 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5505 | case Type::Complex: { |
| 5506 | const ComplexType *CT = T->castAs<ComplexType>(); |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 5507 | S += 'j'; |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5508 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, nullptr); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5509 | return; |
| 5510 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5511 | |
| 5512 | case Type::Atomic: { |
| 5513 | const AtomicType *AT = T->castAs<AtomicType>(); |
| 5514 | S += 'A'; |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5515 | getObjCEncodingForTypeImpl(AT->getValueType(), S, false, false, nullptr); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5516 | return; |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 5517 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5518 | |
| 5519 | // encoding for pointer or reference types. |
| 5520 | case Type::Pointer: |
| 5521 | case Type::LValueReference: |
| 5522 | case Type::RValueReference: { |
| 5523 | QualType PointeeTy; |
| 5524 | if (isa<PointerType>(CT)) { |
| 5525 | const PointerType *PT = T->castAs<PointerType>(); |
| 5526 | if (PT->isObjCSelType()) { |
| 5527 | S += ':'; |
| 5528 | return; |
| 5529 | } |
| 5530 | PointeeTy = PT->getPointeeType(); |
| 5531 | } else { |
| 5532 | PointeeTy = T->castAs<ReferenceType>()->getPointeeType(); |
| 5533 | } |
| 5534 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5535 | bool isReadOnly = false; |
| 5536 | // For historical/compatibility reasons, the read-only qualifier of the |
| 5537 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 5538 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5539 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 5540 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5541 | if (OutermostType && T.isConstQualified()) { |
| 5542 | isReadOnly = true; |
| 5543 | S += 'r'; |
| 5544 | } |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 5545 | } else if (OutermostType) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5546 | QualType P = PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5547 | while (P->getAs<PointerType>()) |
| 5548 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5549 | if (P.isConstQualified()) { |
| 5550 | isReadOnly = true; |
| 5551 | S += 'r'; |
| 5552 | } |
| 5553 | } |
| 5554 | if (isReadOnly) { |
| 5555 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 5556 | // combinations need to be rearranged. |
| 5557 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5558 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 5559 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5560 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5561 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5562 | if (PointeeTy->isCharType()) { |
| 5563 | // char pointer types should be encoded as '*' unless it is a |
| 5564 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 5565 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5566 | S += '*'; |
| 5567 | return; |
| 5568 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5569 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 3de6b70 | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 5570 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 5571 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 5572 | S += '#'; |
| 5573 | return; |
| 5574 | } |
| 5575 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 5576 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 5577 | S += '@'; |
| 5578 | return; |
| 5579 | } |
| 5580 | // fall through... |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5581 | } |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5582 | S += '^'; |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5583 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 5584 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5585 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5586 | nullptr, false, false, false, false, false, false, |
| 5587 | NotEncodedT); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5588 | return; |
| 5589 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5590 | |
| 5591 | case Type::ConstantArray: |
| 5592 | case Type::IncompleteArray: |
| 5593 | case Type::VariableArray: { |
| 5594 | const ArrayType *AT = cast<ArrayType>(CT); |
| 5595 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5596 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5597 | // Incomplete arrays are encoded as a pointer to the array element. |
| 5598 | S += '^'; |
| 5599 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5600 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5601 | false, ExpandStructures, FD); |
| 5602 | } else { |
| 5603 | S += '['; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5604 | |
Fariborz Jahanian | f0dc11a | 2013-06-04 16:04:37 +0000 | [diff] [blame] | 5605 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 5606 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 5607 | else { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5608 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5609 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 5610 | "Unknown array type!"); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5611 | S += '0'; |
| 5612 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5613 | |
| 5614 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5615 | false, ExpandStructures, FD, |
| 5616 | false, false, false, false, false, false, |
| 5617 | NotEncodedT); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5618 | S += ']'; |
| 5619 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5620 | return; |
| 5621 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5622 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5623 | case Type::FunctionNoProto: |
| 5624 | case Type::FunctionProto: |
Anders Carlsson | df4cc61 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 5625 | S += '?'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5626 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5627 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5628 | case Type::Record: { |
| 5629 | RecordDecl *RDecl = cast<RecordType>(CT)->getDecl(); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5630 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 5631 | // Anonymous structures print as '?' |
| 5632 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 5633 | S += II->getName(); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 5634 | if (ClassTemplateSpecializationDecl *Spec |
| 5635 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 5636 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 5637 | llvm::raw_string_ostream OS(S); |
| 5638 | TemplateSpecializationType::PrintTemplateArgumentList(OS, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5639 | TemplateArgs.data(), |
| 5640 | TemplateArgs.size(), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 5641 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 5642 | } |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 5643 | } else { |
| 5644 | S += '?'; |
| 5645 | } |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 5646 | if (ExpandStructures) { |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5647 | S += '='; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5648 | if (!RDecl->isUnion()) { |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5649 | getObjCEncodingForStructureImpl(RDecl, S, FD, true, NotEncodedT); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5650 | } else { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5651 | for (const auto *Field : RDecl->fields()) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5652 | if (FD) { |
| 5653 | S += '"'; |
| 5654 | S += Field->getNameAsString(); |
| 5655 | S += '"'; |
| 5656 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5657 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5658 | // Special case bit-fields. |
| 5659 | if (Field->isBitField()) { |
| 5660 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5661 | Field); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5662 | } else { |
| 5663 | QualType qt = Field->getType(); |
| 5664 | getLegacyIntegralTypeEncoding(qt); |
| 5665 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 5666 | FD, /*OutermostType*/false, |
| 5667 | /*EncodingProperty*/false, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5668 | /*StructField*/true, |
| 5669 | false, false, false, NotEncodedT); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5670 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5671 | } |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5672 | } |
Fariborz Jahanian | bc92fd7 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 5673 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5674 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5675 | return; |
| 5676 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5677 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5678 | case Type::BlockPointer: { |
| 5679 | const BlockPointerType *BT = T->castAs<BlockPointerType>(); |
Steve Naroff | 49140cb | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 5680 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5681 | if (EncodeBlockParameters) { |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5682 | const FunctionType *FT = BT->getPointeeType()->castAs<FunctionType>(); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5683 | |
| 5684 | S += '<'; |
| 5685 | // Block return type |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5686 | getObjCEncodingForTypeImpl( |
| 5687 | FT->getReturnType(), S, ExpandPointedToStructures, ExpandStructures, |
| 5688 | FD, false /* OutermostType */, EncodingProperty, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5689 | false /* StructField */, EncodeBlockParameters, EncodeClassNames, false, |
| 5690 | NotEncodedT); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5691 | // Block self |
| 5692 | S += "@?"; |
| 5693 | // Block parameters |
| 5694 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 5695 | for (const auto &I : FPT->param_types()) |
| 5696 | getObjCEncodingForTypeImpl( |
| 5697 | I, S, ExpandPointedToStructures, ExpandStructures, FD, |
| 5698 | false /* OutermostType */, EncodingProperty, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5699 | false /* StructField */, EncodeBlockParameters, EncodeClassNames, |
| 5700 | false, NotEncodedT); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5701 | } |
| 5702 | S += '>'; |
| 5703 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5704 | return; |
| 5705 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5706 | |
Fariborz Jahanian | 33fa962 | 2014-01-28 20:41:15 +0000 | [diff] [blame] | 5707 | case Type::ObjCObject: { |
| 5708 | // hack to match legacy encoding of *id and *Class |
| 5709 | QualType Ty = getObjCObjectPointerType(CT); |
| 5710 | if (Ty->isObjCIdType()) { |
| 5711 | S += "{objc_object=}"; |
| 5712 | return; |
| 5713 | } |
| 5714 | else if (Ty->isObjCClassType()) { |
| 5715 | S += "{objc_class=}"; |
| 5716 | return; |
| 5717 | } |
| 5718 | } |
| 5719 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5720 | case Type::ObjCInterface: { |
| 5721 | // Ignore protocol qualifiers when mangling at this level. |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5722 | // @encode(class_name) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 5723 | ObjCInterfaceDecl *OI = T->castAs<ObjCObjectType>()->getInterface(); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5724 | S += '{'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 5725 | S += OI->getObjCRuntimeNameAsString(); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5726 | S += '='; |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5727 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5728 | DeepCollectObjCIvars(OI, true, Ivars); |
| 5729 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5730 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5731 | if (Field->isBitField()) |
| 5732 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5733 | else |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5734 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD, |
| 5735 | false, false, false, false, false, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5736 | EncodePointerToObjCTypedef, |
| 5737 | NotEncodedT); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5738 | } |
| 5739 | S += '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5740 | return; |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5741 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5742 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5743 | case Type::ObjCObjectPointer: { |
| 5744 | const ObjCObjectPointerType *OPT = T->castAs<ObjCObjectPointerType>(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5745 | if (OPT->isObjCIdType()) { |
| 5746 | S += '@'; |
| 5747 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5748 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5749 | |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 5750 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 5751 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 5752 | // Since this is a binary compatibility issue, need to consult with runtime |
| 5753 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5754 | S += '#'; |
| 5755 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5756 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5757 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5758 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5759 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5760 | ExpandPointedToStructures, |
| 5761 | ExpandStructures, FD); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5762 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5763 | // Note that we do extended encoding of protocol qualifer list |
| 5764 | // Only when doing ivar or property encoding. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5765 | S += '"'; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 5766 | for (const auto *I : OPT->quals()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5767 | S += '<'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 5768 | S += I->getObjCRuntimeNameAsString(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5769 | S += '>'; |
| 5770 | } |
| 5771 | S += '"'; |
| 5772 | } |
| 5773 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5774 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5775 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5776 | QualType PointeeTy = OPT->getPointeeType(); |
| 5777 | if (!EncodingProperty && |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5778 | isa<TypedefType>(PointeeTy.getTypePtr()) && |
| 5779 | !EncodePointerToObjCTypedef) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5780 | // Another historical/compatibility reason. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5781 | // We encode the underlying type which comes out as |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5782 | // {...}; |
| 5783 | S += '^'; |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 5784 | if (FD && OPT->getInterfaceDecl()) { |
Fariborz Jahanian | c682ef5 | 2013-07-12 16:41:56 +0000 | [diff] [blame] | 5785 | // Prevent recursive encoding of fields in some rare cases. |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 5786 | ObjCInterfaceDecl *OI = OPT->getInterfaceDecl(); |
| 5787 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
| 5788 | DeepCollectObjCIvars(OI, true, Ivars); |
| 5789 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
| 5790 | if (cast<FieldDecl>(Ivars[i]) == FD) { |
| 5791 | S += '{'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 5792 | S += OI->getObjCRuntimeNameAsString(); |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 5793 | S += '}'; |
| 5794 | return; |
| 5795 | } |
| 5796 | } |
| 5797 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5798 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 5799 | false, ExpandPointedToStructures, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5800 | nullptr, |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5801 | false, false, false, false, false, |
| 5802 | /*EncodePointerToObjCTypedef*/true); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5803 | return; |
| 5804 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5805 | |
| 5806 | S += '@'; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5807 | if (OPT->getInterfaceDecl() && |
| 5808 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5809 | S += '"'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 5810 | S += OPT->getInterfaceDecl()->getObjCRuntimeNameAsString(); |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 5811 | for (const auto *I : OPT->quals()) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5812 | S += '<'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 5813 | S += I->getObjCRuntimeNameAsString(); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5814 | S += '>'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5815 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5816 | S += '"'; |
| 5817 | } |
| 5818 | return; |
| 5819 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5820 | |
John McCall | a9e6e8d | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 5821 | // gcc just blithely ignores member pointers. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5822 | // FIXME: we shoul do better than that. 'M' is available. |
| 5823 | case Type::MemberPointer: |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5824 | // This matches gcc's encoding, even though technically it is insufficient. |
| 5825 | //FIXME. We should do a better job than gcc. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5826 | case Type::Vector: |
| 5827 | case Type::ExtVector: |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5828 | // Until we have a coherent encoding of these three types, issue warning. |
| 5829 | { if (NotEncodedT) |
| 5830 | *NotEncodedT = T; |
| 5831 | return; |
| 5832 | } |
| 5833 | |
| 5834 | // We could see an undeduced auto type here during error recovery. |
| 5835 | // Just ignore it. |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5836 | case Type::Auto: |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5837 | return; |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5838 | |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5839 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5840 | #define ABSTRACT_TYPE(KIND, BASE) |
| 5841 | #define TYPE(KIND, BASE) |
| 5842 | #define DEPENDENT_TYPE(KIND, BASE) \ |
| 5843 | case Type::KIND: |
| 5844 | #define NON_CANONICAL_TYPE(KIND, BASE) \ |
| 5845 | case Type::KIND: |
| 5846 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \ |
| 5847 | case Type::KIND: |
| 5848 | #include "clang/AST/TypeNodes.def" |
| 5849 | llvm_unreachable("@encode for dependent type!"); |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5850 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5851 | llvm_unreachable("bad type kind!"); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5852 | } |
| 5853 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5854 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 5855 | std::string &S, |
| 5856 | const FieldDecl *FD, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5857 | bool includeVBases, |
| 5858 | QualType *NotEncodedT) const { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5859 | assert(RDecl && "Expected non-null RecordDecl"); |
| 5860 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 5861 | if (!RDecl->getDefinition()) |
| 5862 | return; |
| 5863 | |
| 5864 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 5865 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 5866 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 5867 | |
| 5868 | if (CXXRec) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5869 | for (const auto &BI : CXXRec->bases()) { |
| 5870 | if (!BI.isVirtual()) { |
| 5871 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5872 | if (base->isEmpty()) |
| 5873 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 5874 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5875 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 5876 | std::make_pair(offs, base)); |
| 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | |
| 5881 | unsigned i = 0; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 5882 | for (auto *Field : RDecl->fields()) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5883 | uint64_t offs = layout.getFieldOffset(i); |
| 5884 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 5885 | std::make_pair(offs, Field)); |
| 5886 | ++i; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5887 | } |
| 5888 | |
| 5889 | if (CXXRec && includeVBases) { |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 5890 | for (const auto &BI : CXXRec->vbases()) { |
| 5891 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5892 | if (base->isEmpty()) |
| 5893 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 5894 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); |
Eli Friedman | 1d24af8 | 2013-09-18 01:59:16 +0000 | [diff] [blame] | 5895 | if (offs >= uint64_t(toBits(layout.getNonVirtualSize())) && |
| 5896 | FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
Argyrios Kyrtzidis | 81c0b5c | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 5897 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 5898 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5899 | } |
| 5900 | } |
| 5901 | |
| 5902 | CharUnits size; |
| 5903 | if (CXXRec) { |
| 5904 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 5905 | } else { |
| 5906 | size = layout.getSize(); |
| 5907 | } |
| 5908 | |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5909 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5910 | uint64_t CurOffs = 0; |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5911 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5912 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 5913 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 5914 | |
Douglas Gregor | f5d6c74 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 5915 | if (CXXRec && CXXRec->isDynamicClass() && |
| 5916 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5917 | if (FD) { |
| 5918 | S += "\"_vptr$"; |
| 5919 | std::string recname = CXXRec->getNameAsString(); |
| 5920 | if (recname.empty()) recname = "?"; |
| 5921 | S += recname; |
| 5922 | S += '"'; |
| 5923 | } |
| 5924 | S += "^^?"; |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5925 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5926 | CurOffs += getTypeSize(VoidPtrTy); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5927 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5928 | } |
| 5929 | |
| 5930 | if (!RDecl->hasFlexibleArrayMember()) { |
| 5931 | // Mark the end of the structure. |
| 5932 | uint64_t offs = toBits(size); |
| 5933 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5934 | std::make_pair(offs, nullptr)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
| 5937 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5938 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5939 | assert(CurOffs <= CurLayObj->first); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5940 | if (CurOffs < CurLayObj->first) { |
| 5941 | uint64_t padding = CurLayObj->first - CurOffs; |
| 5942 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 5943 | // packing/alignment of members is different that normal, in which case |
| 5944 | // the encoding will be out-of-sync with the real layout. |
| 5945 | // If the runtime switches to just consider the size of types without |
| 5946 | // taking into account alignment, we could make padding explicit in the |
| 5947 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 5948 | // longer then though. |
| 5949 | CurOffs += padding; |
| 5950 | } |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5951 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5952 | |
| 5953 | NamedDecl *dcl = CurLayObj->second; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5954 | if (!dcl) |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5955 | break; // reached end of structure. |
| 5956 | |
| 5957 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 5958 | // We expand the bases without their virtual bases since those are going |
| 5959 | // in the initial structure. Note that this differs from gcc which |
| 5960 | // expands virtual bases each time one is encountered in the hierarchy, |
| 5961 | // making the encoding type bigger than it really is. |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5962 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false, |
| 5963 | NotEncodedT); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5964 | assert(!base->isEmpty()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5965 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5966 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5967 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5968 | } else { |
| 5969 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 5970 | if (FD) { |
| 5971 | S += '"'; |
| 5972 | S += field->getNameAsString(); |
| 5973 | S += '"'; |
| 5974 | } |
| 5975 | |
| 5976 | if (field->isBitField()) { |
| 5977 | EncodeBitField(this, S, field->getType(), field); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5978 | #ifndef NDEBUG |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5979 | CurOffs += field->getBitWidthValue(*this); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5980 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5981 | } else { |
| 5982 | QualType qt = field->getType(); |
| 5983 | getLegacyIntegralTypeEncoding(qt); |
| 5984 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 5985 | /*OutermostType*/false, |
| 5986 | /*EncodingProperty*/false, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5987 | /*StructField*/true, |
| 5988 | false, false, false, NotEncodedT); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5989 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5990 | CurOffs += getTypeSize(field->getType()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5991 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5992 | } |
| 5993 | } |
| 5994 | } |
| 5995 | } |
| 5996 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5997 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 5998 | std::string& S) const { |
| 5999 | if (QT & Decl::OBJC_TQ_In) |
| 6000 | S += 'n'; |
| 6001 | if (QT & Decl::OBJC_TQ_Inout) |
| 6002 | S += 'N'; |
| 6003 | if (QT & Decl::OBJC_TQ_Out) |
| 6004 | S += 'o'; |
| 6005 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 6006 | S += 'O'; |
| 6007 | if (QT & Decl::OBJC_TQ_Byref) |
| 6008 | S += 'R'; |
| 6009 | if (QT & Decl::OBJC_TQ_Oneway) |
| 6010 | S += 'V'; |
| 6011 | } |
| 6012 | |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 6013 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 6014 | if (!ObjCIdDecl) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 6015 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, { }, { }); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 6016 | T = getObjCObjectPointerType(T); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6017 | ObjCIdDecl = buildImplicitTypedef(T, "id"); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 6018 | } |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 6019 | return ObjCIdDecl; |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 6020 | } |
| 6021 | |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 6022 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 6023 | if (!ObjCSelDecl) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6024 | QualType T = getPointerType(ObjCBuiltinSelTy); |
| 6025 | ObjCSelDecl = buildImplicitTypedef(T, "SEL"); |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 6026 | } |
| 6027 | return ObjCSelDecl; |
Fariborz Jahanian | 4bef462 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 6028 | } |
| 6029 | |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 6030 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 6031 | if (!ObjCClassDecl) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 6032 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, { }, { }); |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 6033 | T = getObjCObjectPointerType(T); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6034 | ObjCClassDecl = buildImplicitTypedef(T, "Class"); |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 6035 | } |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 6036 | return ObjCClassDecl; |
Anders Carlsson | f56a7ae | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 6037 | } |
| 6038 | |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 6039 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 6040 | if (!ObjCProtocolClassDecl) { |
| 6041 | ObjCProtocolClassDecl |
| 6042 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 6043 | SourceLocation(), |
| 6044 | &Idents.get("Protocol"), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 6045 | /*typeParamList=*/nullptr, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6046 | /*PrevDecl=*/nullptr, |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 6047 | SourceLocation(), true); |
| 6048 | } |
| 6049 | |
| 6050 | return ObjCProtocolClassDecl; |
| 6051 | } |
| 6052 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6053 | //===----------------------------------------------------------------------===// |
| 6054 | // __builtin_va_list Construction Functions |
| 6055 | //===----------------------------------------------------------------------===// |
| 6056 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 6057 | static TypedefDecl *CreateCharPtrNamedVaListDecl(const ASTContext *Context, |
| 6058 | StringRef Name) { |
| 6059 | // typedef char* __builtin[_ms]_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6060 | QualType T = Context->getPointerType(Context->CharTy); |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 6061 | return Context->buildImplicitTypedef(T, Name); |
| 6062 | } |
| 6063 | |
| 6064 | static TypedefDecl *CreateMSVaListDecl(const ASTContext *Context) { |
| 6065 | return CreateCharPtrNamedVaListDecl(Context, "__builtin_ms_va_list"); |
| 6066 | } |
| 6067 | |
| 6068 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 6069 | return CreateCharPtrNamedVaListDecl(Context, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6070 | } |
| 6071 | |
| 6072 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 6073 | // typedef void* __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6074 | QualType T = Context->getPointerType(Context->VoidTy); |
| 6075 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6076 | } |
| 6077 | |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6078 | static TypedefDecl * |
| 6079 | CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6080 | // struct __va_list |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6081 | RecordDecl *VaListTagDecl = Context->buildImplicitRecord("__va_list"); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6082 | if (Context->getLangOpts().CPlusPlus) { |
| 6083 | // namespace std { struct __va_list { |
| 6084 | NamespaceDecl *NS; |
| 6085 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 6086 | Context->getTranslationUnitDecl(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6087 | /*Inline*/ false, SourceLocation(), |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6088 | SourceLocation(), &Context->Idents.get("std"), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6089 | /*PrevDecl*/ nullptr); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6090 | NS->setImplicit(); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6091 | VaListTagDecl->setDeclContext(NS); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6092 | } |
| 6093 | |
| 6094 | VaListTagDecl->startDefinition(); |
| 6095 | |
| 6096 | const size_t NumFields = 5; |
| 6097 | QualType FieldTypes[NumFields]; |
| 6098 | const char *FieldNames[NumFields]; |
| 6099 | |
| 6100 | // void *__stack; |
| 6101 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); |
| 6102 | FieldNames[0] = "__stack"; |
| 6103 | |
| 6104 | // void *__gr_top; |
| 6105 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); |
| 6106 | FieldNames[1] = "__gr_top"; |
| 6107 | |
| 6108 | // void *__vr_top; |
| 6109 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 6110 | FieldNames[2] = "__vr_top"; |
| 6111 | |
| 6112 | // int __gr_offs; |
| 6113 | FieldTypes[3] = Context->IntTy; |
| 6114 | FieldNames[3] = "__gr_offs"; |
| 6115 | |
| 6116 | // int __vr_offs; |
| 6117 | FieldTypes[4] = Context->IntTy; |
| 6118 | FieldNames[4] = "__vr_offs"; |
| 6119 | |
| 6120 | // Create fields |
| 6121 | for (unsigned i = 0; i < NumFields; ++i) { |
| 6122 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 6123 | VaListTagDecl, |
| 6124 | SourceLocation(), |
| 6125 | SourceLocation(), |
| 6126 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6127 | FieldTypes[i], /*TInfo=*/nullptr, |
| 6128 | /*BitWidth=*/nullptr, |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6129 | /*Mutable=*/false, |
| 6130 | ICIS_NoInit); |
| 6131 | Field->setAccess(AS_public); |
| 6132 | VaListTagDecl->addDecl(Field); |
| 6133 | } |
| 6134 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6135 | Context->VaListTagDecl = VaListTagDecl; |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6136 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6137 | |
| 6138 | // } __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6139 | return Context->buildImplicitTypedef(VaListTagType, "__builtin_va_list"); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6140 | } |
| 6141 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6142 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
| 6143 | // typedef struct __va_list_tag { |
| 6144 | RecordDecl *VaListTagDecl; |
| 6145 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6146 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6147 | VaListTagDecl->startDefinition(); |
| 6148 | |
| 6149 | const size_t NumFields = 5; |
| 6150 | QualType FieldTypes[NumFields]; |
| 6151 | const char *FieldNames[NumFields]; |
| 6152 | |
| 6153 | // unsigned char gpr; |
| 6154 | FieldTypes[0] = Context->UnsignedCharTy; |
| 6155 | FieldNames[0] = "gpr"; |
| 6156 | |
| 6157 | // unsigned char fpr; |
| 6158 | FieldTypes[1] = Context->UnsignedCharTy; |
| 6159 | FieldNames[1] = "fpr"; |
| 6160 | |
| 6161 | // unsigned short reserved; |
| 6162 | FieldTypes[2] = Context->UnsignedShortTy; |
| 6163 | FieldNames[2] = "reserved"; |
| 6164 | |
| 6165 | // void* overflow_arg_area; |
| 6166 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 6167 | FieldNames[3] = "overflow_arg_area"; |
| 6168 | |
| 6169 | // void* reg_save_area; |
| 6170 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
| 6171 | FieldNames[4] = "reg_save_area"; |
| 6172 | |
| 6173 | // Create fields |
| 6174 | for (unsigned i = 0; i < NumFields; ++i) { |
| 6175 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
| 6176 | SourceLocation(), |
| 6177 | SourceLocation(), |
| 6178 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6179 | FieldTypes[i], /*TInfo=*/nullptr, |
| 6180 | /*BitWidth=*/nullptr, |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6181 | /*Mutable=*/false, |
| 6182 | ICIS_NoInit); |
| 6183 | Field->setAccess(AS_public); |
| 6184 | VaListTagDecl->addDecl(Field); |
| 6185 | } |
| 6186 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6187 | Context->VaListTagDecl = VaListTagDecl; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6188 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 6189 | |
| 6190 | // } __va_list_tag; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6191 | TypedefDecl *VaListTagTypedefDecl = |
| 6192 | Context->buildImplicitTypedef(VaListTagType, "__va_list_tag"); |
| 6193 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6194 | QualType VaListTagTypedefType = |
| 6195 | Context->getTypedefType(VaListTagTypedefDecl); |
| 6196 | |
| 6197 | // typedef __va_list_tag __builtin_va_list[1]; |
| 6198 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 6199 | QualType VaListTagArrayType |
| 6200 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 6201 | Size, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6202 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6203 | } |
| 6204 | |
| 6205 | static TypedefDecl * |
| 6206 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6207 | // struct __va_list_tag { |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6208 | RecordDecl *VaListTagDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6209 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6210 | VaListTagDecl->startDefinition(); |
| 6211 | |
| 6212 | const size_t NumFields = 4; |
| 6213 | QualType FieldTypes[NumFields]; |
| 6214 | const char *FieldNames[NumFields]; |
| 6215 | |
| 6216 | // unsigned gp_offset; |
| 6217 | FieldTypes[0] = Context->UnsignedIntTy; |
| 6218 | FieldNames[0] = "gp_offset"; |
| 6219 | |
| 6220 | // unsigned fp_offset; |
| 6221 | FieldTypes[1] = Context->UnsignedIntTy; |
| 6222 | FieldNames[1] = "fp_offset"; |
| 6223 | |
| 6224 | // void* overflow_arg_area; |
| 6225 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 6226 | FieldNames[2] = "overflow_arg_area"; |
| 6227 | |
| 6228 | // void* reg_save_area; |
| 6229 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 6230 | FieldNames[3] = "reg_save_area"; |
| 6231 | |
| 6232 | // Create fields |
| 6233 | for (unsigned i = 0; i < NumFields; ++i) { |
| 6234 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 6235 | VaListTagDecl, |
| 6236 | SourceLocation(), |
| 6237 | SourceLocation(), |
| 6238 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6239 | FieldTypes[i], /*TInfo=*/nullptr, |
| 6240 | /*BitWidth=*/nullptr, |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6241 | /*Mutable=*/false, |
| 6242 | ICIS_NoInit); |
| 6243 | Field->setAccess(AS_public); |
| 6244 | VaListTagDecl->addDecl(Field); |
| 6245 | } |
| 6246 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6247 | Context->VaListTagDecl = VaListTagDecl; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6248 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 6249 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6250 | // }; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6251 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6252 | // typedef struct __va_list_tag __builtin_va_list[1]; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6253 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6254 | QualType VaListTagArrayType = |
| 6255 | Context->getConstantArrayType(VaListTagType, Size, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6256 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6257 | } |
| 6258 | |
| 6259 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
| 6260 | // typedef int __builtin_va_list[4]; |
| 6261 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); |
| 6262 | QualType IntArrayType |
| 6263 | = Context->getConstantArrayType(Context->IntTy, |
| 6264 | Size, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6265 | return Context->buildImplicitTypedef(IntArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6266 | } |
| 6267 | |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6268 | static TypedefDecl * |
| 6269 | CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6270 | // struct __va_list |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6271 | RecordDecl *VaListDecl = Context->buildImplicitRecord("__va_list"); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6272 | if (Context->getLangOpts().CPlusPlus) { |
| 6273 | // namespace std { struct __va_list { |
| 6274 | NamespaceDecl *NS; |
| 6275 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 6276 | Context->getTranslationUnitDecl(), |
| 6277 | /*Inline*/false, SourceLocation(), |
| 6278 | SourceLocation(), &Context->Idents.get("std"), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6279 | /*PrevDecl*/ nullptr); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6280 | NS->setImplicit(); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6281 | VaListDecl->setDeclContext(NS); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6282 | } |
| 6283 | |
| 6284 | VaListDecl->startDefinition(); |
| 6285 | |
| 6286 | // void * __ap; |
| 6287 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 6288 | VaListDecl, |
| 6289 | SourceLocation(), |
| 6290 | SourceLocation(), |
| 6291 | &Context->Idents.get("__ap"), |
| 6292 | Context->getPointerType(Context->VoidTy), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6293 | /*TInfo=*/nullptr, |
| 6294 | /*BitWidth=*/nullptr, |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6295 | /*Mutable=*/false, |
| 6296 | ICIS_NoInit); |
| 6297 | Field->setAccess(AS_public); |
| 6298 | VaListDecl->addDecl(Field); |
| 6299 | |
| 6300 | // }; |
| 6301 | VaListDecl->completeDefinition(); |
| 6302 | |
| 6303 | // typedef struct __va_list __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6304 | QualType T = Context->getRecordType(VaListDecl); |
| 6305 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6306 | } |
| 6307 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6308 | static TypedefDecl * |
| 6309 | CreateSystemZBuiltinVaListDecl(const ASTContext *Context) { |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6310 | // struct __va_list_tag { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6311 | RecordDecl *VaListTagDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6312 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6313 | VaListTagDecl->startDefinition(); |
| 6314 | |
| 6315 | const size_t NumFields = 4; |
| 6316 | QualType FieldTypes[NumFields]; |
| 6317 | const char *FieldNames[NumFields]; |
| 6318 | |
| 6319 | // long __gpr; |
| 6320 | FieldTypes[0] = Context->LongTy; |
| 6321 | FieldNames[0] = "__gpr"; |
| 6322 | |
| 6323 | // long __fpr; |
| 6324 | FieldTypes[1] = Context->LongTy; |
| 6325 | FieldNames[1] = "__fpr"; |
| 6326 | |
| 6327 | // void *__overflow_arg_area; |
| 6328 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 6329 | FieldNames[2] = "__overflow_arg_area"; |
| 6330 | |
| 6331 | // void *__reg_save_area; |
| 6332 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 6333 | FieldNames[3] = "__reg_save_area"; |
| 6334 | |
| 6335 | // Create fields |
| 6336 | for (unsigned i = 0; i < NumFields; ++i) { |
| 6337 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 6338 | VaListTagDecl, |
| 6339 | SourceLocation(), |
| 6340 | SourceLocation(), |
| 6341 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6342 | FieldTypes[i], /*TInfo=*/nullptr, |
| 6343 | /*BitWidth=*/nullptr, |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6344 | /*Mutable=*/false, |
| 6345 | ICIS_NoInit); |
| 6346 | Field->setAccess(AS_public); |
| 6347 | VaListTagDecl->addDecl(Field); |
| 6348 | } |
| 6349 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6350 | Context->VaListTagDecl = VaListTagDecl; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6351 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6352 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6353 | // }; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6354 | |
| 6355 | // typedef __va_list_tag __builtin_va_list[1]; |
| 6356 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6357 | QualType VaListTagArrayType = |
| 6358 | Context->getConstantArrayType(VaListTagType, Size, ArrayType::Normal, 0); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6359 | |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6360 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6361 | } |
| 6362 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6363 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
| 6364 | TargetInfo::BuiltinVaListKind Kind) { |
| 6365 | switch (Kind) { |
| 6366 | case TargetInfo::CharPtrBuiltinVaList: |
| 6367 | return CreateCharPtrBuiltinVaListDecl(Context); |
| 6368 | case TargetInfo::VoidPtrBuiltinVaList: |
| 6369 | return CreateVoidPtrBuiltinVaListDecl(Context); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6370 | case TargetInfo::AArch64ABIBuiltinVaList: |
| 6371 | return CreateAArch64ABIBuiltinVaListDecl(Context); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6372 | case TargetInfo::PowerABIBuiltinVaList: |
| 6373 | return CreatePowerABIBuiltinVaListDecl(Context); |
| 6374 | case TargetInfo::X86_64ABIBuiltinVaList: |
| 6375 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
| 6376 | case TargetInfo::PNaClABIBuiltinVaList: |
| 6377 | return CreatePNaClABIBuiltinVaListDecl(Context); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6378 | case TargetInfo::AAPCSABIBuiltinVaList: |
| 6379 | return CreateAAPCSABIBuiltinVaListDecl(Context); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6380 | case TargetInfo::SystemZBuiltinVaList: |
| 6381 | return CreateSystemZBuiltinVaListDecl(Context); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6382 | } |
| 6383 | |
| 6384 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
| 6385 | } |
| 6386 | |
| 6387 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6388 | if (!BuiltinVaListDecl) { |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6389 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6390 | assert(BuiltinVaListDecl->isImplicit()); |
| 6391 | } |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6392 | |
| 6393 | return BuiltinVaListDecl; |
| 6394 | } |
| 6395 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6396 | Decl *ASTContext::getVaListTagDecl() const { |
| 6397 | // Force the creation of VaListTagDecl by building the __builtin_va_list |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 6398 | // declaration. |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6399 | if (!VaListTagDecl) |
| 6400 | (void)getBuiltinVaListDecl(); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 6401 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6402 | return VaListTagDecl; |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 6403 | } |
| 6404 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 6405 | TypedefDecl *ASTContext::getBuiltinMSVaListDecl() const { |
| 6406 | if (!BuiltinMSVaListDecl) |
| 6407 | BuiltinMSVaListDecl = CreateMSVaListDecl(this); |
| 6408 | |
| 6409 | return BuiltinMSVaListDecl; |
| 6410 | } |
| 6411 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6412 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6413 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 6414 | "'NSConstantString' type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6415 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6416 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 6417 | } |
| 6418 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6419 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 6420 | /// lookup. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6421 | TemplateName |
| 6422 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 6423 | UnresolvedSetIterator End) const { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6424 | unsigned size = End - Begin; |
| 6425 | assert(size > 1 && "set is not overloaded!"); |
| 6426 | |
| 6427 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 6428 | size * sizeof(FunctionTemplateDecl*)); |
| 6429 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 6430 | |
| 6431 | NamedDecl **Storage = OT->getStorage(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6432 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6433 | NamedDecl *D = *I; |
| 6434 | assert(isa<FunctionTemplateDecl>(D) || |
| 6435 | (isa<UsingShadowDecl>(D) && |
| 6436 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 6437 | *Storage++ = D; |
| 6438 | } |
| 6439 | |
| 6440 | return TemplateName(OT); |
| 6441 | } |
| 6442 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6443 | /// \brief Retrieve the template name that represents a qualified |
| 6444 | /// template name such as \c std::vector. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6445 | TemplateName |
| 6446 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 6447 | bool TemplateKeyword, |
| 6448 | TemplateDecl *Template) const { |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 6449 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 6450 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6451 | // FIXME: Canonicalization? |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6452 | llvm::FoldingSetNodeID ID; |
| 6453 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 6454 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6455 | void *InsertPos = nullptr; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6456 | QualifiedTemplateName *QTN = |
| 6457 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6458 | if (!QTN) { |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6459 | QTN = new (*this, llvm::alignOf<QualifiedTemplateName>()) |
| 6460 | QualifiedTemplateName(NNS, TemplateKeyword, Template); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6461 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 6462 | } |
| 6463 | |
| 6464 | return TemplateName(QTN); |
| 6465 | } |
| 6466 | |
| 6467 | /// \brief Retrieve the template name that represents a dependent |
| 6468 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6469 | TemplateName |
| 6470 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 6471 | const IdentifierInfo *Name) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6472 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 6473 | "Nested name specifier must be dependent"); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6474 | |
| 6475 | llvm::FoldingSetNodeID ID; |
| 6476 | DependentTemplateName::Profile(ID, NNS, Name); |
| 6477 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6478 | void *InsertPos = nullptr; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6479 | DependentTemplateName *QTN = |
| 6480 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6481 | |
| 6482 | if (QTN) |
| 6483 | return TemplateName(QTN); |
| 6484 | |
| 6485 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 6486 | if (CanonNNS == NNS) { |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6487 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6488 | DependentTemplateName(NNS, Name); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6489 | } else { |
| 6490 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6491 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6492 | DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6493 | DependentTemplateName *CheckQTN = |
| 6494 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6495 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 6496 | (void)CheckQTN; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6497 | } |
| 6498 | |
| 6499 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 6500 | return TemplateName(QTN); |
| 6501 | } |
| 6502 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6503 | /// \brief Retrieve the template name that represents a dependent |
| 6504 | /// template name such as \c MetaFun::template operator+. |
| 6505 | TemplateName |
| 6506 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6507 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6508 | assert((!NNS || NNS->isDependent()) && |
| 6509 | "Nested name specifier must be dependent"); |
| 6510 | |
| 6511 | llvm::FoldingSetNodeID ID; |
| 6512 | DependentTemplateName::Profile(ID, NNS, Operator); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6513 | |
| 6514 | void *InsertPos = nullptr; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6515 | DependentTemplateName *QTN |
| 6516 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6517 | |
| 6518 | if (QTN) |
| 6519 | return TemplateName(QTN); |
| 6520 | |
| 6521 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 6522 | if (CanonNNS == NNS) { |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6523 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6524 | DependentTemplateName(NNS, Operator); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6525 | } else { |
| 6526 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6527 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6528 | DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6529 | |
| 6530 | DependentTemplateName *CheckQTN |
| 6531 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6532 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 6533 | (void)CheckQTN; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6534 | } |
| 6535 | |
| 6536 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 6537 | return TemplateName(QTN); |
| 6538 | } |
| 6539 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6540 | TemplateName |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6541 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 6542 | TemplateName replacement) const { |
| 6543 | llvm::FoldingSetNodeID ID; |
| 6544 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6545 | |
| 6546 | void *insertPos = nullptr; |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6547 | SubstTemplateTemplateParmStorage *subst |
| 6548 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 6549 | |
| 6550 | if (!subst) { |
| 6551 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 6552 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 6553 | } |
| 6554 | |
| 6555 | return TemplateName(subst); |
| 6556 | } |
| 6557 | |
| 6558 | TemplateName |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6559 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 6560 | const TemplateArgument &ArgPack) const { |
| 6561 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 6562 | llvm::FoldingSetNodeID ID; |
| 6563 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6564 | |
| 6565 | void *InsertPos = nullptr; |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6566 | SubstTemplateTemplateParmPackStorage *Subst |
| 6567 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 6568 | |
| 6569 | if (!Subst) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6570 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6571 | ArgPack.pack_size(), |
| 6572 | ArgPack.pack_begin()); |
| 6573 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 6574 | } |
| 6575 | |
| 6576 | return TemplateName(Subst); |
| 6577 | } |
| 6578 | |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6579 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | ab13857 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 6580 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 6581 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 6582 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6583 | switch (Type) { |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 6584 | case TargetInfo::NoInt: return CanQualType(); |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 6585 | case TargetInfo::SignedChar: return SignedCharTy; |
| 6586 | case TargetInfo::UnsignedChar: return UnsignedCharTy; |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6587 | case TargetInfo::SignedShort: return ShortTy; |
| 6588 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 6589 | case TargetInfo::SignedInt: return IntTy; |
| 6590 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 6591 | case TargetInfo::SignedLong: return LongTy; |
| 6592 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 6593 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 6594 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 6595 | } |
| 6596 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6597 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6598 | } |
Ted Kremenek | 77c51b2 | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 6599 | |
| 6600 | //===----------------------------------------------------------------------===// |
| 6601 | // Type Predicates. |
| 6602 | //===----------------------------------------------------------------------===// |
| 6603 | |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6604 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 6605 | /// garbage collection attribute. |
| 6606 | /// |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6607 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6608 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6609 | return Qualifiers::GCNone; |
| 6610 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6611 | assert(getLangOpts().ObjC1); |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6612 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 6613 | |
| 6614 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 6615 | // (or pointers to them) be treated as though they were declared |
| 6616 | // as __strong. |
| 6617 | if (GCAttrs == Qualifiers::GCNone) { |
| 6618 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 6619 | return Qualifiers::Strong; |
| 6620 | else if (Ty->isPointerType()) |
| 6621 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 6622 | } else { |
| 6623 | // It's not valid to set GC attributes on anything that isn't a |
| 6624 | // pointer. |
| 6625 | #ifndef NDEBUG |
| 6626 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 6627 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 6628 | CT = AT->getElementType(); |
| 6629 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 6630 | #endif |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6631 | } |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 6632 | return GCAttrs; |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6633 | } |
| 6634 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6635 | //===----------------------------------------------------------------------===// |
| 6636 | // Type Compatibility Testing |
| 6637 | //===----------------------------------------------------------------------===// |
Chris Lattner | b338a6b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 6638 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6639 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6640 | /// compatible. |
| 6641 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 6642 | const VectorType *RHS) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 6643 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6644 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6645 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6646 | } |
| 6647 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6648 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 6649 | QualType SecondVec) { |
| 6650 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 6651 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 6652 | |
| 6653 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 6654 | return true; |
| 6655 | |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6656 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 6657 | // equivalent GCC vector types. |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6658 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 6659 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6660 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6661 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6662 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 6663 | First->getVectorKind() != VectorType::AltiVecBool && |
| 6664 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 6665 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6666 | return true; |
| 6667 | |
| 6668 | return false; |
| 6669 | } |
| 6670 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6671 | //===----------------------------------------------------------------------===// |
| 6672 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 6673 | //===----------------------------------------------------------------------===// |
| 6674 | |
| 6675 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 6676 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6677 | bool |
| 6678 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 6679 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 6680 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6681 | return true; |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 6682 | for (auto *PI : rProto->protocols()) |
| 6683 | if (ProtocolCompatibleWithProtocol(lProto, PI)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6684 | return true; |
| 6685 | return false; |
| 6686 | } |
| 6687 | |
Dmitri Gribenko | 4364fcf | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 6688 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and |
| 6689 | /// Class<pr1, ...>. |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6690 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 6691 | QualType rhs) { |
| 6692 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 6693 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 6694 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 6695 | |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6696 | for (auto *lhsProto : lhsQID->quals()) { |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6697 | bool match = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6698 | for (auto *rhsProto : rhsOPT->quals()) { |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6699 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 6700 | match = true; |
| 6701 | break; |
| 6702 | } |
| 6703 | } |
| 6704 | if (!match) |
| 6705 | return false; |
| 6706 | } |
| 6707 | return true; |
| 6708 | } |
| 6709 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6710 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 6711 | /// ObjCQualifiedIDType. |
| 6712 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 6713 | bool compare) { |
| 6714 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6715 | if (lhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6716 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 6717 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6718 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6719 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 6720 | return true; |
| 6721 | |
| 6722 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6723 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6724 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6725 | if (!rhsOPT) return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6726 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6727 | if (rhsOPT->qual_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6728 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6729 | // make sure we check the class hierarchy. |
| 6730 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6731 | for (auto *I : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6732 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6733 | // see if static class implements all of id's protocols, directly or |
| 6734 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6735 | if (!rhsID->ClassImplementsProtocol(I, true)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6736 | return false; |
| 6737 | } |
| 6738 | } |
| 6739 | // If there are no qualifiers and no interface, we have an 'id'. |
| 6740 | return true; |
| 6741 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6742 | // Both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6743 | for (auto *lhsProto : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6744 | bool match = false; |
| 6745 | |
| 6746 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6747 | // see if static class implements all of id's protocols, directly or |
| 6748 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6749 | for (auto *rhsProto : rhsOPT->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6750 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6751 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6752 | match = true; |
| 6753 | break; |
| 6754 | } |
| 6755 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6756 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6757 | // make sure we check the class hierarchy. |
| 6758 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6759 | for (auto *I : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6760 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6761 | // see if static class implements all of id's protocols, directly or |
| 6762 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6763 | if (rhsID->ClassImplementsProtocol(I, true)) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6764 | match = true; |
| 6765 | break; |
| 6766 | } |
| 6767 | } |
| 6768 | } |
| 6769 | if (!match) |
| 6770 | return false; |
| 6771 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6772 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6773 | return true; |
| 6774 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6775 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6776 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 6777 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 6778 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6779 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6780 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6781 | // If both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6782 | for (auto *lhsProto : lhsOPT->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6783 | bool match = false; |
| 6784 | |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6785 | // 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] | 6786 | // see if static class implements all of id's protocols, directly or |
| 6787 | // through its super class and categories. |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6788 | // First, lhs protocols in the qualifier list must be found, direct |
| 6789 | // or indirect in rhs's qualifier list or it is a mismatch. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6790 | for (auto *rhsProto : rhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6791 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6792 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6793 | match = true; |
| 6794 | break; |
| 6795 | } |
| 6796 | } |
| 6797 | if (!match) |
| 6798 | return false; |
| 6799 | } |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6800 | |
| 6801 | // Static class's protocols, or its super class or category protocols |
| 6802 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 6803 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 6804 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 6805 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 6806 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 6807 | // no type qualifier and its class has no static protocol(s) |
| 6808 | // assume that it is mismatch. |
| 6809 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 6810 | return false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6811 | for (auto *lhsProto : LHSInheritedProtocols) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6812 | bool match = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6813 | for (auto *rhsProto : rhsQID->quals()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6814 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6815 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6816 | match = true; |
| 6817 | break; |
| 6818 | } |
| 6819 | } |
| 6820 | if (!match) |
| 6821 | return false; |
| 6822 | } |
| 6823 | } |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6824 | return true; |
| 6825 | } |
| 6826 | return false; |
| 6827 | } |
| 6828 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6829 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6830 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 6831 | /// protocol qualifiers on the LHS or RHS. |
| 6832 | /// |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6833 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 6834 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6835 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 6836 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 6837 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 6838 | // 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] | 6839 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 6840 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6841 | return true; |
| 6842 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 6843 | // Function object that propagates a successful result or handles |
| 6844 | // __kindof types. |
| 6845 | auto finish = [&](bool succeeded) -> bool { |
| 6846 | if (succeeded) |
| 6847 | return true; |
| 6848 | |
| 6849 | if (!RHS->isKindOfType()) |
| 6850 | return false; |
| 6851 | |
| 6852 | // Strip off __kindof and protocol qualifiers, then check whether |
| 6853 | // we can assign the other way. |
| 6854 | return canAssignObjCInterfaces(RHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 6855 | LHSOPT->stripObjCKindOfTypeAndQuals(*this)); |
| 6856 | }; |
| 6857 | |
| 6858 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) { |
| 6859 | return finish(ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 6860 | QualType(RHSOPT,0), |
| 6861 | false)); |
| 6862 | } |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6863 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 6864 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) { |
| 6865 | return finish(ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 6866 | QualType(RHSOPT,0))); |
| 6867 | } |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6868 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6869 | // If we have 2 user-defined types, fall into that path. |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 6870 | if (LHS->getInterface() && RHS->getInterface()) { |
| 6871 | return finish(canAssignObjCInterfaces(LHS, RHS)); |
| 6872 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6873 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6874 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6875 | } |
| 6876 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6877 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 6878 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6879 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 6880 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 6881 | /// not OK. For the return type, the opposite is not OK. |
| 6882 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 6883 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6884 | const ObjCObjectPointerType *RHSOPT, |
| 6885 | bool BlockReturnType) { |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 6886 | |
| 6887 | // Function object that propagates a successful result or handles |
| 6888 | // __kindof types. |
| 6889 | auto finish = [&](bool succeeded) -> bool { |
| 6890 | if (succeeded) |
| 6891 | return true; |
| 6892 | |
| 6893 | const ObjCObjectPointerType *Expected = BlockReturnType ? RHSOPT : LHSOPT; |
| 6894 | if (!Expected->isKindOfType()) |
| 6895 | return false; |
| 6896 | |
| 6897 | // Strip off __kindof and protocol qualifiers, then check whether |
| 6898 | // we can assign the other way. |
| 6899 | return canAssignObjCInterfacesInBlockPointer( |
| 6900 | RHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 6901 | LHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 6902 | BlockReturnType); |
| 6903 | }; |
| 6904 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 6905 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6906 | return true; |
| 6907 | |
| 6908 | if (LHSOPT->isObjCBuiltinType()) { |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 6909 | return finish(RHSOPT->isObjCBuiltinType() || |
| 6910 | RHSOPT->isObjCQualifiedIdType()); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6911 | } |
| 6912 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 6913 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 6914 | return finish(ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 6915 | QualType(RHSOPT,0), |
| 6916 | false)); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6917 | |
| 6918 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 6919 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 6920 | if (LHS && RHS) { // We have 2 user-defined types. |
| 6921 | if (LHS != RHS) { |
| 6922 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 6923 | return finish(BlockReturnType); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6924 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 6925 | return finish(!BlockReturnType); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6926 | } |
| 6927 | else |
| 6928 | return true; |
| 6929 | } |
| 6930 | return false; |
| 6931 | } |
| 6932 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 6933 | /// Comparison routine for Objective-C protocols to be used with |
| 6934 | /// llvm::array_pod_sort. |
| 6935 | static int compareObjCProtocolsByName(ObjCProtocolDecl * const *lhs, |
| 6936 | ObjCProtocolDecl * const *rhs) { |
| 6937 | return (*lhs)->getName().compare((*rhs)->getName()); |
| 6938 | |
| 6939 | } |
| 6940 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6941 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 6942 | /// of protocols inherited from two distinct objective-c pointer objects with |
| 6943 | /// the given common base. |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6944 | /// It is used to build composite qualifier list of the composite type of |
| 6945 | /// the conditional expression involving two objective-c pointer objects. |
| 6946 | static |
| 6947 | void getIntersectionOfProtocols(ASTContext &Context, |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 6948 | const ObjCInterfaceDecl *CommonBase, |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6949 | const ObjCObjectPointerType *LHSOPT, |
| 6950 | const ObjCObjectPointerType *RHSOPT, |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 6951 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionSet) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6952 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6953 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 6954 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 6955 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 6956 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 6957 | |
| 6958 | // Add all of the protocols for the LHS. |
| 6959 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSProtocolSet; |
| 6960 | |
| 6961 | // Start with the protocol qualifiers. |
| 6962 | for (auto proto : LHS->quals()) { |
| 6963 | Context.CollectInheritedProtocols(proto, LHSProtocolSet); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6964 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 6965 | |
| 6966 | // Also add the protocols associated with the LHS interface. |
| 6967 | Context.CollectInheritedProtocols(LHS->getInterface(), LHSProtocolSet); |
| 6968 | |
| 6969 | // Add all of the protocls for the RHS. |
| 6970 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSProtocolSet; |
| 6971 | |
| 6972 | // Start with the protocol qualifiers. |
| 6973 | for (auto proto : RHS->quals()) { |
| 6974 | Context.CollectInheritedProtocols(proto, RHSProtocolSet); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6975 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 6976 | |
| 6977 | // Also add the protocols associated with the RHS interface. |
| 6978 | Context.CollectInheritedProtocols(RHS->getInterface(), RHSProtocolSet); |
| 6979 | |
| 6980 | // Compute the intersection of the collected protocol sets. |
| 6981 | for (auto proto : LHSProtocolSet) { |
| 6982 | if (RHSProtocolSet.count(proto)) |
| 6983 | IntersectionSet.push_back(proto); |
| 6984 | } |
| 6985 | |
| 6986 | // Compute the set of protocols that is implied by either the common type or |
| 6987 | // the protocols within the intersection. |
| 6988 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ImpliedProtocols; |
| 6989 | Context.CollectInheritedProtocols(CommonBase, ImpliedProtocols); |
| 6990 | |
| 6991 | // Remove any implied protocols from the list of inherited protocols. |
| 6992 | if (!ImpliedProtocols.empty()) { |
| 6993 | IntersectionSet.erase( |
| 6994 | std::remove_if(IntersectionSet.begin(), |
| 6995 | IntersectionSet.end(), |
| 6996 | [&](ObjCProtocolDecl *proto) -> bool { |
| 6997 | return ImpliedProtocols.count(proto) > 0; |
| 6998 | }), |
| 6999 | IntersectionSet.end()); |
| 7000 | } |
| 7001 | |
| 7002 | // Sort the remaining protocols by name. |
| 7003 | llvm::array_pod_sort(IntersectionSet.begin(), IntersectionSet.end(), |
| 7004 | compareObjCProtocolsByName); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7005 | } |
| 7006 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7007 | /// Determine whether the first type is a subtype of the second. |
| 7008 | static bool canAssignObjCObjectTypes(ASTContext &ctx, QualType lhs, |
| 7009 | QualType rhs) { |
| 7010 | // Common case: two object pointers. |
| 7011 | const ObjCObjectPointerType *lhsOPT = lhs->getAs<ObjCObjectPointerType>(); |
| 7012 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 7013 | if (lhsOPT && rhsOPT) |
| 7014 | return ctx.canAssignObjCInterfaces(lhsOPT, rhsOPT); |
| 7015 | |
| 7016 | // Two block pointers. |
| 7017 | const BlockPointerType *lhsBlock = lhs->getAs<BlockPointerType>(); |
| 7018 | const BlockPointerType *rhsBlock = rhs->getAs<BlockPointerType>(); |
| 7019 | if (lhsBlock && rhsBlock) |
| 7020 | return ctx.typesAreBlockPointerCompatible(lhs, rhs); |
| 7021 | |
| 7022 | // If either is an unqualified 'id' and the other is a block, it's |
| 7023 | // acceptable. |
| 7024 | if ((lhsOPT && lhsOPT->isObjCIdType() && rhsBlock) || |
| 7025 | (rhsOPT && rhsOPT->isObjCIdType() && lhsBlock)) |
| 7026 | return true; |
| 7027 | |
| 7028 | return false; |
| 7029 | } |
| 7030 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7031 | // Check that the given Objective-C type argument lists are equivalent. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7032 | static bool sameObjCTypeArgs(ASTContext &ctx, |
| 7033 | const ObjCInterfaceDecl *iface, |
| 7034 | ArrayRef<QualType> lhsArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7035 | ArrayRef<QualType> rhsArgs, |
| 7036 | bool stripKindOf) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7037 | if (lhsArgs.size() != rhsArgs.size()) |
| 7038 | return false; |
| 7039 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7040 | ObjCTypeParamList *typeParams = iface->getTypeParamList(); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7041 | for (unsigned i = 0, n = lhsArgs.size(); i != n; ++i) { |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7042 | if (ctx.hasSameType(lhsArgs[i], rhsArgs[i])) |
| 7043 | continue; |
| 7044 | |
| 7045 | switch (typeParams->begin()[i]->getVariance()) { |
| 7046 | case ObjCTypeParamVariance::Invariant: |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7047 | if (!stripKindOf || |
| 7048 | !ctx.hasSameType(lhsArgs[i].stripObjCKindOfType(ctx), |
| 7049 | rhsArgs[i].stripObjCKindOfType(ctx))) { |
| 7050 | return false; |
| 7051 | } |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7052 | break; |
| 7053 | |
| 7054 | case ObjCTypeParamVariance::Covariant: |
| 7055 | if (!canAssignObjCObjectTypes(ctx, lhsArgs[i], rhsArgs[i])) |
| 7056 | return false; |
| 7057 | break; |
| 7058 | |
| 7059 | case ObjCTypeParamVariance::Contravariant: |
| 7060 | if (!canAssignObjCObjectTypes(ctx, rhsArgs[i], lhsArgs[i])) |
| 7061 | return false; |
| 7062 | break; |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7063 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7064 | } |
| 7065 | |
| 7066 | return true; |
| 7067 | } |
| 7068 | |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 7069 | QualType ASTContext::areCommonBaseCompatible( |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7070 | const ObjCObjectPointerType *Lptr, |
| 7071 | const ObjCObjectPointerType *Rptr) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7072 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 7073 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 7074 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 7075 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7076 | |
| 7077 | if (!LDecl || !RDecl) |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 7078 | return QualType(); |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7079 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7080 | // Follow the left-hand side up the class hierarchy until we either hit a |
| 7081 | // root or find the RHS. Record the ancestors in case we don't find it. |
| 7082 | llvm::SmallDenseMap<const ObjCInterfaceDecl *, const ObjCObjectType *, 4> |
| 7083 | LHSAncestors; |
| 7084 | while (true) { |
| 7085 | // Record this ancestor. We'll need this if the common type isn't in the |
| 7086 | // path from the LHS to the root. |
| 7087 | LHSAncestors[LHS->getInterface()->getCanonicalDecl()] = LHS; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7088 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7089 | if (declaresSameEntity(LHS->getInterface(), RDecl)) { |
| 7090 | // Get the type arguments. |
| 7091 | ArrayRef<QualType> LHSTypeArgs = LHS->getTypeArgsAsWritten(); |
| 7092 | bool anyChanges = false; |
| 7093 | if (LHS->isSpecialized() && RHS->isSpecialized()) { |
| 7094 | // Both have type arguments, compare them. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7095 | if (!sameObjCTypeArgs(*this, LHS->getInterface(), |
| 7096 | LHS->getTypeArgs(), RHS->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7097 | /*stripKindOf=*/true)) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7098 | return QualType(); |
| 7099 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { |
| 7100 | // If only one has type arguments, the result will not have type |
| 7101 | // arguments. |
| 7102 | LHSTypeArgs = { }; |
| 7103 | anyChanges = true; |
| 7104 | } |
| 7105 | |
| 7106 | // Compute the intersection of protocols. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7107 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7108 | getIntersectionOfProtocols(*this, LHS->getInterface(), Lptr, Rptr, |
| 7109 | Protocols); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7110 | if (!Protocols.empty()) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7111 | anyChanges = true; |
| 7112 | |
| 7113 | // If anything in the LHS will have changed, build a new result type. |
| 7114 | if (anyChanges) { |
| 7115 | QualType Result = getObjCInterfaceType(LHS->getInterface()); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7116 | Result = getObjCObjectType(Result, LHSTypeArgs, Protocols, |
| 7117 | LHS->isKindOfType()); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7118 | return getObjCObjectPointerType(Result); |
| 7119 | } |
| 7120 | |
| 7121 | return getObjCObjectPointerType(QualType(LHS, 0)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7122 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7123 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7124 | // Find the superclass. |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7125 | QualType LHSSuperType = LHS->getSuperClassType(); |
| 7126 | if (LHSSuperType.isNull()) |
| 7127 | break; |
| 7128 | |
| 7129 | LHS = LHSSuperType->castAs<ObjCObjectType>(); |
| 7130 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7131 | |
| 7132 | // We didn't find anything by following the LHS to its root; now check |
| 7133 | // the RHS against the cached set of ancestors. |
| 7134 | while (true) { |
| 7135 | auto KnownLHS = LHSAncestors.find(RHS->getInterface()->getCanonicalDecl()); |
| 7136 | if (KnownLHS != LHSAncestors.end()) { |
| 7137 | LHS = KnownLHS->second; |
| 7138 | |
| 7139 | // Get the type arguments. |
| 7140 | ArrayRef<QualType> RHSTypeArgs = RHS->getTypeArgsAsWritten(); |
| 7141 | bool anyChanges = false; |
| 7142 | if (LHS->isSpecialized() && RHS->isSpecialized()) { |
| 7143 | // Both have type arguments, compare them. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7144 | if (!sameObjCTypeArgs(*this, LHS->getInterface(), |
| 7145 | LHS->getTypeArgs(), RHS->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7146 | /*stripKindOf=*/true)) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7147 | return QualType(); |
| 7148 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { |
| 7149 | // If only one has type arguments, the result will not have type |
| 7150 | // arguments. |
| 7151 | RHSTypeArgs = { }; |
| 7152 | anyChanges = true; |
| 7153 | } |
| 7154 | |
| 7155 | // Compute the intersection of protocols. |
| 7156 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 7157 | getIntersectionOfProtocols(*this, RHS->getInterface(), Lptr, Rptr, |
| 7158 | Protocols); |
| 7159 | if (!Protocols.empty()) |
| 7160 | anyChanges = true; |
| 7161 | |
| 7162 | if (anyChanges) { |
| 7163 | QualType Result = getObjCInterfaceType(RHS->getInterface()); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7164 | Result = getObjCObjectType(Result, RHSTypeArgs, Protocols, |
| 7165 | RHS->isKindOfType()); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7166 | return getObjCObjectPointerType(Result); |
| 7167 | } |
| 7168 | |
| 7169 | return getObjCObjectPointerType(QualType(RHS, 0)); |
| 7170 | } |
| 7171 | |
| 7172 | // Find the superclass of the RHS. |
| 7173 | QualType RHSSuperType = RHS->getSuperClassType(); |
| 7174 | if (RHSSuperType.isNull()) |
| 7175 | break; |
| 7176 | |
| 7177 | RHS = RHSSuperType->castAs<ObjCObjectType>(); |
| 7178 | } |
| 7179 | |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 7180 | return QualType(); |
| 7181 | } |
| 7182 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7183 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 7184 | const ObjCObjectType *RHS) { |
| 7185 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 7186 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 7187 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7188 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 7189 | // the LHS. |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7190 | ObjCInterfaceDecl *LHSInterface = LHS->getInterface(); |
| 7191 | bool IsSuperClass = LHSInterface->isSuperClassOf(RHS->getInterface()); |
| 7192 | if (!IsSuperClass) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7193 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7194 | |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7195 | // If the LHS has protocol qualifiers, determine whether all of them are |
| 7196 | // satisfied by the RHS (i.e., the RHS has a superset of the protocols in the |
| 7197 | // LHS). |
| 7198 | if (LHS->getNumProtocols() > 0) { |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 7199 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 7200 | // ; i.e., SuperClass may implement at least one of the protocols |
| 7201 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 7202 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 7203 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
| 7204 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
| 7205 | // Also, if RHS has explicit quelifiers, include them for comparing with LHS's |
| 7206 | // qualifiers. |
| 7207 | for (auto *RHSPI : RHS->quals()) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7208 | CollectInheritedProtocols(RHSPI, SuperClassInheritedProtocols); |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 7209 | // If there is no protocols associated with RHS, it is not a match. |
| 7210 | if (SuperClassInheritedProtocols.empty()) |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 7211 | return false; |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 7212 | |
| 7213 | for (const auto *LHSProto : LHS->quals()) { |
| 7214 | bool SuperImplementsProtocol = false; |
| 7215 | for (auto *SuperClassProto : SuperClassInheritedProtocols) |
| 7216 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 7217 | SuperImplementsProtocol = true; |
| 7218 | break; |
| 7219 | } |
| 7220 | if (!SuperImplementsProtocol) |
| 7221 | return false; |
| 7222 | } |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7223 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7224 | |
| 7225 | // If the LHS is specialized, we may need to check type arguments. |
| 7226 | if (LHS->isSpecialized()) { |
| 7227 | // Follow the superclass chain until we've matched the LHS class in the |
| 7228 | // hierarchy. This substitutes type arguments through. |
| 7229 | const ObjCObjectType *RHSSuper = RHS; |
| 7230 | while (!declaresSameEntity(RHSSuper->getInterface(), LHSInterface)) |
| 7231 | RHSSuper = RHSSuper->getSuperClassType()->castAs<ObjCObjectType>(); |
| 7232 | |
| 7233 | // If the RHS is specializd, compare type arguments. |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7234 | if (RHSSuper->isSpecialized() && |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7235 | !sameObjCTypeArgs(*this, LHS->getInterface(), |
| 7236 | LHS->getTypeArgs(), RHSSuper->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7237 | /*stripKindOf=*/true)) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7238 | return false; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7239 | } |
| 7240 | } |
| 7241 | |
| 7242 | return true; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7243 | } |
| 7244 | |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 7245 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 7246 | // get the "pointed to" types |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7247 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 7248 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7249 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7250 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 7251 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7252 | |
| 7253 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 7254 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 7255 | } |
| 7256 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7257 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 7258 | return canAssignObjCInterfaces( |
| 7259 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 7260 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 7261 | } |
| 7262 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7263 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7264 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7265 | /// 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] | 7266 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7267 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 7268 | bool CompareUnqualified) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7269 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 7270 | return hasSameType(LHS, RHS); |
| 7271 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7272 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7273 | } |
| 7274 | |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 7275 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | c87c879 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 7276 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 7277 | } |
| 7278 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7279 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 7280 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 7281 | } |
| 7282 | |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 7283 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 7284 | /// of T is compatible with SubType, return the merged type, else return |
| 7285 | /// QualType() |
| 7286 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 7287 | bool OfBlockPointer, |
| 7288 | bool Unqualified) { |
| 7289 | if (const RecordType *UT = T->getAsUnionType()) { |
| 7290 | RecordDecl *UD = UT->getDecl(); |
| 7291 | if (UD->hasAttr<TransparentUnionAttr>()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 7292 | for (const auto *I : UD->fields()) { |
| 7293 | QualType ET = I->getType().getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 7294 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 7295 | if (!MT.isNull()) |
| 7296 | return MT; |
| 7297 | } |
| 7298 | } |
| 7299 | } |
| 7300 | |
| 7301 | return QualType(); |
| 7302 | } |
| 7303 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7304 | /// mergeFunctionParameterTypes - merge two types which appear as function |
| 7305 | /// parameter types |
| 7306 | QualType ASTContext::mergeFunctionParameterTypes(QualType lhs, QualType rhs, |
| 7307 | bool OfBlockPointer, |
| 7308 | bool Unqualified) { |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 7309 | // GNU extension: two types are compatible if they appear as a function |
| 7310 | // argument, one of the types is a transparent union type and the other |
| 7311 | // type is compatible with a union member |
| 7312 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 7313 | Unqualified); |
| 7314 | if (!lmerge.isNull()) |
| 7315 | return lmerge; |
| 7316 | |
| 7317 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 7318 | Unqualified); |
| 7319 | if (!rmerge.isNull()) |
| 7320 | return rmerge; |
| 7321 | |
| 7322 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 7323 | } |
| 7324 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7325 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7326 | bool OfBlockPointer, |
| 7327 | bool Unqualified) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7328 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 7329 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7330 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 7331 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7332 | bool allLTypes = true; |
| 7333 | bool allRTypes = true; |
| 7334 | |
| 7335 | // Check return type |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7336 | QualType retType; |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 7337 | if (OfBlockPointer) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7338 | QualType RHS = rbase->getReturnType(); |
| 7339 | QualType LHS = lbase->getReturnType(); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 7340 | bool UnqualifiedResult = Unqualified; |
| 7341 | if (!UnqualifiedResult) |
| 7342 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7343 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 7344 | } |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7345 | else |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7346 | retType = mergeTypes(lbase->getReturnType(), rbase->getReturnType(), false, |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7347 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7348 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7349 | |
| 7350 | if (Unqualified) |
| 7351 | retType = retType.getUnqualifiedType(); |
| 7352 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7353 | CanQualType LRetType = getCanonicalType(lbase->getReturnType()); |
| 7354 | CanQualType RRetType = getCanonicalType(rbase->getReturnType()); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7355 | if (Unqualified) { |
| 7356 | LRetType = LRetType.getUnqualifiedType(); |
| 7357 | RRetType = RRetType.getUnqualifiedType(); |
| 7358 | } |
| 7359 | |
| 7360 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7361 | allLTypes = false; |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7362 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7363 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7364 | |
Daniel Dunbar | edd5bae | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 7365 | // FIXME: double check this |
| 7366 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 7367 | // rbase->getRegParmAttr() != 0 && |
| 7368 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 7369 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 7370 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7371 | |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 7372 | // Compatible functions must have compatible calling conventions |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 7373 | if (lbaseInfo.getCC() != rbaseInfo.getCC()) |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 7374 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7375 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7376 | // Regparm is part of the calling convention. |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 7377 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 7378 | return QualType(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7379 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 7380 | return QualType(); |
| 7381 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7382 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 7383 | return QualType(); |
| 7384 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7385 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 7386 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7387 | |
Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 7388 | if (lbaseInfo.getNoReturn() != NoReturn) |
| 7389 | allLTypes = false; |
| 7390 | if (rbaseInfo.getNoReturn() != NoReturn) |
| 7391 | allRTypes = false; |
| 7392 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7393 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7394 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7395 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 7396 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 7397 | "C++ shouldn't be here"); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7398 | // Compatible functions must have the same number of parameters |
| 7399 | if (lproto->getNumParams() != rproto->getNumParams()) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7400 | return QualType(); |
| 7401 | |
| 7402 | // Variadic and non-variadic functions aren't compatible |
| 7403 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 7404 | return QualType(); |
| 7405 | |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 7406 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 7407 | return QualType(); |
| 7408 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7409 | if (LangOpts.ObjCAutoRefCount && |
| 7410 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 7411 | return QualType(); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7412 | |
| 7413 | // Check parameter type compatibility |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7414 | SmallVector<QualType, 10> types; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7415 | for (unsigned i = 0, n = lproto->getNumParams(); i < n; i++) { |
| 7416 | QualType lParamType = lproto->getParamType(i).getUnqualifiedType(); |
| 7417 | QualType rParamType = rproto->getParamType(i).getUnqualifiedType(); |
| 7418 | QualType paramType = mergeFunctionParameterTypes( |
| 7419 | lParamType, rParamType, OfBlockPointer, Unqualified); |
| 7420 | if (paramType.isNull()) |
| 7421 | return QualType(); |
| 7422 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7423 | if (Unqualified) |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7424 | paramType = paramType.getUnqualifiedType(); |
| 7425 | |
| 7426 | types.push_back(paramType); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7427 | if (Unqualified) { |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7428 | lParamType = lParamType.getUnqualifiedType(); |
| 7429 | rParamType = rParamType.getUnqualifiedType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7430 | } |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7431 | |
| 7432 | if (getCanonicalType(paramType) != getCanonicalType(lParamType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7433 | allLTypes = false; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7434 | if (getCanonicalType(paramType) != getCanonicalType(rParamType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7435 | allRTypes = false; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7436 | } |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7437 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7438 | if (allLTypes) return lhs; |
| 7439 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7440 | |
| 7441 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 7442 | EPI.ExtInfo = einfo; |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 7443 | return getFunctionType(retType, types, EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7444 | } |
| 7445 | |
| 7446 | if (lproto) allRTypes = false; |
| 7447 | if (rproto) allLTypes = false; |
| 7448 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7449 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7450 | if (proto) { |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 7451 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7452 | if (proto->isVariadic()) return QualType(); |
| 7453 | // Check that the types are compatible with the types that |
| 7454 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 7455 | // The only types actually affected are promotable integer |
| 7456 | // types and floats, which would be passed as a different |
| 7457 | // type depending on whether the prototype is visible. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7458 | for (unsigned i = 0, n = proto->getNumParams(); i < n; ++i) { |
| 7459 | QualType paramTy = proto->getParamType(i); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7460 | |
Eli Friedman | 448ce40 | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 7461 | // Look at the converted type of enum types, since that is the type used |
Douglas Gregor | 2973d40 | 2010-02-03 19:27:29 +0000 | [diff] [blame] | 7462 | // to pass enum values. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7463 | if (const EnumType *Enum = paramTy->getAs<EnumType>()) { |
| 7464 | paramTy = Enum->getDecl()->getIntegerType(); |
| 7465 | if (paramTy.isNull()) |
Eli Friedman | 448ce40 | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 7466 | return QualType(); |
| 7467 | } |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7468 | |
| 7469 | if (paramTy->isPromotableIntegerType() || |
| 7470 | getCanonicalType(paramTy).getUnqualifiedType() == FloatTy) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7471 | return QualType(); |
| 7472 | } |
| 7473 | |
| 7474 | if (allLTypes) return lhs; |
| 7475 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7476 | |
| 7477 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 7478 | EPI.ExtInfo = einfo; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7479 | return getFunctionType(retType, proto->getParamTypes(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7480 | } |
| 7481 | |
| 7482 | if (allLTypes) return lhs; |
| 7483 | if (allRTypes) return rhs; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7484 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7485 | } |
| 7486 | |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7487 | /// Given that we have an enum type and a non-enum type, try to merge them. |
| 7488 | static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET, |
| 7489 | QualType other, bool isBlockReturnType) { |
| 7490 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 7491 | // a signed integer type, or an unsigned integer type. |
| 7492 | // Compatibility is based on the underlying type, not the promotion |
| 7493 | // type. |
| 7494 | QualType underlyingType = ET->getDecl()->getIntegerType(); |
| 7495 | if (underlyingType.isNull()) return QualType(); |
| 7496 | if (Context.hasSameType(underlyingType, other)) |
| 7497 | return other; |
| 7498 | |
| 7499 | // In block return types, we're more permissive and accept any |
| 7500 | // integral type of the same size. |
| 7501 | if (isBlockReturnType && other->isIntegerType() && |
| 7502 | Context.getTypeSize(underlyingType) == Context.getTypeSize(other)) |
| 7503 | return other; |
| 7504 | |
| 7505 | return QualType(); |
| 7506 | } |
| 7507 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7508 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7509 | bool OfBlockPointer, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7510 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | db4e349 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 7511 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 7512 | // type is adjusted to "T" prior to any further analysis, the expression |
| 7513 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 7514 | // expression is an lvalue unless the reference is an rvalue reference and |
| 7515 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 7516 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 7517 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7518 | |
| 7519 | if (Unqualified) { |
| 7520 | LHS = LHS.getUnqualifiedType(); |
| 7521 | RHS = RHS.getUnqualifiedType(); |
| 7522 | } |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 7523 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7524 | QualType LHSCan = getCanonicalType(LHS), |
| 7525 | RHSCan = getCanonicalType(RHS); |
| 7526 | |
| 7527 | // If two types are identical, they are compatible. |
| 7528 | if (LHSCan == RHSCan) |
| 7529 | return LHS; |
| 7530 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7531 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 7532 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 7533 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7534 | if (LQuals != RQuals) { |
| 7535 | // If any of these qualifiers are different, we have a type |
| 7536 | // mismatch. |
| 7537 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7538 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 7539 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7540 | return QualType(); |
| 7541 | |
| 7542 | // Exactly one GC qualifier difference is allowed: __strong is |
| 7543 | // okay if the other type has no GC qualifier but is an Objective |
| 7544 | // C object pointer (i.e. implicitly strong by default). We fix |
| 7545 | // this by pretending that the unqualified type was actually |
| 7546 | // qualified __strong. |
| 7547 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 7548 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 7549 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 7550 | |
| 7551 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 7552 | return QualType(); |
| 7553 | |
| 7554 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 7555 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 7556 | } |
| 7557 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 7558 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 7559 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7560 | return QualType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7561 | } |
| 7562 | |
| 7563 | // Okay, qualifiers are equal. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7564 | |
Eli Friedman | dcca633 | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 7565 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 7566 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7567 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7568 | // We want to consider the two function types to be the same for these |
| 7569 | // comparisons, just force one to the other. |
| 7570 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 7571 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 16f9096 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 7572 | |
| 7573 | // Same as above for arrays |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 7574 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 7575 | LHSClass = Type::ConstantArray; |
| 7576 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 7577 | RHSClass = Type::ConstantArray; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7578 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7579 | // ObjCInterfaces are just specialized ObjCObjects. |
| 7580 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 7581 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 7582 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 7583 | // Canonicalize ExtVector -> Vector. |
| 7584 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 7585 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7586 | |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 7587 | // If the canonical type classes don't match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7588 | if (LHSClass != RHSClass) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7589 | // Note that we only have special rules for turning block enum |
| 7590 | // returns into block int returns, not vice-versa. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7591 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7592 | return mergeEnumWithInteger(*this, ETy, RHS, false); |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 7593 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7594 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7595 | return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType); |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 7596 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 7597 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 194904e | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 7598 | if (OfBlockPointer && !BlockReturnType) { |
| 7599 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 7600 | return LHS; |
| 7601 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 7602 | return RHS; |
| 7603 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 7604 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7605 | return QualType(); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7606 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7607 | |
Steve Naroff | c6edcbd | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 7608 | // The canonical type classes match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7609 | switch (LHSClass) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7610 | #define TYPE(Class, Base) |
| 7611 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 7612 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7613 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 7614 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 7615 | #include "clang/AST/TypeNodes.def" |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7616 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7617 | |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 7618 | case Type::Auto: |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 7619 | case Type::LValueReference: |
| 7620 | case Type::RValueReference: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7621 | case Type::MemberPointer: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7622 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7623 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7624 | case Type::ObjCInterface: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7625 | case Type::IncompleteArray: |
| 7626 | case Type::VariableArray: |
| 7627 | case Type::FunctionProto: |
| 7628 | case Type::ExtVector: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7629 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7630 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7631 | case Type::Pointer: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7632 | { |
| 7633 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7634 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 7635 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7636 | if (Unqualified) { |
| 7637 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 7638 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 7639 | } |
| 7640 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 7641 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7642 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 7643 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7644 | return LHS; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 7645 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7646 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7647 | return getPointerType(ResultType); |
| 7648 | } |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 7649 | case Type::BlockPointer: |
| 7650 | { |
| 7651 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7652 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 7653 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7654 | if (Unqualified) { |
| 7655 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 7656 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 7657 | } |
| 7658 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 7659 | Unqualified); |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 7660 | if (ResultType.isNull()) return QualType(); |
| 7661 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 7662 | return LHS; |
| 7663 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 7664 | return RHS; |
| 7665 | return getBlockPointerType(ResultType); |
| 7666 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7667 | case Type::Atomic: |
| 7668 | { |
| 7669 | // Merge two pointer types, while trying to preserve typedef info |
| 7670 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 7671 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 7672 | if (Unqualified) { |
| 7673 | LHSValue = LHSValue.getUnqualifiedType(); |
| 7674 | RHSValue = RHSValue.getUnqualifiedType(); |
| 7675 | } |
| 7676 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 7677 | Unqualified); |
| 7678 | if (ResultType.isNull()) return QualType(); |
| 7679 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 7680 | return LHS; |
| 7681 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 7682 | return RHS; |
| 7683 | return getAtomicType(ResultType); |
| 7684 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7685 | case Type::ConstantArray: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7686 | { |
| 7687 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 7688 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 7689 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 7690 | return QualType(); |
| 7691 | |
| 7692 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 7693 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7694 | if (Unqualified) { |
| 7695 | LHSElem = LHSElem.getUnqualifiedType(); |
| 7696 | RHSElem = RHSElem.getUnqualifiedType(); |
| 7697 | } |
| 7698 | |
| 7699 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7700 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7701 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 7702 | return LHS; |
| 7703 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 7704 | return RHS; |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 7705 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 7706 | ArrayType::ArraySizeModifier(), 0); |
| 7707 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 7708 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7709 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 7710 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7711 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 7712 | return LHS; |
| 7713 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 7714 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7715 | if (LVAT) { |
| 7716 | // FIXME: This isn't correct! But tricky to implement because |
| 7717 | // the array's size has to be the size of LHS, but the type |
| 7718 | // has to be different. |
| 7719 | return LHS; |
| 7720 | } |
| 7721 | if (RVAT) { |
| 7722 | // FIXME: This isn't correct! But tricky to implement because |
| 7723 | // the array's size has to be the size of RHS, but the type |
| 7724 | // has to be different. |
| 7725 | return RHS; |
| 7726 | } |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 7727 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 7728 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 7729 | return getIncompleteArrayType(ResultType, |
| 7730 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7731 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7732 | case Type::FunctionNoProto: |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7733 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7734 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7735 | case Type::Enum: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7736 | return QualType(); |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7737 | case Type::Builtin: |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 7738 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7739 | return QualType(); |
Daniel Dunbar | 804c044 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 7740 | case Type::Complex: |
| 7741 | // Distinct complex types are incompatible. |
| 7742 | return QualType(); |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 7743 | case Type::Vector: |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 7744 | // FIXME: The merged type should be an ExtVector! |
John McCall | 44c064b | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 7745 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 7746 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7747 | return LHS; |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7748 | return QualType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7749 | case Type::ObjCObject: { |
| 7750 | // Check if the types are assignment compatible. |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 7751 | // FIXME: This should be type compatibility, e.g. whether |
| 7752 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7753 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 7754 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 7755 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 7a7814c | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 7756 | return LHS; |
| 7757 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7758 | return QualType(); |
Cedric Venet | 4fc88b7 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 7759 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7760 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7761 | if (OfBlockPointer) { |
| 7762 | if (canAssignObjCInterfacesInBlockPointer( |
| 7763 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7764 | RHS->getAs<ObjCObjectPointerType>(), |
| 7765 | BlockReturnType)) |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7766 | return LHS; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7767 | return QualType(); |
| 7768 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7769 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 7770 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7771 | return LHS; |
| 7772 | |
Steve Naroff | c68cfcf | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 7773 | return QualType(); |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7774 | } |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7775 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7776 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7777 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7778 | } |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 7779 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7780 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 7781 | const FunctionProtoType *FromFunctionType, |
| 7782 | const FunctionProtoType *ToFunctionType) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7783 | if (FromFunctionType->hasAnyConsumedParams() != |
| 7784 | ToFunctionType->hasAnyConsumedParams()) |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7785 | return false; |
| 7786 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 7787 | FromFunctionType->getExtProtoInfo(); |
| 7788 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 7789 | ToFunctionType->getExtProtoInfo(); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 7790 | if (FromEPI.ConsumedParameters && ToEPI.ConsumedParameters) |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7791 | for (unsigned i = 0, n = FromFunctionType->getNumParams(); i != n; ++i) { |
| 7792 | if (FromEPI.ConsumedParameters[i] != ToEPI.ConsumedParameters[i]) |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7793 | return false; |
| 7794 | } |
| 7795 | return true; |
| 7796 | } |
| 7797 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7798 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 7799 | /// 'RHS' attributes and returns the merged version; including for function |
| 7800 | /// return types. |
| 7801 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 7802 | QualType LHSCan = getCanonicalType(LHS), |
| 7803 | RHSCan = getCanonicalType(RHS); |
| 7804 | // If two types are identical, they are compatible. |
| 7805 | if (LHSCan == RHSCan) |
| 7806 | return LHS; |
| 7807 | if (RHSCan->isFunctionType()) { |
| 7808 | if (!LHSCan->isFunctionType()) |
| 7809 | return QualType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7810 | QualType OldReturnType = |
| 7811 | cast<FunctionType>(RHSCan.getTypePtr())->getReturnType(); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7812 | QualType NewReturnType = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7813 | cast<FunctionType>(LHSCan.getTypePtr())->getReturnType(); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7814 | QualType ResReturnType = |
| 7815 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 7816 | if (ResReturnType.isNull()) |
| 7817 | return QualType(); |
| 7818 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 7819 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 7820 | // In either case, use OldReturnType to build the new function type. |
| 7821 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 7822 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7823 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 7824 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Reid Kleckner | 896b32f | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 7825 | QualType ResultType = |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7826 | getFunctionType(OldReturnType, FPT->getParamTypes(), EPI); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7827 | return ResultType; |
| 7828 | } |
| 7829 | } |
| 7830 | return QualType(); |
| 7831 | } |
| 7832 | |
| 7833 | // If the qualifiers are different, the types can still be merged. |
| 7834 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 7835 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 7836 | if (LQuals != RQuals) { |
| 7837 | // If any of these qualifiers are different, we have a type mismatch. |
| 7838 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 7839 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 7840 | return QualType(); |
| 7841 | |
| 7842 | // Exactly one GC qualifier difference is allowed: __strong is |
| 7843 | // okay if the other type has no GC qualifier but is an Objective |
| 7844 | // C object pointer (i.e. implicitly strong by default). We fix |
| 7845 | // this by pretending that the unqualified type was actually |
| 7846 | // qualified __strong. |
| 7847 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 7848 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 7849 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 7850 | |
| 7851 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 7852 | return QualType(); |
| 7853 | |
| 7854 | if (GC_L == Qualifiers::Strong) |
| 7855 | return LHS; |
| 7856 | if (GC_R == Qualifiers::Strong) |
| 7857 | return RHS; |
| 7858 | return QualType(); |
| 7859 | } |
| 7860 | |
| 7861 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 7862 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 7863 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 7864 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 7865 | if (ResQT == LHSBaseQT) |
| 7866 | return LHS; |
| 7867 | if (ResQT == RHSBaseQT) |
| 7868 | return RHS; |
| 7869 | } |
| 7870 | return QualType(); |
| 7871 | } |
| 7872 | |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 7873 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7874 | // Integer Predicates |
| 7875 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3c91971 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 7876 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7877 | unsigned ASTContext::getIntWidth(QualType T) const { |
Richard Smith | e952106 | 2013-10-15 04:56:17 +0000 | [diff] [blame] | 7878 | if (const EnumType *ET = T->getAs<EnumType>()) |
Eli Friedman | ee275c8 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 7879 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7880 | if (T->isBooleanType()) |
| 7881 | return 1; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 7882 | // For builtin types, just use the standard type sizing method |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7883 | return (unsigned)getTypeSize(T); |
| 7884 | } |
| 7885 | |
Abramo Bagnara | 1364049 | 2012-09-09 10:21:24 +0000 | [diff] [blame] | 7886 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) const { |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7887 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7888 | |
| 7889 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 7890 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 7891 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 7892 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7893 | |
| 7894 | // For enums, we return the unsigned version of the base type. |
| 7895 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7896 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7897 | |
| 7898 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 7899 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7900 | switch (BTy->getKind()) { |
| 7901 | case BuiltinType::Char_S: |
| 7902 | case BuiltinType::SChar: |
| 7903 | return UnsignedCharTy; |
| 7904 | case BuiltinType::Short: |
| 7905 | return UnsignedShortTy; |
| 7906 | case BuiltinType::Int: |
| 7907 | return UnsignedIntTy; |
| 7908 | case BuiltinType::Long: |
| 7909 | return UnsignedLongTy; |
| 7910 | case BuiltinType::LongLong: |
| 7911 | return UnsignedLongLongTy; |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 7912 | case BuiltinType::Int128: |
| 7913 | return UnsignedInt128Ty; |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7914 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7915 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7916 | } |
| 7917 | } |
| 7918 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 7919 | ASTMutationListener::~ASTMutationListener() { } |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 7920 | |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 7921 | void ASTMutationListener::DeducedReturnType(const FunctionDecl *FD, |
| 7922 | QualType ReturnType) {} |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7923 | |
| 7924 | //===----------------------------------------------------------------------===// |
| 7925 | // Builtin Type Computation |
| 7926 | //===----------------------------------------------------------------------===// |
| 7927 | |
| 7928 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7929 | /// pointer over the consumed characters. This returns the resultant type. If |
| 7930 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 7931 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 7932 | /// a vector of "i*". |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7933 | /// |
| 7934 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 7935 | /// to be an Integer Constant Expression. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7936 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7937 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7938 | bool &RequiresICE, |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7939 | bool AllowTypeModifiers) { |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7940 | // Modifiers. |
| 7941 | int HowLong = 0; |
| 7942 | bool Signed = false, Unsigned = false; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7943 | RequiresICE = false; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7944 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7945 | // Read the prefixed modifiers first. |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7946 | bool Done = false; |
| 7947 | while (!Done) { |
| 7948 | switch (*Str++) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7949 | default: Done = true; --Str; break; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7950 | case 'I': |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7951 | RequiresICE = true; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7952 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7953 | case 'S': |
| 7954 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 7955 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 7956 | Signed = true; |
| 7957 | break; |
| 7958 | case 'U': |
| 7959 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
Sean Silva | 2a99514 | 2015-01-16 21:44:26 +0000 | [diff] [blame] | 7960 | assert(!Unsigned && "Can't use 'U' modifier multiple times!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7961 | Unsigned = true; |
| 7962 | break; |
| 7963 | case 'L': |
| 7964 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 7965 | ++HowLong; |
| 7966 | break; |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 7967 | case 'W': |
| 7968 | // This modifier represents int64 type. |
| 7969 | assert(HowLong == 0 && "Can't use both 'L' and 'W' modifiers!"); |
| 7970 | switch (Context.getTargetInfo().getInt64Type()) { |
| 7971 | default: |
| 7972 | llvm_unreachable("Unexpected integer type"); |
| 7973 | case TargetInfo::SignedLong: |
| 7974 | HowLong = 1; |
| 7975 | break; |
| 7976 | case TargetInfo::SignedLongLong: |
| 7977 | HowLong = 2; |
| 7978 | break; |
| 7979 | } |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7980 | } |
| 7981 | } |
| 7982 | |
| 7983 | QualType Type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7984 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7985 | // Read the base type. |
| 7986 | switch (*Str++) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7987 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7988 | case 'v': |
| 7989 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 7990 | "Bad modifiers used with 'v'!"); |
| 7991 | Type = Context.VoidTy; |
| 7992 | break; |
Jack Carter | 24bef98 | 2013-08-15 15:16:57 +0000 | [diff] [blame] | 7993 | case 'h': |
| 7994 | assert(HowLong == 0 && !Signed && !Unsigned && |
Sean Silva | 2a99514 | 2015-01-16 21:44:26 +0000 | [diff] [blame] | 7995 | "Bad modifiers used with 'h'!"); |
Jack Carter | 24bef98 | 2013-08-15 15:16:57 +0000 | [diff] [blame] | 7996 | Type = Context.HalfTy; |
| 7997 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7998 | case 'f': |
| 7999 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 8000 | "Bad modifiers used with 'f'!"); |
| 8001 | Type = Context.FloatTy; |
| 8002 | break; |
| 8003 | case 'd': |
| 8004 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 8005 | "Bad modifiers used with 'd'!"); |
| 8006 | if (HowLong) |
| 8007 | Type = Context.LongDoubleTy; |
| 8008 | else |
| 8009 | Type = Context.DoubleTy; |
| 8010 | break; |
| 8011 | case 's': |
| 8012 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 8013 | if (Unsigned) |
| 8014 | Type = Context.UnsignedShortTy; |
| 8015 | else |
| 8016 | Type = Context.ShortTy; |
| 8017 | break; |
| 8018 | case 'i': |
| 8019 | if (HowLong == 3) |
| 8020 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 8021 | else if (HowLong == 2) |
| 8022 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 8023 | else if (HowLong == 1) |
| 8024 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 8025 | else |
| 8026 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 8027 | break; |
| 8028 | case 'c': |
| 8029 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 8030 | if (Signed) |
| 8031 | Type = Context.SignedCharTy; |
| 8032 | else if (Unsigned) |
| 8033 | Type = Context.UnsignedCharTy; |
| 8034 | else |
| 8035 | Type = Context.CharTy; |
| 8036 | break; |
| 8037 | case 'b': // boolean |
| 8038 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 8039 | Type = Context.BoolTy; |
| 8040 | break; |
| 8041 | case 'z': // size_t. |
| 8042 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 8043 | Type = Context.getSizeType(); |
| 8044 | break; |
| 8045 | case 'F': |
| 8046 | Type = Context.getCFConstantStringType(); |
| 8047 | break; |
Fariborz Jahanian | d11da7e | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 8048 | case 'G': |
| 8049 | Type = Context.getObjCIdType(); |
| 8050 | break; |
| 8051 | case 'H': |
| 8052 | Type = Context.getObjCSelType(); |
| 8053 | break; |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 8054 | case 'M': |
| 8055 | Type = Context.getObjCSuperType(); |
| 8056 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8057 | case 'a': |
| 8058 | Type = Context.getBuiltinVaListType(); |
| 8059 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 8060 | break; |
| 8061 | case 'A': |
| 8062 | // This is a "reference" to a va_list; however, what exactly |
| 8063 | // this means depends on how va_list is defined. There are two |
| 8064 | // different kinds of va_list: ones passed by value, and ones |
| 8065 | // passed by reference. An example of a by-value va_list is |
| 8066 | // x86, where va_list is a char*. An example of by-ref va_list |
| 8067 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 8068 | // we want this argument to be a char*&; for x86-64, we want |
| 8069 | // it to be a __va_list_tag*. |
| 8070 | Type = Context.getBuiltinVaListType(); |
| 8071 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8072 | if (Type->isArrayType()) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8073 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8074 | else |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8075 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8076 | break; |
| 8077 | case 'V': { |
| 8078 | char *End; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8079 | unsigned NumElements = strtoul(Str, &End, 10); |
| 8080 | assert(End != Str && "Missing vector size"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8081 | Str = End; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8082 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8083 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 8084 | RequiresICE, false); |
| 8085 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 8086 | |
| 8087 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 8088 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 8089 | VectorType::GenericVector); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8090 | break; |
| 8091 | } |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 8092 | case 'E': { |
| 8093 | char *End; |
| 8094 | |
| 8095 | unsigned NumElements = strtoul(Str, &End, 10); |
| 8096 | assert(End != Str && "Missing vector size"); |
| 8097 | |
| 8098 | Str = End; |
| 8099 | |
| 8100 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 8101 | false); |
| 8102 | Type = Context.getExtVectorType(ElementType, NumElements); |
| 8103 | break; |
| 8104 | } |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 8105 | case 'X': { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8106 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 8107 | false); |
| 8108 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 8109 | Type = Context.getComplexType(ElementType); |
| 8110 | break; |
Fariborz Jahanian | 73952fc | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 8111 | } |
| 8112 | case 'Y' : { |
| 8113 | Type = Context.getPointerDiffType(); |
| 8114 | break; |
| 8115 | } |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 8116 | case 'P': |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 8117 | Type = Context.getFILEType(); |
| 8118 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 8119 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8120 | return QualType(); |
| 8121 | } |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 8122 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 8123 | case 'J': |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 8124 | if (Signed) |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 8125 | Type = Context.getsigjmp_bufType(); |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 8126 | else |
| 8127 | Type = Context.getjmp_bufType(); |
| 8128 | |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 8129 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 8130 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 8131 | return QualType(); |
| 8132 | } |
| 8133 | break; |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 8134 | case 'K': |
| 8135 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 8136 | Type = Context.getucontext_tType(); |
| 8137 | |
| 8138 | if (Type.isNull()) { |
| 8139 | Error = ASTContext::GE_Missing_ucontext; |
| 8140 | return QualType(); |
| 8141 | } |
| 8142 | break; |
Eli Friedman | 4e91899e | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 8143 | case 'p': |
| 8144 | Type = Context.getProcessIDType(); |
| 8145 | break; |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 8146 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8147 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 8148 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 8149 | Done = !AllowTypeModifiers; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8150 | while (!Done) { |
John McCall | b8b9466 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 8151 | switch (char c = *Str++) { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 8152 | default: Done = true; --Str; break; |
| 8153 | case '*': |
| 8154 | case '&': { |
| 8155 | // Both pointers and references can have their pointee types |
| 8156 | // qualified with an address space. |
| 8157 | char *End; |
| 8158 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 8159 | if (End != Str && AddrSpace != 0) { |
| 8160 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 8161 | Str = End; |
| 8162 | } |
| 8163 | if (c == '*') |
| 8164 | Type = Context.getPointerType(Type); |
| 8165 | else |
| 8166 | Type = Context.getLValueReferenceType(Type); |
| 8167 | break; |
| 8168 | } |
| 8169 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 8170 | case 'C': |
| 8171 | Type = Type.withConst(); |
| 8172 | break; |
| 8173 | case 'D': |
| 8174 | Type = Context.getVolatileType(Type); |
| 8175 | break; |
Ted Kremenek | f2a2f5f | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 8176 | case 'R': |
| 8177 | Type = Type.withRestrict(); |
| 8178 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8179 | } |
| 8180 | } |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 8181 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8182 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 8183 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8184 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8185 | return Type; |
| 8186 | } |
| 8187 | |
| 8188 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 8189 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8190 | GetBuiltinTypeError &Error, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8191 | unsigned *IntegerConstantArgs) const { |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 8192 | const char *TypeStr = BuiltinInfo.getTypeString(Id); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8193 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8194 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8195 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8196 | bool RequiresICE = false; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8197 | Error = GE_None; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8198 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 8199 | RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8200 | if (Error != GE_None) |
| 8201 | return QualType(); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8202 | |
| 8203 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 8204 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8205 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8206 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8207 | if (Error != GE_None) |
| 8208 | return QualType(); |
| 8209 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8210 | // If this argument is required to be an IntegerConstantExpression and the |
| 8211 | // caller cares, fill in the bitmask we return. |
| 8212 | if (RequiresICE && IntegerConstantArgs) |
| 8213 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 8214 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8215 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 8216 | if (Ty->isArrayType()) |
| 8217 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8218 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8219 | ArgTypes.push_back(Ty); |
| 8220 | } |
| 8221 | |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 8222 | if (Id == Builtin::BI__GetExceptionInfo) |
| 8223 | return QualType(); |
| 8224 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8225 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 8226 | "'.' should only occur at end of builtin type list!"); |
| 8227 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 8228 | FunctionType::ExtInfo EI(CC_C); |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 8229 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 8230 | |
| 8231 | bool Variadic = (TypeStr[0] == '.'); |
| 8232 | |
| 8233 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 8234 | if (ArgTypes.empty() && Variadic) |
| 8235 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 8236 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8237 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 8238 | EPI.ExtInfo = EI; |
| 8239 | EPI.Variadic = Variadic; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8240 | |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 8241 | return getFunctionType(ResType, ArgTypes, EPI); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8242 | } |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 8243 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8244 | static GVALinkage basicGVALinkageForFunction(const ASTContext &Context, |
| 8245 | const FunctionDecl *FD) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8246 | if (!FD->isExternallyVisible()) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8247 | return GVA_Internal; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8248 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8249 | GVALinkage External = GVA_StrongExternal; |
| 8250 | switch (FD->getTemplateSpecializationKind()) { |
| 8251 | case TSK_Undeclared: |
| 8252 | case TSK_ExplicitSpecialization: |
| 8253 | External = GVA_StrongExternal; |
| 8254 | break; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8255 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8256 | case TSK_ExplicitInstantiationDefinition: |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 8257 | return GVA_StrongODR; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8258 | |
David Majnemer | c3d0733 | 2014-05-15 06:25:57 +0000 | [diff] [blame] | 8259 | // C++11 [temp.explicit]p10: |
| 8260 | // [ Note: The intent is that an inline function that is the subject of |
| 8261 | // an explicit instantiation declaration will still be implicitly |
| 8262 | // instantiated when used so that the body can be considered for |
| 8263 | // inlining, but that no out-of-line copy of the inline function would be |
| 8264 | // generated in the translation unit. -- end note ] |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8265 | case TSK_ExplicitInstantiationDeclaration: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8266 | return GVA_AvailableExternally; |
| 8267 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8268 | case TSK_ImplicitInstantiation: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8269 | External = GVA_DiscardableODR; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8270 | break; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8271 | } |
| 8272 | |
| 8273 | if (!FD->isInlined()) |
| 8274 | return External; |
David Majnemer | 62f0ffd | 2013-08-01 17:26:42 +0000 | [diff] [blame] | 8275 | |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 8276 | if ((!Context.getLangOpts().CPlusPlus && |
| 8277 | !Context.getTargetInfo().getCXXABI().isMicrosoft() && |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8278 | !FD->hasAttr<DLLExportAttr>()) || |
David Majnemer | 62f0ffd | 2013-08-01 17:26:42 +0000 | [diff] [blame] | 8279 | FD->hasAttr<GNUInlineAttr>()) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8280 | // FIXME: This doesn't match gcc's behavior for dllexport inline functions. |
| 8281 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8282 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 8283 | // externally visible. |
| 8284 | if (FD->isInlineDefinitionExternallyVisible()) |
| 8285 | return External; |
| 8286 | |
| 8287 | // C99 inline semantics, where the symbol is not externally visible. |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8288 | return GVA_AvailableExternally; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8289 | } |
| 8290 | |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 8291 | // Functions specified with extern and inline in -fms-compatibility mode |
| 8292 | // forcibly get emitted. While the body of the function cannot be later |
| 8293 | // replaced, the function definition cannot be discarded. |
David Majnemer | 7376870 | 2015-03-20 00:02:27 +0000 | [diff] [blame] | 8294 | if (FD->isMSExternInline()) |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 8295 | return GVA_StrongODR; |
| 8296 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8297 | return GVA_DiscardableODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8298 | } |
| 8299 | |
Artem Belevich | 7b41f70 | 2015-09-23 17:44:53 +0000 | [diff] [blame] | 8300 | static GVALinkage adjustGVALinkageForAttributes(GVALinkage L, const Decl *D) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8301 | // See http://msdn.microsoft.com/en-us/library/xa0d9ste.aspx |
| 8302 | // dllexport/dllimport on inline functions. |
| 8303 | if (D->hasAttr<DLLImportAttr>()) { |
| 8304 | if (L == GVA_DiscardableODR || L == GVA_StrongODR) |
| 8305 | return GVA_AvailableExternally; |
Artem Belevich | 7b41f70 | 2015-09-23 17:44:53 +0000 | [diff] [blame] | 8306 | } else if (D->hasAttr<DLLExportAttr>() || D->hasAttr<CUDAGlobalAttr>()) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8307 | if (L == GVA_DiscardableODR) |
| 8308 | return GVA_StrongODR; |
| 8309 | } |
| 8310 | return L; |
| 8311 | } |
| 8312 | |
| 8313 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const { |
Artem Belevich | 7b41f70 | 2015-09-23 17:44:53 +0000 | [diff] [blame] | 8314 | return adjustGVALinkageForAttributes(basicGVALinkageForFunction(*this, FD), |
| 8315 | FD); |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8316 | } |
| 8317 | |
| 8318 | static GVALinkage basicGVALinkageForVariable(const ASTContext &Context, |
| 8319 | const VarDecl *VD) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8320 | if (!VD->isExternallyVisible()) |
| 8321 | return GVA_Internal; |
| 8322 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8323 | if (VD->isStaticLocal()) { |
| 8324 | GVALinkage StaticLocalLinkage = GVA_DiscardableODR; |
| 8325 | const DeclContext *LexicalContext = VD->getParentFunctionOrMethod(); |
| 8326 | while (LexicalContext && !isa<FunctionDecl>(LexicalContext)) |
| 8327 | LexicalContext = LexicalContext->getLexicalParent(); |
| 8328 | |
Richard Smith | 9e2341d | 2015-03-23 03:25:59 +0000 | [diff] [blame] | 8329 | // Let the static local variable inherit its linkage from the nearest |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8330 | // enclosing function. |
| 8331 | if (LexicalContext) |
| 8332 | StaticLocalLinkage = |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8333 | Context.GetGVALinkageForFunction(cast<FunctionDecl>(LexicalContext)); |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8334 | |
| 8335 | // GVA_StrongODR function linkage is stronger than what we need, |
| 8336 | // downgrade to GVA_DiscardableODR. |
| 8337 | // This allows us to discard the variable if we never end up needing it. |
| 8338 | return StaticLocalLinkage == GVA_StrongODR ? GVA_DiscardableODR |
| 8339 | : StaticLocalLinkage; |
| 8340 | } |
| 8341 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 8342 | // MSVC treats in-class initialized static data members as definitions. |
| 8343 | // By giving them non-strong linkage, out-of-line definitions won't |
| 8344 | // cause link errors. |
| 8345 | if (Context.isMSStaticDataMemberInlineDefinition(VD)) |
| 8346 | return GVA_DiscardableODR; |
| 8347 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 8348 | switch (VD->getTemplateSpecializationKind()) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8349 | case TSK_Undeclared: |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8350 | return GVA_StrongExternal; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8351 | |
David Majnemer | 6d1780c | 2015-07-17 23:36:49 +0000 | [diff] [blame] | 8352 | case TSK_ExplicitSpecialization: |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 8353 | return Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 8354 | VD->isStaticDataMember() |
David Majnemer | 6d1780c | 2015-07-17 23:36:49 +0000 | [diff] [blame] | 8355 | ? GVA_StrongODR |
| 8356 | : GVA_StrongExternal; |
| 8357 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8358 | case TSK_ExplicitInstantiationDefinition: |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 8359 | return GVA_StrongODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8360 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8361 | case TSK_ExplicitInstantiationDeclaration: |
| 8362 | return GVA_AvailableExternally; |
| 8363 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8364 | case TSK_ImplicitInstantiation: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8365 | return GVA_DiscardableODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8366 | } |
Rafael Espindola | 27699c8 | 2013-05-13 14:05:53 +0000 | [diff] [blame] | 8367 | |
| 8368 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8369 | } |
| 8370 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8371 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
Artem Belevich | 7b41f70 | 2015-09-23 17:44:53 +0000 | [diff] [blame] | 8372 | return adjustGVALinkageForAttributes(basicGVALinkageForVariable(*this, VD), |
| 8373 | VD); |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8374 | } |
| 8375 | |
Argyrios Kyrtzidis | c904933 | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 8376 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8377 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8378 | if (!VD->isFileVarDecl()) |
| 8379 | return false; |
Renato Golin | 9258aa5 | 2014-05-21 10:40:27 +0000 | [diff] [blame] | 8380 | // Global named register variables (GNU extension) are never emitted. |
| 8381 | if (VD->getStorageClass() == SC_Register) |
| 8382 | return false; |
Richard Smith | 7747ce2 | 2015-08-19 20:49:38 +0000 | [diff] [blame] | 8383 | if (VD->getDescribedVarTemplate() || |
| 8384 | isa<VarTemplatePartialSpecializationDecl>(VD)) |
| 8385 | return false; |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 8386 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 8387 | // We never need to emit an uninstantiated function template. |
| 8388 | if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) |
| 8389 | return false; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 8390 | } else if (isa<OMPThreadPrivateDecl>(D)) |
| 8391 | return true; |
| 8392 | else |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 8393 | return false; |
| 8394 | |
| 8395 | // If this is a member of a class template, we do not need to emit it. |
| 8396 | if (D->getDeclContext()->isDependentContext()) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8397 | return false; |
| 8398 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 8399 | // Weak references don't produce any output by themselves. |
| 8400 | if (D->hasAttr<WeakRefAttr>()) |
| 8401 | return false; |
| 8402 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8403 | // Aliases and used decls are required. |
| 8404 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 8405 | return true; |
| 8406 | |
| 8407 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 8408 | // Forward declarations aren't required. |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 8409 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 8410 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8411 | |
| 8412 | // Constructors and destructors are required. |
| 8413 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 8414 | return true; |
| 8415 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 8416 | // The key function for a class is required. This rule only comes |
| 8417 | // into play when inline functions can be key functions, though. |
| 8418 | if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
| 8419 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 8420 | const CXXRecordDecl *RD = MD->getParent(); |
| 8421 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 8422 | const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD); |
| 8423 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 8424 | return true; |
| 8425 | } |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8426 | } |
| 8427 | } |
| 8428 | |
| 8429 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 8430 | |
| 8431 | // static, static inline, always_inline, and extern inline functions can |
| 8432 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 8433 | // Implicit template instantiations can also be deferred in C++. |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8434 | if (Linkage == GVA_Internal || Linkage == GVA_AvailableExternally || |
| 8435 | Linkage == GVA_DiscardableODR) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8436 | return false; |
| 8437 | return true; |
| 8438 | } |
Douglas Gregor | 87d8124 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 8439 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8440 | const VarDecl *VD = cast<VarDecl>(D); |
| 8441 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 8442 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 8443 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly && |
| 8444 | !isMSStaticDataMemberInlineDefinition(VD)) |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 8445 | return false; |
| 8446 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 8447 | // Variables that can be needed in other TUs are required. |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8448 | GVALinkage L = GetGVALinkageForVariable(VD); |
David Majnemer | c3d0733 | 2014-05-15 06:25:57 +0000 | [diff] [blame] | 8449 | if (L != GVA_Internal && L != GVA_AvailableExternally && |
| 8450 | L != GVA_DiscardableODR) |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 8451 | return true; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8452 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 8453 | // Variables that have destruction with side-effects are required. |
| 8454 | if (VD->getType().isDestructedType()) |
| 8455 | return true; |
| 8456 | |
| 8457 | // Variables that have initialization with side-effects are required. |
Richard Smith | 7747ce2 | 2015-08-19 20:49:38 +0000 | [diff] [blame] | 8458 | if (VD->getInit() && VD->getInit()->HasSideEffects(*this) && |
| 8459 | !VD->evaluateValue()) |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 8460 | return true; |
| 8461 | |
| 8462 | return false; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8463 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 8464 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 8465 | CallingConv ASTContext::getDefaultCallingConvention(bool IsVariadic, |
| 8466 | bool IsCXXMethod) const { |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 8467 | // Pass through to the C++ ABI object |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 8468 | if (IsCXXMethod) |
| 8469 | return ABI->getDefaultMethodCallConv(IsVariadic); |
Timur Iskhodzhanov | c5098ad | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 8470 | |
Alexander Kornienko | 21de0ae | 2015-01-20 11:20:41 +0000 | [diff] [blame] | 8471 | if (LangOpts.MRTD && !IsVariadic) return CC_X86StdCall; |
| 8472 | |
| 8473 | return Target->getDefaultCallingConv(TargetInfo::CCMT_Unknown); |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 8474 | } |
| 8475 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8476 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 8477 | // Pass through to the C++ ABI object |
| 8478 | return ABI->isNearlyEmpty(RD); |
| 8479 | } |
| 8480 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 8481 | VTableContextBase *ASTContext::getVTableContext() { |
| 8482 | if (!VTContext.get()) { |
| 8483 | if (Target->getCXXABI().isMicrosoft()) |
| 8484 | VTContext.reset(new MicrosoftVTableContext(*this)); |
| 8485 | else |
| 8486 | VTContext.reset(new ItaniumVTableContext(*this)); |
| 8487 | } |
| 8488 | return VTContext.get(); |
| 8489 | } |
| 8490 | |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 8491 | MangleContext *ASTContext::createMangleContext() { |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 8492 | switch (Target->getCXXABI().getKind()) { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 8493 | case TargetCXXABI::GenericAArch64: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 8494 | case TargetCXXABI::GenericItanium: |
| 8495 | case TargetCXXABI::GenericARM: |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 8496 | case TargetCXXABI::GenericMIPS: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 8497 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8498 | case TargetCXXABI::iOS64: |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 8499 | case TargetCXXABI::WebAssembly: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 8500 | case TargetCXXABI::WatchOS: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 8501 | return ItaniumMangleContext::create(*this, getDiagnostics()); |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 8502 | case TargetCXXABI::Microsoft: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 8503 | return MicrosoftMangleContext::create(*this, getDiagnostics()); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 8504 | } |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8505 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 8506 | } |
| 8507 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 8508 | CXXABI::~CXXABI() {} |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 8509 | |
| 8510 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8511 | return ASTRecordLayouts.getMemorySize() + |
| 8512 | llvm::capacity_in_bytes(ObjCLayouts) + |
| 8513 | llvm::capacity_in_bytes(KeyFunctions) + |
| 8514 | llvm::capacity_in_bytes(ObjCImpls) + |
| 8515 | llvm::capacity_in_bytes(BlockVarCopyInits) + |
| 8516 | llvm::capacity_in_bytes(DeclAttrs) + |
| 8517 | llvm::capacity_in_bytes(TemplateOrInstantiation) + |
| 8518 | llvm::capacity_in_bytes(InstantiatedFromUsingDecl) + |
| 8519 | llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) + |
| 8520 | llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) + |
| 8521 | llvm::capacity_in_bytes(OverriddenMethods) + |
| 8522 | llvm::capacity_in_bytes(Types) + |
| 8523 | llvm::capacity_in_bytes(VariableArrayTypes) + |
| 8524 | llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 8525 | } |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 8526 | |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 8527 | /// getIntTypeForBitwidth - |
| 8528 | /// sets integer QualTy according to specified details: |
| 8529 | /// bitwidth, signed/unsigned. |
| 8530 | /// Returns empty type if there is no appropriate target types. |
| 8531 | QualType ASTContext::getIntTypeForBitwidth(unsigned DestWidth, |
| 8532 | unsigned Signed) const { |
| 8533 | TargetInfo::IntType Ty = getTargetInfo().getIntTypeByWidth(DestWidth, Signed); |
| 8534 | CanQualType QualTy = getFromTargetType(Ty); |
| 8535 | if (!QualTy && DestWidth == 128) |
| 8536 | return Signed ? Int128Ty : UnsignedInt128Ty; |
| 8537 | return QualTy; |
| 8538 | } |
| 8539 | |
| 8540 | /// getRealTypeForBitwidth - |
| 8541 | /// sets floating point QualTy according to specified bitwidth. |
| 8542 | /// Returns empty type if there is no appropriate target types. |
| 8543 | QualType ASTContext::getRealTypeForBitwidth(unsigned DestWidth) const { |
| 8544 | TargetInfo::RealType Ty = getTargetInfo().getRealTypeByWidth(DestWidth); |
| 8545 | switch (Ty) { |
| 8546 | case TargetInfo::Float: |
| 8547 | return FloatTy; |
| 8548 | case TargetInfo::Double: |
| 8549 | return DoubleTy; |
| 8550 | case TargetInfo::LongDouble: |
| 8551 | return LongDoubleTy; |
| 8552 | case TargetInfo::NoFloat: |
| 8553 | return QualType(); |
| 8554 | } |
| 8555 | |
| 8556 | llvm_unreachable("Unhandled TargetInfo::RealType value"); |
| 8557 | } |
| 8558 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 8559 | void ASTContext::setManglingNumber(const NamedDecl *ND, unsigned Number) { |
| 8560 | if (Number > 1) |
| 8561 | MangleNumbers[ND] = Number; |
David Blaikie | 095deba | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 8562 | } |
| 8563 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 8564 | unsigned ASTContext::getManglingNumber(const NamedDecl *ND) const { |
| 8565 | llvm::DenseMap<const NamedDecl *, unsigned>::const_iterator I = |
| 8566 | MangleNumbers.find(ND); |
| 8567 | return I != MangleNumbers.end() ? I->second : 1; |
David Blaikie | 095deba | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 8568 | } |
| 8569 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 8570 | void ASTContext::setStaticLocalNumber(const VarDecl *VD, unsigned Number) { |
| 8571 | if (Number > 1) |
| 8572 | StaticLocalNumbers[VD] = Number; |
| 8573 | } |
| 8574 | |
| 8575 | unsigned ASTContext::getStaticLocalNumber(const VarDecl *VD) const { |
| 8576 | llvm::DenseMap<const VarDecl *, unsigned>::const_iterator I = |
| 8577 | StaticLocalNumbers.find(VD); |
| 8578 | return I != StaticLocalNumbers.end() ? I->second : 1; |
| 8579 | } |
| 8580 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 8581 | MangleNumberingContext & |
| 8582 | ASTContext::getManglingNumberContext(const DeclContext *DC) { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 8583 | assert(LangOpts.CPlusPlus); // We don't need mangling numbers for plain C. |
| 8584 | MangleNumberingContext *&MCtx = MangleNumberingContexts[DC]; |
| 8585 | if (!MCtx) |
| 8586 | MCtx = createMangleNumberingContext(); |
| 8587 | return *MCtx; |
| 8588 | } |
| 8589 | |
| 8590 | MangleNumberingContext *ASTContext::createMangleNumberingContext() const { |
| 8591 | return ABI->createMangleNumberingContext(); |
Douglas Gregor | 6379854 | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 8592 | } |
| 8593 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 8594 | const CXXConstructorDecl * |
| 8595 | ASTContext::getCopyConstructorForExceptionObject(CXXRecordDecl *RD) { |
| 8596 | return ABI->getCopyConstructorForExceptionObject( |
| 8597 | cast<CXXRecordDecl>(RD->getFirstDecl())); |
| 8598 | } |
| 8599 | |
| 8600 | void ASTContext::addCopyConstructorForExceptionObject(CXXRecordDecl *RD, |
| 8601 | CXXConstructorDecl *CD) { |
| 8602 | return ABI->addCopyConstructorForExceptionObject( |
| 8603 | cast<CXXRecordDecl>(RD->getFirstDecl()), |
| 8604 | cast<CXXConstructorDecl>(CD->getFirstDecl())); |
| 8605 | } |
| 8606 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 8607 | void ASTContext::addDefaultArgExprForConstructor(const CXXConstructorDecl *CD, |
| 8608 | unsigned ParmIdx, Expr *DAE) { |
| 8609 | ABI->addDefaultArgExprForConstructor( |
| 8610 | cast<CXXConstructorDecl>(CD->getFirstDecl()), ParmIdx, DAE); |
| 8611 | } |
| 8612 | |
| 8613 | Expr *ASTContext::getDefaultArgExprForConstructor(const CXXConstructorDecl *CD, |
| 8614 | unsigned ParmIdx) { |
| 8615 | return ABI->getDefaultArgExprForConstructor( |
| 8616 | cast<CXXConstructorDecl>(CD->getFirstDecl()), ParmIdx); |
| 8617 | } |
| 8618 | |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 8619 | void ASTContext::addTypedefNameForUnnamedTagDecl(TagDecl *TD, |
| 8620 | TypedefNameDecl *DD) { |
| 8621 | return ABI->addTypedefNameForUnnamedTagDecl(TD, DD); |
| 8622 | } |
| 8623 | |
| 8624 | TypedefNameDecl * |
| 8625 | ASTContext::getTypedefNameForUnnamedTagDecl(const TagDecl *TD) { |
| 8626 | return ABI->getTypedefNameForUnnamedTagDecl(TD); |
| 8627 | } |
| 8628 | |
| 8629 | void ASTContext::addDeclaratorForUnnamedTagDecl(TagDecl *TD, |
| 8630 | DeclaratorDecl *DD) { |
| 8631 | return ABI->addDeclaratorForUnnamedTagDecl(TD, DD); |
| 8632 | } |
| 8633 | |
| 8634 | DeclaratorDecl *ASTContext::getDeclaratorForUnnamedTagDecl(const TagDecl *TD) { |
| 8635 | return ABI->getDeclaratorForUnnamedTagDecl(TD); |
| 8636 | } |
| 8637 | |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 8638 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 8639 | ParamIndices[D] = index; |
| 8640 | } |
| 8641 | |
| 8642 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 8643 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 8644 | assert(I != ParamIndices.end() && |
| 8645 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 8646 | return I->second; |
| 8647 | } |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 8648 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 8649 | APValue * |
| 8650 | ASTContext::getMaterializedTemporaryValue(const MaterializeTemporaryExpr *E, |
| 8651 | bool MayCreate) { |
| 8652 | assert(E && E->getStorageDuration() == SD_Static && |
| 8653 | "don't need to cache the computed value for this temporary"); |
David Majnemer | 2dcef9e | 2015-08-13 23:50:15 +0000 | [diff] [blame] | 8654 | if (MayCreate) { |
| 8655 | APValue *&MTVI = MaterializedTemporaryValues[E]; |
| 8656 | if (!MTVI) |
| 8657 | MTVI = new (*this) APValue; |
| 8658 | return MTVI; |
| 8659 | } |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 8660 | |
David Majnemer | 2dcef9e | 2015-08-13 23:50:15 +0000 | [diff] [blame] | 8661 | return MaterializedTemporaryValues.lookup(E); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 8662 | } |
| 8663 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 8664 | bool ASTContext::AtomicUsesUnsupportedLibcall(const AtomicExpr *E) const { |
| 8665 | const llvm::Triple &T = getTargetInfo().getTriple(); |
| 8666 | if (!T.isOSDarwin()) |
| 8667 | return false; |
| 8668 | |
Bob Wilson | 2c82c3d | 2013-11-02 23:27:49 +0000 | [diff] [blame] | 8669 | if (!(T.isiOS() && T.isOSVersionLT(7)) && |
| 8670 | !(T.isMacOSX() && T.isOSVersionLT(10, 9))) |
| 8671 | return false; |
| 8672 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 8673 | QualType AtomicTy = E->getPtr()->getType()->getPointeeType(); |
| 8674 | CharUnits sizeChars = getTypeSizeInChars(AtomicTy); |
| 8675 | uint64_t Size = sizeChars.getQuantity(); |
| 8676 | CharUnits alignChars = getTypeAlignInChars(AtomicTy); |
| 8677 | unsigned Align = alignChars.getQuantity(); |
| 8678 | unsigned MaxInlineWidthInBits = getTargetInfo().getMaxAtomicInlineWidth(); |
| 8679 | return (Size != Align || toBits(sizeChars) > MaxInlineWidthInBits); |
| 8680 | } |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8681 | |
| 8682 | namespace { |
| 8683 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 8684 | ast_type_traits::DynTypedNode getSingleDynTypedNodeFromParentMap( |
| 8685 | ASTContext::ParentMapPointers::mapped_type U) { |
Benjamin Kramer | fbfa7a1 | 2015-10-22 11:26:35 +0000 | [diff] [blame] | 8686 | if (const auto *D = U.dyn_cast<const Decl *>()) |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 8687 | return ast_type_traits::DynTypedNode::create(*D); |
Benjamin Kramer | fbfa7a1 | 2015-10-22 11:26:35 +0000 | [diff] [blame] | 8688 | if (const auto *S = U.dyn_cast<const Stmt *>()) |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 8689 | return ast_type_traits::DynTypedNode::create(*S); |
Benjamin Kramer | fbfa7a1 | 2015-10-22 11:26:35 +0000 | [diff] [blame] | 8690 | return *U.get<ast_type_traits::DynTypedNode *>(); |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 8691 | } |
| 8692 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 8693 | /// Template specializations to abstract away from pointers and TypeLocs. |
| 8694 | /// @{ |
| 8695 | template <typename T> |
| 8696 | ast_type_traits::DynTypedNode createDynTypedNode(const T &Node) { |
| 8697 | return ast_type_traits::DynTypedNode::create(*Node); |
| 8698 | } |
| 8699 | template <> |
| 8700 | ast_type_traits::DynTypedNode createDynTypedNode(const TypeLoc &Node) { |
| 8701 | return ast_type_traits::DynTypedNode::create(Node); |
| 8702 | } |
| 8703 | template <> |
| 8704 | ast_type_traits::DynTypedNode |
| 8705 | createDynTypedNode(const NestedNameSpecifierLoc &Node) { |
| 8706 | return ast_type_traits::DynTypedNode::create(Node); |
| 8707 | } |
| 8708 | /// @} |
| 8709 | |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8710 | /// \brief A \c RecursiveASTVisitor that builds a map from nodes to their |
| 8711 | /// parents as defined by the \c RecursiveASTVisitor. |
| 8712 | /// |
| 8713 | /// Note that the relationship described here is purely in terms of AST |
| 8714 | /// traversal - there are other relationships (for example declaration context) |
| 8715 | /// in the AST that are better modeled by special matchers. |
| 8716 | /// |
Benjamin Kramer | e8c51fd | 2015-10-21 10:07:26 +0000 | [diff] [blame] | 8717 | /// FIXME: Currently only builds up the map using \c Stmt and \c Decl nodes. |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8718 | class ParentMapASTVisitor : public RecursiveASTVisitor<ParentMapASTVisitor> { |
| 8719 | |
| 8720 | public: |
| 8721 | /// \brief Builds and returns the translation unit's parent map. |
| 8722 | /// |
| 8723 | /// The caller takes ownership of the returned \c ParentMap. |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 8724 | static std::pair<ASTContext::ParentMapPointers *, |
| 8725 | ASTContext::ParentMapOtherNodes *> |
| 8726 | buildMap(TranslationUnitDecl &TU) { |
| 8727 | ParentMapASTVisitor Visitor(new ASTContext::ParentMapPointers, |
| 8728 | new ASTContext::ParentMapOtherNodes); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8729 | Visitor.TraverseDecl(&TU); |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 8730 | return std::make_pair(Visitor.Parents, Visitor.OtherParents); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8731 | } |
| 8732 | |
| 8733 | private: |
| 8734 | typedef RecursiveASTVisitor<ParentMapASTVisitor> VisitorBase; |
| 8735 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 8736 | ParentMapASTVisitor(ASTContext::ParentMapPointers *Parents, |
| 8737 | ASTContext::ParentMapOtherNodes *OtherParents) |
| 8738 | : Parents(Parents), OtherParents(OtherParents) {} |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8739 | |
| 8740 | bool shouldVisitTemplateInstantiations() const { |
| 8741 | return true; |
| 8742 | } |
| 8743 | bool shouldVisitImplicitCode() const { |
| 8744 | return true; |
| 8745 | } |
| 8746 | // Disables data recursion. We intercept Traverse* methods in the RAV, which |
| 8747 | // are not triggered during data recursion. |
| 8748 | bool shouldUseDataRecursionFor(clang::Stmt *S) const { |
| 8749 | return false; |
| 8750 | } |
| 8751 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 8752 | template <typename T, typename MapNodeTy, typename MapTy> |
| 8753 | bool TraverseNode(T Node, MapNodeTy MapNode, |
| 8754 | bool (VisitorBase::*traverse)(T), MapTy *Parents) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 8755 | if (!Node) |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8756 | return true; |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 8757 | if (ParentStack.size() > 0) { |
Benjamin Kramer | e8c51fd | 2015-10-21 10:07:26 +0000 | [diff] [blame] | 8758 | // FIXME: Currently we add the same parent multiple times, but only |
| 8759 | // when no memoization data is available for the type. |
| 8760 | // For example when we visit all subexpressions of template |
| 8761 | // instantiations; this is suboptimal, but benign: the only way to |
| 8762 | // visit those is with hasAncestor / hasParent, and those do not create |
| 8763 | // new matches. |
| 8764 | // The plan is to enable DynTypedNode to be storable in a map or hash |
| 8765 | // map. The main problem there is to implement hash functions / |
| 8766 | // comparison operators for all types that DynTypedNode supports that |
| 8767 | // do not have pointer identity. |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 8768 | auto &NodeOrVector = (*Parents)[MapNode]; |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 8769 | if (NodeOrVector.isNull()) { |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 8770 | if (const auto *D = ParentStack.back().get<Decl>()) |
| 8771 | NodeOrVector = D; |
| 8772 | else if (const auto *S = ParentStack.back().get<Stmt>()) |
| 8773 | NodeOrVector = S; |
| 8774 | else |
| 8775 | NodeOrVector = |
| 8776 | new ast_type_traits::DynTypedNode(ParentStack.back()); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 8777 | } else { |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 8778 | if (!NodeOrVector.template is<ASTContext::ParentVector *>()) { |
| 8779 | auto *Vector = new ASTContext::ParentVector( |
| 8780 | 1, getSingleDynTypedNodeFromParentMap(NodeOrVector)); |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 8781 | if (auto *Node = |
| 8782 | NodeOrVector |
| 8783 | .template dyn_cast<ast_type_traits::DynTypedNode *>()) |
| 8784 | delete Node; |
Benjamin Kramer | 422b3ff | 2015-10-23 13:24:18 +0000 | [diff] [blame] | 8785 | NodeOrVector = Vector; |
Samuel Benzaquen | 3ca0a7b | 2014-06-13 13:31:40 +0000 | [diff] [blame] | 8786 | } |
Samuel Benzaquen | 3ca0a7b | 2014-06-13 13:31:40 +0000 | [diff] [blame] | 8787 | |
| 8788 | auto *Vector = |
| 8789 | NodeOrVector.template get<ASTContext::ParentVector *>(); |
| 8790 | // Skip duplicates for types that have memoization data. |
| 8791 | // We must check that the type has memoization data before calling |
| 8792 | // std::find() because DynTypedNode::operator== can't compare all |
| 8793 | // types. |
| 8794 | bool Found = ParentStack.back().getMemoizationData() && |
| 8795 | std::find(Vector->begin(), Vector->end(), |
| 8796 | ParentStack.back()) != Vector->end(); |
| 8797 | if (!Found) |
| 8798 | Vector->push_back(ParentStack.back()); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 8799 | } |
| 8800 | } |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 8801 | ParentStack.push_back(createDynTypedNode(Node)); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8802 | bool Result = (this ->* traverse) (Node); |
| 8803 | ParentStack.pop_back(); |
| 8804 | return Result; |
| 8805 | } |
| 8806 | |
| 8807 | bool TraverseDecl(Decl *DeclNode) { |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 8808 | return TraverseNode(DeclNode, DeclNode, &VisitorBase::TraverseDecl, |
| 8809 | Parents); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8810 | } |
| 8811 | |
| 8812 | bool TraverseStmt(Stmt *StmtNode) { |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 8813 | return TraverseNode(StmtNode, StmtNode, &VisitorBase::TraverseStmt, |
| 8814 | Parents); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8815 | } |
| 8816 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 8817 | bool TraverseTypeLoc(TypeLoc TypeLocNode) { |
| 8818 | return TraverseNode(TypeLocNode, |
| 8819 | ast_type_traits::DynTypedNode::create(TypeLocNode), |
| 8820 | &VisitorBase::TraverseTypeLoc, OtherParents); |
| 8821 | } |
| 8822 | |
| 8823 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSLocNode) { |
| 8824 | return TraverseNode( |
| 8825 | NNSLocNode, ast_type_traits::DynTypedNode::create(NNSLocNode), |
| 8826 | &VisitorBase::TraverseNestedNameSpecifierLoc, OtherParents); |
| 8827 | } |
| 8828 | |
| 8829 | ASTContext::ParentMapPointers *Parents; |
| 8830 | ASTContext::ParentMapOtherNodes *OtherParents; |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8831 | llvm::SmallVector<ast_type_traits::DynTypedNode, 16> ParentStack; |
| 8832 | |
| 8833 | friend class RecursiveASTVisitor<ParentMapASTVisitor>; |
| 8834 | }; |
| 8835 | |
| 8836 | } // end namespace |
| 8837 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 8838 | template <typename NodeTy, typename MapTy> |
| 8839 | static ASTContext::DynTypedNodeList getDynNodeFromMap(const NodeTy &Node, |
| 8840 | const MapTy &Map) { |
| 8841 | auto I = Map.find(Node); |
| 8842 | if (I == Map.end()) { |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 8843 | return llvm::ArrayRef<ast_type_traits::DynTypedNode>(); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8844 | } |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 8845 | if (auto *V = I->second.template dyn_cast<ASTContext::ParentVector *>()) { |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 8846 | return llvm::makeArrayRef(*V); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 8847 | } |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 8848 | return getSingleDynTypedNodeFromParentMap(I->second); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8849 | } |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 8850 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 8851 | ASTContext::DynTypedNodeList |
| 8852 | ASTContext::getParents(const ast_type_traits::DynTypedNode &Node) { |
| 8853 | if (!PointerParents) { |
| 8854 | // We always need to run over the whole translation unit, as |
| 8855 | // hasAncestor can escape any subtree. |
| 8856 | auto Maps = ParentMapASTVisitor::buildMap(*getTranslationUnitDecl()); |
| 8857 | PointerParents.reset(Maps.first); |
| 8858 | OtherParents.reset(Maps.second); |
| 8859 | } |
| 8860 | if (Node.getNodeKind().hasPointerIdentity()) |
| 8861 | return getDynNodeFromMap(Node.getMemoizationData(), *PointerParents); |
| 8862 | return getDynNodeFromMap(Node, *OtherParents); |
| 8863 | } |
| 8864 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 8865 | bool |
| 8866 | ASTContext::ObjCMethodsAreEqual(const ObjCMethodDecl *MethodDecl, |
| 8867 | const ObjCMethodDecl *MethodImpl) { |
| 8868 | // No point trying to match an unavailable/deprecated mothod. |
| 8869 | if (MethodDecl->hasAttr<UnavailableAttr>() |
| 8870 | || MethodDecl->hasAttr<DeprecatedAttr>()) |
| 8871 | return false; |
| 8872 | if (MethodDecl->getObjCDeclQualifier() != |
| 8873 | MethodImpl->getObjCDeclQualifier()) |
| 8874 | return false; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8875 | if (!hasSameType(MethodDecl->getReturnType(), MethodImpl->getReturnType())) |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 8876 | return false; |
| 8877 | |
| 8878 | if (MethodDecl->param_size() != MethodImpl->param_size()) |
| 8879 | return false; |
| 8880 | |
| 8881 | for (ObjCMethodDecl::param_const_iterator IM = MethodImpl->param_begin(), |
| 8882 | IF = MethodDecl->param_begin(), EM = MethodImpl->param_end(), |
| 8883 | EF = MethodDecl->param_end(); |
| 8884 | IM != EM && IF != EF; ++IM, ++IF) { |
| 8885 | const ParmVarDecl *DeclVar = (*IF); |
| 8886 | const ParmVarDecl *ImplVar = (*IM); |
| 8887 | if (ImplVar->getObjCDeclQualifier() != DeclVar->getObjCDeclQualifier()) |
| 8888 | return false; |
| 8889 | if (!hasSameType(DeclVar->getType(), ImplVar->getType())) |
| 8890 | return false; |
| 8891 | } |
| 8892 | return (MethodDecl->isVariadic() == MethodImpl->isVariadic()); |
| 8893 | |
| 8894 | } |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 8895 | |
| 8896 | // Explicitly instantiate this in case a Redeclarable<T> is used from a TU that |
| 8897 | // doesn't include ASTContext.h |
| 8898 | template |
| 8899 | clang::LazyGenerationalUpdatePtr< |
| 8900 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::ValueType |
| 8901 | clang::LazyGenerationalUpdatePtr< |
| 8902 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::makeValue( |
| 8903 | const clang::ASTContext &Ctx, Decl *Value); |