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" |
Chandler Carruth | aa36b89 | 2015-12-30 03:40:23 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclContextInternals.h" |
Steve Naroff | 67391b8 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 25 | #include "clang/AST/Expr.h" |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 27 | #include "clang/AST/ExternalASTSource.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 28 | #include "clang/AST/Mangle.h" |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 29 | #include "clang/AST/MangleNumberingContext.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 30 | #include "clang/AST/RecordLayout.h" |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 31 | #include "clang/AST/RecursiveASTVisitor.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 32 | #include "clang/AST/TypeLoc.h" |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 33 | #include "clang/AST/VTableBuilder.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 34 | #include "clang/Basic/Builtins.h" |
Chris Lattner | d286851 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 35 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 36 | #include "clang/Basic/TargetInfo.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 { |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 60 | HalfRank, FloatRank, DoubleRank, LongDoubleRank, Float128Rank |
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 | } |
Eugene Zelenko | d4304d2 | 2015-11-04 21:37:17 +0000 | [diff] [blame] | 330 | } // anonymous namespace |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 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; |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Richard Smith | b39b9d5 | 2013-05-21 05:24:00 +0000 | [diff] [blame] | 435 | comments::FullComment *ASTContext::getLocalCommentForDeclUncached(const Decl *D) const { |
| 436 | const RawComment *RC = getRawCommentForDeclNoCache(D); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 437 | return RC ? RC->parse(*this, nullptr, D) : nullptr; |
Richard Smith | b39b9d5 | 2013-05-21 05:24:00 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 440 | comments::FullComment *ASTContext::getCommentForDecl( |
| 441 | const Decl *D, |
| 442 | const Preprocessor *PP) const { |
Fariborz Jahanian | 096f7c1 | 2013-05-13 17:27:00 +0000 | [diff] [blame] | 443 | if (D->isInvalidDecl()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 444 | return nullptr; |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 445 | D = adjustDeclToTemplate(D); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 446 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 447 | const Decl *Canonical = D->getCanonicalDecl(); |
| 448 | llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos = |
| 449 | ParsedComments.find(Canonical); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 450 | |
| 451 | if (Pos != ParsedComments.end()) { |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 452 | if (Canonical != D) { |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 453 | comments::FullComment *FC = Pos->second; |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 454 | comments::FullComment *CFC = cloneFullComment(FC, D); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 455 | return CFC; |
| 456 | } |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 457 | return Pos->second; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 460 | const Decl *OriginalDecl; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 461 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 462 | const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 463 | if (!RC) { |
| 464 | if (isa<ObjCMethodDecl>(D) || isa<FunctionDecl>(D)) { |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 465 | SmallVector<const NamedDecl*, 8> Overridden; |
Fariborz Jahanian | 37494a1 | 2013-01-12 00:28:34 +0000 | [diff] [blame] | 466 | const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 467 | if (OMD && OMD->isPropertyAccessor()) |
| 468 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) |
| 469 | if (comments::FullComment *FC = getCommentForDecl(PDecl, PP)) |
| 470 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 37494a1 | 2013-01-12 00:28:34 +0000 | [diff] [blame] | 471 | if (OMD) |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 472 | addRedeclaredMethods(OMD, Overridden); |
| 473 | getOverriddenMethods(dyn_cast<NamedDecl>(D), Overridden); |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 474 | for (unsigned i = 0, e = Overridden.size(); i < e; i++) |
| 475 | if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP)) |
| 476 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 477 | } |
Fariborz Jahanian | 6384fbb | 2013-05-02 15:44:16 +0000 | [diff] [blame] | 478 | else if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 479 | // Attach any tag type's documentation to its typedef if latter |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 480 | // does not have one of its own. |
Fariborz Jahanian | 40abf34 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 481 | QualType QT = TD->getUnderlyingType(); |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 482 | if (const TagType *TT = QT->getAs<TagType>()) |
| 483 | if (const Decl *TD = TT->getDecl()) |
| 484 | if (comments::FullComment *FC = getCommentForDecl(TD, PP)) |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 485 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 40abf34 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 486 | } |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 487 | else if (const ObjCInterfaceDecl *IC = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 488 | while (IC->getSuperClass()) { |
| 489 | IC = IC->getSuperClass(); |
| 490 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
| 491 | return cloneFullComment(FC, D); |
| 492 | } |
| 493 | } |
| 494 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
| 495 | if (const ObjCInterfaceDecl *IC = CD->getClassInterface()) |
| 496 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
| 497 | return cloneFullComment(FC, D); |
| 498 | } |
| 499 | else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 500 | if (!(RD = RD->getDefinition())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 501 | return nullptr; |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 502 | // Check non-virtual bases. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 503 | for (const auto &I : RD->bases()) { |
| 504 | if (I.isVirtual() || (I.getAccessSpecifier() != AS_public)) |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 505 | continue; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 506 | QualType Ty = I.getType(); |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 507 | if (Ty.isNull()) |
| 508 | continue; |
| 509 | if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) { |
| 510 | if (!(NonVirtualBase= NonVirtualBase->getDefinition())) |
| 511 | continue; |
| 512 | |
| 513 | if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP)) |
| 514 | return cloneFullComment(FC, D); |
| 515 | } |
| 516 | } |
| 517 | // Check virtual bases. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 518 | for (const auto &I : RD->vbases()) { |
| 519 | if (I.getAccessSpecifier() != AS_public) |
Fariborz Jahanian | 5a2e4a2 | 2013-04-26 23:34:36 +0000 | [diff] [blame] | 520 | continue; |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 521 | QualType Ty = I.getType(); |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 522 | if (Ty.isNull()) |
| 523 | continue; |
| 524 | if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) { |
| 525 | if (!(VirtualBase= VirtualBase->getDefinition())) |
| 526 | continue; |
| 527 | if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP)) |
| 528 | return cloneFullComment(FC, D); |
| 529 | } |
| 530 | } |
| 531 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 532 | return nullptr; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Dmitri Gribenko | bfda9f7 | 2012-08-22 18:12:19 +0000 | [diff] [blame] | 535 | // If the RawComment was attached to other redeclaration of this Decl, we |
| 536 | // should parse the comment in context of that other Decl. This is important |
| 537 | // because comments can contain references to parameter names which can be |
| 538 | // different across redeclarations. |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 539 | if (D != OriginalDecl) |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 540 | return getCommentForDecl(OriginalDecl, PP); |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 541 | |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 542 | comments::FullComment *FC = RC->parse(*this, PP, D); |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 543 | ParsedComments[Canonical] = FC; |
| 544 | return FC; |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 547 | void |
| 548 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 549 | TemplateTemplateParmDecl *Parm) { |
| 550 | ID.AddInteger(Parm->getDepth()); |
| 551 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 552 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 553 | |
| 554 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 555 | ID.AddInteger(Params->size()); |
| 556 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 557 | PEnd = Params->end(); |
| 558 | P != PEnd; ++P) { |
| 559 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 560 | ID.AddInteger(0); |
| 561 | ID.AddBoolean(TTP->isParameterPack()); |
| 562 | continue; |
| 563 | } |
| 564 | |
| 565 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 566 | ID.AddInteger(1); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 567 | ID.AddBoolean(NTTP->isParameterPack()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 568 | ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 569 | if (NTTP->isExpandedParameterPack()) { |
| 570 | ID.AddBoolean(true); |
| 571 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 572 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 573 | QualType T = NTTP->getExpansionType(I); |
| 574 | ID.AddPointer(T.getCanonicalType().getAsOpaquePtr()); |
| 575 | } |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 576 | } else |
| 577 | ID.AddBoolean(false); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 578 | continue; |
| 579 | } |
| 580 | |
| 581 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 582 | ID.AddInteger(2); |
| 583 | Profile(ID, TTP); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | TemplateTemplateParmDecl * |
| 588 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 589 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 590 | // Check if we already have a canonical template template parameter. |
| 591 | llvm::FoldingSetNodeID ID; |
| 592 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 593 | void *InsertPos = nullptr; |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 594 | CanonicalTemplateTemplateParm *Canonical |
| 595 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 596 | if (Canonical) |
| 597 | return Canonical->getParam(); |
| 598 | |
| 599 | // Build a canonical template parameter list. |
| 600 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 601 | SmallVector<NamedDecl *, 4> CanonParams; |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 602 | CanonParams.reserve(Params->size()); |
| 603 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 604 | PEnd = Params->end(); |
| 605 | P != PEnd; ++P) { |
| 606 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 607 | CanonParams.push_back( |
| 608 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 609 | SourceLocation(), |
| 610 | SourceLocation(), |
| 611 | TTP->getDepth(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 612 | TTP->getIndex(), nullptr, false, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 613 | TTP->isParameterPack())); |
| 614 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 615 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 616 | QualType T = getCanonicalType(NTTP->getType()); |
| 617 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 618 | NonTypeTemplateParmDecl *Param; |
| 619 | if (NTTP->isExpandedParameterPack()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 620 | SmallVector<QualType, 2> ExpandedTypes; |
| 621 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 622 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 623 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 624 | ExpandedTInfos.push_back( |
| 625 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 626 | } |
| 627 | |
| 628 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 629 | SourceLocation(), |
| 630 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 631 | NTTP->getDepth(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 632 | NTTP->getPosition(), nullptr, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 633 | T, |
| 634 | TInfo, |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 635 | ExpandedTypes, |
| 636 | ExpandedTInfos); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 637 | } else { |
| 638 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 639 | SourceLocation(), |
| 640 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 641 | NTTP->getDepth(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 642 | NTTP->getPosition(), nullptr, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 643 | T, |
| 644 | NTTP->isParameterPack(), |
| 645 | TInfo); |
| 646 | } |
| 647 | CanonParams.push_back(Param); |
| 648 | |
| 649 | } else |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 650 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 651 | cast<TemplateTemplateParmDecl>(*P))); |
| 652 | } |
| 653 | |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 654 | assert(!TTP->getRequiresClause() && |
| 655 | "Unexpected requires-clause on template template-parameter"); |
George Burgess IV | b7e4e48 | 2016-08-25 01:54:37 +0000 | [diff] [blame] | 656 | Expr *const CanonRequiresClause = nullptr; |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 657 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 658 | TemplateTemplateParmDecl *CanonTTP |
| 659 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 660 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 661 | TTP->getPosition(), |
| 662 | TTP->isParameterPack(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 663 | nullptr, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 664 | TemplateParameterList::Create(*this, SourceLocation(), |
| 665 | SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 666 | CanonParams, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 667 | SourceLocation(), |
| 668 | CanonRequiresClause)); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 669 | |
| 670 | // Get the new insert position for the node we care about. |
| 671 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 672 | assert(!Canonical && "Shouldn't be in the map!"); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 673 | (void)Canonical; |
| 674 | |
| 675 | // Create the canonical template template parameter entry. |
| 676 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 677 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 678 | return CanonTTP; |
| 679 | } |
| 680 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 681 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 682 | if (!LangOpts.CPlusPlus) return nullptr; |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 683 | |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 684 | switch (T.getCXXABI().getKind()) { |
Joerg Sonnenberger | daa13aa | 2014-05-13 11:20:16 +0000 | [diff] [blame] | 685 | case TargetCXXABI::GenericARM: // Same as Itanium at this level |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 686 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 687 | case TargetCXXABI::iOS64: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 688 | case TargetCXXABI::WatchOS: |
Joerg Sonnenberger | daa13aa | 2014-05-13 11:20:16 +0000 | [diff] [blame] | 689 | case TargetCXXABI::GenericAArch64: |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 690 | case TargetCXXABI::GenericMIPS: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 691 | case TargetCXXABI::GenericItanium: |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 692 | case TargetCXXABI::WebAssembly: |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 693 | return CreateItaniumCXXABI(*this); |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 694 | case TargetCXXABI::Microsoft: |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 695 | return CreateMicrosoftCXXABI(*this); |
| 696 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 697 | llvm_unreachable("Invalid CXXABI type!"); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 700 | static const LangAS::Map *getAddressSpaceMap(const TargetInfo &T, |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 701 | const LangOptions &LOpts) { |
| 702 | if (LOpts.FakeAddressSpaceMap) { |
| 703 | // The fake address space map must have a distinct entry for each |
| 704 | // language-specific address space. |
| 705 | static const unsigned FakeAddrSpaceMap[] = { |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame^] | 706 | 0, // Default |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 707 | 1, // opencl_global |
Egor Churaev | 28f00aa | 2016-12-23 16:11:25 +0000 | [diff] [blame] | 708 | 3, // opencl_local |
| 709 | 2, // opencl_constant |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 710 | 4, // opencl_generic |
| 711 | 5, // cuda_device |
| 712 | 6, // cuda_constant |
| 713 | 7 // cuda_shared |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 714 | }; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 715 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 716 | } else { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 717 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 721 | static bool isAddrSpaceMapManglingEnabled(const TargetInfo &TI, |
| 722 | const LangOptions &LangOpts) { |
| 723 | switch (LangOpts.getAddressSpaceMapMangling()) { |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 724 | case LangOptions::ASMM_Target: |
| 725 | return TI.useAddressSpaceMapMangling(); |
| 726 | case LangOptions::ASMM_On: |
| 727 | return true; |
| 728 | case LangOptions::ASMM_Off: |
| 729 | return false; |
| 730 | } |
NAKAMURA Takumi | 5c81ca4 | 2013-09-13 17:12:09 +0000 | [diff] [blame] | 731 | llvm_unreachable("getAddressSpaceMapMangling() doesn't cover anything."); |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Alexey Samsonov | 0b15e34 | 2014-10-15 22:17:27 +0000 | [diff] [blame] | 734 | ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM, |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 735 | IdentifierTable &idents, SelectorTable &sels, |
Alp Toker | 0804343 | 2014-05-03 03:46:04 +0000 | [diff] [blame] | 736 | Builtin::Context &builtins) |
Alexey Samsonov | 0b15e34 | 2014-10-15 22:17:27 +0000 | [diff] [blame] | 737 | : FunctionProtoTypes(this_()), TemplateSpecializationTypes(this_()), |
| 738 | DependentTemplateSpecializationTypes(this_()), |
| 739 | SubstTemplateTemplateParmPacks(this_()), |
| 740 | GlobalNestedNameSpecifier(nullptr), Int128Decl(nullptr), |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 741 | UInt128Decl(nullptr), BuiltinVaListDecl(nullptr), |
| 742 | BuiltinMSVaListDecl(nullptr), ObjCIdDecl(nullptr), ObjCSelDecl(nullptr), |
| 743 | ObjCClassDecl(nullptr), ObjCProtocolClassDecl(nullptr), BOOLDecl(nullptr), |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 744 | CFConstantStringTagDecl(nullptr), CFConstantStringTypeDecl(nullptr), |
| 745 | ObjCInstanceTypeDecl(nullptr), FILEDecl(nullptr), jmp_bufDecl(nullptr), |
| 746 | sigjmp_bufDecl(nullptr), ucontext_tDecl(nullptr), |
| 747 | BlockDescriptorType(nullptr), BlockDescriptorExtendedType(nullptr), |
| 748 | cudaConfigureCallDecl(nullptr), FirstLocalImport(), LastLocalImport(), |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 749 | ExternCContext(nullptr), MakeIntegerSeqDecl(nullptr), |
| 750 | TypePackElementDecl(nullptr), SourceMgr(SM), LangOpts(LOpts), |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 751 | SanitizerBL(new SanitizerBlacklist(LangOpts.SanitizerBlacklistFiles, SM)), |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 752 | XRayFilter(new XRayFunctionFilter(LangOpts.XRayAlwaysInstrumentFiles, |
| 753 | LangOpts.XRayNeverInstrumentFiles, SM)), |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 754 | AddrSpaceMap(nullptr), Target(nullptr), AuxTarget(nullptr), |
| 755 | PrintingPolicy(LOpts), Idents(idents), Selectors(sels), |
| 756 | BuiltinInfo(builtins), DeclarationNames(*this), ExternalSource(nullptr), |
| 757 | Listener(nullptr), Comments(SM), CommentsLoaded(false), |
Alexey Samsonov | 0b15e34 | 2014-10-15 22:17:27 +0000 | [diff] [blame] | 758 | CommentCommandTraits(BumpAlloc, LOpts.CommentOpts), LastSDM(nullptr, 0) { |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 759 | TUDecl = TranslationUnitDecl::Create(*this); |
| 760 | } |
| 761 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 762 | ASTContext::~ASTContext() { |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 763 | ReleaseParentMapEntries(); |
| 764 | |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 765 | // Release the DenseMaps associated with DeclContext objects. |
| 766 | // FIXME: Is this the ideal solution? |
| 767 | ReleaseDeclContextMaps(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 768 | |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 769 | // Call all of the deallocation functions on all of their targets. |
Chandler Carruth | ff5a01a | 2015-12-30 03:00:23 +0000 | [diff] [blame] | 770 | for (auto &Pair : Deallocations) |
| 771 | (Pair.first)(Pair.second); |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 772 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 773 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 774 | // because they can contain DenseMaps. |
| 775 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 776 | const ASTRecordLayout*>::iterator |
| 777 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 778 | // Increment in loop to prevent using deallocated memory. |
| 779 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 780 | R->Destroy(*this); |
| 781 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 782 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 783 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 784 | // Increment in loop to prevent using deallocated memory. |
| 785 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 786 | R->Destroy(*this); |
| 787 | } |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 788 | |
| 789 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 790 | AEnd = DeclAttrs.end(); |
| 791 | A != AEnd; ++A) |
| 792 | A->second->~AttrVec(); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 793 | |
David Majnemer | e694f3e | 2015-08-14 14:43:50 +0000 | [diff] [blame] | 794 | for (std::pair<const MaterializeTemporaryExpr *, APValue *> &MTVPair : |
| 795 | MaterializedTemporaryValues) |
| 796 | MTVPair.second->~APValue(); |
| 797 | |
Richard Smith | 423f46f | 2016-07-20 21:38:26 +0000 | [diff] [blame] | 798 | for (const auto &Value : ModuleInitializers) |
| 799 | Value.second->~PerModuleInitializers(); |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 800 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 801 | |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 802 | void ASTContext::ReleaseParentMapEntries() { |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 803 | if (!PointerParents) return; |
| 804 | for (const auto &Entry : *PointerParents) { |
| 805 | if (Entry.second.is<ast_type_traits::DynTypedNode *>()) { |
| 806 | delete Entry.second.get<ast_type_traits::DynTypedNode *>(); |
| 807 | } else if (Entry.second.is<ParentVector *>()) { |
| 808 | delete Entry.second.get<ParentVector *>(); |
| 809 | } |
| 810 | } |
| 811 | for (const auto &Entry : *OtherParents) { |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 812 | if (Entry.second.is<ast_type_traits::DynTypedNode *>()) { |
| 813 | delete Entry.second.get<ast_type_traits::DynTypedNode *>(); |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 814 | } else if (Entry.second.is<ParentVector *>()) { |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 815 | delete Entry.second.get<ParentVector *>(); |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 820 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
Chandler Carruth | ff5a01a | 2015-12-30 03:00:23 +0000 | [diff] [blame] | 821 | Deallocations.push_back({Callback, Data}); |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 824 | void |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 825 | ASTContext::setExternalSource(IntrusiveRefCntPtr<ExternalASTSource> Source) { |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 826 | ExternalSource = std::move(Source); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 829 | void ASTContext::PrintStats() const { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 830 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 831 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 832 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 833 | unsigned counts[] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 835 | #define ABSTRACT_TYPE(Name, Parent) |
| 836 | #include "clang/AST/TypeNodes.def" |
| 837 | 0 // Extra |
| 838 | }; |
Douglas Gregor | b1fe2c9 | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 839 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 840 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 841 | Type *T = Types[i]; |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 842 | counts[(unsigned)T->getTypeClass()]++; |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 845 | unsigned Idx = 0; |
| 846 | unsigned TotalBytes = 0; |
| 847 | #define TYPE(Name, Parent) \ |
| 848 | if (counts[Idx]) \ |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 849 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 850 | << " types\n"; \ |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 851 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 852 | ++Idx; |
| 853 | #define ABSTRACT_TYPE(Name, Parent) |
| 854 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 855 | |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 856 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 857 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 858 | // Implicit special member functions. |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 859 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 860 | << NumImplicitDefaultConstructors |
| 861 | << " implicit default constructors created\n"; |
| 862 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 863 | << NumImplicitCopyConstructors |
| 864 | << " implicit copy constructors created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 865 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 866 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 867 | << NumImplicitMoveConstructors |
| 868 | << " implicit move constructors created\n"; |
| 869 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 870 | << NumImplicitCopyAssignmentOperators |
| 871 | << " implicit copy assignment operators created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 872 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 873 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 874 | << NumImplicitMoveAssignmentOperators |
| 875 | << " implicit move assignment operators created\n"; |
| 876 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 877 | << NumImplicitDestructors |
| 878 | << " implicit destructors created\n"; |
| 879 | |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 880 | if (ExternalSource) { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 881 | llvm::errs() << "\n"; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 882 | ExternalSource->PrintStats(); |
| 883 | } |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 884 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 885 | BumpAlloc.PrintStats(); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 888 | void ASTContext::mergeDefinitionIntoModule(NamedDecl *ND, Module *M, |
| 889 | bool NotifyListeners) { |
| 890 | if (NotifyListeners) |
| 891 | if (auto *Listener = getASTMutationListener()) |
| 892 | Listener->RedefinedHiddenDefinition(ND, M); |
| 893 | |
Benjamin Kramer | a72a70a | 2016-10-17 13:00:44 +0000 | [diff] [blame] | 894 | if (getLangOpts().ModulesLocalVisibility) |
| 895 | MergedDefModules[ND].push_back(M); |
| 896 | else |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 897 | ND->setHidden(false); |
| 898 | } |
| 899 | |
| 900 | void ASTContext::deduplicateMergedDefinitonsFor(NamedDecl *ND) { |
| 901 | auto It = MergedDefModules.find(ND); |
| 902 | if (It == MergedDefModules.end()) |
| 903 | return; |
| 904 | |
Benjamin Kramer | a72a70a | 2016-10-17 13:00:44 +0000 | [diff] [blame] | 905 | auto &Merged = It->second; |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 906 | llvm::DenseSet<Module*> Found; |
| 907 | for (Module *&M : Merged) |
| 908 | if (!Found.insert(M).second) |
| 909 | M = nullptr; |
| 910 | Merged.erase(std::remove(Merged.begin(), Merged.end(), nullptr), Merged.end()); |
| 911 | } |
| 912 | |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 913 | void ASTContext::PerModuleInitializers::resolve(ASTContext &Ctx) { |
| 914 | if (LazyInitializers.empty()) |
| 915 | return; |
| 916 | |
| 917 | auto *Source = Ctx.getExternalSource(); |
| 918 | assert(Source && "lazy initializers but no external source"); |
| 919 | |
| 920 | auto LazyInits = std::move(LazyInitializers); |
| 921 | LazyInitializers.clear(); |
| 922 | |
| 923 | for (auto ID : LazyInits) |
| 924 | Initializers.push_back(Source->GetExternalDecl(ID)); |
| 925 | |
| 926 | assert(LazyInitializers.empty() && |
| 927 | "GetExternalDecl for lazy module initializer added more inits"); |
| 928 | } |
| 929 | |
| 930 | void ASTContext::addModuleInitializer(Module *M, Decl *D) { |
| 931 | // One special case: if we add a module initializer that imports another |
| 932 | // module, and that module's only initializer is an ImportDecl, simplify. |
| 933 | if (auto *ID = dyn_cast<ImportDecl>(D)) { |
| 934 | auto It = ModuleInitializers.find(ID->getImportedModule()); |
| 935 | |
| 936 | // Maybe the ImportDecl does nothing at all. (Common case.) |
| 937 | if (It == ModuleInitializers.end()) |
| 938 | return; |
| 939 | |
| 940 | // Maybe the ImportDecl only imports another ImportDecl. |
| 941 | auto &Imported = *It->second; |
| 942 | if (Imported.Initializers.size() + Imported.LazyInitializers.size() == 1) { |
| 943 | Imported.resolve(*this); |
| 944 | auto *OnlyDecl = Imported.Initializers.front(); |
| 945 | if (isa<ImportDecl>(OnlyDecl)) |
| 946 | D = OnlyDecl; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | auto *&Inits = ModuleInitializers[M]; |
| 951 | if (!Inits) |
| 952 | Inits = new (*this) PerModuleInitializers; |
| 953 | Inits->Initializers.push_back(D); |
| 954 | } |
| 955 | |
| 956 | void ASTContext::addLazyModuleInitializers(Module *M, ArrayRef<uint32_t> IDs) { |
| 957 | auto *&Inits = ModuleInitializers[M]; |
| 958 | if (!Inits) |
| 959 | Inits = new (*this) PerModuleInitializers; |
| 960 | Inits->LazyInitializers.insert(Inits->LazyInitializers.end(), |
| 961 | IDs.begin(), IDs.end()); |
| 962 | } |
| 963 | |
| 964 | ArrayRef<Decl*> ASTContext::getModuleInitializers(Module *M) { |
| 965 | auto It = ModuleInitializers.find(M); |
| 966 | if (It == ModuleInitializers.end()) |
| 967 | return None; |
| 968 | |
| 969 | auto *Inits = It->second; |
| 970 | Inits->resolve(*this); |
| 971 | return Inits->Initializers; |
| 972 | } |
| 973 | |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 974 | ExternCContextDecl *ASTContext::getExternCContextDecl() const { |
| 975 | if (!ExternCContext) |
| 976 | ExternCContext = ExternCContextDecl::Create(*this, getTranslationUnitDecl()); |
| 977 | |
| 978 | return ExternCContext; |
| 979 | } |
| 980 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 981 | BuiltinTemplateDecl * |
| 982 | ASTContext::buildBuiltinTemplateDecl(BuiltinTemplateKind BTK, |
| 983 | const IdentifierInfo *II) const { |
| 984 | auto *BuiltinTemplate = BuiltinTemplateDecl::Create(*this, TUDecl, II, BTK); |
| 985 | BuiltinTemplate->setImplicit(); |
| 986 | TUDecl->addDecl(BuiltinTemplate); |
| 987 | |
| 988 | return BuiltinTemplate; |
| 989 | } |
| 990 | |
| 991 | BuiltinTemplateDecl * |
| 992 | ASTContext::getMakeIntegerSeqDecl() const { |
| 993 | if (!MakeIntegerSeqDecl) |
| 994 | MakeIntegerSeqDecl = buildBuiltinTemplateDecl(BTK__make_integer_seq, |
| 995 | getMakeIntegerSeqName()); |
| 996 | return MakeIntegerSeqDecl; |
| 997 | } |
| 998 | |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 999 | BuiltinTemplateDecl * |
| 1000 | ASTContext::getTypePackElementDecl() const { |
| 1001 | if (!TypePackElementDecl) |
| 1002 | TypePackElementDecl = buildBuiltinTemplateDecl(BTK__type_pack_element, |
| 1003 | getTypePackElementName()); |
| 1004 | return TypePackElementDecl; |
| 1005 | } |
| 1006 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 1007 | RecordDecl *ASTContext::buildImplicitRecord(StringRef Name, |
| 1008 | RecordDecl::TagKind TK) const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1009 | SourceLocation Loc; |
| 1010 | RecordDecl *NewDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 1011 | if (getLangOpts().CPlusPlus) |
| 1012 | NewDecl = CXXRecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, |
| 1013 | Loc, &Idents.get(Name)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1014 | else |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 1015 | NewDecl = RecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, Loc, |
| 1016 | &Idents.get(Name)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1017 | NewDecl->setImplicit(); |
David Majnemer | f863736 | 2015-01-15 08:41:25 +0000 | [diff] [blame] | 1018 | NewDecl->addAttr(TypeVisibilityAttr::CreateImplicit( |
| 1019 | const_cast<ASTContext &>(*this), TypeVisibilityAttr::Default)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1020 | return NewDecl; |
| 1021 | } |
| 1022 | |
| 1023 | TypedefDecl *ASTContext::buildImplicitTypedef(QualType T, |
| 1024 | StringRef Name) const { |
| 1025 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 1026 | TypedefDecl *NewDecl = TypedefDecl::Create( |
| 1027 | const_cast<ASTContext &>(*this), getTranslationUnitDecl(), |
| 1028 | SourceLocation(), SourceLocation(), &Idents.get(Name), TInfo); |
| 1029 | NewDecl->setImplicit(); |
| 1030 | return NewDecl; |
| 1031 | } |
| 1032 | |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 1033 | TypedefDecl *ASTContext::getInt128Decl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1034 | if (!Int128Decl) |
| 1035 | Int128Decl = buildImplicitTypedef(Int128Ty, "__int128_t"); |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 1036 | return Int128Decl; |
| 1037 | } |
| 1038 | |
| 1039 | TypedefDecl *ASTContext::getUInt128Decl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1040 | if (!UInt128Decl) |
| 1041 | UInt128Decl = buildImplicitTypedef(UnsignedInt128Ty, "__uint128_t"); |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 1042 | return UInt128Decl; |
| 1043 | } |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 1044 | |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 1045 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1046 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 1047 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1048 | Types.push_back(Ty); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 1051 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target, |
| 1052 | const TargetInfo *AuxTarget) { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1053 | assert((!this->Target || this->Target == &Target) && |
| 1054 | "Incorrect target reinitialization"); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1055 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1056 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1057 | this->Target = &Target; |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 1058 | this->AuxTarget = AuxTarget; |
| 1059 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1060 | ABI.reset(createCXXABI(Target)); |
| 1061 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 1062 | AddrSpaceMapMangling = isAddrSpaceMapManglingEnabled(Target, LangOpts); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1063 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1064 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1065 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1067 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1068 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1069 | // C99 6.2.5p3. |
Eli Friedman | 9ffd4a9 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 1070 | if (LangOpts.CharIsSigned) |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 1071 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 1072 | else |
| 1073 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1074 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1075 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 1076 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 1077 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 1078 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 1079 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1080 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1081 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1082 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 1083 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 1084 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 1085 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 1086 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1087 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1088 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1089 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 1090 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 1091 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1092 | |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1093 | // GNU extension, __float128 for IEEE quadruple precision |
| 1094 | InitBuiltinType(Float128Ty, BuiltinType::Float128); |
| 1095 | |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 1096 | // GNU extension, 128-bit integers. |
| 1097 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 1098 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 1099 | |
Hans Wennborg | 0d81e01 | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 1100 | // C++ 3.9.1p5 |
| 1101 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
| 1102 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 1103 | else // -fshort-wchar makes wchar_t be unsigned. |
| 1104 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 1105 | if (LangOpts.CPlusPlus && LangOpts.WChar) |
| 1106 | WideCharTy = WCharTy; |
| 1107 | else { |
| 1108 | // C99 (or C++ using -fno-wchar). |
| 1109 | WideCharTy = getFromTargetType(Target.getWCharType()); |
| 1110 | } |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1111 | |
James Molloy | 3636554 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 1112 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 1113 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1114 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 1115 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 1116 | else // C99 |
| 1117 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 1118 | |
| 1119 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 1120 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 1121 | else // C99 |
| 1122 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 1123 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1124 | // Placeholder type for type-dependent expressions whose type is |
| 1125 | // completely unknown. No code should ever check a type against |
| 1126 | // DependentTy and users should never see it; however, it is here to |
| 1127 | // help diagnose failures to properly check for type-dependent |
| 1128 | // expressions. |
| 1129 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1130 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 1131 | // Placeholder type for functions. |
| 1132 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 1133 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1134 | // Placeholder type for bound members. |
| 1135 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 1136 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1137 | // Placeholder type for pseudo-objects. |
| 1138 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 1139 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 1140 | // "any" type; useful for debugger-like clients. |
| 1141 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 1142 | |
John McCall | 8a6b59a | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 1143 | // Placeholder type for unbridged ARC casts. |
| 1144 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 1145 | |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 1146 | // Placeholder type for builtin functions. |
| 1147 | InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn); |
| 1148 | |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 1149 | // Placeholder type for OMP array sections. |
| 1150 | if (LangOpts.OpenMP) |
| 1151 | InitBuiltinType(OMPArraySectionTy, BuiltinType::OMPArraySection); |
| 1152 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1153 | // C99 6.2.5p11. |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1154 | FloatComplexTy = getComplexType(FloatTy); |
| 1155 | DoubleComplexTy = getComplexType(DoubleTy); |
| 1156 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1157 | Float128ComplexTy = getComplexType(Float128Ty); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1158 | |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1159 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1160 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 1161 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1162 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1163 | |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 1164 | if (LangOpts.OpenCL) { |
| 1165 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1166 | InitBuiltinType(SingletonId, BuiltinType::Id); |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 1167 | #include "clang/Basic/OpenCLImageTypes.def" |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1168 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1169 | InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1170 | InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1171 | InitBuiltinType(OCLClkEventTy, BuiltinType::OCLClkEvent); |
| 1172 | InitBuiltinType(OCLQueueTy, BuiltinType::OCLQueue); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1173 | InitBuiltinType(OCLReserveIDTy, BuiltinType::OCLReserveID); |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1174 | } |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1175 | |
| 1176 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 29898f4 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 1177 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 1178 | SignedCharTy : BoolTy); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1179 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1180 | ObjCConstantStringType = QualType(); |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1181 | |
| 1182 | ObjCSuperType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1183 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1184 | // void * type |
| 1185 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1186 | |
| 1187 | // nullptr type (C++0x 2.14.7) |
| 1188 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1189 | |
| 1190 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 1191 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 1192 | |
| 1193 | // Builtin type used to help define __builtin_va_list. |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 1194 | VaListTagDecl = nullptr; |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1197 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1198 | return SourceMgr.getDiagnostics(); |
| 1199 | } |
| 1200 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 1201 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 1202 | AttrVec *&Result = DeclAttrs[D]; |
| 1203 | if (!Result) { |
| 1204 | void *Mem = Allocate(sizeof(AttrVec)); |
| 1205 | Result = new (Mem) AttrVec; |
| 1206 | } |
| 1207 | |
| 1208 | return *Result; |
| 1209 | } |
| 1210 | |
| 1211 | /// \brief Erase the attributes corresponding to the given declaration. |
| 1212 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 1213 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 1214 | if (Pos != DeclAttrs.end()) { |
| 1215 | Pos->second->~AttrVec(); |
| 1216 | DeclAttrs.erase(Pos); |
| 1217 | } |
| 1218 | } |
| 1219 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1220 | // FIXME: Remove ? |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1221 | MemberSpecializationInfo * |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1222 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1223 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1224 | return getTemplateOrSpecializationInfo(Var) |
| 1225 | .dyn_cast<MemberSpecializationInfo *>(); |
| 1226 | } |
| 1227 | |
| 1228 | ASTContext::TemplateOrSpecializationInfo |
| 1229 | ASTContext::getTemplateOrSpecializationInfo(const VarDecl *Var) { |
| 1230 | llvm::DenseMap<const VarDecl *, TemplateOrSpecializationInfo>::iterator Pos = |
| 1231 | TemplateOrInstantiation.find(Var); |
| 1232 | if (Pos == TemplateOrInstantiation.end()) |
| 1233 | return TemplateOrSpecializationInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1234 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1235 | return Pos->second; |
| 1236 | } |
| 1237 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | void |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1239 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 1240 | TemplateSpecializationKind TSK, |
| 1241 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1242 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 1243 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1244 | setTemplateOrSpecializationInfo(Inst, new (*this) MemberSpecializationInfo( |
| 1245 | Tmpl, TSK, PointOfInstantiation)); |
| 1246 | } |
| 1247 | |
| 1248 | void |
| 1249 | ASTContext::setTemplateOrSpecializationInfo(VarDecl *Inst, |
| 1250 | TemplateOrSpecializationInfo TSI) { |
| 1251 | assert(!TemplateOrInstantiation[Inst] && |
| 1252 | "Already noted what the variable was instantiated from"); |
| 1253 | TemplateOrInstantiation[Inst] = TSI; |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1256 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 1257 | const FunctionDecl *FD){ |
| 1258 | assert(FD && "Specialization is 0"); |
| 1259 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1260 | = ClassScopeSpecializationPattern.find(FD); |
| 1261 | if (Pos == ClassScopeSpecializationPattern.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1262 | return nullptr; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1263 | |
| 1264 | return Pos->second; |
| 1265 | } |
| 1266 | |
| 1267 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 1268 | FunctionDecl *Pattern) { |
| 1269 | assert(FD && "Specialization is 0"); |
| 1270 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1271 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1274 | NamedDecl * |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1275 | ASTContext::getInstantiatedFromUsingDecl(NamedDecl *UUD) { |
| 1276 | auto Pos = InstantiatedFromUsingDecl.find(UUD); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1277 | if (Pos == InstantiatedFromUsingDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1278 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1279 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1280 | return Pos->second; |
| 1281 | } |
| 1282 | |
| 1283 | void |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1284 | ASTContext::setInstantiatedFromUsingDecl(NamedDecl *Inst, NamedDecl *Pattern) { |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1285 | assert((isa<UsingDecl>(Pattern) || |
| 1286 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 1287 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 1288 | "pattern decl is not a using decl"); |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1289 | assert((isa<UsingDecl>(Inst) || |
| 1290 | isa<UnresolvedUsingValueDecl>(Inst) || |
| 1291 | isa<UnresolvedUsingTypenameDecl>(Inst)) && |
| 1292 | "instantiation did not produce a using decl"); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1293 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 1294 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 1295 | } |
| 1296 | |
| 1297 | UsingShadowDecl * |
| 1298 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 1299 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 1300 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 1301 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1302 | return nullptr; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1303 | |
| 1304 | return Pos->second; |
| 1305 | } |
| 1306 | |
| 1307 | void |
| 1308 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 1309 | UsingShadowDecl *Pattern) { |
| 1310 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 1311 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1314 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 1315 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 1316 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 1317 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1318 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1319 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1320 | return Pos->second; |
| 1321 | } |
| 1322 | |
| 1323 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 1324 | FieldDecl *Tmpl) { |
| 1325 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 1326 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 1327 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 1328 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1330 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 1331 | } |
| 1332 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1333 | ASTContext::overridden_cxx_method_iterator |
| 1334 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
Clement Courbet | 6ecaec8 | 2016-07-05 07:49:31 +0000 | [diff] [blame] | 1335 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos = |
| 1336 | OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1337 | if (Pos == OverriddenMethods.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1338 | return nullptr; |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1339 | return Pos->second.begin(); |
| 1340 | } |
| 1341 | |
| 1342 | ASTContext::overridden_cxx_method_iterator |
| 1343 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
Clement Courbet | 6ecaec8 | 2016-07-05 07:49:31 +0000 | [diff] [blame] | 1344 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos = |
| 1345 | OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1346 | if (Pos == OverriddenMethods.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1347 | return nullptr; |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1348 | return Pos->second.end(); |
| 1349 | } |
| 1350 | |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1351 | unsigned |
| 1352 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
Clement Courbet | 6ecaec8 | 2016-07-05 07:49:31 +0000 | [diff] [blame] | 1353 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos = |
| 1354 | OverriddenMethods.find(Method->getCanonicalDecl()); |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1355 | if (Pos == OverriddenMethods.end()) |
| 1356 | return 0; |
Chandler Carruth | b1bcd5d | 2016-06-11 04:45:38 +0000 | [diff] [blame] | 1357 | return Pos->second.size(); |
Clement Courbet | 8251ebf | 2016-06-10 11:54:43 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Clement Courbet | 6ecaec8 | 2016-07-05 07:49:31 +0000 | [diff] [blame] | 1360 | ASTContext::overridden_method_range |
| 1361 | ASTContext::overridden_methods(const CXXMethodDecl *Method) const { |
| 1362 | return overridden_method_range(overridden_methods_begin(Method), |
| 1363 | overridden_methods_end(Method)); |
| 1364 | } |
| 1365 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1366 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 1367 | const CXXMethodDecl *Overridden) { |
Argyrios Kyrtzidis | cc4ca0a | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1368 | assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl()); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1369 | OverriddenMethods[Method].push_back(Overridden); |
| 1370 | } |
| 1371 | |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 1372 | void ASTContext::getOverriddenMethods( |
| 1373 | const NamedDecl *D, |
| 1374 | SmallVectorImpl<const NamedDecl *> &Overridden) const { |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1375 | assert(D); |
| 1376 | |
| 1377 | if (const CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | c8e7008 | 2013-04-17 00:09:03 +0000 | [diff] [blame] | 1378 | Overridden.append(overridden_methods_begin(CXXMethod), |
| 1379 | overridden_methods_end(CXXMethod)); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1380 | return; |
| 1381 | } |
| 1382 | |
| 1383 | const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D); |
| 1384 | if (!Method) |
| 1385 | return; |
| 1386 | |
Argyrios Kyrtzidis | 353f6a4 | 2012-10-09 18:19:01 +0000 | [diff] [blame] | 1387 | SmallVector<const ObjCMethodDecl *, 8> OverDecls; |
| 1388 | Method->getOverriddenMethods(OverDecls); |
Argyrios Kyrtzidis | a7a1081 | 2012-10-09 20:08:43 +0000 | [diff] [blame] | 1389 | Overridden.append(OverDecls.begin(), OverDecls.end()); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 1392 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 1393 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 1394 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 1395 | if (!FirstLocalImport) { |
| 1396 | FirstLocalImport = Import; |
| 1397 | LastLocalImport = Import; |
| 1398 | return; |
| 1399 | } |
| 1400 | |
| 1401 | LastLocalImport->NextLocalImport = Import; |
| 1402 | LastLocalImport = Import; |
| 1403 | } |
| 1404 | |
Chris Lattner | 53cfe80 | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1405 | //===----------------------------------------------------------------------===// |
| 1406 | // Type Sizing and Analysis |
| 1407 | //===----------------------------------------------------------------------===// |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1408 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1409 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 1410 | /// scalar floating point type. |
| 1411 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1412 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1413 | assert(BT && "Not a floating point type!"); |
| 1414 | switch (BT->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1415 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1416 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1417 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 1418 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 1419 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1420 | case BuiltinType::Float128: return Target->getFloat128Format(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1424 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1425 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1426 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1427 | bool UseAlignAttrOnly = false; |
| 1428 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 1429 | Align = AlignFromAttr; |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1430 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1431 | // __attribute__((aligned)) can increase or decrease alignment |
| 1432 | // *except* on a struct or struct member, where it only increases |
| 1433 | // alignment unless 'packed' is also specified. |
| 1434 | // |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1435 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1436 | // ignore that possibility; Sema should diagnose it. |
| 1437 | if (isa<FieldDecl>(D)) { |
| 1438 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 1439 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 1440 | } else { |
| 1441 | UseAlignAttrOnly = true; |
| 1442 | } |
| 1443 | } |
Fariborz Jahanian | 9f10718 | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 1444 | else if (isa<FieldDecl>(D)) |
| 1445 | UseAlignAttrOnly = |
| 1446 | D->hasAttr<PackedAttr>() || |
| 1447 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1448 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1449 | // If we're using the align attribute only, just ignore everything |
| 1450 | // else about the declaration and its type. |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1451 | if (UseAlignAttrOnly) { |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1452 | // do nothing |
| 1453 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1454 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1455 | QualType T = VD->getType(); |
Richard Smith | f6d7030 | 2014-06-10 23:34:28 +0000 | [diff] [blame] | 1456 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1457 | if (ForAlignof) |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1458 | T = RT->getPointeeType(); |
| 1459 | else |
| 1460 | T = getPointerType(RT->getPointeeType()); |
| 1461 | } |
Richard Smith | f6d7030 | 2014-06-10 23:34:28 +0000 | [diff] [blame] | 1462 | QualType BaseT = getBaseElementType(T); |
Akira Hatanaka | d62f2c8 | 2017-01-06 17:56:15 +0000 | [diff] [blame] | 1463 | if (T->isFunctionType()) |
| 1464 | Align = getTypeInfoImpl(T.getTypePtr()).Align; |
| 1465 | else if (!BaseT->isIncompleteType()) { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1466 | // Adjust alignments of declarations with array type by the |
| 1467 | // large-array alignment on the target. |
Rafael Espindola | 8857275 | 2013-08-07 18:08:19 +0000 | [diff] [blame] | 1468 | if (const ArrayType *arrayType = getAsArrayType(T)) { |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1469 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
| 1470 | if (!ForAlignof && MinWidth) { |
Rafael Espindola | 8857275 | 2013-08-07 18:08:19 +0000 | [diff] [blame] | 1471 | if (isa<VariableArrayType>(arrayType)) |
| 1472 | Align = std::max(Align, Target->getLargeArrayAlign()); |
| 1473 | else if (isa<ConstantArrayType>(arrayType) && |
| 1474 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
| 1475 | Align = std::max(Align, Target->getLargeArrayAlign()); |
| 1476 | } |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1477 | } |
Chad Rosier | 99ee782 | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 1478 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Roger Ferrer Ibanez | 3fa38a1 | 2017-03-08 14:00:44 +0000 | [diff] [blame] | 1479 | if (BaseT.getQualifiers().hasUnaligned()) |
| 1480 | Align = Target->getCharWidth(); |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1481 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Ulrich Weigand | b63f779 | 2015-04-21 17:26:18 +0000 | [diff] [blame] | 1482 | if (VD->hasGlobalStorage() && !ForAlignof) |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1483 | Align = std::max(Align, getTargetInfo().getMinGlobalAlign()); |
| 1484 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1485 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1486 | |
| 1487 | // Fields can be subject to extra alignment constraints, like if |
| 1488 | // the field is packed, the struct is packed, or the struct has a |
| 1489 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 1490 | // the actual alignment of the field within the struct, and then |
| 1491 | // (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] | 1492 | if (const FieldDecl *Field = dyn_cast<FieldDecl>(VD)) { |
| 1493 | const RecordDecl *Parent = Field->getParent(); |
| 1494 | // We can only produce a sensible answer if the record is valid. |
| 1495 | if (!Parent->isInvalidDecl()) { |
| 1496 | const ASTRecordLayout &Layout = getASTRecordLayout(Parent); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1497 | |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1498 | // Start with the record's overall alignment. |
| 1499 | unsigned FieldAlign = toBits(Layout.getAlignment()); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1500 | |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1501 | // Use the GCD of that and the offset within the record. |
| 1502 | uint64_t Offset = Layout.getFieldOffset(Field->getFieldIndex()); |
| 1503 | if (Offset > 0) { |
| 1504 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 1505 | // which means we get to do this crazy thing instead of Euclid's. |
| 1506 | uint64_t LowBitOfOffset = Offset & (~Offset + 1); |
| 1507 | if (LowBitOfOffset < FieldAlign) |
| 1508 | FieldAlign = static_cast<unsigned>(LowBitOfOffset); |
| 1509 | } |
| 1510 | |
| 1511 | Align = std::min(Align, FieldAlign); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1512 | } |
Charles Davis | 3fc5107 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 1513 | } |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1514 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1515 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1516 | return toCharUnitsFromBits(Align); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1517 | } |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1518 | |
John McCall | f124992 | 2012-08-21 04:10:00 +0000 | [diff] [blame] | 1519 | // getTypeInfoDataSizeInChars - Return the size of a type, in |
| 1520 | // chars. If the type is a record, its data size is returned. This is |
| 1521 | // the size of the memcpy that's performed when assigning this type |
| 1522 | // using a trivial copy/move assignment operator. |
| 1523 | std::pair<CharUnits, CharUnits> |
| 1524 | ASTContext::getTypeInfoDataSizeInChars(QualType T) const { |
| 1525 | std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T); |
| 1526 | |
| 1527 | // In C++, objects can sometimes be allocated into the tail padding |
| 1528 | // of a base-class subobject. We decide whether that's possible |
| 1529 | // during class layout, so here we can just trust the layout results. |
| 1530 | if (getLangOpts().CPlusPlus) { |
| 1531 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 1532 | const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl()); |
| 1533 | sizeAndAlign.first = layout.getDataSize(); |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | return sizeAndAlign; |
| 1538 | } |
| 1539 | |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1540 | /// getConstantArrayInfoInChars - Performing the computation in CharUnits |
| 1541 | /// instead of in bits prevents overflowing the uint64_t for some large arrays. |
| 1542 | std::pair<CharUnits, CharUnits> |
| 1543 | static getConstantArrayInfoInChars(const ASTContext &Context, |
| 1544 | const ConstantArrayType *CAT) { |
| 1545 | std::pair<CharUnits, CharUnits> EltInfo = |
| 1546 | Context.getTypeInfoInChars(CAT->getElementType()); |
| 1547 | uint64_t Size = CAT->getSize().getZExtValue(); |
Richard Trieu | c750907 | 2013-05-14 23:41:50 +0000 | [diff] [blame] | 1548 | assert((Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity()) <= |
| 1549 | (uint64_t)(-1)/Size) && |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1550 | "Overflow in array type char size evaluation"); |
| 1551 | uint64_t Width = EltInfo.first.getQuantity() * Size; |
| 1552 | unsigned Align = EltInfo.second.getQuantity(); |
Warren Hunt | 5ae586a | 2013-11-01 23:59:41 +0000 | [diff] [blame] | 1553 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 1554 | Context.getTargetInfo().getPointerWidth(0) == 64) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1555 | Width = llvm::alignTo(Width, Align); |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1556 | return std::make_pair(CharUnits::fromQuantity(Width), |
| 1557 | CharUnits::fromQuantity(Align)); |
| 1558 | } |
| 1559 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1560 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1561 | ASTContext::getTypeInfoInChars(const Type *T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1562 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) |
| 1563 | return getConstantArrayInfoInChars(*this, CAT); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1564 | TypeInfo Info = getTypeInfo(T); |
| 1565 | return std::make_pair(toCharUnitsFromBits(Info.Width), |
| 1566 | toCharUnitsFromBits(Info.Align)); |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1570 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1571 | return getTypeInfoInChars(T.getTypePtr()); |
| 1572 | } |
| 1573 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1574 | bool ASTContext::isAlignmentRequired(const Type *T) const { |
| 1575 | return getTypeInfo(T).AlignIsRequired; |
| 1576 | } |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1577 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1578 | bool ASTContext::isAlignmentRequired(QualType T) const { |
| 1579 | return isAlignmentRequired(T.getTypePtr()); |
| 1580 | } |
| 1581 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1582 | unsigned ASTContext::getTypeAlignIfKnown(QualType T) const { |
| 1583 | // An alignment on a typedef overrides anything else. |
| 1584 | if (auto *TT = T->getAs<TypedefType>()) |
| 1585 | if (unsigned Align = TT->getDecl()->getMaxAlignment()) |
| 1586 | return Align; |
| 1587 | |
| 1588 | // If we have an (array of) complete type, we're done. |
| 1589 | T = getBaseElementType(T); |
| 1590 | if (!T->isIncompleteType()) |
| 1591 | return getTypeAlign(T); |
| 1592 | |
| 1593 | // If we had an array type, its element type might be a typedef |
| 1594 | // type with an alignment attribute. |
| 1595 | if (auto *TT = T->getAs<TypedefType>()) |
| 1596 | if (unsigned Align = TT->getDecl()->getMaxAlignment()) |
| 1597 | return Align; |
| 1598 | |
| 1599 | // Otherwise, see if the declaration of the type had an attribute. |
| 1600 | if (auto *TT = T->getAs<TagType>()) |
| 1601 | return TT->getDecl()->getMaxAlignment(); |
| 1602 | |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1606 | TypeInfo ASTContext::getTypeInfo(const Type *T) const { |
David Majnemer | f8d3864 | 2014-07-30 08:42:33 +0000 | [diff] [blame] | 1607 | TypeInfoMap::iterator I = MemoizedTypeInfo.find(T); |
| 1608 | if (I != MemoizedTypeInfo.end()) |
| 1609 | return I->second; |
| 1610 | |
| 1611 | // This call can invalidate MemoizedTypeInfo[T], so we need a second lookup. |
| 1612 | TypeInfo TI = getTypeInfoImpl(T); |
| 1613 | MemoizedTypeInfo[T] = TI; |
Rafael Espindola | eaa88c1 | 2014-07-30 04:40:23 +0000 | [diff] [blame] | 1614 | return TI; |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 1618 | /// method does not work on incomplete types. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1619 | /// |
| 1620 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 1621 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 1622 | /// should take a QualType, &c. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1623 | TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const { |
| 1624 | uint64_t Width = 0; |
| 1625 | unsigned Align = 8; |
| 1626 | bool AlignIsRequired = false; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1627 | switch (T->getTypeClass()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1628 | #define TYPE(Class, Base) |
| 1629 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1630 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1631 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
David Blaikie | ab277d6 | 2013-07-13 21:08:03 +0000 | [diff] [blame] | 1632 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) \ |
| 1633 | case Type::Class: \ |
| 1634 | assert(!T->isDependentType() && "should not see dependent types here"); \ |
| 1635 | return getTypeInfo(cast<Class##Type>(T)->desugar().getTypePtr()); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1636 | #include "clang/AST/TypeNodes.def" |
John McCall | 15547bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 1637 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1638 | |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1639 | case Type::FunctionNoProto: |
| 1640 | case Type::FunctionProto: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1641 | // GCC extension: alignof(function) = 32 bits |
| 1642 | Width = 0; |
| 1643 | Align = 32; |
| 1644 | break; |
| 1645 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1646 | case Type::IncompleteArray: |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1647 | case Type::VariableArray: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1648 | Width = 0; |
| 1649 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 1650 | break; |
| 1651 | |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1652 | case Type::ConstantArray: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1653 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1654 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1655 | TypeInfo EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | d541a4c | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1656 | uint64_t Size = CAT->getSize().getZExtValue(); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1657 | assert((Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) && |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1658 | "Overflow in array type bit size evaluation"); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1659 | Width = EltInfo.Width * Size; |
| 1660 | Align = EltInfo.Align; |
Warren Hunt | 5ae586a | 2013-11-01 23:59:41 +0000 | [diff] [blame] | 1661 | if (!getTargetInfo().getCXXABI().isMicrosoft() || |
| 1662 | getTargetInfo().getPointerWidth(0) == 64) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1663 | Width = llvm::alignTo(Width, Align); |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1664 | break; |
Christopher Lamb | c5fafa2 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 1665 | } |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1666 | case Type::ExtVector: |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1667 | case Type::Vector: { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1668 | const VectorType *VT = cast<VectorType>(T); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1669 | TypeInfo EltInfo = getTypeInfo(VT->getElementType()); |
| 1670 | Width = EltInfo.Width * VT->getNumElements(); |
Eli Friedman | 3df5efe | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1671 | Align = Width; |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1672 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 1673 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | ef78c8e | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 1674 | if (Align & (Align-1)) { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1675 | Align = llvm::NextPowerOf2(Align); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1676 | Width = llvm::alignTo(Width, Align); |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1677 | } |
Chad Rosier | cc40ea7 | 2012-07-13 23:57:43 +0000 | [diff] [blame] | 1678 | // Adjust the alignment based on the target max. |
| 1679 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
| 1680 | if (TargetVectorAlign && TargetVectorAlign < Align) |
| 1681 | Align = TargetVectorAlign; |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1682 | break; |
| 1683 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1684 | |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1685 | case Type::Builtin: |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1686 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1687 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1688 | case BuiltinType::Void: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1689 | // GCC extension: alignof(void) = 8 bits. |
| 1690 | Width = 0; |
| 1691 | Align = 8; |
| 1692 | break; |
| 1693 | |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1694 | case BuiltinType::Bool: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1695 | Width = Target->getBoolWidth(); |
| 1696 | Align = Target->getBoolAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1697 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1698 | case BuiltinType::Char_S: |
| 1699 | case BuiltinType::Char_U: |
| 1700 | case BuiltinType::UChar: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1701 | case BuiltinType::SChar: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1702 | Width = Target->getCharWidth(); |
| 1703 | Align = Target->getCharAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1704 | break; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1705 | case BuiltinType::WChar_S: |
| 1706 | case BuiltinType::WChar_U: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1707 | Width = Target->getWCharWidth(); |
| 1708 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1709 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1710 | case BuiltinType::Char16: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1711 | Width = Target->getChar16Width(); |
| 1712 | Align = Target->getChar16Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1713 | break; |
| 1714 | case BuiltinType::Char32: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1715 | Width = Target->getChar32Width(); |
| 1716 | Align = Target->getChar32Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1717 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1718 | case BuiltinType::UShort: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1719 | case BuiltinType::Short: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1720 | Width = Target->getShortWidth(); |
| 1721 | Align = Target->getShortAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1722 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1723 | case BuiltinType::UInt: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1724 | case BuiltinType::Int: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1725 | Width = Target->getIntWidth(); |
| 1726 | Align = Target->getIntAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1727 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1728 | case BuiltinType::ULong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1729 | case BuiltinType::Long: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1730 | Width = Target->getLongWidth(); |
| 1731 | Align = Target->getLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1732 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1733 | case BuiltinType::ULongLong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1734 | case BuiltinType::LongLong: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1735 | Width = Target->getLongLongWidth(); |
| 1736 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1737 | break; |
Chris Lattner | 0a415ec | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 1738 | case BuiltinType::Int128: |
| 1739 | case BuiltinType::UInt128: |
| 1740 | Width = 128; |
| 1741 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 1742 | break; |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1743 | case BuiltinType::Half: |
| 1744 | Width = Target->getHalfWidth(); |
| 1745 | Align = Target->getHalfAlign(); |
| 1746 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1747 | case BuiltinType::Float: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1748 | Width = Target->getFloatWidth(); |
| 1749 | Align = Target->getFloatAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1750 | break; |
| 1751 | case BuiltinType::Double: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1752 | Width = Target->getDoubleWidth(); |
| 1753 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1754 | break; |
| 1755 | case BuiltinType::LongDouble: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1756 | Width = Target->getLongDoubleWidth(); |
| 1757 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1758 | break; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1759 | case BuiltinType::Float128: |
| 1760 | Width = Target->getFloat128Width(); |
| 1761 | Align = Target->getFloat128Align(); |
| 1762 | break; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1763 | case BuiltinType::NullPtr: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1764 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 1765 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | a81b0b7 | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 1766 | break; |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1767 | case BuiltinType::ObjCId: |
| 1768 | case BuiltinType::ObjCClass: |
| 1769 | case BuiltinType::ObjCSel: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1770 | Width = Target->getPointerWidth(0); |
| 1771 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1772 | break; |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 1773 | case BuiltinType::OCLSampler: { |
| 1774 | auto AS = getTargetAddressSpace(LangAS::opencl_constant); |
| 1775 | Width = Target->getPointerWidth(AS); |
| 1776 | Align = Target->getPointerAlign(AS); |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1777 | break; |
Yaxun Liu | 0bc4b2d | 2016-07-28 19:26:30 +0000 | [diff] [blame] | 1778 | } |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1779 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1780 | case BuiltinType::OCLClkEvent: |
| 1781 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1782 | case BuiltinType::OCLReserveID: |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1783 | // Currently these types are pointers to opaque types. |
| 1784 | Width = Target->getPointerWidth(0); |
| 1785 | Align = Target->getPointerAlign(0); |
| 1786 | break; |
Yaxun Liu | 99444cb | 2016-08-03 20:38:06 +0000 | [diff] [blame] | 1787 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1788 | case BuiltinType::Id: |
| 1789 | #include "clang/Basic/OpenCLImageTypes.def" |
| 1790 | { |
| 1791 | auto AS = getTargetAddressSpace(Target->getOpenCLImageAddrSpace()); |
| 1792 | Width = Target->getPointerWidth(AS); |
| 1793 | Align = Target->getPointerAlign(AS); |
| 1794 | } |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1795 | } |
Chris Lattner | 48f84b8 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 1796 | break; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1797 | case Type::ObjCObjectPointer: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1798 | Width = Target->getPointerWidth(0); |
| 1799 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1800 | break; |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1801 | case Type::BlockPointer: { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1802 | unsigned AS = getTargetAddressSpace( |
| 1803 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1804 | Width = Target->getPointerWidth(AS); |
| 1805 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1806 | break; |
| 1807 | } |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1808 | case Type::LValueReference: |
| 1809 | case Type::RValueReference: { |
| 1810 | // alignof and sizeof should never enter this code path here, so we go |
| 1811 | // the pointer route. |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1812 | unsigned AS = getTargetAddressSpace( |
| 1813 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1814 | Width = Target->getPointerWidth(AS); |
| 1815 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1816 | break; |
| 1817 | } |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1818 | case Type::Pointer: { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1819 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1820 | Width = Target->getPointerWidth(AS); |
| 1821 | Align = Target->getPointerAlign(AS); |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1822 | break; |
| 1823 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1824 | case Type::MemberPointer: { |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1825 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1826 | std::tie(Width, Align) = ABI->getMemberPointerWidthAndAlign(MPT); |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1827 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1828 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1829 | case Type::Complex: { |
| 1830 | // Complex types have the same alignment as their elements, but twice the |
| 1831 | // size. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1832 | TypeInfo EltInfo = getTypeInfo(cast<ComplexType>(T)->getElementType()); |
| 1833 | Width = EltInfo.Width * 2; |
| 1834 | Align = EltInfo.Align; |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1835 | break; |
| 1836 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1837 | case Type::ObjCObject: |
| 1838 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1839 | case Type::Adjusted: |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 1840 | case Type::Decayed: |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1841 | return getTypeInfo(cast<AdjustedType>(T)->getAdjustedType().getTypePtr()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1842 | case Type::ObjCInterface: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1843 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1844 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1845 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1846 | Align = toBits(Layout.getAlignment()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1847 | break; |
| 1848 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1849 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1850 | case Type::Enum: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1851 | const TagType *TT = cast<TagType>(T); |
| 1852 | |
| 1853 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 7f97189 | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1854 | Width = 8; |
| 1855 | Align = 8; |
Chris Lattner | 572100b | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1856 | break; |
| 1857 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1858 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 1859 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) { |
| 1860 | const EnumDecl *ED = ET->getDecl(); |
| 1861 | TypeInfo Info = |
| 1862 | getTypeInfo(ED->getIntegerType()->getUnqualifiedDesugaredType()); |
| 1863 | if (unsigned AttrAlign = ED->getMaxAlignment()) { |
| 1864 | Info.Align = AttrAlign; |
| 1865 | Info.AlignIsRequired = true; |
| 1866 | } |
| 1867 | return Info; |
| 1868 | } |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1869 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1870 | const RecordType *RT = cast<RecordType>(TT); |
David Majnemer | 5821ff7 | 2015-02-03 08:49:29 +0000 | [diff] [blame] | 1871 | const RecordDecl *RD = RT->getDecl(); |
| 1872 | const ASTRecordLayout &Layout = getASTRecordLayout(RD); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1873 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1874 | Align = toBits(Layout.getAlignment()); |
David Majnemer | 5821ff7 | 2015-02-03 08:49:29 +0000 | [diff] [blame] | 1875 | AlignIsRequired = RD->hasAttr<AlignedAttr>(); |
Chris Lattner | 49a953a | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1876 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1877 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1878 | |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1879 | case Type::SubstTemplateTypeParm: |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1880 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1881 | getReplacementType().getTypePtr()); |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1882 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1883 | case Type::Auto: |
| 1884 | case Type::DeducedTemplateSpecialization: { |
| 1885 | const DeducedType *A = cast<DeducedType>(T); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 1886 | assert(!A->getDeducedType().isNull() && |
| 1887 | "cannot request the size of an undeduced or dependent auto type"); |
Matt Beaumont-Gay | 7a24210e | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1888 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1891 | case Type::Paren: |
| 1892 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1893 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 1894 | case Type::ObjCTypeParam: |
| 1895 | return getTypeInfo(cast<ObjCTypeParamType>(T)->desugar().getTypePtr()); |
| 1896 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1897 | case Type::Typedef: { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1898 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1899 | TypeInfo Info = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1900 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1901 | // alignment we have. This violates the GCC documentation (which says that |
| 1902 | // attribute(aligned) can only round up) but matches its implementation. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1903 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) { |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1904 | Align = AttrAlign; |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1905 | AlignIsRequired = true; |
David Majnemer | 37bffb6 | 2014-08-04 05:11:01 +0000 | [diff] [blame] | 1906 | } else { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1907 | Align = Info.Align; |
David Majnemer | 37bffb6 | 2014-08-04 05:11:01 +0000 | [diff] [blame] | 1908 | AlignIsRequired = Info.AlignIsRequired; |
| 1909 | } |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1910 | Width = Info.Width; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1911 | break; |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1912 | } |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1913 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1914 | case Type::Elaborated: |
| 1915 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1916 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1917 | case Type::Attributed: |
| 1918 | return getTypeInfo( |
| 1919 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1920 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1921 | case Type::Atomic: { |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1922 | // Start with the base type information. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1923 | TypeInfo Info = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1924 | Width = Info.Width; |
| 1925 | Align = Info.Align; |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1926 | |
| 1927 | // If the size of the type doesn't exceed the platform's max |
| 1928 | // atomic promotion width, make the size and alignment more |
| 1929 | // favorable to atomic operations: |
| 1930 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth()) { |
| 1931 | // Round the size up to a power of 2. |
| 1932 | if (!llvm::isPowerOf2_64(Width)) |
| 1933 | Width = llvm::NextPowerOf2(Width); |
| 1934 | |
| 1935 | // Set the alignment equal to the size. |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1936 | Align = static_cast<unsigned>(Width); |
| 1937 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1938 | } |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1939 | break; |
| 1940 | |
| 1941 | case Type::Pipe: { |
| 1942 | TypeInfo Info = getTypeInfo(cast<PipeType>(T)->getElementType()); |
| 1943 | Width = Info.Width; |
| 1944 | Align = Info.Align; |
| 1945 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1946 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1947 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1949 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1950 | return TypeInfo(Width, Align, AlignIsRequired); |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1953 | unsigned ASTContext::getOpenMPDefaultSimdAlign(QualType T) const { |
| 1954 | unsigned SimdAlign = getTargetInfo().getSimdDefaultAlign(); |
| 1955 | // Target ppc64 with QPX: simd default alignment for pointer to double is 32. |
| 1956 | if ((getTargetInfo().getTriple().getArch() == llvm::Triple::ppc64 || |
| 1957 | getTargetInfo().getTriple().getArch() == llvm::Triple::ppc64le) && |
| 1958 | getTargetInfo().getABI() == "elfv1-qpx" && |
| 1959 | T->isSpecificBuiltinType(BuiltinType::Double)) |
| 1960 | SimdAlign = 256; |
| 1961 | return SimdAlign; |
| 1962 | } |
| 1963 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1964 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1965 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1966 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1967 | } |
| 1968 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1969 | /// toBits - Convert a size in characters to a size in characters. |
| 1970 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1971 | return CharSize.getQuantity() * getCharWidth(); |
| 1972 | } |
| 1973 | |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1974 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1975 | /// This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1976 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1977 | return getTypeInfoInChars(T).first; |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1978 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1979 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1980 | return getTypeInfoInChars(T).first; |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Ken Dyck | a6046ab | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1983 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1984 | /// characters. This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1985 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1986 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1987 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1988 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1989 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1992 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1993 | /// type for the current target in bits. This can be different than the ABI |
| 1994 | /// alignment in cases where it is beneficial for performance to overalign |
| 1995 | /// a data type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1996 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1997 | TypeInfo TI = getTypeInfo(T); |
| 1998 | unsigned ABIAlign = TI.Align; |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1999 | |
David Majnemer | e154456 | 2015-04-24 01:25:05 +0000 | [diff] [blame] | 2000 | T = T->getBaseElementTypeUnsafe(); |
| 2001 | |
| 2002 | // The preferred alignment of member pointers is that of a pointer. |
| 2003 | if (T->isMemberPointerType()) |
| 2004 | return getPreferredTypeAlign(getPointerDiffType().getTypePtr()); |
| 2005 | |
Andrey Turetskiy | db6655f | 2016-02-10 11:58:46 +0000 | [diff] [blame] | 2006 | if (!Target->allowsLargerPreferedTypeAlignment()) |
| 2007 | return ABIAlign; |
Robert Lytton | eaf6f36 | 2013-11-12 10:09:34 +0000 | [diff] [blame] | 2008 | |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2009 | // Double and long long should be naturally aligned if possible. |
David Majnemer | 1d4db8f | 2014-02-24 23:43:27 +0000 | [diff] [blame] | 2010 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2011 | T = CT->getElementType().getTypePtr(); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 2012 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 2013 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2014 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | b57321a | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 2015 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2016 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
David Majnemer | 8b6bd57 | 2014-02-24 23:34:17 +0000 | [diff] [blame] | 2017 | // Don't increase the alignment if an alignment attribute was specified on a |
| 2018 | // typedef declaration. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2019 | if (!TI.AlignIsRequired) |
David Majnemer | 8b6bd57 | 2014-02-24 23:34:17 +0000 | [diff] [blame] | 2020 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2021 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 2022 | return ABIAlign; |
| 2023 | } |
| 2024 | |
Ulrich Weigand | ca3cb7f | 2015-04-21 17:29:35 +0000 | [diff] [blame] | 2025 | /// getTargetDefaultAlignForAttributeAligned - Return the default alignment |
| 2026 | /// for __attribute__((aligned)) on this target, to be used if no alignment |
| 2027 | /// value is specified. |
Eugene Zelenko | d4304d2 | 2015-11-04 21:37:17 +0000 | [diff] [blame] | 2028 | unsigned ASTContext::getTargetDefaultAlignForAttributeAligned() const { |
Ulrich Weigand | ca3cb7f | 2015-04-21 17:29:35 +0000 | [diff] [blame] | 2029 | return getTargetInfo().getDefaultAlignForAttributeAligned(); |
| 2030 | } |
| 2031 | |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 2032 | /// getAlignOfGlobalVar - Return the alignment in bits that should be given |
| 2033 | /// to a global variable of the specified type. |
| 2034 | unsigned ASTContext::getAlignOfGlobalVar(QualType T) const { |
| 2035 | return std::max(getTypeAlign(T), getTargetInfo().getMinGlobalAlign()); |
| 2036 | } |
| 2037 | |
| 2038 | /// getAlignOfGlobalVarInChars - Return the alignment in characters that |
| 2039 | /// should be given to a global variable of the specified type. |
| 2040 | CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T) const { |
| 2041 | return toCharUnitsFromBits(getAlignOfGlobalVar(T)); |
| 2042 | } |
| 2043 | |
David Majnemer | 08ef2ba | 2015-06-23 20:34:18 +0000 | [diff] [blame] | 2044 | CharUnits ASTContext::getOffsetOfBaseWithVBPtr(const CXXRecordDecl *RD) const { |
| 2045 | CharUnits Offset = CharUnits::Zero(); |
| 2046 | const ASTRecordLayout *Layout = &getASTRecordLayout(RD); |
| 2047 | while (const CXXRecordDecl *Base = Layout->getBaseSharingVBPtr()) { |
| 2048 | Offset += Layout->getBaseClassOffset(Base); |
| 2049 | Layout = &getASTRecordLayout(Base); |
| 2050 | } |
| 2051 | return Offset; |
| 2052 | } |
| 2053 | |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2054 | /// DeepCollectObjCIvars - |
| 2055 | /// This routine first collects all declared, but not synthesized, ivars in |
| 2056 | /// super class and then collects all ivars, including those synthesized for |
| 2057 | /// current class. This routine is used for implementation of current class |
| 2058 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 2059 | /// |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2060 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 2061 | bool leafClass, |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2062 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2063 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 2064 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 2065 | if (!leafClass) { |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 2066 | for (const auto *I : OI->ivars()) |
| 2067 | Ivars.push_back(I); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2068 | } else { |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 2069 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2070 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 2071 | Iv= Iv->getNextIvar()) |
| 2072 | Ivars.push_back(Iv); |
| 2073 | } |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2076 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 2077 | /// those inherited by it. |
| 2078 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 2079 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2080 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2081 | // We can use protocol_iterator here instead of |
| 2082 | // all_referenced_protocol_iterator since we are walking all categories. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2083 | for (auto *Proto : OI->all_referenced_protocols()) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 2084 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 2085 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2086 | |
| 2087 | // Categories of this Interface. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 2088 | for (const auto *Cat : OI->visible_categories()) |
| 2089 | CollectInheritedProtocols(Cat, Protocols); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2090 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2091 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 2092 | while (SD) { |
| 2093 | CollectInheritedProtocols(SD, Protocols); |
| 2094 | SD = SD->getSuperClass(); |
| 2095 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 2096 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 2097 | for (auto *Proto : OC->protocols()) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 2098 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2099 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 2100 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 2101 | // Insert the protocol. |
| 2102 | if (!Protocols.insert( |
| 2103 | const_cast<ObjCProtocolDecl *>(OP->getCanonicalDecl())).second) |
| 2104 | return; |
| 2105 | |
| 2106 | for (auto *Proto : OP->protocols()) |
| 2107 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2108 | } |
| 2109 | } |
| 2110 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2111 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 2112 | unsigned count = 0; |
| 2113 | // Count ivars declared in class extension. |
Aaron Ballman | b4a5345 | 2014-03-13 21:57:01 +0000 | [diff] [blame] | 2114 | for (const auto *Ext : OI->known_extensions()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2115 | count += Ext->ivar_size(); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2116 | |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 2117 | // Count ivar defined in this class's implementation. This |
| 2118 | // includes synthesized ivars. |
| 2119 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 2120 | count += ImplDecl->ivar_size(); |
| 2121 | |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2122 | return count; |
| 2123 | } |
| 2124 | |
Argyrios Kyrtzidis | 2e809ce | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 2125 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 2126 | if (!E) |
| 2127 | return false; |
| 2128 | |
| 2129 | // nullptr_t is always treated as null. |
| 2130 | if (E->getType()->isNullPtrType()) return true; |
| 2131 | |
| 2132 | if (E->getType()->isAnyPointerType() && |
| 2133 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 2134 | Expr::NPC_ValueDependentIsNull)) |
| 2135 | return true; |
| 2136 | |
| 2137 | // Unfortunately, __null has type 'int'. |
| 2138 | if (isa<GNUNullExpr>(E)) return true; |
| 2139 | |
| 2140 | return false; |
| 2141 | } |
| 2142 | |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2143 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 2144 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 2145 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 2146 | I = ObjCImpls.find(D); |
| 2147 | if (I != ObjCImpls.end()) |
| 2148 | return cast<ObjCImplementationDecl>(I->second); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2149 | return nullptr; |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2150 | } |
| 2151 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 2152 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 2153 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 2154 | I = ObjCImpls.find(D); |
| 2155 | if (I != ObjCImpls.end()) |
| 2156 | return cast<ObjCCategoryImplDecl>(I->second); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2157 | return nullptr; |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 2161 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 2162 | ObjCImplementationDecl *ImplD) { |
| 2163 | assert(IFaceD && ImplD && "Passed null params"); |
| 2164 | ObjCImpls[IFaceD] = ImplD; |
| 2165 | } |
| 2166 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 2167 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 2168 | ObjCCategoryImplDecl *ImplD) { |
| 2169 | assert(CatD && ImplD && "Passed null params"); |
| 2170 | ObjCImpls[CatD] = ImplD; |
| 2171 | } |
| 2172 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 2173 | const ObjCMethodDecl * |
| 2174 | ASTContext::getObjCMethodRedeclaration(const ObjCMethodDecl *MD) const { |
| 2175 | return ObjCMethodRedecls.lookup(MD); |
| 2176 | } |
| 2177 | |
| 2178 | void ASTContext::setObjCMethodRedeclaration(const ObjCMethodDecl *MD, |
| 2179 | const ObjCMethodDecl *Redecl) { |
| 2180 | assert(!getObjCMethodRedeclaration(MD) && "MD already has a redeclaration"); |
| 2181 | ObjCMethodRedecls[MD] = Redecl; |
| 2182 | } |
| 2183 | |
Dmitri Gribenko | 37527c2 | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 2184 | const ObjCInterfaceDecl *ASTContext::getObjContainingInterface( |
| 2185 | const NamedDecl *ND) const { |
| 2186 | if (const ObjCInterfaceDecl *ID = |
| 2187 | dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2188 | return ID; |
Dmitri Gribenko | 37527c2 | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 2189 | if (const ObjCCategoryDecl *CD = |
| 2190 | dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2191 | return CD->getClassInterface(); |
Dmitri Gribenko | 37527c2 | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 2192 | if (const ObjCImplDecl *IMD = |
| 2193 | dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2194 | return IMD->getClassInterface(); |
| 2195 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2196 | return nullptr; |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2199 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 2200 | /// none exists. |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 2201 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2202 | assert(VD && "Passed null params"); |
| 2203 | assert(VD->hasAttr<BlocksAttr>() && |
| 2204 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 2205 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2206 | I = BlockVarCopyInits.find(VD); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2207 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : nullptr; |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | /// \brief Set the copy inialization expression of a block var decl. |
| 2211 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 2212 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2213 | assert(VD->hasAttr<BlocksAttr>() && |
| 2214 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2215 | BlockVarCopyInits[VD] = Init; |
| 2216 | } |
| 2217 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2218 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2219 | unsigned DataSize) const { |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 2220 | if (!DataSize) |
| 2221 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 2222 | else |
| 2223 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2224 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 2225 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2226 | TypeSourceInfo *TInfo = |
| 2227 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 2228 | new (TInfo) TypeSourceInfo(T); |
| 2229 | return TInfo; |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2232 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2233 | SourceLocation L) const { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2234 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | 2d525f0 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2235 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | 3665e00 | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 2236 | return DI; |
| 2237 | } |
| 2238 | |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2239 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2240 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2241 | return getObjCLayout(D, nullptr); |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2245 | ASTContext::getASTObjCImplementationLayout( |
| 2246 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2247 | return getObjCLayout(D->getClassInterface(), D); |
| 2248 | } |
| 2249 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2250 | //===----------------------------------------------------------------------===// |
| 2251 | // Type creation/memoization methods |
| 2252 | //===----------------------------------------------------------------------===// |
| 2253 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2254 | QualType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2255 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 2256 | unsigned fastQuals = quals.getFastQualifiers(); |
| 2257 | quals.removeFastQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2258 | |
| 2259 | // Check if we've already instantiated this type. |
| 2260 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2261 | ExtQuals::Profile(ID, baseType, quals); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2262 | void *insertPos = nullptr; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2263 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 2264 | assert(eq->getQualifiers() == quals); |
| 2265 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2268 | // If the base type is not canonical, make the appropriate canonical type. |
| 2269 | QualType canon; |
| 2270 | if (!baseType->isCanonicalUnqualified()) { |
| 2271 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2272 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 2273 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2274 | |
| 2275 | // Re-find the insert position. |
| 2276 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 2277 | } |
| 2278 | |
| 2279 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 2280 | ExtQualNodes.InsertNode(eq, insertPos); |
| 2281 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2284 | QualType |
| 2285 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2286 | QualType CanT = getCanonicalType(T); |
| 2287 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | 445fcab | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2288 | return T; |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2289 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2290 | // If we are composing extended qualifiers together, merge together |
| 2291 | // into one ExtQuals node. |
| 2292 | QualifierCollector Quals; |
| 2293 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2294 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2295 | // If this type already has an address space specified, it cannot get |
| 2296 | // another one. |
| 2297 | assert(!Quals.hasAddressSpace() && |
| 2298 | "Type cannot be in multiple addr spaces!"); |
| 2299 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2300 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2301 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | 025b5fb | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2304 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2305 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2306 | QualType CanT = getCanonicalType(T); |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2307 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2308 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2309 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2310 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 2311 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 2312 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | b68215c | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 2313 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 2314 | return getPointerType(ResultType); |
| 2315 | } |
| 2316 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2317 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2318 | // If we are composing extended qualifiers together, merge together |
| 2319 | // into one ExtQuals node. |
| 2320 | QualifierCollector Quals; |
| 2321 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2322 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2323 | // If this type already has an ObjCGC specified, it cannot get |
| 2324 | // another one. |
| 2325 | assert(!Quals.hasObjCGCAttr() && |
| 2326 | "Type cannot have multiple ObjCGCs!"); |
| 2327 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2328 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2329 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2330 | } |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2331 | |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2332 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 2333 | FunctionType::ExtInfo Info) { |
| 2334 | if (T->getExtInfo() == Info) |
| 2335 | return T; |
| 2336 | |
| 2337 | QualType Result; |
| 2338 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2339 | Result = getFunctionNoProtoType(FNPT->getReturnType(), Info); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2340 | } else { |
| 2341 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 2342 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 2343 | EPI.ExtInfo = Info; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2344 | Result = getFunctionType(FPT->getReturnType(), FPT->getParamTypes(), EPI); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | return cast<FunctionType>(Result.getTypePtr()); |
| 2348 | } |
| 2349 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2350 | void ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD, |
| 2351 | QualType ResultType) { |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2352 | FD = FD->getMostRecentDecl(); |
| 2353 | while (true) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2354 | const FunctionProtoType *FPT = FD->getType()->castAs<FunctionProtoType>(); |
| 2355 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2356 | FD->setType(getFunctionType(ResultType, FPT->getParamTypes(), EPI)); |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2357 | if (FunctionDecl *Next = FD->getPreviousDecl()) |
| 2358 | FD = Next; |
| 2359 | else |
| 2360 | break; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2361 | } |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2362 | if (ASTMutationListener *L = getASTMutationListener()) |
| 2363 | L->DeducedReturnType(FD, ResultType); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2366 | /// Get a function type and produce the equivalent function type with the |
| 2367 | /// specified exception specification. Type sugar that can be present on a |
| 2368 | /// declaration of a function with an exception specification is permitted |
| 2369 | /// and preserved. Other type sugar (for instance, typedefs) is not. |
| 2370 | static QualType getFunctionTypeWithExceptionSpec( |
| 2371 | ASTContext &Context, QualType Orig, |
| 2372 | const FunctionProtoType::ExceptionSpecInfo &ESI) { |
| 2373 | // Might have some parens. |
| 2374 | if (auto *PT = dyn_cast<ParenType>(Orig)) |
| 2375 | return Context.getParenType( |
| 2376 | getFunctionTypeWithExceptionSpec(Context, PT->getInnerType(), ESI)); |
| 2377 | |
| 2378 | // Might have a calling-convention attribute. |
| 2379 | if (auto *AT = dyn_cast<AttributedType>(Orig)) |
| 2380 | return Context.getAttributedType( |
| 2381 | AT->getAttrKind(), |
| 2382 | getFunctionTypeWithExceptionSpec(Context, AT->getModifiedType(), ESI), |
| 2383 | getFunctionTypeWithExceptionSpec(Context, AT->getEquivalentType(), |
| 2384 | ESI)); |
| 2385 | |
| 2386 | // Anything else must be a function type. Rebuild it with the new exception |
| 2387 | // specification. |
| 2388 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Orig); |
| 2389 | return Context.getFunctionType( |
| 2390 | Proto->getReturnType(), Proto->getParamTypes(), |
| 2391 | Proto->getExtProtoInfo().withExceptionSpec(ESI)); |
| 2392 | } |
| 2393 | |
Richard Smith | dfe85e2 | 2016-12-15 02:35:39 +0000 | [diff] [blame] | 2394 | bool ASTContext::hasSameFunctionTypeIgnoringExceptionSpec(QualType T, |
| 2395 | QualType U) { |
| 2396 | return hasSameType(T, U) || |
| 2397 | (getLangOpts().CPlusPlus1z && |
| 2398 | hasSameType(getFunctionTypeWithExceptionSpec(*this, T, EST_None), |
| 2399 | getFunctionTypeWithExceptionSpec(*this, U, EST_None))); |
| 2400 | } |
| 2401 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2402 | void ASTContext::adjustExceptionSpec( |
| 2403 | FunctionDecl *FD, const FunctionProtoType::ExceptionSpecInfo &ESI, |
| 2404 | bool AsWritten) { |
| 2405 | // Update the type. |
| 2406 | QualType Updated = |
| 2407 | getFunctionTypeWithExceptionSpec(*this, FD->getType(), ESI); |
| 2408 | FD->setType(Updated); |
| 2409 | |
| 2410 | if (!AsWritten) |
| 2411 | return; |
| 2412 | |
| 2413 | // Update the type in the type source information too. |
| 2414 | if (TypeSourceInfo *TSInfo = FD->getTypeSourceInfo()) { |
| 2415 | // If the type and the type-as-written differ, we may need to update |
| 2416 | // the type-as-written too. |
| 2417 | if (TSInfo->getType() != FD->getType()) |
| 2418 | Updated = getFunctionTypeWithExceptionSpec(*this, TSInfo->getType(), ESI); |
| 2419 | |
| 2420 | // FIXME: When we get proper type location information for exceptions, |
| 2421 | // we'll also have to rebuild the TypeSourceInfo. For now, we just patch |
| 2422 | // up the TypeSourceInfo; |
| 2423 | assert(TypeLoc::getFullDataSizeForType(Updated) == |
| 2424 | TypeLoc::getFullDataSizeForType(TSInfo->getType()) && |
| 2425 | "TypeLoc size mismatch from updating exception specification"); |
| 2426 | TSInfo->overrideType(Updated); |
| 2427 | } |
| 2428 | } |
| 2429 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2430 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 2431 | /// number with the specified element type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2432 | QualType ASTContext::getComplexType(QualType T) const { |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2433 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2434 | // structure. |
| 2435 | llvm::FoldingSetNodeID ID; |
| 2436 | ComplexType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2437 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2438 | void *InsertPos = nullptr; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2439 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2440 | return QualType(CT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2441 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2442 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2443 | // so fill in the canonical type field. |
| 2444 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2445 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2446 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2447 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2448 | // Get the new insert position for the node we care about. |
| 2449 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2450 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2451 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2452 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2453 | Types.push_back(New); |
| 2454 | ComplexTypes.InsertNode(New, InsertPos); |
| 2455 | return QualType(New, 0); |
| 2456 | } |
| 2457 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 2458 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 2459 | /// the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2460 | QualType ASTContext::getPointerType(QualType T) const { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 2461 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2462 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2463 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2464 | PointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2465 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2466 | void *InsertPos = nullptr; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2467 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2468 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2469 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 2470 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2471 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2472 | QualType Canonical; |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2473 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2474 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2476 | // Get the new insert position for the node we care about. |
| 2477 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2478 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2479 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2480 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2481 | Types.push_back(New); |
| 2482 | PointerTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2483 | return QualType(New, 0); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2486 | QualType ASTContext::getAdjustedType(QualType Orig, QualType New) const { |
| 2487 | llvm::FoldingSetNodeID ID; |
| 2488 | AdjustedType::Profile(ID, Orig, New); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2489 | void *InsertPos = nullptr; |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2490 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2491 | if (AT) |
| 2492 | return QualType(AT, 0); |
| 2493 | |
| 2494 | QualType Canonical = getCanonicalType(New); |
| 2495 | |
| 2496 | // Get the new insert position for the node we care about. |
| 2497 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2498 | assert(!AT && "Shouldn't be in the map!"); |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2499 | |
| 2500 | AT = new (*this, TypeAlignment) |
| 2501 | AdjustedType(Type::Adjusted, Orig, New, Canonical); |
| 2502 | Types.push_back(AT); |
| 2503 | AdjustedTypes.InsertNode(AT, InsertPos); |
| 2504 | return QualType(AT, 0); |
| 2505 | } |
| 2506 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2507 | QualType ASTContext::getDecayedType(QualType T) const { |
| 2508 | assert((T->isArrayType() || T->isFunctionType()) && "T does not decay"); |
| 2509 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2510 | QualType Decayed; |
| 2511 | |
| 2512 | // C99 6.7.5.3p7: |
| 2513 | // A declaration of a parameter as "array of type" shall be |
| 2514 | // adjusted to "qualified pointer to type", where the type |
| 2515 | // qualifiers (if any) are those specified within the [ and ] of |
| 2516 | // the array type derivation. |
| 2517 | if (T->isArrayType()) |
| 2518 | Decayed = getArrayDecayedType(T); |
| 2519 | |
| 2520 | // C99 6.7.5.3p8: |
| 2521 | // A declaration of a parameter as "function returning type" |
| 2522 | // shall be adjusted to "pointer to function returning type", as |
| 2523 | // in 6.3.2.1. |
| 2524 | if (T->isFunctionType()) |
| 2525 | Decayed = getPointerType(T); |
| 2526 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2527 | llvm::FoldingSetNodeID ID; |
| 2528 | AdjustedType::Profile(ID, T, Decayed); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2529 | void *InsertPos = nullptr; |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2530 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2531 | if (AT) |
| 2532 | return QualType(AT, 0); |
| 2533 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2534 | QualType Canonical = getCanonicalType(Decayed); |
| 2535 | |
| 2536 | // Get the new insert position for the node we care about. |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2537 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2538 | assert(!AT && "Shouldn't be in the map!"); |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2539 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2540 | AT = new (*this, TypeAlignment) DecayedType(T, Decayed, Canonical); |
| 2541 | Types.push_back(AT); |
| 2542 | AdjustedTypes.InsertNode(AT, InsertPos); |
| 2543 | return QualType(AT, 0); |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2546 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2547 | /// a pointer to the specified block. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2548 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 0ac01283 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2549 | assert(T->isFunctionType() && "block of function types only"); |
| 2550 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2551 | // structure. |
| 2552 | llvm::FoldingSetNodeID ID; |
| 2553 | BlockPointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2554 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2555 | void *InsertPos = nullptr; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2556 | if (BlockPointerType *PT = |
| 2557 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2558 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2559 | |
| 2560 | // 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] | 2561 | // type either so fill in the canonical type field. |
| 2562 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2563 | if (!T.isCanonical()) { |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2564 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2565 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2566 | // Get the new insert position for the node we care about. |
| 2567 | BlockPointerType *NewIP = |
| 2568 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2569 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2570 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2571 | BlockPointerType *New |
| 2572 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2573 | Types.push_back(New); |
| 2574 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 2575 | return QualType(New, 0); |
| 2576 | } |
| 2577 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2578 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 2579 | /// lvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2580 | QualType |
| 2581 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 291e8ee | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 2582 | assert(getCanonicalType(T) != OverloadTy && |
| 2583 | "Unresolved overloaded function type"); |
| 2584 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2585 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2586 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2587 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2588 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2589 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2590 | void *InsertPos = nullptr; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2591 | if (LValueReferenceType *RT = |
| 2592 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2593 | return QualType(RT, 0); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2594 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2595 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2596 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2597 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2598 | // either, so fill in the canonical type field. |
| 2599 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2600 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 2601 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2602 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2603 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2604 | // Get the new insert position for the node we care about. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2605 | LValueReferenceType *NewIP = |
| 2606 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2607 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2610 | LValueReferenceType *New |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2611 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 2612 | SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2613 | Types.push_back(New); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2614 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2615 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2616 | return QualType(New, 0); |
| 2617 | } |
| 2618 | |
| 2619 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 2620 | /// rvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2621 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2622 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2623 | // structure. |
| 2624 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2625 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2626 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2627 | void *InsertPos = nullptr; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2628 | if (RValueReferenceType *RT = |
| 2629 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2630 | return QualType(RT, 0); |
| 2631 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2632 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2633 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2634 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2635 | // either, so fill in the canonical type field. |
| 2636 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2637 | if (InnerRef || !T.isCanonical()) { |
| 2638 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2639 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2640 | |
| 2641 | // Get the new insert position for the node we care about. |
| 2642 | RValueReferenceType *NewIP = |
| 2643 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2644 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2647 | RValueReferenceType *New |
| 2648 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2649 | Types.push_back(New); |
| 2650 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2651 | return QualType(New, 0); |
| 2652 | } |
| 2653 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2654 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 2655 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2656 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2657 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2658 | // structure. |
| 2659 | llvm::FoldingSetNodeID ID; |
| 2660 | MemberPointerType::Profile(ID, T, Cls); |
| 2661 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2662 | void *InsertPos = nullptr; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2663 | if (MemberPointerType *PT = |
| 2664 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2665 | return QualType(PT, 0); |
| 2666 | |
| 2667 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 2668 | // type either, so fill in the canonical type field. |
| 2669 | QualType Canonical; |
Douglas Gregor | 615ac67 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2670 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2671 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 2672 | |
| 2673 | // Get the new insert position for the node we care about. |
| 2674 | MemberPointerType *NewIP = |
| 2675 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2676 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2677 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2678 | MemberPointerType *New |
| 2679 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2680 | Types.push_back(New); |
| 2681 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 2682 | return QualType(New, 0); |
| 2683 | } |
| 2684 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2685 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2686 | /// array of the specified element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2687 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2688 | const llvm::APInt &ArySizeIn, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2689 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2690 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 2dfdb82 | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 2691 | assert((EltTy->isDependentType() || |
| 2692 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 2693 | "Constant array of VLAs is illegal!"); |
| 2694 | |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2695 | // Convert the array size into a canonical width matching the pointer size for |
| 2696 | // the target. |
| 2697 | llvm::APInt ArySize(ArySizeIn); |
Konstantin Zhuravlyov | 9c1e310 | 2017-03-21 18:55:39 +0000 | [diff] [blame] | 2698 | ArySize = ArySize.zextOrTrunc(Target->getMaxPointerWidth()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2699 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2700 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2701 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2703 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2704 | if (ConstantArrayType *ATP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2705 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2706 | return QualType(ATP, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2707 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2708 | // If the element type isn't canonical or has qualifiers, this won't |
| 2709 | // be a canonical type either, so fill in the canonical type field. |
| 2710 | QualType Canon; |
| 2711 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2712 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2713 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2714 | ASM, IndexTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2715 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2716 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 2717 | // Get the new insert position for the node we care about. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2718 | ConstantArrayType *NewIP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2719 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2720 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 2721 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2722 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2723 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2724 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2725 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 2726 | Types.push_back(New); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2727 | return QualType(New, 0); |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2730 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 2731 | /// variably-modified, into the corresponding type with all the known |
| 2732 | /// sizes replaced with [*]. |
| 2733 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 2734 | // Vastly most common case. |
| 2735 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2736 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2737 | QualType result; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2738 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2739 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2740 | const Type *ty = split.Ty; |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2741 | switch (ty->getTypeClass()) { |
| 2742 | #define TYPE(Class, Base) |
| 2743 | #define ABSTRACT_TYPE(Class, Base) |
| 2744 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2745 | #include "clang/AST/TypeNodes.def" |
| 2746 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 2747 | |
| 2748 | // These types should never be variably-modified. |
| 2749 | case Type::Builtin: |
| 2750 | case Type::Complex: |
| 2751 | case Type::Vector: |
| 2752 | case Type::ExtVector: |
| 2753 | case Type::DependentSizedExtVector: |
| 2754 | case Type::ObjCObject: |
| 2755 | case Type::ObjCInterface: |
| 2756 | case Type::ObjCObjectPointer: |
| 2757 | case Type::Record: |
| 2758 | case Type::Enum: |
| 2759 | case Type::UnresolvedUsing: |
| 2760 | case Type::TypeOfExpr: |
| 2761 | case Type::TypeOf: |
| 2762 | case Type::Decltype: |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2763 | case Type::UnaryTransform: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2764 | case Type::DependentName: |
| 2765 | case Type::InjectedClassName: |
| 2766 | case Type::TemplateSpecialization: |
| 2767 | case Type::DependentTemplateSpecialization: |
| 2768 | case Type::TemplateTypeParm: |
| 2769 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2770 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 2771 | case Type::DeducedTemplateSpecialization: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2772 | case Type::PackExpansion: |
| 2773 | llvm_unreachable("type should never be variably-modified"); |
| 2774 | |
| 2775 | // These types can be variably-modified but should never need to |
| 2776 | // further decay. |
| 2777 | case Type::FunctionNoProto: |
| 2778 | case Type::FunctionProto: |
| 2779 | case Type::BlockPointer: |
| 2780 | case Type::MemberPointer: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 2781 | case Type::Pipe: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2782 | return type; |
| 2783 | |
| 2784 | // These types can be variably-modified. All these modifications |
| 2785 | // preserve structure except as noted by comments. |
| 2786 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 2787 | // optimizations available here. |
| 2788 | case Type::Pointer: |
| 2789 | result = getPointerType(getVariableArrayDecayedType( |
| 2790 | cast<PointerType>(ty)->getPointeeType())); |
| 2791 | break; |
| 2792 | |
| 2793 | case Type::LValueReference: { |
| 2794 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 2795 | result = getLValueReferenceType( |
| 2796 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 2797 | lv->isSpelledAsLValue()); |
| 2798 | break; |
| 2799 | } |
| 2800 | |
| 2801 | case Type::RValueReference: { |
| 2802 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 2803 | result = getRValueReferenceType( |
| 2804 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 2805 | break; |
| 2806 | } |
| 2807 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2808 | case Type::Atomic: { |
| 2809 | const AtomicType *at = cast<AtomicType>(ty); |
| 2810 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 2811 | break; |
| 2812 | } |
| 2813 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2814 | case Type::ConstantArray: { |
| 2815 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 2816 | result = getConstantArrayType( |
| 2817 | getVariableArrayDecayedType(cat->getElementType()), |
| 2818 | cat->getSize(), |
| 2819 | cat->getSizeModifier(), |
| 2820 | cat->getIndexTypeCVRQualifiers()); |
| 2821 | break; |
| 2822 | } |
| 2823 | |
| 2824 | case Type::DependentSizedArray: { |
| 2825 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 2826 | result = getDependentSizedArrayType( |
| 2827 | getVariableArrayDecayedType(dat->getElementType()), |
| 2828 | dat->getSizeExpr(), |
| 2829 | dat->getSizeModifier(), |
| 2830 | dat->getIndexTypeCVRQualifiers(), |
| 2831 | dat->getBracketsRange()); |
| 2832 | break; |
| 2833 | } |
| 2834 | |
| 2835 | // Turn incomplete types into [*] types. |
| 2836 | case Type::IncompleteArray: { |
| 2837 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 2838 | result = getVariableArrayType( |
| 2839 | getVariableArrayDecayedType(iat->getElementType()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2840 | /*size*/ nullptr, |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2841 | ArrayType::Normal, |
| 2842 | iat->getIndexTypeCVRQualifiers(), |
| 2843 | SourceRange()); |
| 2844 | break; |
| 2845 | } |
| 2846 | |
| 2847 | // Turn VLA types into [*] types. |
| 2848 | case Type::VariableArray: { |
| 2849 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 2850 | result = getVariableArrayType( |
| 2851 | getVariableArrayDecayedType(vat->getElementType()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2852 | /*size*/ nullptr, |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2853 | ArrayType::Star, |
| 2854 | vat->getIndexTypeCVRQualifiers(), |
| 2855 | vat->getBracketsRange()); |
| 2856 | break; |
| 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | // Apply the top-level qualifiers from the original. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2861 | return getQualifiedType(result, split.Quals); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2862 | } |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2863 | |
Steve Naroff | cadebd0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 2864 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 2865 | /// variable array of the specified element type. |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2866 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 2867 | Expr *NumElts, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2868 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2869 | unsigned IndexTypeQuals, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2870 | SourceRange Brackets) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2871 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 2872 | // that have an expression provided for their size. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2873 | QualType Canon; |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2874 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2875 | // Be sure to pull qualifiers off the element type. |
| 2876 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2877 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2878 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2879 | IndexTypeQuals, Brackets); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2880 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2883 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2884 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2885 | |
| 2886 | VariableArrayTypes.push_back(New); |
| 2887 | Types.push_back(New); |
| 2888 | return QualType(New, 0); |
| 2889 | } |
| 2890 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2891 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 2892 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 2893 | /// type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2894 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 2895 | Expr *numElements, |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2896 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2897 | unsigned elementTypeQuals, |
| 2898 | SourceRange brackets) const { |
| 2899 | assert((!numElements || numElements->isTypeDependent() || |
| 2900 | numElements->isValueDependent()) && |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2901 | "Size must be type- or value-dependent!"); |
| 2902 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2903 | // Dependently-sized array types that do not have a specified number |
| 2904 | // of elements will have their sizes deduced from a dependent |
| 2905 | // initializer. We do no canonicalization here at all, which is okay |
| 2906 | // because they can't be used in most locations. |
| 2907 | if (!numElements) { |
| 2908 | DependentSizedArrayType *newType |
| 2909 | = new (*this, TypeAlignment) |
| 2910 | DependentSizedArrayType(*this, elementType, QualType(), |
| 2911 | numElements, ASM, elementTypeQuals, |
| 2912 | brackets); |
| 2913 | Types.push_back(newType); |
| 2914 | return QualType(newType, 0); |
| 2915 | } |
| 2916 | |
| 2917 | // Otherwise, we actually build a new type every time, but we |
| 2918 | // also build a canonical type. |
| 2919 | |
| 2920 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 2921 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2922 | void *insertPos = nullptr; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2923 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2924 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2925 | QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2926 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2927 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2928 | // Look for an existing type with these properties. |
| 2929 | DependentSizedArrayType *canonTy = |
| 2930 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2931 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2932 | // If we don't have one, build one. |
| 2933 | if (!canonTy) { |
| 2934 | canonTy = new (*this, TypeAlignment) |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2935 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2936 | QualType(), numElements, ASM, elementTypeQuals, |
| 2937 | brackets); |
| 2938 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 2939 | Types.push_back(canonTy); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2942 | // Apply qualifiers from the element type to the array. |
| 2943 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2944 | canonElementType.Quals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2945 | |
David Majnemer | 16a7470 | 2015-07-24 05:54:19 +0000 | [diff] [blame] | 2946 | // If we didn't need extra canonicalization for the element type or the size |
| 2947 | // expression, then just use that as our result. |
| 2948 | if (QualType(canonElementType.Ty, 0) == elementType && |
| 2949 | canonTy->getSizeExpr() == numElements) |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2950 | return canon; |
| 2951 | |
| 2952 | // Otherwise, we need to build a type which follows the spelling |
| 2953 | // of the element type. |
| 2954 | DependentSizedArrayType *sugaredType |
| 2955 | = new (*this, TypeAlignment) |
| 2956 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 2957 | ASM, elementTypeQuals, brackets); |
| 2958 | Types.push_back(sugaredType); |
| 2959 | return QualType(sugaredType, 0); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2962 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2963 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2964 | unsigned elementTypeQuals) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2965 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2966 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2967 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2968 | void *insertPos = nullptr; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2969 | if (IncompleteArrayType *iat = |
| 2970 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 2971 | return QualType(iat, 0); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2972 | |
| 2973 | // 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] | 2974 | // either, so fill in the canonical type field. We also have to pull |
| 2975 | // qualifiers off the element type. |
| 2976 | QualType canon; |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2977 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2978 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 2979 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2980 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2981 | ASM, elementTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2982 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2983 | |
| 2984 | // Get the new insert position for the node we care about. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2985 | IncompleteArrayType *existing = |
| 2986 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 2987 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 843ebedd | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 2988 | } |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2989 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2990 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 2991 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2992 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2993 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 2994 | Types.push_back(newType); |
| 2995 | return QualType(newType, 0); |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2998 | /// getVectorType - Return the unique reference to a vector type of |
| 2999 | /// 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] | 3000 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3001 | VectorType::VectorKind VecKind) const { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3002 | assert(vecType->isBuiltinType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3003 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3004 | // Check if we've already instantiated a vector of this type. |
| 3005 | llvm::FoldingSetNodeID ID; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 3006 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 3007 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3008 | void *InsertPos = nullptr; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3009 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3010 | return QualType(VTP, 0); |
| 3011 | |
| 3012 | // If the element type isn't canonical, this won't be a canonical type either, |
| 3013 | // so fill in the canonical type field. |
| 3014 | QualType Canonical; |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 3015 | if (!vecType.isCanonical()) { |
Bob Wilson | 7795480 | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 3016 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3017 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3018 | // Get the new insert position for the node we care about. |
| 3019 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3020 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3021 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3022 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 3023 | VectorType(vecType, NumElts, Canonical, VecKind); |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3024 | VectorTypes.InsertNode(New, InsertPos); |
| 3025 | Types.push_back(New); |
| 3026 | return QualType(New, 0); |
| 3027 | } |
| 3028 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3029 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3030 | /// 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] | 3031 | QualType |
| 3032 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 3033 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3034 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3035 | // Check if we've already instantiated a vector of this type. |
| 3036 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 3037 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 3038 | VectorType::GenericVector); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3039 | void *InsertPos = nullptr; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3040 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3041 | return QualType(VTP, 0); |
| 3042 | |
| 3043 | // If the element type isn't canonical, this won't be a canonical type either, |
| 3044 | // so fill in the canonical type field. |
| 3045 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3046 | if (!vecType.isCanonical()) { |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3047 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3048 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3049 | // Get the new insert position for the node we care about. |
| 3050 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3051 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3052 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3053 | ExtVectorType *New = new (*this, TypeAlignment) |
| 3054 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3055 | VectorTypes.InsertNode(New, InsertPos); |
| 3056 | Types.push_back(New); |
| 3057 | return QualType(New, 0); |
| 3058 | } |
| 3059 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3060 | QualType |
| 3061 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 3062 | Expr *SizeExpr, |
| 3063 | SourceLocation AttrLoc) const { |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3064 | llvm::FoldingSetNodeID ID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3065 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3066 | SizeExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3068 | void *InsertPos = nullptr; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3069 | DependentSizedExtVectorType *Canon |
| 3070 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3071 | DependentSizedExtVectorType *New; |
| 3072 | if (Canon) { |
| 3073 | // We already have a canonical version of this array type; use it as |
| 3074 | // the canonical type for a newly-built type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3075 | New = new (*this, TypeAlignment) |
| 3076 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 3077 | SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3078 | } else { |
| 3079 | QualType CanonVecTy = getCanonicalType(vecType); |
| 3080 | if (CanonVecTy == vecType) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3081 | New = new (*this, TypeAlignment) |
| 3082 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 3083 | AttrLoc); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3084 | |
| 3085 | DependentSizedExtVectorType *CanonCheck |
| 3086 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3087 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 3088 | (void)CanonCheck; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3089 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 3090 | } else { |
| 3091 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 3092 | SourceLocation()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3093 | New = new (*this, TypeAlignment) |
| 3094 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3095 | } |
| 3096 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3097 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 3098 | Types.push_back(New); |
| 3099 | return QualType(New, 0); |
| 3100 | } |
| 3101 | |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3102 | /// \brief Determine whether \p T is canonical as the result type of a function. |
| 3103 | static bool isCanonicalResultType(QualType T) { |
| 3104 | return T.isCanonical() && |
| 3105 | (T.getObjCLifetime() == Qualifiers::OCL_None || |
| 3106 | T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone); |
| 3107 | } |
| 3108 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3109 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3110 | /// |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3111 | QualType |
| 3112 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 3113 | const FunctionType::ExtInfo &Info) const { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3114 | // Unique functions, to guarantee there is only one function of a particular |
| 3115 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 3116 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 3117 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3118 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3119 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3120 | if (FunctionNoProtoType *FT = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3121 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3122 | return QualType(FT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3123 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3124 | QualType Canonical; |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3125 | if (!isCanonicalResultType(ResultTy)) { |
| 3126 | Canonical = |
| 3127 | getFunctionNoProtoType(getCanonicalFunctionResultType(ResultTy), Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3128 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 3129 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3130 | FunctionNoProtoType *NewIP = |
| 3131 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3132 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 3133 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3134 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3135 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3136 | FunctionNoProtoType(ResultTy, Canonical, Info); |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 3137 | Types.push_back(New); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3138 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3139 | return QualType(New, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
Douglas Gregor | a602a15 | 2015-10-01 20:20:47 +0000 | [diff] [blame] | 3142 | CanQualType |
| 3143 | ASTContext::getCanonicalFunctionResultType(QualType ResultType) const { |
| 3144 | CanQualType CanResultType = getCanonicalType(ResultType); |
| 3145 | |
| 3146 | // Canonical result types do not have ARC lifetime qualifiers. |
| 3147 | if (CanResultType.getQualifiers().hasObjCLifetime()) { |
| 3148 | Qualifiers Qs = CanResultType.getQualifiers(); |
| 3149 | Qs.removeObjCLifetime(); |
| 3150 | return CanQualType::CreateUnsafe( |
| 3151 | getQualifiedType(CanResultType.getUnqualifiedType(), Qs)); |
| 3152 | } |
| 3153 | |
| 3154 | return CanResultType; |
| 3155 | } |
| 3156 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3157 | static bool isCanonicalExceptionSpecification( |
| 3158 | const FunctionProtoType::ExceptionSpecInfo &ESI, bool NoexceptInType) { |
| 3159 | if (ESI.Type == EST_None) |
| 3160 | return true; |
| 3161 | if (!NoexceptInType) |
| 3162 | return false; |
| 3163 | |
| 3164 | // C++17 onwards: exception specification is part of the type, as a simple |
| 3165 | // boolean "can this function type throw". |
| 3166 | if (ESI.Type == EST_BasicNoexcept) |
| 3167 | return true; |
| 3168 | |
| 3169 | // A dynamic exception specification is canonical if it only contains pack |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3170 | // expansions (so we can't tell whether it's non-throwing) and all its |
| 3171 | // contained types are canonical. |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3172 | if (ESI.Type == EST_Dynamic) { |
Richard Smith | fda59e5 | 2016-10-26 01:05:54 +0000 | [diff] [blame] | 3173 | bool AnyPackExpansions = false; |
| 3174 | for (QualType ET : ESI.Exceptions) { |
| 3175 | if (!ET.isCanonical()) |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3176 | return false; |
Richard Smith | fda59e5 | 2016-10-26 01:05:54 +0000 | [diff] [blame] | 3177 | if (ET->getAs<PackExpansionType>()) |
| 3178 | AnyPackExpansions = true; |
| 3179 | } |
| 3180 | return AnyPackExpansions; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3183 | // A noexcept(expr) specification is (possibly) canonical if expr is |
| 3184 | // value-dependent. |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3185 | if (ESI.Type == EST_ComputedNoexcept) |
| 3186 | return ESI.NoexceptExpr && ESI.NoexceptExpr->isValueDependent(); |
| 3187 | |
| 3188 | return false; |
| 3189 | } |
| 3190 | |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3191 | QualType ASTContext::getFunctionTypeInternal( |
| 3192 | QualType ResultTy, ArrayRef<QualType> ArgArray, |
| 3193 | const FunctionProtoType::ExtProtoInfo &EPI, bool OnlyWantCanonical) const { |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3194 | size_t NumArgs = ArgArray.size(); |
| 3195 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3196 | // Unique functions, to guarantee there is only one function of a particular |
| 3197 | // structure. |
| 3198 | llvm::FoldingSetNodeID ID; |
| 3199 | FunctionProtoType::Profile(ID, ResultTy, ArgArray.begin(), NumArgs, EPI, |
| 3200 | *this, true); |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3201 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3202 | QualType Canonical; |
| 3203 | bool Unique = false; |
| 3204 | |
| 3205 | void *InsertPos = nullptr; |
| 3206 | if (FunctionProtoType *FPT = |
| 3207 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 3208 | QualType Existing = QualType(FPT, 0); |
| 3209 | |
| 3210 | // If we find a pre-existing equivalent FunctionProtoType, we can just reuse |
| 3211 | // it so long as our exception specification doesn't contain a dependent |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3212 | // noexcept expression, or we're just looking for a canonical type. |
| 3213 | // Otherwise, we're going to need to create a type |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3214 | // sugar node to hold the concrete expression. |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3215 | if (OnlyWantCanonical || EPI.ExceptionSpec.Type != EST_ComputedNoexcept || |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3216 | EPI.ExceptionSpec.NoexceptExpr == FPT->getNoexceptExpr()) |
| 3217 | return Existing; |
| 3218 | |
| 3219 | // We need a new type sugar node for this one, to hold the new noexcept |
| 3220 | // expression. We do no canonicalization here, but that's OK since we don't |
| 3221 | // expect to see the same noexcept expression much more than once. |
| 3222 | Canonical = getCanonicalType(Existing); |
| 3223 | Unique = true; |
| 3224 | } |
| 3225 | |
| 3226 | bool NoexceptInType = getLangOpts().CPlusPlus1z; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3227 | bool IsCanonicalExceptionSpec = |
| 3228 | isCanonicalExceptionSpecification(EPI.ExceptionSpec, NoexceptInType); |
| 3229 | |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3230 | // Determine whether the type being created is already canonical or not. |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3231 | bool isCanonical = !Unique && IsCanonicalExceptionSpec && |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3232 | isCanonicalResultType(ResultTy) && !EPI.HasTrailingReturn; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3233 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3234 | if (!ArgArray[i].isCanonicalAsParam()) |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3235 | isCanonical = false; |
| 3236 | |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3237 | if (OnlyWantCanonical) |
| 3238 | assert(isCanonical && |
| 3239 | "given non-canonical parameters constructing canonical type"); |
| 3240 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3241 | // If this type isn't canonical, get the canonical version of it if we don't |
| 3242 | // already have it. The exception spec is only partially part of the |
| 3243 | // canonical type, and only in C++17 onwards. |
| 3244 | if (!isCanonical && Canonical.isNull()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3245 | SmallVector<QualType, 16> CanonicalArgs; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3246 | CanonicalArgs.reserve(NumArgs); |
| 3247 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3248 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3249 | |
Benjamin Kramer | 3f515cd | 2016-10-26 12:51:45 +0000 | [diff] [blame] | 3250 | llvm::SmallVector<QualType, 8> ExceptionTypeStorage; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3251 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 3252 | CanonicalEPI.HasTrailingReturn = false; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3253 | |
| 3254 | if (IsCanonicalExceptionSpec) { |
| 3255 | // Exception spec is already OK. |
| 3256 | } else if (NoexceptInType) { |
| 3257 | switch (EPI.ExceptionSpec.Type) { |
| 3258 | case EST_Unparsed: case EST_Unevaluated: case EST_Uninstantiated: |
| 3259 | // We don't know yet. It shouldn't matter what we pick here; no-one |
| 3260 | // should ever look at this. |
| 3261 | LLVM_FALLTHROUGH; |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3262 | case EST_None: case EST_MSAny: |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3263 | CanonicalEPI.ExceptionSpec.Type = EST_None; |
| 3264 | break; |
| 3265 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3266 | // A dynamic exception specification is almost always "not noexcept", |
| 3267 | // with the exception that a pack expansion might expand to no types. |
| 3268 | case EST_Dynamic: { |
| 3269 | bool AnyPacks = false; |
| 3270 | for (QualType ET : EPI.ExceptionSpec.Exceptions) { |
| 3271 | if (ET->getAs<PackExpansionType>()) |
| 3272 | AnyPacks = true; |
| 3273 | ExceptionTypeStorage.push_back(getCanonicalType(ET)); |
| 3274 | } |
| 3275 | if (!AnyPacks) |
| 3276 | CanonicalEPI.ExceptionSpec.Type = EST_None; |
| 3277 | else { |
| 3278 | CanonicalEPI.ExceptionSpec.Type = EST_Dynamic; |
| 3279 | CanonicalEPI.ExceptionSpec.Exceptions = ExceptionTypeStorage; |
| 3280 | } |
| 3281 | break; |
| 3282 | } |
| 3283 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3284 | case EST_DynamicNone: case EST_BasicNoexcept: |
| 3285 | CanonicalEPI.ExceptionSpec.Type = EST_BasicNoexcept; |
| 3286 | break; |
| 3287 | |
| 3288 | case EST_ComputedNoexcept: |
| 3289 | llvm::APSInt Value(1); |
| 3290 | auto *E = CanonicalEPI.ExceptionSpec.NoexceptExpr; |
| 3291 | if (!E || !E->isIntegerConstantExpr(Value, *this, nullptr, |
| 3292 | /*IsEvaluated*/false)) { |
| 3293 | // This noexcept specification is invalid. |
| 3294 | // FIXME: Should this be able to happen? |
| 3295 | CanonicalEPI.ExceptionSpec.Type = EST_None; |
| 3296 | break; |
| 3297 | } |
| 3298 | |
| 3299 | CanonicalEPI.ExceptionSpec.Type = |
| 3300 | Value.getBoolValue() ? EST_BasicNoexcept : EST_None; |
| 3301 | break; |
| 3302 | } |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3303 | } else { |
| 3304 | CanonicalEPI.ExceptionSpec = FunctionProtoType::ExceptionSpecInfo(); |
| 3305 | } |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3306 | |
Douglas Gregor | a602a15 | 2015-10-01 20:20:47 +0000 | [diff] [blame] | 3307 | // Adjust the canonical function result type. |
| 3308 | CanQualType CanResultTy = getCanonicalFunctionResultType(ResultTy); |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3309 | Canonical = |
| 3310 | getFunctionTypeInternal(CanResultTy, CanonicalArgs, CanonicalEPI, true); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3311 | |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 3312 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3313 | FunctionProtoType *NewIP = |
| 3314 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3315 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3316 | } |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3317 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3318 | // FunctionProtoType objects are allocated with extra bytes after |
| 3319 | // them for three variable size arrays at the end: |
| 3320 | // - parameter types |
| 3321 | // - exception types |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3322 | // - extended parameter information |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3323 | // Instead of the exception types, there could be a noexcept |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3324 | // expression, or information used to resolve the exception |
| 3325 | // specification. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3326 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3327 | NumArgs * sizeof(QualType); |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3328 | |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3329 | if (EPI.ExceptionSpec.Type == EST_Dynamic) { |
| 3330 | Size += EPI.ExceptionSpec.Exceptions.size() * sizeof(QualType); |
| 3331 | } else if (EPI.ExceptionSpec.Type == EST_ComputedNoexcept) { |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 3332 | Size += sizeof(Expr*); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3333 | } else if (EPI.ExceptionSpec.Type == EST_Uninstantiated) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3334 | Size += 2 * sizeof(FunctionDecl*); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3335 | } else if (EPI.ExceptionSpec.Type == EST_Unevaluated) { |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3336 | Size += sizeof(FunctionDecl*); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3337 | } |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3338 | |
| 3339 | // Put the ExtParameterInfos last. If all were equal, it would make |
| 3340 | // more sense to put these before the exception specification, because |
| 3341 | // it's much easier to skip past them compared to the elaborate switch |
| 3342 | // required to skip the exception specification. However, all is not |
| 3343 | // equal; ExtParameterInfos are used to model very uncommon features, |
| 3344 | // and it's better not to burden the more common paths. |
| 3345 | if (EPI.ExtParameterInfos) { |
| 3346 | Size += NumArgs * sizeof(FunctionProtoType::ExtParameterInfo); |
| 3347 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3348 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3349 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 3350 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3351 | new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3352 | Types.push_back(FTP); |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3353 | if (!Unique) |
| 3354 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3355 | return QualType(FTP, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3356 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 3357 | |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3358 | QualType ASTContext::getPipeType(QualType T, bool ReadOnly) const { |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3359 | llvm::FoldingSetNodeID ID; |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3360 | PipeType::Profile(ID, T, ReadOnly); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3361 | |
| 3362 | void *InsertPos = 0; |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3363 | if (PipeType *PT = PipeTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3364 | return QualType(PT, 0); |
| 3365 | |
| 3366 | // If the pipe element type isn't canonical, this won't be a canonical type |
| 3367 | // either, so fill in the canonical type field. |
| 3368 | QualType Canonical; |
| 3369 | if (!T.isCanonical()) { |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3370 | Canonical = getPipeType(getCanonicalType(T), ReadOnly); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3371 | |
| 3372 | // Get the new insert position for the node we care about. |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3373 | PipeType *NewIP = PipeTypes.FindNodeOrInsertPos(ID, InsertPos); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3374 | assert(!NewIP && "Shouldn't be in the map!"); |
| 3375 | (void)NewIP; |
| 3376 | } |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3377 | PipeType *New = new (*this, TypeAlignment) PipeType(T, Canonical, ReadOnly); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3378 | Types.push_back(New); |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3379 | PipeTypes.InsertNode(New, InsertPos); |
Joey Gouly | 5788b78 | 2016-11-18 14:10:54 +0000 | [diff] [blame] | 3380 | return QualType(New, 0); |
| 3381 | } |
| 3382 | |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3383 | QualType ASTContext::getReadPipeType(QualType T) const { |
| 3384 | return getPipeType(T, true); |
| 3385 | } |
| 3386 | |
Joey Gouly | 5788b78 | 2016-11-18 14:10:54 +0000 | [diff] [blame] | 3387 | QualType ASTContext::getWritePipeType(QualType T) const { |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3388 | return getPipeType(T, false); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3391 | #ifndef NDEBUG |
| 3392 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 3393 | if (!isa<CXXRecordDecl>(D)) return false; |
| 3394 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 3395 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 3396 | return true; |
| 3397 | if (RD->getDescribedClassTemplate() && |
| 3398 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 3399 | return true; |
| 3400 | return false; |
| 3401 | } |
| 3402 | #endif |
| 3403 | |
| 3404 | /// getInjectedClassNameType - Return the unique reference to the |
| 3405 | /// injected class name type for the specified templated declaration. |
| 3406 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3407 | QualType TST) const { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3408 | assert(NeedsInjectedClassNameType(Decl)); |
| 3409 | if (Decl->TypeForDecl) { |
| 3410 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3411 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3412 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 3413 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3414 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 3415 | } else { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3416 | Type *newType = |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3417 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3418 | Decl->TypeForDecl = newType; |
| 3419 | Types.push_back(newType); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3420 | } |
| 3421 | return QualType(Decl->TypeForDecl, 0); |
| 3422 | } |
| 3423 | |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3424 | /// getTypeDeclType - Return the unique reference to the type for the |
| 3425 | /// specified type declaration. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3426 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 89656d2 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 3427 | assert(Decl && "Passed null for Decl param"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3428 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3429 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3430 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3431 | return getTypedefType(Typedef); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3432 | |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3433 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 3434 | "Template type parameter types are always available."); |
| 3435 | |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 3436 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 3437 | assert(Record->isFirstDecl() && "struct/union has previous declaration"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3438 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3439 | return getRecordType(Record); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 3440 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 3441 | assert(Enum->isFirstDecl() && "enum has previous declaration"); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3442 | return getEnumType(Enum); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 3443 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3444 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3445 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 3446 | Decl->TypeForDecl = newType; |
| 3447 | Types.push_back(newType); |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3448 | } else |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3449 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 3450 | |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 3451 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3452 | } |
| 3453 | |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 3454 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3455 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3456 | QualType |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3457 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 3458 | QualType Canonical) const { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3459 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3460 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3461 | if (Canonical.isNull()) |
| 3462 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3463 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3464 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3465 | Decl->TypeForDecl = newType; |
| 3466 | Types.push_back(newType); |
| 3467 | return QualType(newType, 0); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3470 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3471 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 3472 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3473 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3474 | if (PrevDecl->TypeForDecl) |
| 3475 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 3476 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3477 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 3478 | Decl->TypeForDecl = newType; |
| 3479 | Types.push_back(newType); |
| 3480 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3483 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3484 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 3485 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3486 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3487 | if (PrevDecl->TypeForDecl) |
| 3488 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 3489 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3490 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 3491 | Decl->TypeForDecl = newType; |
| 3492 | Types.push_back(newType); |
| 3493 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 3496 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 3497 | QualType modifiedType, |
| 3498 | QualType equivalentType) { |
| 3499 | llvm::FoldingSetNodeID id; |
| 3500 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 3501 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3502 | void *insertPos = nullptr; |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 3503 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 3504 | if (type) return QualType(type, 0); |
| 3505 | |
| 3506 | QualType canon = getCanonicalType(equivalentType); |
| 3507 | type = new (*this, TypeAlignment) |
| 3508 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 3509 | |
| 3510 | Types.push_back(type); |
| 3511 | AttributedTypes.InsertNode(type, insertPos); |
| 3512 | |
| 3513 | return QualType(type, 0); |
| 3514 | } |
| 3515 | |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3516 | /// \brief Retrieve a substitution-result type. |
| 3517 | QualType |
| 3518 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3519 | QualType Replacement) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3520 | assert(Replacement.isCanonical() |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3521 | && "replacement types must always be canonical"); |
| 3522 | |
| 3523 | llvm::FoldingSetNodeID ID; |
| 3524 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3525 | void *InsertPos = nullptr; |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3526 | SubstTemplateTypeParmType *SubstParm |
| 3527 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3528 | |
| 3529 | if (!SubstParm) { |
| 3530 | SubstParm = new (*this, TypeAlignment) |
| 3531 | SubstTemplateTypeParmType(Parm, Replacement); |
| 3532 | Types.push_back(SubstParm); |
| 3533 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 3534 | } |
| 3535 | |
| 3536 | return QualType(SubstParm, 0); |
| 3537 | } |
| 3538 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3539 | /// \brief Retrieve a |
| 3540 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 3541 | const TemplateTypeParmType *Parm, |
| 3542 | const TemplateArgument &ArgPack) { |
| 3543 | #ifndef NDEBUG |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 3544 | for (const auto &P : ArgPack.pack_elements()) { |
| 3545 | assert(P.getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 3546 | assert(P.getAsType().isCanonical() && "Pack contains non-canonical type"); |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3547 | } |
| 3548 | #endif |
| 3549 | |
| 3550 | llvm::FoldingSetNodeID ID; |
| 3551 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3552 | void *InsertPos = nullptr; |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3553 | if (SubstTemplateTypeParmPackType *SubstParm |
| 3554 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3555 | return QualType(SubstParm, 0); |
| 3556 | |
| 3557 | QualType Canon; |
| 3558 | if (!Parm->isCanonicalUnqualified()) { |
| 3559 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 3560 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 3561 | ArgPack); |
| 3562 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3563 | } |
| 3564 | |
| 3565 | SubstTemplateTypeParmPackType *SubstParm |
| 3566 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 3567 | ArgPack); |
| 3568 | Types.push_back(SubstParm); |
| 3569 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 3570 | return QualType(SubstParm, 0); |
| 3571 | } |
| 3572 | |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3573 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3574 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3575 | /// name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3576 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3577 | bool ParameterPack, |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3578 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3579 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3580 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3581 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3582 | TemplateTypeParmType *TypeParm |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3583 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3584 | |
| 3585 | if (TypeParm) |
| 3586 | return QualType(TypeParm, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3587 | |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3588 | if (TTPDecl) { |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3589 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3590 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3591 | |
| 3592 | TemplateTypeParmType *TypeCheck |
| 3593 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3594 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 3595 | (void)TypeCheck; |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3596 | } else |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3597 | TypeParm = new (*this, TypeAlignment) |
| 3598 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3599 | |
| 3600 | Types.push_back(TypeParm); |
| 3601 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 3602 | |
| 3603 | return QualType(TypeParm, 0); |
| 3604 | } |
| 3605 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3606 | TypeSourceInfo * |
| 3607 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 3608 | SourceLocation NameLoc, |
| 3609 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3610 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3611 | assert(!Name.getAsDependentTemplateName() && |
| 3612 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3613 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3614 | |
| 3615 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3616 | TemplateSpecializationTypeLoc TL = |
| 3617 | DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>(); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3618 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3619 | TL.setTemplateNameLoc(NameLoc); |
| 3620 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 3621 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 3622 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 3623 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 3624 | return DI; |
| 3625 | } |
| 3626 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3627 | QualType |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3628 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3629 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3630 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3631 | assert(!Template.getAsDependentTemplateName() && |
| 3632 | "No dependent template names here!"); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3633 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3634 | SmallVector<TemplateArgument, 4> ArgVec; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3635 | ArgVec.reserve(Args.size()); |
| 3636 | for (const TemplateArgumentLoc &Arg : Args.arguments()) |
| 3637 | ArgVec.push_back(Arg.getArgument()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3638 | |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3639 | return getTemplateSpecializationType(Template, ArgVec, Underlying); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3642 | #ifndef NDEBUG |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3643 | static bool hasAnyPackExpansions(ArrayRef<TemplateArgument> Args) { |
| 3644 | for (const TemplateArgument &Arg : Args) |
| 3645 | if (Arg.isPackExpansion()) |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3646 | return true; |
| 3647 | |
| 3648 | return true; |
| 3649 | } |
| 3650 | #endif |
| 3651 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3652 | QualType |
| 3653 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3654 | ArrayRef<TemplateArgument> Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3655 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3656 | assert(!Template.getAsDependentTemplateName() && |
| 3657 | "No dependent template names here!"); |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3658 | // Look through qualified template names. |
| 3659 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3660 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3661 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3662 | bool IsTypeAlias = |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3663 | Template.getAsTemplateDecl() && |
| 3664 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3665 | QualType CanonType; |
| 3666 | if (!Underlying.isNull()) |
| 3667 | CanonType = getCanonicalType(Underlying); |
| 3668 | else { |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3669 | // We can get here with an alias template when the specialization contains |
| 3670 | // a pack expansion that does not match up with a parameter pack. |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3671 | assert((!IsTypeAlias || hasAnyPackExpansions(Args)) && |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3672 | "Caller must compute aliased type"); |
| 3673 | IsTypeAlias = false; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3674 | CanonType = getCanonicalTemplateSpecializationType(Template, Args); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3675 | } |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3676 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3677 | // Allocate the (non-canonical) template specialization type, but don't |
| 3678 | // try to unique it: these types typically have location information that |
| 3679 | // we don't unique and don't want to lose. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3680 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3681 | sizeof(TemplateArgument) * Args.size() + |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3682 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3683 | TypeAlignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | TemplateSpecializationType *Spec |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3685 | = new (Mem) TemplateSpecializationType(Template, Args, CanonType, |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3686 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3687 | |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3688 | Types.push_back(Spec); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3689 | return QualType(Spec, 0); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3692 | QualType ASTContext::getCanonicalTemplateSpecializationType( |
| 3693 | TemplateName Template, ArrayRef<TemplateArgument> Args) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3694 | assert(!Template.getAsDependentTemplateName() && |
| 3695 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3696 | |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3697 | // Look through qualified template names. |
| 3698 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3699 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3700 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3701 | // Build the canonical template specialization type. |
| 3702 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3703 | SmallVector<TemplateArgument, 4> CanonArgs; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3704 | unsigned NumArgs = Args.size(); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3705 | CanonArgs.reserve(NumArgs); |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3706 | for (const TemplateArgument &Arg : Args) |
| 3707 | CanonArgs.push_back(getCanonicalTemplateArgument(Arg)); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3708 | |
| 3709 | // Determine whether this canonical template specialization type already |
| 3710 | // exists. |
| 3711 | llvm::FoldingSetNodeID ID; |
| 3712 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3713 | CanonArgs, *this); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3714 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3715 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3716 | TemplateSpecializationType *Spec |
| 3717 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3718 | |
| 3719 | if (!Spec) { |
| 3720 | // Allocate a new canonical template specialization type. |
| 3721 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 3722 | sizeof(TemplateArgument) * NumArgs), |
| 3723 | TypeAlignment); |
| 3724 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3725 | CanonArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3726 | QualType(), QualType()); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3727 | Types.push_back(Spec); |
| 3728 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 3729 | } |
| 3730 | |
| 3731 | assert(Spec->isDependentType() && |
| 3732 | "Non-dependent template-id type must have a canonical type"); |
| 3733 | return QualType(Spec, 0); |
| 3734 | } |
| 3735 | |
| 3736 | QualType |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3737 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 3738 | NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3739 | QualType NamedType) const { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3740 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3741 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3742 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3743 | void *InsertPos = nullptr; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3744 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3745 | if (T) |
| 3746 | return QualType(T, 0); |
| 3747 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3748 | QualType Canon = NamedType; |
| 3749 | if (!Canon.isCanonical()) { |
| 3750 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3751 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3752 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3753 | (void)CheckT; |
| 3754 | } |
| 3755 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 3756 | T = new (*this, TypeAlignment) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3757 | Types.push_back(T); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3758 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3759 | return QualType(T, 0); |
| 3760 | } |
| 3761 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3762 | QualType |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3763 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3764 | llvm::FoldingSetNodeID ID; |
| 3765 | ParenType::Profile(ID, InnerType); |
| 3766 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3767 | void *InsertPos = nullptr; |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3768 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3769 | if (T) |
| 3770 | return QualType(T, 0); |
| 3771 | |
| 3772 | QualType Canon = InnerType; |
| 3773 | if (!Canon.isCanonical()) { |
| 3774 | Canon = getCanonicalType(InnerType); |
| 3775 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3776 | assert(!CheckT && "Paren canonical type broken"); |
| 3777 | (void)CheckT; |
| 3778 | } |
| 3779 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 3780 | T = new (*this, TypeAlignment) ParenType(InnerType, Canon); |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3781 | Types.push_back(T); |
| 3782 | ParenTypes.InsertNode(T, InsertPos); |
| 3783 | return QualType(T, 0); |
| 3784 | } |
| 3785 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3786 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 3787 | NestedNameSpecifier *NNS, |
| 3788 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3789 | QualType Canon) const { |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3790 | if (Canon.isNull()) { |
| 3791 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 3792 | if (CanonNNS != NNS) |
| 3793 | Canon = getDependentNameType(Keyword, CanonNNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3797 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3798 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3799 | void *InsertPos = nullptr; |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3800 | DependentNameType *T |
| 3801 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3802 | if (T) |
| 3803 | return QualType(T, 0); |
| 3804 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 3805 | T = new (*this, TypeAlignment) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3806 | Types.push_back(T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3807 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3808 | return QualType(T, 0); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3811 | QualType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3812 | ASTContext::getDependentTemplateSpecializationType( |
| 3813 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3814 | NestedNameSpecifier *NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3815 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3816 | const TemplateArgumentListInfo &Args) const { |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3817 | // TODO: avoid this copy |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3818 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3819 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 3820 | ArgCopy.push_back(Args[I].getArgument()); |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3821 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, ArgCopy); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | QualType |
| 3825 | ASTContext::getDependentTemplateSpecializationType( |
| 3826 | ElaboratedTypeKeyword Keyword, |
| 3827 | NestedNameSpecifier *NNS, |
| 3828 | const IdentifierInfo *Name, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3829 | ArrayRef<TemplateArgument> Args) const { |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 3830 | assert((!NNS || NNS->isDependent()) && |
| 3831 | "nested-name-specifier must be dependent"); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3832 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3833 | llvm::FoldingSetNodeID ID; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3834 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3835 | Name, Args); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3836 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3837 | void *InsertPos = nullptr; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3838 | DependentTemplateSpecializationType *T |
| 3839 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3840 | if (T) |
| 3841 | return QualType(T, 0); |
| 3842 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3843 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3844 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3845 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3846 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 3847 | |
| 3848 | bool AnyNonCanonArgs = false; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3849 | unsigned NumArgs = Args.size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3850 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3851 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 3852 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 3853 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 3854 | AnyNonCanonArgs = true; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3855 | } |
| 3856 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3857 | QualType Canon; |
| 3858 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 3859 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3860 | Name, |
| 3861 | CanonArgs); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3862 | |
| 3863 | // Find the insert position again. |
| 3864 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3865 | } |
| 3866 | |
| 3867 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 3868 | sizeof(TemplateArgument) * NumArgs), |
| 3869 | TypeAlignment); |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 3870 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3871 | Name, Args, Canon); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3872 | Types.push_back(T); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3873 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3874 | return QualType(T, 0); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3877 | TemplateArgument ASTContext::getInjectedTemplateArg(NamedDecl *Param) { |
| 3878 | TemplateArgument Arg; |
| 3879 | if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 3880 | QualType ArgType = getTypeDeclType(TTP); |
| 3881 | if (TTP->isParameterPack()) |
| 3882 | ArgType = getPackExpansionType(ArgType, None); |
| 3883 | |
| 3884 | Arg = TemplateArgument(ArgType); |
| 3885 | } else if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 3886 | Expr *E = new (*this) DeclRefExpr( |
| 3887 | NTTP, /*enclosing*/false, |
| 3888 | NTTP->getType().getNonLValueExprType(*this), |
| 3889 | Expr::getValueKindForType(NTTP->getType()), NTTP->getLocation()); |
| 3890 | |
| 3891 | if (NTTP->isParameterPack()) |
| 3892 | E = new (*this) PackExpansionExpr(DependentTy, E, NTTP->getLocation(), |
| 3893 | None); |
| 3894 | Arg = TemplateArgument(E); |
| 3895 | } else { |
| 3896 | auto *TTP = cast<TemplateTemplateParmDecl>(Param); |
| 3897 | if (TTP->isParameterPack()) |
| 3898 | Arg = TemplateArgument(TemplateName(TTP), Optional<unsigned>()); |
| 3899 | else |
| 3900 | Arg = TemplateArgument(TemplateName(TTP)); |
| 3901 | } |
| 3902 | |
| 3903 | if (Param->isTemplateParameterPack()) |
| 3904 | Arg = TemplateArgument::CreatePackCopy(*this, Arg); |
| 3905 | |
| 3906 | return Arg; |
| 3907 | } |
| 3908 | |
Richard Smith | 43e14d2 | 2016-12-23 02:10:11 +0000 | [diff] [blame] | 3909 | void |
| 3910 | ASTContext::getInjectedTemplateArgs(const TemplateParameterList *Params, |
| 3911 | SmallVectorImpl<TemplateArgument> &Args) { |
| 3912 | Args.reserve(Args.size() + Params->size()); |
| 3913 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3914 | for (NamedDecl *Param : *Params) |
| 3915 | Args.push_back(getInjectedTemplateArg(Param)); |
Richard Smith | 43e14d2 | 2016-12-23 02:10:11 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3918 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3919 | Optional<unsigned> NumExpansions) { |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3920 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3921 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3922 | |
| 3923 | assert(Pattern->containsUnexpandedParameterPack() && |
| 3924 | "Pack expansions must expand one or more parameter packs"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3925 | void *InsertPos = nullptr; |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3926 | PackExpansionType *T |
| 3927 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3928 | if (T) |
| 3929 | return QualType(T, 0); |
| 3930 | |
| 3931 | QualType Canon; |
| 3932 | if (!Pattern.isCanonical()) { |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3933 | Canon = getCanonicalType(Pattern); |
| 3934 | // The canonical type might not contain an unexpanded parameter pack, if it |
| 3935 | // contains an alias template specialization which ignores one of its |
| 3936 | // parameters. |
| 3937 | if (Canon->containsUnexpandedParameterPack()) { |
Richard Smith | 8b4e1e2 | 2014-07-10 01:20:17 +0000 | [diff] [blame] | 3938 | Canon = getPackExpansionType(Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3939 | |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3940 | // Find the insert position again, in case we inserted an element into |
| 3941 | // PackExpansionTypes and invalidated our insert position. |
| 3942 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3943 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 3946 | T = new (*this, TypeAlignment) |
| 3947 | PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3948 | Types.push_back(T); |
| 3949 | PackExpansionTypes.InsertNode(T, InsertPos); |
Richard Smith | 8b4e1e2 | 2014-07-10 01:20:17 +0000 | [diff] [blame] | 3950 | return QualType(T, 0); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3951 | } |
| 3952 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3953 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 3954 | /// alphabetically. |
Benjamin Kramer | 0eb262f | 2015-03-14 13:32:49 +0000 | [diff] [blame] | 3955 | static int CmpProtocolNames(ObjCProtocolDecl *const *LHS, |
| 3956 | ObjCProtocolDecl *const *RHS) { |
| 3957 | return DeclarationName::compare((*LHS)->getDeclName(), (*RHS)->getDeclName()); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
Craig Topper | 1f26d5b | 2016-01-03 19:43:23 +0000 | [diff] [blame] | 3960 | static bool areSortedAndUniqued(ArrayRef<ObjCProtocolDecl *> Protocols) { |
| 3961 | if (Protocols.empty()) return true; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3962 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3963 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 3964 | return false; |
| 3965 | |
Craig Topper | 1f26d5b | 2016-01-03 19:43:23 +0000 | [diff] [blame] | 3966 | for (unsigned i = 1; i != Protocols.size(); ++i) |
Benjamin Kramer | 0eb262f | 2015-03-14 13:32:49 +0000 | [diff] [blame] | 3967 | if (CmpProtocolNames(&Protocols[i - 1], &Protocols[i]) >= 0 || |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3968 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3969 | return false; |
| 3970 | return true; |
| 3971 | } |
| 3972 | |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 3973 | static void |
| 3974 | SortAndUniqueProtocols(SmallVectorImpl<ObjCProtocolDecl *> &Protocols) { |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3975 | // Sort protocols, keyed by name. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 3976 | llvm::array_pod_sort(Protocols.begin(), Protocols.end(), CmpProtocolNames); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3977 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3978 | // Canonicalize. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 3979 | for (ObjCProtocolDecl *&P : Protocols) |
| 3980 | P = P->getCanonicalDecl(); |
| 3981 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3982 | // Remove duplicates. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 3983 | auto ProtocolsEnd = std::unique(Protocols.begin(), Protocols.end()); |
| 3984 | Protocols.erase(ProtocolsEnd, Protocols.end()); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3985 | } |
| 3986 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3987 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 3988 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3989 | unsigned NumProtocols) const { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3990 | return getObjCObjectType(BaseType, { }, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3991 | llvm::makeArrayRef(Protocols, NumProtocols), |
| 3992 | /*isKindOf=*/false); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 3993 | } |
| 3994 | |
| 3995 | QualType ASTContext::getObjCObjectType( |
| 3996 | QualType baseType, |
| 3997 | ArrayRef<QualType> typeArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 3998 | ArrayRef<ObjCProtocolDecl *> protocols, |
| 3999 | bool isKindOf) const { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4000 | // If the base type is an interface and there aren't any protocols or |
| 4001 | // type arguments to add, then the interface type will do just fine. |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4002 | if (typeArgs.empty() && protocols.empty() && !isKindOf && |
| 4003 | isa<ObjCInterfaceType>(baseType)) |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4004 | return baseType; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4005 | |
| 4006 | // Look in the folding set for an existing type. |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4007 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4008 | ObjCObjectTypeImpl::Profile(ID, baseType, typeArgs, protocols, isKindOf); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4009 | void *InsertPos = nullptr; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4010 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4011 | return QualType(QT, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4012 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4013 | // Determine the type arguments to be used for canonicalization, |
| 4014 | // which may be explicitly specified here or written on the base |
| 4015 | // type. |
| 4016 | ArrayRef<QualType> effectiveTypeArgs = typeArgs; |
| 4017 | if (effectiveTypeArgs.empty()) { |
| 4018 | if (auto baseObject = baseType->getAs<ObjCObjectType>()) |
| 4019 | effectiveTypeArgs = baseObject->getTypeArgs(); |
| 4020 | } |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4021 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4022 | // Build the canonical type, which has the canonical base type and a |
| 4023 | // sorted-and-uniqued list of protocols and the type arguments |
| 4024 | // canonicalized. |
| 4025 | QualType canonical; |
| 4026 | bool typeArgsAreCanonical = std::all_of(effectiveTypeArgs.begin(), |
| 4027 | effectiveTypeArgs.end(), |
| 4028 | [&](QualType type) { |
| 4029 | return type.isCanonical(); |
| 4030 | }); |
Craig Topper | 1f26d5b | 2016-01-03 19:43:23 +0000 | [diff] [blame] | 4031 | bool protocolsSorted = areSortedAndUniqued(protocols); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4032 | if (!typeArgsAreCanonical || !protocolsSorted || !baseType.isCanonical()) { |
| 4033 | // Determine the canonical type arguments. |
| 4034 | ArrayRef<QualType> canonTypeArgs; |
| 4035 | SmallVector<QualType, 4> canonTypeArgsVec; |
| 4036 | if (!typeArgsAreCanonical) { |
| 4037 | canonTypeArgsVec.reserve(effectiveTypeArgs.size()); |
| 4038 | for (auto typeArg : effectiveTypeArgs) |
| 4039 | canonTypeArgsVec.push_back(getCanonicalType(typeArg)); |
| 4040 | canonTypeArgs = canonTypeArgsVec; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4041 | } else { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4042 | canonTypeArgs = effectiveTypeArgs; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4043 | } |
| 4044 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4045 | ArrayRef<ObjCProtocolDecl *> canonProtocols; |
| 4046 | SmallVector<ObjCProtocolDecl*, 8> canonProtocolsVec; |
| 4047 | if (!protocolsSorted) { |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4048 | canonProtocolsVec.append(protocols.begin(), protocols.end()); |
| 4049 | SortAndUniqueProtocols(canonProtocolsVec); |
| 4050 | canonProtocols = canonProtocolsVec; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4051 | } else { |
| 4052 | canonProtocols = protocols; |
| 4053 | } |
| 4054 | |
| 4055 | canonical = getObjCObjectType(getCanonicalType(baseType), canonTypeArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4056 | canonProtocols, isKindOf); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4057 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4058 | // Regenerate InsertPos. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4059 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4060 | } |
| 4061 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4062 | unsigned size = sizeof(ObjCObjectTypeImpl); |
| 4063 | size += typeArgs.size() * sizeof(QualType); |
| 4064 | size += protocols.size() * sizeof(ObjCProtocolDecl *); |
| 4065 | void *mem = Allocate(size, TypeAlignment); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4066 | ObjCObjectTypeImpl *T = |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4067 | new (mem) ObjCObjectTypeImpl(canonical, baseType, typeArgs, protocols, |
| 4068 | isKindOf); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4069 | |
| 4070 | Types.push_back(T); |
| 4071 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 4072 | return QualType(T, 0); |
| 4073 | } |
| 4074 | |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4075 | /// Apply Objective-C protocol qualifiers to the given type. |
| 4076 | /// If this is for the canonical type of a type parameter, we can apply |
| 4077 | /// protocol qualifiers on the ObjCObjectPointerType. |
| 4078 | QualType |
| 4079 | ASTContext::applyObjCProtocolQualifiers(QualType type, |
| 4080 | ArrayRef<ObjCProtocolDecl *> protocols, bool &hasError, |
| 4081 | bool allowOnPointerType) const { |
| 4082 | hasError = false; |
| 4083 | |
Manman Ren | c5705ba | 2016-09-13 17:41:05 +0000 | [diff] [blame] | 4084 | if (const ObjCTypeParamType *objT = |
| 4085 | dyn_cast<ObjCTypeParamType>(type.getTypePtr())) { |
| 4086 | return getObjCTypeParamType(objT->getDecl(), protocols); |
| 4087 | } |
| 4088 | |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4089 | // Apply protocol qualifiers to ObjCObjectPointerType. |
| 4090 | if (allowOnPointerType) { |
| 4091 | if (const ObjCObjectPointerType *objPtr = |
| 4092 | dyn_cast<ObjCObjectPointerType>(type.getTypePtr())) { |
| 4093 | const ObjCObjectType *objT = objPtr->getObjectType(); |
| 4094 | // Merge protocol lists and construct ObjCObjectType. |
| 4095 | SmallVector<ObjCProtocolDecl*, 8> protocolsVec; |
| 4096 | protocolsVec.append(objT->qual_begin(), |
| 4097 | objT->qual_end()); |
| 4098 | protocolsVec.append(protocols.begin(), protocols.end()); |
| 4099 | ArrayRef<ObjCProtocolDecl *> protocols = protocolsVec; |
| 4100 | type = getObjCObjectType( |
| 4101 | objT->getBaseType(), |
| 4102 | objT->getTypeArgsAsWritten(), |
| 4103 | protocols, |
| 4104 | objT->isKindOfTypeAsWritten()); |
| 4105 | return getObjCObjectPointerType(type); |
| 4106 | } |
| 4107 | } |
| 4108 | |
| 4109 | // Apply protocol qualifiers to ObjCObjectType. |
| 4110 | if (const ObjCObjectType *objT = dyn_cast<ObjCObjectType>(type.getTypePtr())){ |
| 4111 | // FIXME: Check for protocols to which the class type is already |
| 4112 | // known to conform. |
| 4113 | |
| 4114 | return getObjCObjectType(objT->getBaseType(), |
| 4115 | objT->getTypeArgsAsWritten(), |
| 4116 | protocols, |
| 4117 | objT->isKindOfTypeAsWritten()); |
| 4118 | } |
| 4119 | |
| 4120 | // If the canonical type is ObjCObjectType, ... |
| 4121 | if (type->isObjCObjectType()) { |
| 4122 | // Silently overwrite any existing protocol qualifiers. |
| 4123 | // TODO: determine whether that's the right thing to do. |
| 4124 | |
| 4125 | // FIXME: Check for protocols to which the class type is already |
| 4126 | // known to conform. |
| 4127 | return getObjCObjectType(type, { }, protocols, false); |
| 4128 | } |
| 4129 | |
| 4130 | // id<protocol-list> |
| 4131 | if (type->isObjCIdType()) { |
| 4132 | const ObjCObjectPointerType *objPtr = type->castAs<ObjCObjectPointerType>(); |
| 4133 | type = getObjCObjectType(ObjCBuiltinIdTy, { }, protocols, |
| 4134 | objPtr->isKindOfType()); |
| 4135 | return getObjCObjectPointerType(type); |
| 4136 | } |
| 4137 | |
| 4138 | // Class<protocol-list> |
| 4139 | if (type->isObjCClassType()) { |
| 4140 | const ObjCObjectPointerType *objPtr = type->castAs<ObjCObjectPointerType>(); |
| 4141 | type = getObjCObjectType(ObjCBuiltinClassTy, { }, protocols, |
| 4142 | objPtr->isKindOfType()); |
| 4143 | return getObjCObjectPointerType(type); |
| 4144 | } |
| 4145 | |
| 4146 | hasError = true; |
| 4147 | return type; |
| 4148 | } |
| 4149 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 4150 | QualType |
| 4151 | ASTContext::getObjCTypeParamType(const ObjCTypeParamDecl *Decl, |
| 4152 | ArrayRef<ObjCProtocolDecl *> protocols, |
| 4153 | QualType Canonical) const { |
| 4154 | // Look in the folding set for an existing type. |
| 4155 | llvm::FoldingSetNodeID ID; |
| 4156 | ObjCTypeParamType::Profile(ID, Decl, protocols); |
| 4157 | void *InsertPos = nullptr; |
| 4158 | if (ObjCTypeParamType *TypeParam = |
| 4159 | ObjCTypeParamTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4160 | return QualType(TypeParam, 0); |
| 4161 | |
| 4162 | if (Canonical.isNull()) { |
| 4163 | // We canonicalize to the underlying type. |
| 4164 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
| 4165 | if (!protocols.empty()) { |
| 4166 | // Apply the protocol qualifers. |
| 4167 | bool hasError; |
| 4168 | Canonical = applyObjCProtocolQualifiers(Canonical, protocols, hasError, |
| 4169 | true/*allowOnPointerType*/); |
| 4170 | assert(!hasError && "Error when apply protocol qualifier to bound type"); |
| 4171 | } |
| 4172 | } |
| 4173 | |
| 4174 | unsigned size = sizeof(ObjCTypeParamType); |
| 4175 | size += protocols.size() * sizeof(ObjCProtocolDecl *); |
| 4176 | void *mem = Allocate(size, TypeAlignment); |
| 4177 | ObjCTypeParamType *newType = new (mem) |
| 4178 | ObjCTypeParamType(Decl, Canonical, protocols); |
| 4179 | |
| 4180 | Types.push_back(newType); |
| 4181 | ObjCTypeParamTypes.InsertNode(newType, InsertPos); |
| 4182 | return QualType(newType, 0); |
| 4183 | } |
| 4184 | |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4185 | /// ObjCObjectAdoptsQTypeProtocols - Checks that protocols in IC's |
| 4186 | /// protocol list adopt all protocols in QT's qualified-id protocol |
| 4187 | /// list. |
| 4188 | bool ASTContext::ObjCObjectAdoptsQTypeProtocols(QualType QT, |
| 4189 | ObjCInterfaceDecl *IC) { |
| 4190 | if (!QT->isObjCQualifiedIdType()) |
| 4191 | return false; |
| 4192 | |
| 4193 | if (const ObjCObjectPointerType *OPT = QT->getAs<ObjCObjectPointerType>()) { |
| 4194 | // If both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4195 | for (auto *Proto : OPT->quals()) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4196 | if (!IC->ClassImplementsProtocol(Proto, false)) |
| 4197 | return false; |
| 4198 | } |
| 4199 | return true; |
| 4200 | } |
| 4201 | return false; |
| 4202 | } |
| 4203 | |
| 4204 | /// QIdProtocolsAdoptObjCObjectProtocols - Checks that protocols in |
| 4205 | /// QT's qualified-id protocol list adopt all protocols in IDecl's list |
| 4206 | /// of protocols. |
| 4207 | bool ASTContext::QIdProtocolsAdoptObjCObjectProtocols(QualType QT, |
| 4208 | ObjCInterfaceDecl *IDecl) { |
| 4209 | if (!QT->isObjCQualifiedIdType()) |
| 4210 | return false; |
| 4211 | const ObjCObjectPointerType *OPT = QT->getAs<ObjCObjectPointerType>(); |
| 4212 | if (!OPT) |
| 4213 | return false; |
| 4214 | if (!IDecl->hasDefinition()) |
| 4215 | return false; |
Fariborz Jahanian | 91fb0be | 2013-11-20 19:01:50 +0000 | [diff] [blame] | 4216 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocols; |
| 4217 | CollectInheritedProtocols(IDecl, InheritedProtocols); |
| 4218 | if (InheritedProtocols.empty()) |
| 4219 | return false; |
Fariborz Jahanian | 48a01cb | 2014-04-04 23:53:45 +0000 | [diff] [blame] | 4220 | // Check that if every protocol in list of id<plist> conforms to a protcol |
| 4221 | // of IDecl's, then bridge casting is ok. |
| 4222 | bool Conforms = false; |
| 4223 | for (auto *Proto : OPT->quals()) { |
| 4224 | Conforms = false; |
| 4225 | for (auto *PI : InheritedProtocols) { |
| 4226 | if (ProtocolCompatibleWithProtocol(Proto, PI)) { |
| 4227 | Conforms = true; |
| 4228 | break; |
| 4229 | } |
| 4230 | } |
| 4231 | if (!Conforms) |
| 4232 | break; |
| 4233 | } |
| 4234 | if (Conforms) |
| 4235 | return true; |
| 4236 | |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4237 | for (auto *PI : InheritedProtocols) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4238 | // If both the right and left sides have qualifiers. |
| 4239 | bool Adopts = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4240 | for (auto *Proto : OPT->quals()) { |
Fariborz Jahanian | 48a01cb | 2014-04-04 23:53:45 +0000 | [diff] [blame] | 4241 | // return 'true' if 'PI' is in the inheritance hierarchy of Proto |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4242 | if ((Adopts = ProtocolCompatibleWithProtocol(PI, Proto))) |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4243 | break; |
| 4244 | } |
| 4245 | if (!Adopts) |
Fariborz Jahanian | 91fb0be | 2013-11-20 19:01:50 +0000 | [diff] [blame] | 4246 | return false; |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4247 | } |
| 4248 | return true; |
| 4249 | } |
| 4250 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4251 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 4252 | /// the given object type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4253 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4254 | llvm::FoldingSetNodeID ID; |
| 4255 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 4256 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4257 | void *InsertPos = nullptr; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4258 | if (ObjCObjectPointerType *QT = |
| 4259 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4260 | return QualType(QT, 0); |
| 4261 | |
| 4262 | // Find the canonical object type. |
| 4263 | QualType Canonical; |
| 4264 | if (!ObjectT.isCanonical()) { |
| 4265 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 4266 | |
| 4267 | // Regenerate InsertPos. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4268 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4269 | } |
| 4270 | |
Douglas Gregor | f85bee6 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 4271 | // No match. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4272 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 4273 | ObjCObjectPointerType *QType = |
| 4274 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4275 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4276 | Types.push_back(QType); |
| 4277 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4278 | return QualType(QType, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4279 | } |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4280 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4281 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 4282 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 4283 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 4284 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4285 | if (Decl->TypeForDecl) |
| 4286 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4287 | |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 4288 | if (PrevDecl) { |
| 4289 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 4290 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 4291 | return QualType(PrevDecl->TypeForDecl, 0); |
| 4292 | } |
| 4293 | |
Douglas Gregor | 7671e53 | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 4294 | // Prefer the definition, if there is one. |
| 4295 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 4296 | Decl = Def; |
| 4297 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4298 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 4299 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 4300 | Decl->TypeForDecl = T; |
| 4301 | Types.push_back(T); |
| 4302 | return QualType(T, 0); |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 4303 | } |
| 4304 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4305 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 4306 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4307 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4308 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4309 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4310 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4311 | TypeOfExprType *toe; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4312 | if (tofExpr->isTypeDependent()) { |
| 4313 | llvm::FoldingSetNodeID ID; |
| 4314 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4315 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4316 | void *InsertPos = nullptr; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4317 | DependentTypeOfExprType *Canon |
| 4318 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4319 | if (Canon) { |
| 4320 | // We already have a "canonical" version of an identical, dependent |
| 4321 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4322 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4323 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 4324 | } else { |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4325 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4326 | Canon |
| 4327 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4328 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 4329 | toe = Canon; |
| 4330 | } |
| 4331 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4332 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4333 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4334 | } |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4335 | Types.push_back(toe); |
| 4336 | return QualType(toe, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4337 | } |
| 4338 | |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4339 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4340 | /// TypeOfType nodes. The only motivation to unique these nodes would be |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4341 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4342 | /// an issue. This doesn't affect the type checker, since it operates |
| 4343 | /// on canonical types (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4344 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4345 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4346 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4347 | Types.push_back(tot); |
| 4348 | return QualType(tot, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4351 | /// \brief Unlike many "get<Type>" functions, we don't unique DecltypeType |
| 4352 | /// nodes. This would never be helpful, since each such type has its own |
| 4353 | /// expression, and would not give a significant memory saving, since there |
| 4354 | /// is an Expr tree under each such type. |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 4355 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4356 | DecltypeType *dt; |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4357 | |
| 4358 | // C++11 [temp.type]p2: |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4359 | // If an expression e involves a template parameter, decltype(e) denotes a |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4360 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 4361 | // type only if their expressions are equivalent (14.5.6.1). |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4362 | if (e->isInstantiationDependent()) { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4363 | llvm::FoldingSetNodeID ID; |
| 4364 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4365 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4366 | void *InsertPos = nullptr; |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4367 | DependentDecltypeType *Canon |
| 4368 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4369 | if (!Canon) { |
Yaron Keren | 633e14a | 2016-11-29 10:08:20 +0000 | [diff] [blame] | 4370 | // Build a new, canonical decltype(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4371 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4372 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4373 | } |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4374 | dt = new (*this, TypeAlignment) |
| 4375 | DecltypeType(e, UnderlyingType, QualType((DecltypeType *)Canon, 0)); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4376 | } else { |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4377 | dt = new (*this, TypeAlignment) |
| 4378 | DecltypeType(e, UnderlyingType, getCanonicalType(UnderlyingType)); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4379 | } |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 4380 | Types.push_back(dt); |
| 4381 | return QualType(dt, 0); |
| 4382 | } |
| 4383 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4384 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 4385 | /// savings are minimal and these are rare. |
| 4386 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 4387 | QualType UnderlyingType, |
| 4388 | UnaryTransformType::UTTKind Kind) |
| 4389 | const { |
Vassil Vassilev | bab6f96 | 2016-03-30 22:18:29 +0000 | [diff] [blame] | 4390 | UnaryTransformType *ut = nullptr; |
| 4391 | |
| 4392 | if (BaseType->isDependentType()) { |
| 4393 | // Look in the folding set for an existing type. |
| 4394 | llvm::FoldingSetNodeID ID; |
| 4395 | DependentUnaryTransformType::Profile(ID, getCanonicalType(BaseType), Kind); |
| 4396 | |
| 4397 | void *InsertPos = nullptr; |
| 4398 | DependentUnaryTransformType *Canon |
| 4399 | = DependentUnaryTransformTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4400 | |
| 4401 | if (!Canon) { |
| 4402 | // Build a new, canonical __underlying_type(type) type. |
| 4403 | Canon = new (*this, TypeAlignment) |
| 4404 | DependentUnaryTransformType(*this, getCanonicalType(BaseType), |
| 4405 | Kind); |
| 4406 | DependentUnaryTransformTypes.InsertNode(Canon, InsertPos); |
| 4407 | } |
| 4408 | ut = new (*this, TypeAlignment) UnaryTransformType (BaseType, |
| 4409 | QualType(), Kind, |
| 4410 | QualType(Canon, 0)); |
| 4411 | } else { |
| 4412 | QualType CanonType = getCanonicalType(UnderlyingType); |
| 4413 | ut = new (*this, TypeAlignment) UnaryTransformType (BaseType, |
| 4414 | UnderlyingType, Kind, |
| 4415 | CanonType); |
| 4416 | } |
| 4417 | Types.push_back(ut); |
| 4418 | return QualType(ut, 0); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4419 | } |
| 4420 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4421 | /// getAutoType - Return the uniqued reference to the 'auto' type which has been |
| 4422 | /// deduced to the given type, or to the canonical undeduced 'auto' type, or the |
| 4423 | /// canonical deduced-but-dependent 'auto' type. |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4424 | QualType ASTContext::getAutoType(QualType DeducedType, AutoTypeKeyword Keyword, |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4425 | bool IsDependent) const { |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4426 | if (DeducedType.isNull() && Keyword == AutoTypeKeyword::Auto && !IsDependent) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4427 | return getAutoDeductType(); |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4428 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4429 | // Look in the folding set for an existing type. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4430 | void *InsertPos = nullptr; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4431 | llvm::FoldingSetNodeID ID; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4432 | AutoType::Profile(ID, DeducedType, Keyword, IsDependent); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4433 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4434 | return QualType(AT, 0); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4435 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4436 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType, |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4437 | Keyword, |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4438 | IsDependent); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4439 | Types.push_back(AT); |
| 4440 | if (InsertPos) |
| 4441 | AutoTypes.InsertNode(AT, InsertPos); |
| 4442 | return QualType(AT, 0); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4443 | } |
| 4444 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 4445 | /// Return the uniqued reference to the deduced template specialization type |
| 4446 | /// which has been deduced to the given type, or to the canonical undeduced |
| 4447 | /// such type, or the canonical deduced-but-dependent such type. |
| 4448 | QualType ASTContext::getDeducedTemplateSpecializationType( |
| 4449 | TemplateName Template, QualType DeducedType, bool IsDependent) const { |
| 4450 | // Look in the folding set for an existing type. |
| 4451 | void *InsertPos = nullptr; |
| 4452 | llvm::FoldingSetNodeID ID; |
| 4453 | DeducedTemplateSpecializationType::Profile(ID, Template, DeducedType, |
| 4454 | IsDependent); |
| 4455 | if (DeducedTemplateSpecializationType *DTST = |
| 4456 | DeducedTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4457 | return QualType(DTST, 0); |
| 4458 | |
| 4459 | DeducedTemplateSpecializationType *DTST = new (*this, TypeAlignment) |
| 4460 | DeducedTemplateSpecializationType(Template, DeducedType, IsDependent); |
| 4461 | Types.push_back(DTST); |
| 4462 | if (InsertPos) |
| 4463 | DeducedTemplateSpecializationTypes.InsertNode(DTST, InsertPos); |
| 4464 | return QualType(DTST, 0); |
| 4465 | } |
| 4466 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4467 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 4468 | /// the given value type. |
| 4469 | QualType ASTContext::getAtomicType(QualType T) const { |
| 4470 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 4471 | // structure. |
| 4472 | llvm::FoldingSetNodeID ID; |
| 4473 | AtomicType::Profile(ID, T); |
| 4474 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4475 | void *InsertPos = nullptr; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4476 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4477 | return QualType(AT, 0); |
| 4478 | |
| 4479 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 4480 | // either, so fill in the canonical type field. |
| 4481 | QualType Canonical; |
| 4482 | if (!T.isCanonical()) { |
| 4483 | Canonical = getAtomicType(getCanonicalType(T)); |
| 4484 | |
| 4485 | // Get the new insert position for the node we care about. |
| 4486 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4487 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4488 | } |
| 4489 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 4490 | Types.push_back(New); |
| 4491 | AtomicTypes.InsertNode(New, InsertPos); |
| 4492 | return QualType(New, 0); |
| 4493 | } |
| 4494 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 4495 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 4496 | QualType ASTContext::getAutoDeductType() const { |
| 4497 | if (AutoDeductTy.isNull()) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4498 | AutoDeductTy = QualType( |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4499 | new (*this, TypeAlignment) AutoType(QualType(), AutoTypeKeyword::Auto, |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4500 | /*dependent*/false), |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4501 | 0); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 4502 | return AutoDeductTy; |
| 4503 | } |
| 4504 | |
| 4505 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 4506 | QualType ASTContext::getAutoRRefDeductType() const { |
| 4507 | if (AutoRRefDeductTy.isNull()) |
| 4508 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 4509 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 4510 | return AutoRRefDeductTy; |
| 4511 | } |
| 4512 | |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 4513 | /// getTagDeclType - Return the unique reference to the type for the |
| 4514 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4515 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | 2b0ce11 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 4516 | assert (Decl); |
Mike Stump | b93185d | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 4517 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 4518 | // away const? mutable? |
| 4519 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 4520 | } |
| 4521 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4522 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 4523 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 4524 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4525 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4526 | return getFromTargetType(Target->getSizeType()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 4527 | } |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 4528 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 4529 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 4530 | CanQualType ASTContext::getIntMaxType() const { |
| 4531 | return getFromTargetType(Target->getIntMaxType()); |
| 4532 | } |
| 4533 | |
| 4534 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 4535 | CanQualType ASTContext::getUIntMaxType() const { |
| 4536 | return getFromTargetType(Target->getUIntMaxType()); |
| 4537 | } |
| 4538 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4539 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 4540 | /// Used when in C++, as a GCC extension. |
| 4541 | QualType ASTContext::getSignedWCharType() const { |
| 4542 | // FIXME: derive from "Target" ? |
| 4543 | return WCharTy; |
| 4544 | } |
| 4545 | |
| 4546 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 4547 | /// Used when in C++, as a GCC extension. |
| 4548 | QualType ASTContext::getUnsignedWCharType() const { |
| 4549 | // FIXME: derive from "Target" ? |
| 4550 | return UnsignedIntTy; |
| 4551 | } |
| 4552 | |
Enea Zaffanella | f11ceb6 | 2013-01-26 17:08:37 +0000 | [diff] [blame] | 4553 | QualType ASTContext::getIntPtrType() const { |
| 4554 | return getFromTargetType(Target->getIntPtrType()); |
| 4555 | } |
| 4556 | |
| 4557 | QualType ASTContext::getUIntPtrType() const { |
| 4558 | return getCorrespondingUnsignedType(getIntPtrType()); |
| 4559 | } |
| 4560 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 4561 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 4562 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 4563 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4564 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 4565 | } |
| 4566 | |
Eli Friedman | 4e91899e | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 4567 | /// \brief Return the unique type for "pid_t" defined in |
| 4568 | /// <sys/types.h>. We need this to compute the correct type for vfork(). |
| 4569 | QualType ASTContext::getProcessIDType() const { |
| 4570 | return getFromTargetType(Target->getProcessIDType()); |
| 4571 | } |
| 4572 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4573 | //===----------------------------------------------------------------------===// |
| 4574 | // Type Operators |
| 4575 | //===----------------------------------------------------------------------===// |
| 4576 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4577 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4578 | // Push qualifiers into arrays, and then discard any remaining |
| 4579 | // qualifiers. |
| 4580 | T = getCanonicalType(T); |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4581 | T = getVariableArrayDecayedType(T); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4582 | const Type *Ty = T.getTypePtr(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4583 | QualType Result; |
| 4584 | if (isa<ArrayType>(Ty)) { |
| 4585 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 4586 | } else if (isa<FunctionType>(Ty)) { |
| 4587 | Result = getPointerType(QualType(Ty, 0)); |
| 4588 | } else { |
| 4589 | Result = QualType(Ty, 0); |
| 4590 | } |
| 4591 | |
| 4592 | return CanQualType::CreateUnsafe(Result); |
| 4593 | } |
| 4594 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4595 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 4596 | Qualifiers &quals) { |
| 4597 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 4598 | |
| 4599 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 4600 | // the unqualified desugared type and then drops it on the floor. |
| 4601 | // We then have to strip that sugar back off with |
| 4602 | // getUnqualifiedDesugaredType(), which is silly. |
| 4603 | const ArrayType *AT = |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4604 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4605 | |
| 4606 | // 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] | 4607 | if (!AT) { |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4608 | quals = splitType.Quals; |
| 4609 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4610 | } |
| 4611 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4612 | // Otherwise, recurse on the array's element type. |
| 4613 | QualType elementType = AT->getElementType(); |
| 4614 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 4615 | |
| 4616 | // If that didn't change the element type, AT has no qualifiers, so we |
| 4617 | // can just use the results in splitType. |
| 4618 | if (elementType == unqualElementType) { |
| 4619 | assert(quals.empty()); // from the recursive call |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4620 | quals = splitType.Quals; |
| 4621 | return QualType(splitType.Ty, 0); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | // Otherwise, add in the qualifiers from the outermost type, then |
| 4625 | // build the type back up. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4626 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4627 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 4628 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4629 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4630 | CAT->getSizeModifier(), 0); |
| 4631 | } |
| 4632 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 4633 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4634 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 4637 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4638 | return getVariableArrayType(unqualElementType, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4639 | VAT->getSizeExpr(), |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 4640 | VAT->getSizeModifier(), |
| 4641 | VAT->getIndexTypeCVRQualifiers(), |
| 4642 | VAT->getBracketsRange()); |
| 4643 | } |
| 4644 | |
| 4645 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4646 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4647 | DSAT->getSizeModifier(), 0, |
| 4648 | SourceRange()); |
| 4649 | } |
| 4650 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 4651 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 4652 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 4653 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 4654 | /// or pointer-to-member types, or if they are not similar at this |
| 4655 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 4656 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 4657 | /// be called in a loop that successively "unwraps" pointer and |
| 4658 | /// pointer-to-member types to compare them at each level. |
| 4659 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 4660 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 4661 | *T2PtrType = T2->getAs<PointerType>(); |
| 4662 | if (T1PtrType && T2PtrType) { |
| 4663 | T1 = T1PtrType->getPointeeType(); |
| 4664 | T2 = T2PtrType->getPointeeType(); |
| 4665 | return true; |
| 4666 | } |
| 4667 | |
| 4668 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 4669 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 4670 | if (T1MPType && T2MPType && |
| 4671 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 4672 | QualType(T2MPType->getClass(), 0))) { |
| 4673 | T1 = T1MPType->getPointeeType(); |
| 4674 | T2 = T2MPType->getPointeeType(); |
| 4675 | return true; |
| 4676 | } |
| 4677 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4678 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 4679 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 4680 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 4681 | if (T1OPType && T2OPType) { |
| 4682 | T1 = T1OPType->getPointeeType(); |
| 4683 | T2 = T2OPType->getPointeeType(); |
| 4684 | return true; |
| 4685 | } |
| 4686 | } |
| 4687 | |
| 4688 | // FIXME: Block pointers, too? |
| 4689 | |
| 4690 | return false; |
| 4691 | } |
| 4692 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4693 | DeclarationNameInfo |
| 4694 | ASTContext::getNameForTemplate(TemplateName Name, |
| 4695 | SourceLocation NameLoc) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4696 | switch (Name.getKind()) { |
| 4697 | case TemplateName::QualifiedTemplate: |
| 4698 | case TemplateName::Template: |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4699 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4700 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 4701 | NameLoc); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4702 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4703 | case TemplateName::OverloadedTemplate: { |
| 4704 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 4705 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 4706 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 4707 | } |
| 4708 | |
| 4709 | case TemplateName::DependentTemplate: { |
| 4710 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4711 | DeclarationName DName; |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 4712 | if (DTN->isIdentifier()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4713 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 4714 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 4715 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4716 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 4717 | // DNInfo work in progress: FIXME: source locations? |
| 4718 | DeclarationNameLoc DNLoc; |
| 4719 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 4720 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 4721 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 4722 | } |
| 4723 | } |
| 4724 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4725 | case TemplateName::SubstTemplateTemplateParm: { |
| 4726 | SubstTemplateTemplateParmStorage *subst |
| 4727 | = Name.getAsSubstTemplateTemplateParm(); |
| 4728 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 4729 | NameLoc); |
| 4730 | } |
| 4731 | |
| 4732 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 4733 | SubstTemplateTemplateParmPackStorage *subst |
| 4734 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 4735 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 4736 | NameLoc); |
| 4737 | } |
| 4738 | } |
| 4739 | |
| 4740 | llvm_unreachable("bad template name kind!"); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4743 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4744 | switch (Name.getKind()) { |
| 4745 | case TemplateName::QualifiedTemplate: |
| 4746 | case TemplateName::Template: { |
| 4747 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 4748 | if (TemplateTemplateParmDecl *TTP |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4749 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 4750 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 4751 | |
| 4752 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4753 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 4754 | } |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 4755 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4756 | case TemplateName::OverloadedTemplate: |
| 4757 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4758 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4759 | case TemplateName::DependentTemplate: { |
| 4760 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 4761 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 4762 | return DTN->CanonicalTemplateName; |
| 4763 | } |
| 4764 | |
| 4765 | case TemplateName::SubstTemplateTemplateParm: { |
| 4766 | SubstTemplateTemplateParmStorage *subst |
| 4767 | = Name.getAsSubstTemplateTemplateParm(); |
| 4768 | return getCanonicalTemplateName(subst->getReplacement()); |
| 4769 | } |
| 4770 | |
| 4771 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 4772 | SubstTemplateTemplateParmPackStorage *subst |
| 4773 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 4774 | TemplateTemplateParmDecl *canonParameter |
| 4775 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 4776 | TemplateArgument canonArgPack |
| 4777 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 4778 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 4779 | } |
| 4780 | } |
| 4781 | |
| 4782 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 4785 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 4786 | X = getCanonicalTemplateName(X); |
| 4787 | Y = getCanonicalTemplateName(Y); |
| 4788 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 4789 | } |
| 4790 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4791 | TemplateArgument |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4792 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4793 | switch (Arg.getKind()) { |
| 4794 | case TemplateArgument::Null: |
| 4795 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4796 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4797 | case TemplateArgument::Expression: |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4798 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4799 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4800 | case TemplateArgument::Declaration: { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4801 | ValueDecl *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl()); |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 4802 | return TemplateArgument(D, Arg.getParamTypeForDecl()); |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4803 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4804 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4805 | case TemplateArgument::NullPtr: |
| 4806 | return TemplateArgument(getCanonicalType(Arg.getNullPtrType()), |
| 4807 | /*isNullPtr*/true); |
| 4808 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4809 | case TemplateArgument::Template: |
| 4810 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4811 | |
| 4812 | case TemplateArgument::TemplateExpansion: |
| 4813 | return TemplateArgument(getCanonicalTemplateName( |
| 4814 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 4815 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4816 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4817 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4818 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4819 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4820 | case TemplateArgument::Type: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4821 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4822 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4823 | case TemplateArgument::Pack: { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 4824 | if (Arg.pack_size() == 0) |
| 4825 | return Arg; |
| 4826 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4827 | TemplateArgument *CanonArgs |
| 4828 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4829 | unsigned Idx = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4830 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4831 | AEnd = Arg.pack_end(); |
| 4832 | A != AEnd; (void)++A, ++Idx) |
| 4833 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4834 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 4835 | return TemplateArgument(llvm::makeArrayRef(CanonArgs, Arg.pack_size())); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4836 | } |
| 4837 | } |
| 4838 | |
| 4839 | // Silence GCC warning |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4840 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4841 | } |
| 4842 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4843 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4844 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4845 | if (!NNS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4846 | return nullptr; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4847 | |
| 4848 | switch (NNS->getKind()) { |
| 4849 | case NestedNameSpecifier::Identifier: |
| 4850 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4851 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4852 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 4853 | NNS->getAsIdentifier()); |
| 4854 | |
| 4855 | case NestedNameSpecifier::Namespace: |
| 4856 | // A namespace is canonical; build a nested-name-specifier with |
| 4857 | // this namespace and no prefix. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4858 | return NestedNameSpecifier::Create(*this, nullptr, |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4859 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 4860 | |
| 4861 | case NestedNameSpecifier::NamespaceAlias: |
| 4862 | // A namespace is canonical; build a nested-name-specifier with |
| 4863 | // this namespace and no prefix. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4864 | return NestedNameSpecifier::Create(*this, nullptr, |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4865 | NNS->getAsNamespaceAlias()->getNamespace() |
| 4866 | ->getOriginalNamespace()); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4867 | |
| 4868 | case NestedNameSpecifier::TypeSpec: |
| 4869 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 4870 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4871 | |
| 4872 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 4873 | // break it apart into its prefix and identifier, then reconsititute those |
| 4874 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 4875 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 4876 | // types, e.g., |
| 4877 | // typedef typename T::type T1; |
| 4878 | // typedef typename T1::type T2; |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 4879 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) |
| 4880 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4881 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4882 | |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 4883 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 4884 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 4885 | // first place? |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4886 | return NestedNameSpecifier::Create(*this, nullptr, false, |
| 4887 | const_cast<Type *>(T.getTypePtr())); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4888 | } |
| 4889 | |
| 4890 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4891 | case NestedNameSpecifier::Super: |
| 4892 | // The global specifier and __super specifer are canonical and unique. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4893 | return NNS; |
| 4894 | } |
| 4895 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4896 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4897 | } |
| 4898 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4899 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4900 | // Handle the non-qualified case efficiently. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4901 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4902 | // Handle the common positive case fast. |
| 4903 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 4904 | return AT; |
| 4905 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4906 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4907 | // Handle the common negative case fast. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4908 | if (!isa<ArrayType>(T.getCanonicalType())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4909 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4910 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4911 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4912 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 4913 | // 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] | 4914 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4915 | // If we get here, we either have type qualifiers on the type, or we have |
| 4916 | // 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] | 4917 | // we must propagate them down into the element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4918 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4919 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4920 | Qualifiers qs = split.Quals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4921 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4922 | // If we have a simple case, just return now. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4923 | const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4924 | if (!ATy || qs.empty()) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4925 | return ATy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4926 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4927 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 4928 | // qualifiers into the array element type and return a new array type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4929 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4930 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4931 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 4932 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 4933 | CAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4934 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4935 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 4936 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 4937 | IAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4938 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4939 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4940 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4941 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 4942 | return cast<ArrayType>( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4943 | getDependentSizedArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4944 | DSAT->getSizeExpr(), |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4945 | DSAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4946 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4947 | DSAT->getBracketsRange())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4948 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4949 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4950 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4951 | VAT->getSizeExpr(), |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4952 | VAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4953 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4954 | VAT->getBracketsRange())); |
Chris Lattner | ed0d079 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 4955 | } |
| 4956 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4957 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 4958 | if (T->isArrayType() || T->isFunctionType()) |
| 4959 | return getDecayedType(T); |
| 4960 | return T; |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4961 | } |
| 4962 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4963 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4964 | T = getVariableArrayDecayedType(T); |
| 4965 | T = getAdjustedParameterType(T); |
| 4966 | return T.getUnqualifiedType(); |
| 4967 | } |
| 4968 | |
David Majnemer | d09a51c | 2015-03-03 01:50:05 +0000 | [diff] [blame] | 4969 | QualType ASTContext::getExceptionObjectType(QualType T) const { |
| 4970 | // C++ [except.throw]p3: |
| 4971 | // A throw-expression initializes a temporary object, called the exception |
| 4972 | // object, the type of which is determined by removing any top-level |
| 4973 | // cv-qualifiers from the static type of the operand of throw and adjusting |
| 4974 | // the type from "array of T" or "function returning T" to "pointer to T" |
| 4975 | // or "pointer to function returning T", [...] |
| 4976 | T = getVariableArrayDecayedType(T); |
| 4977 | if (T->isArrayType() || T->isFunctionType()) |
| 4978 | T = getDecayedType(T); |
| 4979 | return T.getUnqualifiedType(); |
| 4980 | } |
| 4981 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4982 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 4983 | /// specified array type to a pointer. This operation is non-trivial when |
| 4984 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 4985 | /// this returns a pointer to a properly qualified element of the array. |
| 4986 | /// |
| 4987 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4988 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4989 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 4990 | // typedefs in the element type of the array. This also handles propagation |
| 4991 | // of type qualifiers from the array type into the element type if present |
| 4992 | // (C99 6.7.3p8). |
| 4993 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 4994 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4995 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4996 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4997 | |
| 4998 | // int x[restrict 4] -> int *restrict |
Jordan Rose | 303e2f1 | 2016-11-10 23:28:17 +0000 | [diff] [blame] | 4999 | QualType Result = getQualifiedType(PtrTy, |
| 5000 | PrettyArrayType->getIndexTypeQualifiers()); |
| 5001 | |
| 5002 | // int x[_Nullable] -> int * _Nullable |
| 5003 | if (auto Nullability = Ty->getNullability(*this)) { |
| 5004 | Result = const_cast<ASTContext *>(this)->getAttributedType( |
| 5005 | AttributedType::getNullabilityAttrKind(*Nullability), Result, Result); |
| 5006 | } |
| 5007 | return Result; |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 5008 | } |
| 5009 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5010 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 5011 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 5012 | } |
| 5013 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5014 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 5015 | Qualifiers qs; |
| 5016 | while (true) { |
| 5017 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5018 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5019 | if (!array) break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5020 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5021 | type = array->getElementType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5022 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5023 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5024 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5025 | return getQualifiedType(type, qs); |
Anders Carlsson | e0808df | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 5026 | } |
| 5027 | |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 5028 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5029 | uint64_t |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 5030 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 5031 | uint64_t ElementCount = 1; |
| 5032 | do { |
| 5033 | ElementCount *= CA->getSize().getZExtValue(); |
Richard Smith | 7808c6a | 2012-12-06 03:04:50 +0000 | [diff] [blame] | 5034 | CA = dyn_cast_or_null<ConstantArrayType>( |
| 5035 | CA->getElementType()->getAsArrayTypeUnsafe()); |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 5036 | } while (CA); |
| 5037 | return ElementCount; |
| 5038 | } |
| 5039 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 5040 | /// getFloatingRank - Return a relative rank for floating point types. |
| 5041 | /// 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] | 5042 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5043 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 5044 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5045 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5046 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 5047 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5048 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 5049 | case BuiltinType::Half: return HalfRank; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 5050 | case BuiltinType::Float: return FloatRank; |
| 5051 | case BuiltinType::Double: return DoubleRank; |
| 5052 | case BuiltinType::LongDouble: return LongDoubleRank; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5053 | case BuiltinType::Float128: return Float128Rank; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5054 | } |
| 5055 | } |
| 5056 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5057 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 5058 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | fc6ffa2 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 5059 | /// 'typeDomain' is a real floating point or complex type. |
| 5060 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 5061 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 5062 | QualType Domain) const { |
| 5063 | FloatingRank EltRank = getFloatingRank(Size); |
| 5064 | if (Domain->isComplexType()) { |
| 5065 | switch (EltRank) { |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 5066 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 5067 | case FloatRank: return FloatComplexTy; |
| 5068 | case DoubleRank: return DoubleComplexTy; |
| 5069 | case LongDoubleRank: return LongDoubleComplexTy; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5070 | case Float128Rank: return Float128ComplexTy; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 5071 | } |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 5072 | } |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 5073 | |
| 5074 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 5075 | switch (EltRank) { |
Joey Gouly | dd7f456 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 5076 | case HalfRank: return HalfTy; |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 5077 | case FloatRank: return FloatTy; |
| 5078 | case DoubleRank: return DoubleTy; |
| 5079 | case LongDoubleRank: return LongDoubleTy; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5080 | case Float128Rank: return Float128Ty; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 5081 | } |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 5082 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5085 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 5086 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 5087 | /// '_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] | 5088 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5089 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5090 | FloatingRank LHSR = getFloatingRank(LHS); |
| 5091 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5092 | |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5093 | if (LHSR == RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 5094 | return 0; |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5095 | if (LHSR > RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 5096 | return 1; |
| 5097 | return -1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5098 | } |
| 5099 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5100 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 5101 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 5102 | /// or if it is not canonicalized. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5103 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 5104 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 5105 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5106 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5107 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5108 | case BuiltinType::Bool: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5109 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5110 | case BuiltinType::Char_S: |
| 5111 | case BuiltinType::Char_U: |
| 5112 | case BuiltinType::SChar: |
| 5113 | case BuiltinType::UChar: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5114 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5115 | case BuiltinType::Short: |
| 5116 | case BuiltinType::UShort: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5117 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5118 | case BuiltinType::Int: |
| 5119 | case BuiltinType::UInt: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5120 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5121 | case BuiltinType::Long: |
| 5122 | case BuiltinType::ULong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5123 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5124 | case BuiltinType::LongLong: |
| 5125 | case BuiltinType::ULongLong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5126 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 5127 | case BuiltinType::Int128: |
| 5128 | case BuiltinType::UInt128: |
| 5129 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5130 | } |
| 5131 | } |
| 5132 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5133 | /// \brief Whether this is a promotable bitfield reference according |
| 5134 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 5135 | /// |
| 5136 | /// \returns the type this bit-field will promote to, or NULL if no |
| 5137 | /// promotion occurs. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5138 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | e05d3cb | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 5139 | if (E->isTypeDependent() || E->isValueDependent()) |
| 5140 | return QualType(); |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 5141 | |
| 5142 | // FIXME: We should not do this unless E->refersToBitField() is true. This |
| 5143 | // matters in C where getSourceBitField() will find bit-fields for various |
| 5144 | // cases where the source expression is not a bit-field designator. |
| 5145 | |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5146 | FieldDecl *Field = E->getSourceBitField(); // FIXME: conditional bit-fields? |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5147 | if (!Field) |
| 5148 | return QualType(); |
| 5149 | |
| 5150 | QualType FT = Field->getType(); |
| 5151 | |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5152 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5153 | uint64_t IntSize = getTypeSize(IntTy); |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 5154 | // C++ [conv.prom]p5: |
| 5155 | // A prvalue for an integral bit-field can be converted to a prvalue of type |
| 5156 | // int if int can represent all the values of the bit-field; otherwise, it |
| 5157 | // can be converted to unsigned int if unsigned int can represent all the |
| 5158 | // values of the bit-field. If the bit-field is larger yet, no integral |
| 5159 | // promotion applies to it. |
| 5160 | // C11 6.3.1.1/2: |
| 5161 | // [For a bit-field of type _Bool, int, signed int, or unsigned int:] |
| 5162 | // If an int can represent all values of the original type (as restricted by |
| 5163 | // the width, for a bit-field), the value is converted to an int; otherwise, |
| 5164 | // it is converted to an unsigned int. |
| 5165 | // |
| 5166 | // FIXME: C does not permit promotion of a 'long : 3' bitfield to int. |
| 5167 | // We perform that promotion here to match GCC and C++. |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5168 | if (BitWidth < IntSize) |
| 5169 | return IntTy; |
| 5170 | |
| 5171 | if (BitWidth == IntSize) |
| 5172 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 5173 | |
| 5174 | // Types bigger than int are not subject to promotions, and therefore act |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 5175 | // like the base type. GCC has some weird bugs in this area that we |
| 5176 | // deliberately do not follow (GCC follows a pre-standard resolution to |
| 5177 | // C's DR315 which treats bit-width as being part of the type, and this leaks |
| 5178 | // into their semantics in some cases). |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5179 | return QualType(); |
| 5180 | } |
| 5181 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5182 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 5183 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 5184 | /// integer type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5185 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5186 | assert(!Promotable.isNull()); |
| 5187 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5188 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 5189 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 5190 | |
| 5191 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 5192 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 5193 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 5194 | // types that can represent all the values of its underlying type: |
| 5195 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 5196 | // unsigned long long int [...] |
| 5197 | // FIXME: Is there some better way to compute this? |
| 5198 | if (BT->getKind() == BuiltinType::WChar_S || |
| 5199 | BT->getKind() == BuiltinType::WChar_U || |
| 5200 | BT->getKind() == BuiltinType::Char16 || |
| 5201 | BT->getKind() == BuiltinType::Char32) { |
| 5202 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 5203 | uint64_t FromSize = getTypeSize(BT); |
| 5204 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 5205 | LongLongTy, UnsignedLongLongTy }; |
| 5206 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 5207 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 5208 | if (FromSize < ToSize || |
| 5209 | (FromSize == ToSize && |
| 5210 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 5211 | return PromoteTypes[Idx]; |
| 5212 | } |
| 5213 | llvm_unreachable("char type should fit into long long"); |
| 5214 | } |
| 5215 | } |
| 5216 | |
| 5217 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5218 | if (Promotable->isSignedIntegerType()) |
| 5219 | return IntTy; |
Eli Friedman | 6745c3b | 2012-11-15 01:21:59 +0000 | [diff] [blame] | 5220 | uint64_t PromotableSize = getIntWidth(Promotable); |
| 5221 | uint64_t IntSize = getIntWidth(IntTy); |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5222 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 5223 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 5224 | } |
| 5225 | |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 5226 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 5227 | /// type and returns its ownership. |
| 5228 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 5229 | while (!T.isNull()) { |
| 5230 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 5231 | return T.getObjCLifetime(); |
| 5232 | if (T->isArrayType()) |
| 5233 | T = getBaseElementType(T); |
| 5234 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 5235 | T = PT->getPointeeType(); |
| 5236 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 8e25253 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 5237 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 5238 | else |
| 5239 | break; |
| 5240 | } |
| 5241 | |
| 5242 | return Qualifiers::OCL_None; |
| 5243 | } |
| 5244 | |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5245 | static const Type *getIntegerTypeForEnum(const EnumType *ET) { |
| 5246 | // Incomplete enum types are not treated as integer types. |
| 5247 | // FIXME: In C++, enum types are never integer types. |
| 5248 | if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped()) |
| 5249 | return ET->getDecl()->getIntegerType().getTypePtr(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5250 | return nullptr; |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5251 | } |
| 5252 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5253 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5254 | /// 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] | 5255 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5256 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5257 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 5258 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5259 | |
| 5260 | // Unwrap enums to their underlying type. |
| 5261 | if (const EnumType *ET = dyn_cast<EnumType>(LHSC)) |
| 5262 | LHSC = getIntegerTypeForEnum(ET); |
| 5263 | if (const EnumType *ET = dyn_cast<EnumType>(RHSC)) |
| 5264 | RHSC = getIntegerTypeForEnum(ET); |
| 5265 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5266 | if (LHSC == RHSC) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5267 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5268 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 5269 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5270 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5271 | unsigned LHSRank = getIntegerRank(LHSC); |
| 5272 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5273 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5274 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 5275 | if (LHSRank == RHSRank) return 0; |
| 5276 | return LHSRank > RHSRank ? 1 : -1; |
| 5277 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5278 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5279 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 5280 | if (LHSUnsigned) { |
| 5281 | // If the unsigned [LHS] type is larger, return it. |
| 5282 | if (LHSRank >= RHSRank) |
| 5283 | return 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5284 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5285 | // If the signed type can represent all values of the unsigned type, it |
| 5286 | // 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] | 5287 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5288 | return -1; |
| 5289 | } |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5290 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5291 | // If the unsigned [RHS] type is larger, return it. |
| 5292 | if (RHSRank >= LHSRank) |
| 5293 | return -1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5294 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5295 | // If the signed type can represent all values of the unsigned type, it |
| 5296 | // 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] | 5297 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5298 | return 1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5299 | } |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5300 | |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5301 | TypedefDecl *ASTContext::getCFConstantStringDecl() const { |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5302 | if (!CFConstantStringTypeDecl) { |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5303 | assert(!CFConstantStringTagDecl && |
| 5304 | "tag and typedef should be initialized together"); |
| 5305 | CFConstantStringTagDecl = buildImplicitRecord("__NSConstantString_tag"); |
| 5306 | CFConstantStringTagDecl->startDefinition(); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 5307 | |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 5308 | QualType FieldTypes[4]; |
Ben Langmuir | 4791a80 | 2016-02-25 16:36:26 +0000 | [diff] [blame] | 5309 | const char *FieldNames[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5310 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5311 | // const int *isa; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5312 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Ben Langmuir | 4791a80 | 2016-02-25 16:36:26 +0000 | [diff] [blame] | 5313 | FieldNames[0] = "isa"; |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 5314 | // int flags; |
| 5315 | FieldTypes[1] = IntTy; |
Ben Langmuir | 4791a80 | 2016-02-25 16:36:26 +0000 | [diff] [blame] | 5316 | FieldNames[1] = "flags"; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5317 | // const char *str; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5318 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Ben Langmuir | 4791a80 | 2016-02-25 16:36:26 +0000 | [diff] [blame] | 5319 | FieldNames[2] = "str"; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5320 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5321 | FieldTypes[3] = LongTy; |
Ben Langmuir | 4791a80 | 2016-02-25 16:36:26 +0000 | [diff] [blame] | 5322 | FieldNames[3] = "length"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5323 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5324 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5325 | for (unsigned i = 0; i < 4; ++i) { |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5326 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTagDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5327 | SourceLocation(), |
Ben Langmuir | 4791a80 | 2016-02-25 16:36:26 +0000 | [diff] [blame] | 5328 | SourceLocation(), |
| 5329 | &Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5330 | FieldTypes[i], /*TInfo=*/nullptr, |
| 5331 | /*BitWidth=*/nullptr, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 5332 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5333 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 5334 | Field->setAccess(AS_public); |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5335 | CFConstantStringTagDecl->addDecl(Field); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5336 | } |
| 5337 | |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5338 | CFConstantStringTagDecl->completeDefinition(); |
| 5339 | // This type is designed to be compatible with NSConstantString, but cannot |
| 5340 | // use the same name, since NSConstantString is an interface. |
| 5341 | auto tagType = getTagDeclType(CFConstantStringTagDecl); |
| 5342 | CFConstantStringTypeDecl = |
| 5343 | buildImplicitTypedef(tagType, "__NSConstantString"); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5344 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5345 | |
Quentin Colombet | 043406b | 2016-02-03 22:41:00 +0000 | [diff] [blame] | 5346 | return CFConstantStringTypeDecl; |
| 5347 | } |
| 5348 | |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5349 | RecordDecl *ASTContext::getCFConstantStringTagDecl() const { |
| 5350 | if (!CFConstantStringTagDecl) |
| 5351 | getCFConstantStringDecl(); // Build the tag and the typedef. |
| 5352 | return CFConstantStringTagDecl; |
| 5353 | } |
| 5354 | |
Quentin Colombet | 043406b | 2016-02-03 22:41:00 +0000 | [diff] [blame] | 5355 | // getCFConstantStringType - Return the type used for constant CFStrings. |
| 5356 | QualType ASTContext::getCFConstantStringType() const { |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5357 | return getTypedefType(getCFConstantStringDecl()); |
Gabor Greif | 412af03 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 5358 | } |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 5359 | |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 5360 | QualType ASTContext::getObjCSuperType() const { |
| 5361 | if (ObjCSuperType.isNull()) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5362 | RecordDecl *ObjCSuperTypeDecl = buildImplicitRecord("objc_super"); |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 5363 | TUDecl->addDecl(ObjCSuperTypeDecl); |
| 5364 | ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); |
| 5365 | } |
| 5366 | return ObjCSuperType; |
| 5367 | } |
| 5368 | |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 5369 | void ASTContext::setCFConstantStringType(QualType T) { |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5370 | const TypedefType *TD = T->getAs<TypedefType>(); |
| 5371 | assert(TD && "Invalid CFConstantStringType"); |
| 5372 | CFConstantStringTypeDecl = cast<TypedefDecl>(TD->getDecl()); |
| 5373 | auto TagType = |
| 5374 | CFConstantStringTypeDecl->getUnderlyingType()->getAs<RecordType>(); |
| 5375 | assert(TagType && "Invalid CFConstantStringType"); |
| 5376 | CFConstantStringTagDecl = TagType->getDecl(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 5377 | } |
| 5378 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5379 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5380 | if (BlockDescriptorType) |
| 5381 | return getTagDeclType(BlockDescriptorType); |
| 5382 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5383 | RecordDecl *RD; |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5384 | // FIXME: Needs the FlagAppleBlock bit. |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5385 | RD = buildImplicitRecord("__block_descriptor"); |
| 5386 | RD->startDefinition(); |
| 5387 | |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5388 | QualType FieldTypes[] = { |
| 5389 | UnsignedLongTy, |
| 5390 | UnsignedLongTy, |
| 5391 | }; |
| 5392 | |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 5393 | static const char *const FieldNames[] = { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5394 | "reserved", |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5395 | "Size" |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5396 | }; |
| 5397 | |
| 5398 | for (size_t i = 0; i < 2; ++i) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5399 | FieldDecl *Field = FieldDecl::Create( |
| 5400 | *this, RD, SourceLocation(), SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5401 | &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
| 5402 | /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 5403 | Field->setAccess(AS_public); |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5404 | RD->addDecl(Field); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5405 | } |
| 5406 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5407 | RD->completeDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5408 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5409 | BlockDescriptorType = RD; |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5410 | |
| 5411 | return getTagDeclType(BlockDescriptorType); |
| 5412 | } |
| 5413 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5414 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5415 | if (BlockDescriptorExtendedType) |
| 5416 | return getTagDeclType(BlockDescriptorExtendedType); |
| 5417 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5418 | RecordDecl *RD; |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5419 | // FIXME: Needs the FlagAppleBlock bit. |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5420 | RD = buildImplicitRecord("__block_descriptor_withcopydispose"); |
| 5421 | RD->startDefinition(); |
| 5422 | |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5423 | QualType FieldTypes[] = { |
| 5424 | UnsignedLongTy, |
| 5425 | UnsignedLongTy, |
| 5426 | getPointerType(VoidPtrTy), |
| 5427 | getPointerType(VoidPtrTy) |
| 5428 | }; |
| 5429 | |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 5430 | static const char *const FieldNames[] = { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5431 | "reserved", |
| 5432 | "Size", |
| 5433 | "CopyFuncPtr", |
| 5434 | "DestroyFuncPtr" |
| 5435 | }; |
| 5436 | |
| 5437 | for (size_t i = 0; i < 4; ++i) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5438 | FieldDecl *Field = FieldDecl::Create( |
| 5439 | *this, RD, SourceLocation(), SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5440 | &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
| 5441 | /*BitWidth=*/nullptr, |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5442 | /*Mutable=*/false, ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 5443 | Field->setAccess(AS_public); |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5444 | RD->addDecl(Field); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5445 | } |
| 5446 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5447 | RD->completeDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5448 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5449 | BlockDescriptorExtendedType = RD; |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5450 | return getTagDeclType(BlockDescriptorExtendedType); |
| 5451 | } |
| 5452 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 5453 | /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty" |
| 5454 | /// requires copy/dispose. Note that this must match the logic |
| 5455 | /// in buildByrefHelpers. |
| 5456 | bool ASTContext::BlockRequiresCopying(QualType Ty, |
| 5457 | const VarDecl *D) { |
| 5458 | if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) { |
| 5459 | const Expr *copyExpr = getBlockVarCopyInits(D); |
| 5460 | if (!copyExpr && record->hasTrivialDestructor()) return false; |
| 5461 | |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 5462 | return true; |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 5463 | } |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 5464 | |
| 5465 | if (!Ty->isObjCRetainableType()) return false; |
| 5466 | |
| 5467 | Qualifiers qs = Ty.getQualifiers(); |
| 5468 | |
| 5469 | // If we have lifetime, that dominates. |
| 5470 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 5471 | switch (lifetime) { |
| 5472 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
| 5473 | |
| 5474 | // These are just bits as far as the runtime is concerned. |
| 5475 | case Qualifiers::OCL_ExplicitNone: |
| 5476 | case Qualifiers::OCL_Autoreleasing: |
| 5477 | return false; |
| 5478 | |
| 5479 | // Tell the runtime that this is ARC __weak, called by the |
| 5480 | // byref routines. |
| 5481 | case Qualifiers::OCL_Weak: |
| 5482 | // ARC __strong __block variables need to be retained. |
| 5483 | case Qualifiers::OCL_Strong: |
| 5484 | return true; |
| 5485 | } |
| 5486 | llvm_unreachable("fell out of lifetime switch!"); |
| 5487 | } |
| 5488 | return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) || |
| 5489 | Ty->isObjCObjectPointerType()); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 5490 | } |
| 5491 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 5492 | bool ASTContext::getByrefLifetime(QualType Ty, |
| 5493 | Qualifiers::ObjCLifetime &LifeTime, |
| 5494 | bool &HasByrefExtendedLayout) const { |
| 5495 | |
| 5496 | if (!getLangOpts().ObjC1 || |
| 5497 | getLangOpts().getGC() != LangOptions::NonGC) |
| 5498 | return false; |
| 5499 | |
| 5500 | HasByrefExtendedLayout = false; |
Fariborz Jahanian | f762b72 | 2012-12-11 19:58:01 +0000 | [diff] [blame] | 5501 | if (Ty->isRecordType()) { |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 5502 | HasByrefExtendedLayout = true; |
| 5503 | LifeTime = Qualifiers::OCL_None; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 5504 | } else if ((LifeTime = Ty.getObjCLifetime())) { |
| 5505 | // Honor the ARC qualifiers. |
| 5506 | } else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) { |
| 5507 | // The MRR rule. |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 5508 | LifeTime = Qualifiers::OCL_ExplicitNone; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 5509 | } else { |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 5510 | LifeTime = Qualifiers::OCL_None; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 5511 | } |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 5512 | return true; |
| 5513 | } |
| 5514 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 5515 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 5516 | if (!ObjCInstanceTypeDecl) |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 5517 | ObjCInstanceTypeDecl = |
| 5518 | buildImplicitTypedef(getObjCIdType(), "instancetype"); |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 5519 | return ObjCInstanceTypeDecl; |
| 5520 | } |
| 5521 | |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 5522 | // This returns true if a type has been typedefed to BOOL: |
| 5523 | // typedef <type> BOOL; |
Chris Lattner | e021899 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 5524 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 5525 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 9b1f279 | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 5526 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 5527 | return II->isStr("BOOL"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5528 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5529 | return false; |
| 5530 | } |
| 5531 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5532 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5533 | /// purpose. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5534 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5535 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 5536 | return CharUnits::Zero(); |
| 5537 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5538 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5539 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5540 | // 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] | 5541 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5542 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5543 | // Treat arrays as pointers, since that's how they're passed in. |
| 5544 | else if (type->isArrayType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5545 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5546 | return sz; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5547 | } |
| 5548 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 5549 | bool ASTContext::isMSStaticDataMemberInlineDefinition(const VarDecl *VD) const { |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 5550 | return getTargetInfo().getCXXABI().isMicrosoft() && |
| 5551 | VD->isStaticDataMember() && |
David Majnemer | fac5243 | 2015-10-19 23:22:49 +0000 | [diff] [blame] | 5552 | VD->getType()->isIntegralOrEnumerationType() && |
| 5553 | !VD->getFirstDecl()->isOutOfLine() && VD->getFirstDecl()->hasInit(); |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 5554 | } |
| 5555 | |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 5556 | ASTContext::InlineVariableDefinitionKind |
| 5557 | ASTContext::getInlineVariableDefinitionKind(const VarDecl *VD) const { |
| 5558 | if (!VD->isInline()) |
| 5559 | return InlineVariableDefinitionKind::None; |
| 5560 | |
| 5561 | // In almost all cases, it's a weak definition. |
| 5562 | auto *First = VD->getFirstDecl(); |
| 5563 | if (!First->isConstexpr() || First->isInlineSpecified() || |
| 5564 | !VD->isStaticDataMember()) |
| 5565 | return InlineVariableDefinitionKind::Weak; |
| 5566 | |
| 5567 | // If there's a file-context declaration in this translation unit, it's a |
| 5568 | // non-discardable definition. |
| 5569 | for (auto *D : VD->redecls()) |
| 5570 | if (D->getLexicalDeclContext()->isFileContext()) |
| 5571 | return InlineVariableDefinitionKind::Strong; |
| 5572 | |
| 5573 | // If we've not seen one yet, we don't know. |
| 5574 | return InlineVariableDefinitionKind::WeakUnknown; |
| 5575 | } |
| 5576 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5577 | static inline |
| 5578 | std::string charUnitsToString(const CharUnits &CU) { |
| 5579 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5580 | } |
| 5581 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 5582 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5583 | /// declaration. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 5584 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 5585 | std::string S; |
| 5586 | |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5587 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 5588 | QualType BlockTy = |
| 5589 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 5590 | // Encode result type. |
Fariborz Jahanian | 0e3043b | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 5591 | if (getLangOpts().EncodeExtendedBlockSig) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5592 | getObjCEncodingForMethodParameter( |
| 5593 | Decl::OBJC_TQ_None, BlockTy->getAs<FunctionType>()->getReturnType(), S, |
| 5594 | true /*Extended*/); |
Fariborz Jahanian | 64223e6 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 5595 | else |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5596 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getReturnType(), S); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5597 | // Compute size of all parameters. |
| 5598 | // Start with computing size of a pointer in number of bytes. |
| 5599 | // FIXME: There might(should) be a better way of doing this computation! |
| 5600 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5601 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 5602 | CharUnits ParmOffset = PtrSize; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 5603 | for (auto PI : Decl->parameters()) { |
Aaron Ballman | b2b8b1d | 2014-03-07 16:09:59 +0000 | [diff] [blame] | 5604 | QualType PType = PI->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5605 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | d487941 | 2012-06-30 00:48:59 +0000 | [diff] [blame] | 5606 | if (sz.isZero()) |
| 5607 | continue; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5608 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5609 | ParmOffset += sz; |
| 5610 | } |
| 5611 | // Size of the argument frame |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5612 | S += charUnitsToString(ParmOffset); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5613 | // Block pointer and offset. |
| 5614 | S += "@?0"; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5615 | |
| 5616 | // Argument types. |
| 5617 | ParmOffset = PtrSize; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 5618 | for (auto PVDecl : Decl->parameters()) { |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5619 | QualType PType = PVDecl->getOriginalType(); |
| 5620 | if (const ArrayType *AT = |
| 5621 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 5622 | // Use array's original type only if it has known number of |
| 5623 | // elements. |
| 5624 | if (!isa<ConstantArrayType>(AT)) |
| 5625 | PType = PVDecl->getType(); |
| 5626 | } else if (PType->isFunctionType()) |
| 5627 | PType = PVDecl->getType(); |
Fariborz Jahanian | 0e3043b | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 5628 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 64223e6 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 5629 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType, |
| 5630 | S, true /*Extended*/); |
| 5631 | else |
| 5632 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5633 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5634 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5635 | } |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 5636 | |
| 5637 | return S; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5638 | } |
| 5639 | |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 5640 | std::string |
| 5641 | ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl) const { |
| 5642 | std::string S; |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 5643 | // Encode result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5644 | getObjCEncodingForType(Decl->getReturnType(), S); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 5645 | CharUnits ParmOffset; |
| 5646 | // Compute size of all parameters. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 5647 | for (auto PI : Decl->parameters()) { |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 5648 | QualType PType = PI->getType(); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 5649 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5650 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 5651 | continue; |
| 5652 | |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 5653 | assert(sz.isPositive() && |
| 5654 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 5655 | ParmOffset += sz; |
| 5656 | } |
| 5657 | S += charUnitsToString(ParmOffset); |
| 5658 | ParmOffset = CharUnits::Zero(); |
| 5659 | |
| 5660 | // Argument types. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 5661 | for (auto PVDecl : Decl->parameters()) { |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 5662 | QualType PType = PVDecl->getOriginalType(); |
| 5663 | if (const ArrayType *AT = |
| 5664 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 5665 | // Use array's original type only if it has known number of |
| 5666 | // elements. |
| 5667 | if (!isa<ConstantArrayType>(AT)) |
| 5668 | PType = PVDecl->getType(); |
| 5669 | } else if (PType->isFunctionType()) |
| 5670 | PType = PVDecl->getType(); |
| 5671 | getObjCEncodingForType(PType, S); |
| 5672 | S += charUnitsToString(ParmOffset); |
| 5673 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 5674 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5675 | |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 5676 | return S; |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 5677 | } |
| 5678 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5679 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 5680 | /// method parameter or return type. If Extended, include class names and |
| 5681 | /// block object types. |
| 5682 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 5683 | QualType T, std::string& S, |
| 5684 | bool Extended) const { |
| 5685 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 5686 | getObjCEncodingForTypeQualifier(QT, S); |
| 5687 | // Encode parameter type. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5688 | getObjCEncodingForTypeImpl(T, S, true, true, nullptr, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5689 | true /*OutermostType*/, |
| 5690 | false /*EncodingProperty*/, |
| 5691 | false /*StructField*/, |
| 5692 | Extended /*EncodeBlockParameters*/, |
| 5693 | Extended /*EncodeClassNames*/); |
| 5694 | } |
| 5695 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5696 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5697 | /// declaration. |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 5698 | std::string ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
| 5699 | bool Extended) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5700 | // FIXME: This is not very efficient. |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5701 | // Encode return type. |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 5702 | std::string S; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5703 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 5704 | Decl->getReturnType(), S, Extended); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5705 | // Compute size of all parameters. |
| 5706 | // Start with computing size of a pointer in number of bytes. |
| 5707 | // FIXME: There might(should) be a better way of doing this computation! |
| 5708 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5709 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5710 | // The first two arguments (self and _cmd) are pointers; account for |
| 5711 | // their size. |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5712 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 5713 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 5714 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 5715 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5716 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5717 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 5718 | continue; |
| 5719 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5720 | assert (sz.isPositive() && |
| 5721 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5722 | ParmOffset += sz; |
| 5723 | } |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5724 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5725 | S += "@0:"; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5726 | S += charUnitsToString(PtrSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5727 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5728 | // Argument types. |
| 5729 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 5730 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 5731 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 5732 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5733 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | a0befc0 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 5734 | if (const ArrayType *AT = |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 5735 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 5736 | // Use array's original type only if it has known number of |
| 5737 | // elements. |
Steve Naroff | 323827e | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 5738 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 5739 | PType = PVDecl->getType(); |
| 5740 | } else if (PType->isFunctionType()) |
| 5741 | PType = PVDecl->getType(); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5742 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 5743 | PType, S, Extended); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5744 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5745 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5746 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5747 | |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 5748 | return S; |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5749 | } |
| 5750 | |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 5751 | ObjCPropertyImplDecl * |
| 5752 | ASTContext::getObjCPropertyImplDeclForPropertyDecl( |
| 5753 | const ObjCPropertyDecl *PD, |
| 5754 | const Decl *Container) const { |
| 5755 | if (!Container) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5756 | return nullptr; |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 5757 | if (const ObjCCategoryImplDecl *CID = |
| 5758 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 5759 | for (auto *PID : CID->property_impls()) |
| 5760 | if (PID->getPropertyDecl() == PD) |
| 5761 | return PID; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5762 | } else { |
| 5763 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
| 5764 | for (auto *PID : OID->property_impls()) |
| 5765 | if (PID->getPropertyDecl() == PD) |
| 5766 | return PID; |
| 5767 | } |
| 5768 | return nullptr; |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 5769 | } |
| 5770 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5771 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 5772 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5773 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 5774 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5775 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 5776 | /// attributes readonly and bycopy are encoded as single characters. The |
| 5777 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 5778 | /// encoded as single characters, followed by an identifier. Property types |
| 5779 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 5780 | /// these attributes are defined by the following enumeration: |
| 5781 | /// @code |
| 5782 | /// enum PropertyAttributes { |
| 5783 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 5784 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 5785 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 5786 | /// kPropertyDynamic = 'D', // property is dynamic |
| 5787 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 5788 | /// kPropertySetter = 'S', // followed by setter selector name |
| 5789 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 8cf6185 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 5790 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 5791 | /// kPropertyWeak = 'W' // 'weak' property |
| 5792 | /// kPropertyStrong = 'P' // property GC'able |
| 5793 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 5794 | /// }; |
| 5795 | /// @endcode |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 5796 | std::string |
| 5797 | ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
| 5798 | const Decl *Container) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5799 | // Collect information from the property implementation decl(s). |
| 5800 | bool Dynamic = false; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5801 | ObjCPropertyImplDecl *SynthesizePID = nullptr; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5802 | |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 5803 | if (ObjCPropertyImplDecl *PropertyImpDecl = |
| 5804 | getObjCPropertyImplDeclForPropertyDecl(PD, Container)) { |
| 5805 | if (PropertyImpDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 5806 | Dynamic = true; |
| 5807 | else |
| 5808 | SynthesizePID = PropertyImpDecl; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5809 | } |
| 5810 | |
| 5811 | // FIXME: This is not very efficient. |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 5812 | std::string S = "T"; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5813 | |
| 5814 | // Encode result type. |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 5815 | // GCC has some special rules regarding encoding of properties which |
| 5816 | // closely resembles encoding of ivars. |
Joe Groff | 98ac7d2 | 2014-07-07 22:25:15 +0000 | [diff] [blame] | 5817 | getObjCEncodingForPropertyType(PD->getType(), S); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5818 | |
| 5819 | if (PD->isReadOnly()) { |
| 5820 | S += ",R"; |
Nico Weber | 4e8626f | 2013-05-08 23:47:40 +0000 | [diff] [blame] | 5821 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) |
| 5822 | S += ",C"; |
| 5823 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) |
| 5824 | S += ",&"; |
Fariborz Jahanian | 33079ee | 2014-04-02 22:49:42 +0000 | [diff] [blame] | 5825 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) |
| 5826 | S += ",W"; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5827 | } else { |
| 5828 | switch (PD->getSetterKind()) { |
| 5829 | case ObjCPropertyDecl::Assign: break; |
| 5830 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5831 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 70a315c | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 5832 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5833 | } |
| 5834 | } |
| 5835 | |
| 5836 | // It really isn't clear at all what this means, since properties |
| 5837 | // are "dynamic by default". |
| 5838 | if (Dynamic) |
| 5839 | S += ",D"; |
| 5840 | |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 5841 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 5842 | S += ",N"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5843 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5844 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 5845 | S += ",G"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5846 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5847 | } |
| 5848 | |
| 5849 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 5850 | S += ",S"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5851 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5852 | } |
| 5853 | |
| 5854 | if (SynthesizePID) { |
| 5855 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 5856 | S += ",V"; |
Chris Lattner | 1cbaacc | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 5857 | S += OID->getNameAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5858 | } |
| 5859 | |
| 5860 | // FIXME: OBJCGC: weak & strong |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 5861 | return S; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5862 | } |
| 5863 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5864 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5865 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 5866 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5867 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 5868 | /// |
| 5869 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 5870 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5871 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5872 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5873 | PointeeTy = UnsignedIntTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5874 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5875 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5876 | PointeeTy = IntTy; |
| 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5881 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5882 | const FieldDecl *Field, |
| 5883 | QualType *NotEncodedT) const { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 5884 | // We follow the behavior of gcc, expanding structures which are |
| 5885 | // directly pointed to, and expanding embedded structures. Note that |
| 5886 | // these rules are sufficient to prevent recursive encoding of the |
| 5887 | // same type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5888 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5889 | true /* outermost type */, false, false, |
| 5890 | false, false, false, NotEncodedT); |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 5891 | } |
| 5892 | |
Joe Groff | 98ac7d2 | 2014-07-07 22:25:15 +0000 | [diff] [blame] | 5893 | void ASTContext::getObjCEncodingForPropertyType(QualType T, |
| 5894 | std::string& S) const { |
| 5895 | // Encode result type. |
| 5896 | // GCC has some special rules regarding encoding of properties which |
| 5897 | // closely resembles encoding of ivars. |
| 5898 | getObjCEncodingForTypeImpl(T, S, true, true, nullptr, |
| 5899 | true /* outermost type */, |
| 5900 | true /* encoding property */); |
| 5901 | } |
| 5902 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5903 | static char getObjCEncodingForPrimitiveKind(const ASTContext *C, |
| 5904 | BuiltinType::Kind kind) { |
| 5905 | switch (kind) { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5906 | case BuiltinType::Void: return 'v'; |
| 5907 | case BuiltinType::Bool: return 'B'; |
| 5908 | case BuiltinType::Char_U: |
| 5909 | case BuiltinType::UChar: return 'C'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5910 | case BuiltinType::Char16: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5911 | case BuiltinType::UShort: return 'S'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5912 | case BuiltinType::Char32: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5913 | case BuiltinType::UInt: return 'I'; |
| 5914 | case BuiltinType::ULong: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5915 | return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5916 | case BuiltinType::UInt128: return 'T'; |
| 5917 | case BuiltinType::ULongLong: return 'Q'; |
| 5918 | case BuiltinType::Char_S: |
| 5919 | case BuiltinType::SChar: return 'c'; |
| 5920 | case BuiltinType::Short: return 's'; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 5921 | case BuiltinType::WChar_S: |
| 5922 | case BuiltinType::WChar_U: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5923 | case BuiltinType::Int: return 'i'; |
| 5924 | case BuiltinType::Long: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5925 | return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5926 | case BuiltinType::LongLong: return 'q'; |
| 5927 | case BuiltinType::Int128: return 't'; |
| 5928 | case BuiltinType::Float: return 'f'; |
| 5929 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 7cba5a7 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 5930 | case BuiltinType::LongDouble: return 'D'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5931 | case BuiltinType::NullPtr: return '*'; // like char* |
| 5932 | |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5933 | case BuiltinType::Float128: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5934 | case BuiltinType::Half: |
| 5935 | // FIXME: potentially need @encodes for these! |
| 5936 | return ' '; |
| 5937 | |
| 5938 | case BuiltinType::ObjCId: |
| 5939 | case BuiltinType::ObjCClass: |
| 5940 | case BuiltinType::ObjCSel: |
| 5941 | llvm_unreachable("@encoding ObjC primitive type"); |
| 5942 | |
| 5943 | // OpenCL and placeholder types don't need @encodings. |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 5944 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 5945 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 5946 | #include "clang/Basic/OpenCLImageTypes.def" |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5947 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 5948 | case BuiltinType::OCLClkEvent: |
| 5949 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 5950 | case BuiltinType::OCLReserveID: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5951 | case BuiltinType::OCLSampler: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5952 | case BuiltinType::Dependent: |
| 5953 | #define BUILTIN_TYPE(KIND, ID) |
| 5954 | #define PLACEHOLDER_TYPE(KIND, ID) \ |
| 5955 | case BuiltinType::KIND: |
| 5956 | #include "clang/AST/BuiltinTypes.def" |
| 5957 | llvm_unreachable("invalid builtin type for @encode"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5958 | } |
David Blaikie | 5a6a020 | 2013-01-09 17:48:41 +0000 | [diff] [blame] | 5959 | llvm_unreachable("invalid BuiltinType::Kind value"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5960 | } |
| 5961 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5962 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 5963 | EnumDecl *Enum = ET->getDecl(); |
| 5964 | |
| 5965 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 5966 | if (!Enum->isFixed()) |
| 5967 | return 'i'; |
| 5968 | |
| 5969 | // The encoding of a fixed enum type matches its fixed underlying type. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5970 | const BuiltinType *BT = Enum->getIntegerType()->castAs<BuiltinType>(); |
| 5971 | return getObjCEncodingForPrimitiveKind(C, BT->getKind()); |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5972 | } |
| 5973 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5974 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5975 | QualType T, const FieldDecl *FD) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5976 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 5977 | S += 'b'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5978 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 5979 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 5980 | // then the offset (in bits) of the first element, then the type of the |
| 5981 | // bitfield, then the size in bits. For example, in this structure: |
| 5982 | // |
| 5983 | // struct |
| 5984 | // { |
| 5985 | // int integer; |
| 5986 | // int flags:2; |
| 5987 | // }; |
| 5988 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 5989 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 5990 | // information is not especially sensible, but we're stuck with it for |
| 5991 | // compatibility with GCC, although providing it breaks anything that |
| 5992 | // actually uses runtime introspection and wants to work on both runtimes... |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 5993 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5994 | const RecordDecl *RD = FD->getParent(); |
| 5995 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | a3c122d | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 5996 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5997 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 5998 | S += ObjCEncodingForEnumType(Ctx, ET); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5999 | else { |
| 6000 | const BuiltinType *BT = T->castAs<BuiltinType>(); |
| 6001 | S += getObjCEncodingForPrimitiveKind(Ctx, BT->getKind()); |
| 6002 | } |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6003 | } |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6004 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 6005 | } |
| 6006 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6007 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 6008 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 6009 | bool ExpandPointedToStructures, |
| 6010 | bool ExpandStructures, |
Daniel Dunbar | c040ce4 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 6011 | const FieldDecl *FD, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 6012 | bool OutermostType, |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6013 | bool EncodingProperty, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6014 | bool StructField, |
| 6015 | bool EncodeBlockParameters, |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 6016 | bool EncodeClassNames, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6017 | bool EncodePointerToObjCTypedef, |
| 6018 | QualType *NotEncodedT) const { |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6019 | CanQualType CT = getCanonicalType(T); |
| 6020 | switch (CT->getTypeClass()) { |
| 6021 | case Type::Builtin: |
| 6022 | case Type::Enum: |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6023 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6024 | return EncodeBitField(this, S, T, FD); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6025 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CT)) |
| 6026 | S += getObjCEncodingForPrimitiveKind(this, BT->getKind()); |
| 6027 | else |
| 6028 | S += ObjCEncodingForEnumType(this, cast<EnumType>(CT)); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6029 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6030 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6031 | case Type::Complex: { |
| 6032 | const ComplexType *CT = T->castAs<ComplexType>(); |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 6033 | S += 'j'; |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6034 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, nullptr); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6035 | return; |
| 6036 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6037 | |
| 6038 | case Type::Atomic: { |
| 6039 | const AtomicType *AT = T->castAs<AtomicType>(); |
| 6040 | S += 'A'; |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6041 | getObjCEncodingForTypeImpl(AT->getValueType(), S, false, false, nullptr); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6042 | return; |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 6043 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6044 | |
| 6045 | // encoding for pointer or reference types. |
| 6046 | case Type::Pointer: |
| 6047 | case Type::LValueReference: |
| 6048 | case Type::RValueReference: { |
| 6049 | QualType PointeeTy; |
| 6050 | if (isa<PointerType>(CT)) { |
| 6051 | const PointerType *PT = T->castAs<PointerType>(); |
| 6052 | if (PT->isObjCSelType()) { |
| 6053 | S += ':'; |
| 6054 | return; |
| 6055 | } |
| 6056 | PointeeTy = PT->getPointeeType(); |
| 6057 | } else { |
| 6058 | PointeeTy = T->castAs<ReferenceType>()->getPointeeType(); |
| 6059 | } |
| 6060 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6061 | bool isReadOnly = false; |
| 6062 | // For historical/compatibility reasons, the read-only qualifier of the |
| 6063 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 6064 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6065 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 6066 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6067 | if (OutermostType && T.isConstQualified()) { |
| 6068 | isReadOnly = true; |
| 6069 | S += 'r'; |
| 6070 | } |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 6071 | } else if (OutermostType) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6072 | QualType P = PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6073 | while (P->getAs<PointerType>()) |
| 6074 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6075 | if (P.isConstQualified()) { |
| 6076 | isReadOnly = true; |
| 6077 | S += 'r'; |
| 6078 | } |
| 6079 | } |
| 6080 | if (isReadOnly) { |
| 6081 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 6082 | // combinations need to be rearranged. |
| 6083 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6084 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 6085 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6086 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6087 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6088 | if (PointeeTy->isCharType()) { |
| 6089 | // char pointer types should be encoded as '*' unless it is a |
| 6090 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 6091 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6092 | S += '*'; |
| 6093 | return; |
| 6094 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6095 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 3de6b70 | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 6096 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 6097 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 6098 | S += '#'; |
| 6099 | return; |
| 6100 | } |
| 6101 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 6102 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 6103 | S += '@'; |
| 6104 | return; |
| 6105 | } |
| 6106 | // fall through... |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6107 | } |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6108 | S += '^'; |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6109 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 6110 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6111 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6112 | nullptr, false, false, false, false, false, false, |
| 6113 | NotEncodedT); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6114 | return; |
| 6115 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6116 | |
| 6117 | case Type::ConstantArray: |
| 6118 | case Type::IncompleteArray: |
| 6119 | case Type::VariableArray: { |
| 6120 | const ArrayType *AT = cast<ArrayType>(CT); |
| 6121 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6122 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6123 | // Incomplete arrays are encoded as a pointer to the array element. |
| 6124 | S += '^'; |
| 6125 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6126 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6127 | false, ExpandStructures, FD); |
| 6128 | } else { |
| 6129 | S += '['; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6130 | |
Fariborz Jahanian | f0dc11a | 2013-06-04 16:04:37 +0000 | [diff] [blame] | 6131 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 6132 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 6133 | else { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6134 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6135 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 6136 | "Unknown array type!"); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6137 | S += '0'; |
| 6138 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6139 | |
| 6140 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6141 | false, ExpandStructures, FD, |
| 6142 | false, false, false, false, false, false, |
| 6143 | NotEncodedT); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6144 | S += ']'; |
| 6145 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6146 | return; |
| 6147 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6148 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6149 | case Type::FunctionNoProto: |
| 6150 | case Type::FunctionProto: |
Anders Carlsson | df4cc61 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 6151 | S += '?'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6152 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6153 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6154 | case Type::Record: { |
| 6155 | RecordDecl *RDecl = cast<RecordType>(CT)->getDecl(); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 6156 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 6157 | // Anonymous structures print as '?' |
| 6158 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 6159 | S += II->getName(); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 6160 | if (ClassTemplateSpecializationDecl *Spec |
| 6161 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 6162 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 6163 | llvm::raw_string_ostream OS(S); |
| 6164 | TemplateSpecializationType::PrintTemplateArgumentList(OS, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 6165 | TemplateArgs.asArray(), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 6166 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 6167 | } |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 6168 | } else { |
| 6169 | S += '?'; |
| 6170 | } |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 6171 | if (ExpandStructures) { |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 6172 | S += '='; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6173 | if (!RDecl->isUnion()) { |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6174 | getObjCEncodingForStructureImpl(RDecl, S, FD, true, NotEncodedT); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6175 | } else { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6176 | for (const auto *Field : RDecl->fields()) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6177 | if (FD) { |
| 6178 | S += '"'; |
| 6179 | S += Field->getNameAsString(); |
| 6180 | S += '"'; |
| 6181 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6182 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6183 | // Special case bit-fields. |
| 6184 | if (Field->isBitField()) { |
| 6185 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6186 | Field); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6187 | } else { |
| 6188 | QualType qt = Field->getType(); |
| 6189 | getLegacyIntegralTypeEncoding(qt); |
| 6190 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 6191 | FD, /*OutermostType*/false, |
| 6192 | /*EncodingProperty*/false, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6193 | /*StructField*/true, |
| 6194 | false, false, false, NotEncodedT); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6195 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 6196 | } |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 6197 | } |
Fariborz Jahanian | bc92fd7 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 6198 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 6199 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6200 | return; |
| 6201 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6202 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6203 | case Type::BlockPointer: { |
| 6204 | const BlockPointerType *BT = T->castAs<BlockPointerType>(); |
Steve Naroff | 49140cb | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 6205 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6206 | if (EncodeBlockParameters) { |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6207 | const FunctionType *FT = BT->getPointeeType()->castAs<FunctionType>(); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6208 | |
| 6209 | S += '<'; |
| 6210 | // Block return type |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6211 | getObjCEncodingForTypeImpl( |
| 6212 | FT->getReturnType(), S, ExpandPointedToStructures, ExpandStructures, |
| 6213 | FD, false /* OutermostType */, EncodingProperty, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6214 | false /* StructField */, EncodeBlockParameters, EncodeClassNames, false, |
| 6215 | NotEncodedT); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6216 | // Block self |
| 6217 | S += "@?"; |
| 6218 | // Block parameters |
| 6219 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 6220 | for (const auto &I : FPT->param_types()) |
| 6221 | getObjCEncodingForTypeImpl( |
| 6222 | I, S, ExpandPointedToStructures, ExpandStructures, FD, |
| 6223 | false /* OutermostType */, EncodingProperty, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6224 | false /* StructField */, EncodeBlockParameters, EncodeClassNames, |
| 6225 | false, NotEncodedT); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6226 | } |
| 6227 | S += '>'; |
| 6228 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6229 | return; |
| 6230 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6231 | |
Fariborz Jahanian | 33fa962 | 2014-01-28 20:41:15 +0000 | [diff] [blame] | 6232 | case Type::ObjCObject: { |
| 6233 | // hack to match legacy encoding of *id and *Class |
| 6234 | QualType Ty = getObjCObjectPointerType(CT); |
| 6235 | if (Ty->isObjCIdType()) { |
| 6236 | S += "{objc_object=}"; |
| 6237 | return; |
| 6238 | } |
| 6239 | else if (Ty->isObjCClassType()) { |
| 6240 | S += "{objc_class=}"; |
| 6241 | return; |
| 6242 | } |
| 6243 | } |
| 6244 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6245 | case Type::ObjCInterface: { |
| 6246 | // Ignore protocol qualifiers when mangling at this level. |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 6247 | // @encode(class_name) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 6248 | ObjCInterfaceDecl *OI = T->castAs<ObjCObjectType>()->getInterface(); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 6249 | S += '{'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6250 | S += OI->getObjCRuntimeNameAsString(); |
Akira Hatanaka | fd0fb20 | 2016-08-17 19:42:22 +0000 | [diff] [blame] | 6251 | if (ExpandStructures) { |
| 6252 | S += '='; |
| 6253 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
| 6254 | DeepCollectObjCIvars(OI, true, Ivars); |
| 6255 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
| 6256 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
| 6257 | if (Field->isBitField()) |
| 6258 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
| 6259 | else |
| 6260 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD, |
| 6261 | false, false, false, false, false, |
| 6262 | EncodePointerToObjCTypedef, |
| 6263 | NotEncodedT); |
| 6264 | } |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 6265 | } |
| 6266 | S += '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6267 | return; |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 6268 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6269 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6270 | case Type::ObjCObjectPointer: { |
| 6271 | const ObjCObjectPointerType *OPT = T->castAs<ObjCObjectPointerType>(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6272 | if (OPT->isObjCIdType()) { |
| 6273 | S += '@'; |
| 6274 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6275 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6276 | |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 6277 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 6278 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 6279 | // Since this is a binary compatibility issue, need to consult with runtime |
| 6280 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6281 | S += '#'; |
| 6282 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6283 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6284 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6285 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6286 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6287 | ExpandPointedToStructures, |
| 6288 | ExpandStructures, FD); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6289 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6290 | // Note that we do extended encoding of protocol qualifer list |
| 6291 | // Only when doing ivar or property encoding. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6292 | S += '"'; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6293 | for (const auto *I : OPT->quals()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6294 | S += '<'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6295 | S += I->getObjCRuntimeNameAsString(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6296 | S += '>'; |
| 6297 | } |
| 6298 | S += '"'; |
| 6299 | } |
| 6300 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6301 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6302 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6303 | QualType PointeeTy = OPT->getPointeeType(); |
| 6304 | if (!EncodingProperty && |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 6305 | isa<TypedefType>(PointeeTy.getTypePtr()) && |
| 6306 | !EncodePointerToObjCTypedef) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6307 | // Another historical/compatibility reason. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6308 | // We encode the underlying type which comes out as |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6309 | // {...}; |
| 6310 | S += '^'; |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 6311 | if (FD && OPT->getInterfaceDecl()) { |
Fariborz Jahanian | c682ef5 | 2013-07-12 16:41:56 +0000 | [diff] [blame] | 6312 | // Prevent recursive encoding of fields in some rare cases. |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 6313 | ObjCInterfaceDecl *OI = OPT->getInterfaceDecl(); |
| 6314 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
| 6315 | DeepCollectObjCIvars(OI, true, Ivars); |
| 6316 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
| 6317 | if (cast<FieldDecl>(Ivars[i]) == FD) { |
| 6318 | S += '{'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6319 | S += OI->getObjCRuntimeNameAsString(); |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 6320 | S += '}'; |
| 6321 | return; |
| 6322 | } |
| 6323 | } |
| 6324 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6325 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 6326 | false, ExpandPointedToStructures, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6327 | nullptr, |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 6328 | false, false, false, false, false, |
| 6329 | /*EncodePointerToObjCTypedef*/true); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6330 | return; |
| 6331 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6332 | |
| 6333 | S += '@'; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6334 | if (OPT->getInterfaceDecl() && |
| 6335 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6336 | S += '"'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6337 | S += OPT->getInterfaceDecl()->getObjCRuntimeNameAsString(); |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6338 | for (const auto *I : OPT->quals()) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6339 | S += '<'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6340 | S += I->getObjCRuntimeNameAsString(); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6341 | S += '>'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6342 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6343 | S += '"'; |
| 6344 | } |
| 6345 | return; |
| 6346 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6347 | |
John McCall | a9e6e8d | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 6348 | // gcc just blithely ignores member pointers. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6349 | // FIXME: we shoul do better than that. 'M' is available. |
| 6350 | case Type::MemberPointer: |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6351 | // This matches gcc's encoding, even though technically it is insufficient. |
| 6352 | //FIXME. We should do a better job than gcc. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6353 | case Type::Vector: |
| 6354 | case Type::ExtVector: |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6355 | // Until we have a coherent encoding of these three types, issue warning. |
| 6356 | { if (NotEncodedT) |
| 6357 | *NotEncodedT = T; |
| 6358 | return; |
| 6359 | } |
| 6360 | |
| 6361 | // We could see an undeduced auto type here during error recovery. |
| 6362 | // Just ignore it. |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6363 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 6364 | case Type::DeducedTemplateSpecialization: |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6365 | return; |
| 6366 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 6367 | case Type::Pipe: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6368 | #define ABSTRACT_TYPE(KIND, BASE) |
| 6369 | #define TYPE(KIND, BASE) |
| 6370 | #define DEPENDENT_TYPE(KIND, BASE) \ |
| 6371 | case Type::KIND: |
| 6372 | #define NON_CANONICAL_TYPE(KIND, BASE) \ |
| 6373 | case Type::KIND: |
| 6374 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \ |
| 6375 | case Type::KIND: |
| 6376 | #include "clang/AST/TypeNodes.def" |
| 6377 | llvm_unreachable("@encode for dependent type!"); |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 6378 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6379 | llvm_unreachable("bad type kind!"); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6380 | } |
| 6381 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6382 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 6383 | std::string &S, |
| 6384 | const FieldDecl *FD, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6385 | bool includeVBases, |
| 6386 | QualType *NotEncodedT) const { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6387 | assert(RDecl && "Expected non-null RecordDecl"); |
| 6388 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 6389 | if (!RDecl->getDefinition() || RDecl->getDefinition()->isInvalidDecl()) |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6390 | return; |
| 6391 | |
| 6392 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 6393 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 6394 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 6395 | |
| 6396 | if (CXXRec) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6397 | for (const auto &BI : CXXRec->bases()) { |
| 6398 | if (!BI.isVirtual()) { |
| 6399 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 6400 | if (base->isEmpty()) |
| 6401 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 6402 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6403 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 6404 | std::make_pair(offs, base)); |
| 6405 | } |
| 6406 | } |
| 6407 | } |
| 6408 | |
| 6409 | unsigned i = 0; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6410 | for (auto *Field : RDecl->fields()) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6411 | uint64_t offs = layout.getFieldOffset(i); |
| 6412 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6413 | std::make_pair(offs, Field)); |
| 6414 | ++i; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6415 | } |
| 6416 | |
| 6417 | if (CXXRec && includeVBases) { |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 6418 | for (const auto &BI : CXXRec->vbases()) { |
| 6419 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 6420 | if (base->isEmpty()) |
| 6421 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 6422 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); |
Eli Friedman | 1d24af8 | 2013-09-18 01:59:16 +0000 | [diff] [blame] | 6423 | if (offs >= uint64_t(toBits(layout.getNonVirtualSize())) && |
| 6424 | FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
Argyrios Kyrtzidis | 81c0b5c | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 6425 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 6426 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6427 | } |
| 6428 | } |
| 6429 | |
| 6430 | CharUnits size; |
| 6431 | if (CXXRec) { |
| 6432 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 6433 | } else { |
| 6434 | size = layout.getSize(); |
| 6435 | } |
| 6436 | |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6437 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6438 | uint64_t CurOffs = 0; |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6439 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6440 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 6441 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 6442 | |
Douglas Gregor | f5d6c74 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 6443 | if (CXXRec && CXXRec->isDynamicClass() && |
| 6444 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6445 | if (FD) { |
| 6446 | S += "\"_vptr$"; |
| 6447 | std::string recname = CXXRec->getNameAsString(); |
| 6448 | if (recname.empty()) recname = "?"; |
| 6449 | S += recname; |
| 6450 | S += '"'; |
| 6451 | } |
| 6452 | S += "^^?"; |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6453 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6454 | CurOffs += getTypeSize(VoidPtrTy); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6455 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6456 | } |
| 6457 | |
| 6458 | if (!RDecl->hasFlexibleArrayMember()) { |
| 6459 | // Mark the end of the structure. |
| 6460 | uint64_t offs = toBits(size); |
| 6461 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6462 | std::make_pair(offs, nullptr)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6463 | } |
| 6464 | |
| 6465 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6466 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6467 | assert(CurOffs <= CurLayObj->first); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6468 | if (CurOffs < CurLayObj->first) { |
| 6469 | uint64_t padding = CurLayObj->first - CurOffs; |
| 6470 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 6471 | // packing/alignment of members is different that normal, in which case |
| 6472 | // the encoding will be out-of-sync with the real layout. |
| 6473 | // If the runtime switches to just consider the size of types without |
| 6474 | // taking into account alignment, we could make padding explicit in the |
| 6475 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 6476 | // longer then though. |
| 6477 | CurOffs += padding; |
| 6478 | } |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6479 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6480 | |
| 6481 | NamedDecl *dcl = CurLayObj->second; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6482 | if (!dcl) |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6483 | break; // reached end of structure. |
| 6484 | |
| 6485 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 6486 | // We expand the bases without their virtual bases since those are going |
| 6487 | // in the initial structure. Note that this differs from gcc which |
| 6488 | // expands virtual bases each time one is encountered in the hierarchy, |
| 6489 | // making the encoding type bigger than it really is. |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6490 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false, |
| 6491 | NotEncodedT); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 6492 | assert(!base->isEmpty()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6493 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 6494 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6495 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6496 | } else { |
| 6497 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 6498 | if (FD) { |
| 6499 | S += '"'; |
| 6500 | S += field->getNameAsString(); |
| 6501 | S += '"'; |
| 6502 | } |
| 6503 | |
| 6504 | if (field->isBitField()) { |
| 6505 | EncodeBitField(this, S, field->getType(), field); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6506 | #ifndef NDEBUG |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6507 | CurOffs += field->getBitWidthValue(*this); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6508 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6509 | } else { |
| 6510 | QualType qt = field->getType(); |
| 6511 | getLegacyIntegralTypeEncoding(qt); |
| 6512 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 6513 | /*OutermostType*/false, |
| 6514 | /*EncodingProperty*/false, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6515 | /*StructField*/true, |
| 6516 | false, false, false, NotEncodedT); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6517 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6518 | CurOffs += getTypeSize(field->getType()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6519 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6520 | } |
| 6521 | } |
| 6522 | } |
| 6523 | } |
| 6524 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6525 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 6526 | std::string& S) const { |
| 6527 | if (QT & Decl::OBJC_TQ_In) |
| 6528 | S += 'n'; |
| 6529 | if (QT & Decl::OBJC_TQ_Inout) |
| 6530 | S += 'N'; |
| 6531 | if (QT & Decl::OBJC_TQ_Out) |
| 6532 | S += 'o'; |
| 6533 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 6534 | S += 'O'; |
| 6535 | if (QT & Decl::OBJC_TQ_Byref) |
| 6536 | S += 'R'; |
| 6537 | if (QT & Decl::OBJC_TQ_Oneway) |
| 6538 | S += 'V'; |
| 6539 | } |
| 6540 | |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 6541 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 6542 | if (!ObjCIdDecl) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 6543 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, { }, { }); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 6544 | T = getObjCObjectPointerType(T); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6545 | ObjCIdDecl = buildImplicitTypedef(T, "id"); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 6546 | } |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 6547 | return ObjCIdDecl; |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 6548 | } |
| 6549 | |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 6550 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 6551 | if (!ObjCSelDecl) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6552 | QualType T = getPointerType(ObjCBuiltinSelTy); |
| 6553 | ObjCSelDecl = buildImplicitTypedef(T, "SEL"); |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 6554 | } |
| 6555 | return ObjCSelDecl; |
Fariborz Jahanian | 4bef462 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 6556 | } |
| 6557 | |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 6558 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 6559 | if (!ObjCClassDecl) { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 6560 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, { }, { }); |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 6561 | T = getObjCObjectPointerType(T); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6562 | ObjCClassDecl = buildImplicitTypedef(T, "Class"); |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 6563 | } |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 6564 | return ObjCClassDecl; |
Anders Carlsson | f56a7ae | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 6565 | } |
| 6566 | |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 6567 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 6568 | if (!ObjCProtocolClassDecl) { |
| 6569 | ObjCProtocolClassDecl |
| 6570 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 6571 | SourceLocation(), |
| 6572 | &Idents.get("Protocol"), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 6573 | /*typeParamList=*/nullptr, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6574 | /*PrevDecl=*/nullptr, |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 6575 | SourceLocation(), true); |
| 6576 | } |
| 6577 | |
| 6578 | return ObjCProtocolClassDecl; |
| 6579 | } |
| 6580 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6581 | //===----------------------------------------------------------------------===// |
| 6582 | // __builtin_va_list Construction Functions |
| 6583 | //===----------------------------------------------------------------------===// |
| 6584 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 6585 | static TypedefDecl *CreateCharPtrNamedVaListDecl(const ASTContext *Context, |
| 6586 | StringRef Name) { |
| 6587 | // typedef char* __builtin[_ms]_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6588 | QualType T = Context->getPointerType(Context->CharTy); |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 6589 | return Context->buildImplicitTypedef(T, Name); |
| 6590 | } |
| 6591 | |
| 6592 | static TypedefDecl *CreateMSVaListDecl(const ASTContext *Context) { |
| 6593 | return CreateCharPtrNamedVaListDecl(Context, "__builtin_ms_va_list"); |
| 6594 | } |
| 6595 | |
| 6596 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 6597 | return CreateCharPtrNamedVaListDecl(Context, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6598 | } |
| 6599 | |
| 6600 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 6601 | // typedef void* __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6602 | QualType T = Context->getPointerType(Context->VoidTy); |
| 6603 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6604 | } |
| 6605 | |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6606 | static TypedefDecl * |
| 6607 | CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6608 | // struct __va_list |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6609 | RecordDecl *VaListTagDecl = Context->buildImplicitRecord("__va_list"); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6610 | if (Context->getLangOpts().CPlusPlus) { |
| 6611 | // namespace std { struct __va_list { |
| 6612 | NamespaceDecl *NS; |
| 6613 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 6614 | Context->getTranslationUnitDecl(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6615 | /*Inline*/ false, SourceLocation(), |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6616 | SourceLocation(), &Context->Idents.get("std"), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6617 | /*PrevDecl*/ nullptr); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6618 | NS->setImplicit(); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6619 | VaListTagDecl->setDeclContext(NS); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6620 | } |
| 6621 | |
| 6622 | VaListTagDecl->startDefinition(); |
| 6623 | |
| 6624 | const size_t NumFields = 5; |
| 6625 | QualType FieldTypes[NumFields]; |
| 6626 | const char *FieldNames[NumFields]; |
| 6627 | |
| 6628 | // void *__stack; |
| 6629 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); |
| 6630 | FieldNames[0] = "__stack"; |
| 6631 | |
| 6632 | // void *__gr_top; |
| 6633 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); |
| 6634 | FieldNames[1] = "__gr_top"; |
| 6635 | |
| 6636 | // void *__vr_top; |
| 6637 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 6638 | FieldNames[2] = "__vr_top"; |
| 6639 | |
| 6640 | // int __gr_offs; |
| 6641 | FieldTypes[3] = Context->IntTy; |
| 6642 | FieldNames[3] = "__gr_offs"; |
| 6643 | |
| 6644 | // int __vr_offs; |
| 6645 | FieldTypes[4] = Context->IntTy; |
| 6646 | FieldNames[4] = "__vr_offs"; |
| 6647 | |
| 6648 | // Create fields |
| 6649 | for (unsigned i = 0; i < NumFields; ++i) { |
| 6650 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 6651 | VaListTagDecl, |
| 6652 | SourceLocation(), |
| 6653 | SourceLocation(), |
| 6654 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6655 | FieldTypes[i], /*TInfo=*/nullptr, |
| 6656 | /*BitWidth=*/nullptr, |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6657 | /*Mutable=*/false, |
| 6658 | ICIS_NoInit); |
| 6659 | Field->setAccess(AS_public); |
| 6660 | VaListTagDecl->addDecl(Field); |
| 6661 | } |
| 6662 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6663 | Context->VaListTagDecl = VaListTagDecl; |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6664 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6665 | |
| 6666 | // } __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6667 | return Context->buildImplicitTypedef(VaListTagType, "__builtin_va_list"); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6668 | } |
| 6669 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6670 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
| 6671 | // typedef struct __va_list_tag { |
| 6672 | RecordDecl *VaListTagDecl; |
| 6673 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6674 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6675 | VaListTagDecl->startDefinition(); |
| 6676 | |
| 6677 | const size_t NumFields = 5; |
| 6678 | QualType FieldTypes[NumFields]; |
| 6679 | const char *FieldNames[NumFields]; |
| 6680 | |
| 6681 | // unsigned char gpr; |
| 6682 | FieldTypes[0] = Context->UnsignedCharTy; |
| 6683 | FieldNames[0] = "gpr"; |
| 6684 | |
| 6685 | // unsigned char fpr; |
| 6686 | FieldTypes[1] = Context->UnsignedCharTy; |
| 6687 | FieldNames[1] = "fpr"; |
| 6688 | |
| 6689 | // unsigned short reserved; |
| 6690 | FieldTypes[2] = Context->UnsignedShortTy; |
| 6691 | FieldNames[2] = "reserved"; |
| 6692 | |
| 6693 | // void* overflow_arg_area; |
| 6694 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 6695 | FieldNames[3] = "overflow_arg_area"; |
| 6696 | |
| 6697 | // void* reg_save_area; |
| 6698 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
| 6699 | FieldNames[4] = "reg_save_area"; |
| 6700 | |
| 6701 | // Create fields |
| 6702 | for (unsigned i = 0; i < NumFields; ++i) { |
| 6703 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
| 6704 | SourceLocation(), |
| 6705 | SourceLocation(), |
| 6706 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6707 | FieldTypes[i], /*TInfo=*/nullptr, |
| 6708 | /*BitWidth=*/nullptr, |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6709 | /*Mutable=*/false, |
| 6710 | ICIS_NoInit); |
| 6711 | Field->setAccess(AS_public); |
| 6712 | VaListTagDecl->addDecl(Field); |
| 6713 | } |
| 6714 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6715 | Context->VaListTagDecl = VaListTagDecl; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6716 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 6717 | |
| 6718 | // } __va_list_tag; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6719 | TypedefDecl *VaListTagTypedefDecl = |
| 6720 | Context->buildImplicitTypedef(VaListTagType, "__va_list_tag"); |
| 6721 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6722 | QualType VaListTagTypedefType = |
| 6723 | Context->getTypedefType(VaListTagTypedefDecl); |
| 6724 | |
| 6725 | // typedef __va_list_tag __builtin_va_list[1]; |
| 6726 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 6727 | QualType VaListTagArrayType |
| 6728 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 6729 | Size, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6730 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6731 | } |
| 6732 | |
| 6733 | static TypedefDecl * |
| 6734 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6735 | // struct __va_list_tag { |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6736 | RecordDecl *VaListTagDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6737 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6738 | VaListTagDecl->startDefinition(); |
| 6739 | |
| 6740 | const size_t NumFields = 4; |
| 6741 | QualType FieldTypes[NumFields]; |
| 6742 | const char *FieldNames[NumFields]; |
| 6743 | |
| 6744 | // unsigned gp_offset; |
| 6745 | FieldTypes[0] = Context->UnsignedIntTy; |
| 6746 | FieldNames[0] = "gp_offset"; |
| 6747 | |
| 6748 | // unsigned fp_offset; |
| 6749 | FieldTypes[1] = Context->UnsignedIntTy; |
| 6750 | FieldNames[1] = "fp_offset"; |
| 6751 | |
| 6752 | // void* overflow_arg_area; |
| 6753 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 6754 | FieldNames[2] = "overflow_arg_area"; |
| 6755 | |
| 6756 | // void* reg_save_area; |
| 6757 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 6758 | FieldNames[3] = "reg_save_area"; |
| 6759 | |
| 6760 | // Create fields |
| 6761 | for (unsigned i = 0; i < NumFields; ++i) { |
| 6762 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 6763 | VaListTagDecl, |
| 6764 | SourceLocation(), |
| 6765 | SourceLocation(), |
| 6766 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6767 | FieldTypes[i], /*TInfo=*/nullptr, |
| 6768 | /*BitWidth=*/nullptr, |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6769 | /*Mutable=*/false, |
| 6770 | ICIS_NoInit); |
| 6771 | Field->setAccess(AS_public); |
| 6772 | VaListTagDecl->addDecl(Field); |
| 6773 | } |
| 6774 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6775 | Context->VaListTagDecl = VaListTagDecl; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6776 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 6777 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6778 | // }; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6779 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6780 | // typedef struct __va_list_tag __builtin_va_list[1]; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6781 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6782 | QualType VaListTagArrayType = |
| 6783 | Context->getConstantArrayType(VaListTagType, Size, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6784 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6785 | } |
| 6786 | |
| 6787 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
| 6788 | // typedef int __builtin_va_list[4]; |
| 6789 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); |
Yaron Keren | e0bcdd4 | 2016-10-08 06:45:10 +0000 | [diff] [blame] | 6790 | QualType IntArrayType = |
| 6791 | Context->getConstantArrayType(Context->IntTy, Size, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6792 | return Context->buildImplicitTypedef(IntArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6793 | } |
| 6794 | |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6795 | static TypedefDecl * |
| 6796 | CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6797 | // struct __va_list |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6798 | RecordDecl *VaListDecl = Context->buildImplicitRecord("__va_list"); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6799 | if (Context->getLangOpts().CPlusPlus) { |
| 6800 | // namespace std { struct __va_list { |
| 6801 | NamespaceDecl *NS; |
| 6802 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 6803 | Context->getTranslationUnitDecl(), |
| 6804 | /*Inline*/false, SourceLocation(), |
| 6805 | SourceLocation(), &Context->Idents.get("std"), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6806 | /*PrevDecl*/ nullptr); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6807 | NS->setImplicit(); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6808 | VaListDecl->setDeclContext(NS); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6809 | } |
| 6810 | |
| 6811 | VaListDecl->startDefinition(); |
| 6812 | |
| 6813 | // void * __ap; |
| 6814 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 6815 | VaListDecl, |
| 6816 | SourceLocation(), |
| 6817 | SourceLocation(), |
| 6818 | &Context->Idents.get("__ap"), |
| 6819 | Context->getPointerType(Context->VoidTy), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6820 | /*TInfo=*/nullptr, |
| 6821 | /*BitWidth=*/nullptr, |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6822 | /*Mutable=*/false, |
| 6823 | ICIS_NoInit); |
| 6824 | Field->setAccess(AS_public); |
| 6825 | VaListDecl->addDecl(Field); |
| 6826 | |
| 6827 | // }; |
| 6828 | VaListDecl->completeDefinition(); |
Oleg Ranevskyy | b88d247 | 2016-03-30 21:30:30 +0000 | [diff] [blame] | 6829 | Context->VaListTagDecl = VaListDecl; |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6830 | |
| 6831 | // typedef struct __va_list __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6832 | QualType T = Context->getRecordType(VaListDecl); |
| 6833 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6834 | } |
| 6835 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6836 | static TypedefDecl * |
| 6837 | CreateSystemZBuiltinVaListDecl(const ASTContext *Context) { |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6838 | // struct __va_list_tag { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6839 | RecordDecl *VaListTagDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6840 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6841 | VaListTagDecl->startDefinition(); |
| 6842 | |
| 6843 | const size_t NumFields = 4; |
| 6844 | QualType FieldTypes[NumFields]; |
| 6845 | const char *FieldNames[NumFields]; |
| 6846 | |
| 6847 | // long __gpr; |
| 6848 | FieldTypes[0] = Context->LongTy; |
| 6849 | FieldNames[0] = "__gpr"; |
| 6850 | |
| 6851 | // long __fpr; |
| 6852 | FieldTypes[1] = Context->LongTy; |
| 6853 | FieldNames[1] = "__fpr"; |
| 6854 | |
| 6855 | // void *__overflow_arg_area; |
| 6856 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 6857 | FieldNames[2] = "__overflow_arg_area"; |
| 6858 | |
| 6859 | // void *__reg_save_area; |
| 6860 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 6861 | FieldNames[3] = "__reg_save_area"; |
| 6862 | |
| 6863 | // Create fields |
| 6864 | for (unsigned i = 0; i < NumFields; ++i) { |
| 6865 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 6866 | VaListTagDecl, |
| 6867 | SourceLocation(), |
| 6868 | SourceLocation(), |
| 6869 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6870 | FieldTypes[i], /*TInfo=*/nullptr, |
| 6871 | /*BitWidth=*/nullptr, |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6872 | /*Mutable=*/false, |
| 6873 | ICIS_NoInit); |
| 6874 | Field->setAccess(AS_public); |
| 6875 | VaListTagDecl->addDecl(Field); |
| 6876 | } |
| 6877 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6878 | Context->VaListTagDecl = VaListTagDecl; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6879 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6880 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6881 | // }; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6882 | |
| 6883 | // typedef __va_list_tag __builtin_va_list[1]; |
| 6884 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6885 | QualType VaListTagArrayType = |
| 6886 | Context->getConstantArrayType(VaListTagType, Size, ArrayType::Normal, 0); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6887 | |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6888 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6889 | } |
| 6890 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6891 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
| 6892 | TargetInfo::BuiltinVaListKind Kind) { |
| 6893 | switch (Kind) { |
| 6894 | case TargetInfo::CharPtrBuiltinVaList: |
| 6895 | return CreateCharPtrBuiltinVaListDecl(Context); |
| 6896 | case TargetInfo::VoidPtrBuiltinVaList: |
| 6897 | return CreateVoidPtrBuiltinVaListDecl(Context); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6898 | case TargetInfo::AArch64ABIBuiltinVaList: |
| 6899 | return CreateAArch64ABIBuiltinVaListDecl(Context); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6900 | case TargetInfo::PowerABIBuiltinVaList: |
| 6901 | return CreatePowerABIBuiltinVaListDecl(Context); |
| 6902 | case TargetInfo::X86_64ABIBuiltinVaList: |
| 6903 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
| 6904 | case TargetInfo::PNaClABIBuiltinVaList: |
| 6905 | return CreatePNaClABIBuiltinVaListDecl(Context); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6906 | case TargetInfo::AAPCSABIBuiltinVaList: |
| 6907 | return CreateAAPCSABIBuiltinVaListDecl(Context); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6908 | case TargetInfo::SystemZBuiltinVaList: |
| 6909 | return CreateSystemZBuiltinVaListDecl(Context); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6910 | } |
| 6911 | |
| 6912 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
| 6913 | } |
| 6914 | |
| 6915 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6916 | if (!BuiltinVaListDecl) { |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6917 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6918 | assert(BuiltinVaListDecl->isImplicit()); |
| 6919 | } |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6920 | |
| 6921 | return BuiltinVaListDecl; |
| 6922 | } |
| 6923 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6924 | Decl *ASTContext::getVaListTagDecl() const { |
| 6925 | // Force the creation of VaListTagDecl by building the __builtin_va_list |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 6926 | // declaration. |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6927 | if (!VaListTagDecl) |
| 6928 | (void)getBuiltinVaListDecl(); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 6929 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 6930 | return VaListTagDecl; |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 6931 | } |
| 6932 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 6933 | TypedefDecl *ASTContext::getBuiltinMSVaListDecl() const { |
| 6934 | if (!BuiltinMSVaListDecl) |
| 6935 | BuiltinMSVaListDecl = CreateMSVaListDecl(this); |
| 6936 | |
| 6937 | return BuiltinMSVaListDecl; |
| 6938 | } |
| 6939 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6940 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6941 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 6942 | "'NSConstantString' type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6943 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6944 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 6945 | } |
| 6946 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6947 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 6948 | /// lookup. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6949 | TemplateName |
| 6950 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 6951 | UnresolvedSetIterator End) const { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6952 | unsigned size = End - Begin; |
| 6953 | assert(size > 1 && "set is not overloaded!"); |
| 6954 | |
| 6955 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 6956 | size * sizeof(FunctionTemplateDecl*)); |
| 6957 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 6958 | |
| 6959 | NamedDecl **Storage = OT->getStorage(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6960 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6961 | NamedDecl *D = *I; |
| 6962 | assert(isa<FunctionTemplateDecl>(D) || |
| 6963 | (isa<UsingShadowDecl>(D) && |
| 6964 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 6965 | *Storage++ = D; |
| 6966 | } |
| 6967 | |
| 6968 | return TemplateName(OT); |
| 6969 | } |
| 6970 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6971 | /// \brief Retrieve the template name that represents a qualified |
| 6972 | /// template name such as \c std::vector. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6973 | TemplateName |
| 6974 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 6975 | bool TemplateKeyword, |
| 6976 | TemplateDecl *Template) const { |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 6977 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 6978 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6979 | // FIXME: Canonicalization? |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6980 | llvm::FoldingSetNodeID ID; |
| 6981 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 6982 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6983 | void *InsertPos = nullptr; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6984 | QualifiedTemplateName *QTN = |
| 6985 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6986 | if (!QTN) { |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 6987 | QTN = new (*this, alignof(QualifiedTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6988 | QualifiedTemplateName(NNS, TemplateKeyword, Template); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6989 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 6990 | } |
| 6991 | |
| 6992 | return TemplateName(QTN); |
| 6993 | } |
| 6994 | |
| 6995 | /// \brief Retrieve the template name that represents a dependent |
| 6996 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6997 | TemplateName |
| 6998 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 6999 | const IdentifierInfo *Name) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7000 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 7001 | "Nested name specifier must be dependent"); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7002 | |
| 7003 | llvm::FoldingSetNodeID ID; |
| 7004 | DependentTemplateName::Profile(ID, NNS, Name); |
| 7005 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7006 | void *InsertPos = nullptr; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7007 | DependentTemplateName *QTN = |
| 7008 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7009 | |
| 7010 | if (QTN) |
| 7011 | return TemplateName(QTN); |
| 7012 | |
| 7013 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 7014 | if (CanonNNS == NNS) { |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7015 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7016 | DependentTemplateName(NNS, Name); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7017 | } else { |
| 7018 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7019 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7020 | DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7021 | DependentTemplateName *CheckQTN = |
| 7022 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7023 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 7024 | (void)CheckQTN; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7025 | } |
| 7026 | |
| 7027 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 7028 | return TemplateName(QTN); |
| 7029 | } |
| 7030 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7031 | /// \brief Retrieve the template name that represents a dependent |
| 7032 | /// template name such as \c MetaFun::template operator+. |
| 7033 | TemplateName |
| 7034 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7035 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7036 | assert((!NNS || NNS->isDependent()) && |
| 7037 | "Nested name specifier must be dependent"); |
| 7038 | |
| 7039 | llvm::FoldingSetNodeID ID; |
| 7040 | DependentTemplateName::Profile(ID, NNS, Operator); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7041 | |
| 7042 | void *InsertPos = nullptr; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7043 | DependentTemplateName *QTN |
| 7044 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7045 | |
| 7046 | if (QTN) |
| 7047 | return TemplateName(QTN); |
| 7048 | |
| 7049 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 7050 | if (CanonNNS == NNS) { |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7051 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7052 | DependentTemplateName(NNS, Operator); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7053 | } else { |
| 7054 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7055 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7056 | DependentTemplateName(NNS, Operator, Canon); |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7057 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7058 | DependentTemplateName *CheckQTN |
| 7059 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7060 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 7061 | (void)CheckQTN; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7062 | } |
| 7063 | |
| 7064 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 7065 | return TemplateName(QTN); |
| 7066 | } |
| 7067 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7068 | TemplateName |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 7069 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 7070 | TemplateName replacement) const { |
| 7071 | llvm::FoldingSetNodeID ID; |
| 7072 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7073 | |
| 7074 | void *insertPos = nullptr; |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 7075 | SubstTemplateTemplateParmStorage *subst |
| 7076 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 7077 | |
| 7078 | if (!subst) { |
| 7079 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 7080 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 7081 | } |
| 7082 | |
| 7083 | return TemplateName(subst); |
| 7084 | } |
| 7085 | |
| 7086 | TemplateName |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7087 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 7088 | const TemplateArgument &ArgPack) const { |
| 7089 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 7090 | llvm::FoldingSetNodeID ID; |
| 7091 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7092 | |
| 7093 | void *InsertPos = nullptr; |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7094 | SubstTemplateTemplateParmPackStorage *Subst |
| 7095 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 7096 | |
| 7097 | if (!Subst) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 7098 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7099 | ArgPack.pack_size(), |
| 7100 | ArgPack.pack_begin()); |
| 7101 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 7102 | } |
| 7103 | |
| 7104 | return TemplateName(Subst); |
| 7105 | } |
| 7106 | |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7107 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | ab13857 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 7108 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 7109 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 7110 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7111 | switch (Type) { |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 7112 | case TargetInfo::NoInt: return CanQualType(); |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 7113 | case TargetInfo::SignedChar: return SignedCharTy; |
| 7114 | case TargetInfo::UnsignedChar: return UnsignedCharTy; |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7115 | case TargetInfo::SignedShort: return ShortTy; |
| 7116 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 7117 | case TargetInfo::SignedInt: return IntTy; |
| 7118 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 7119 | case TargetInfo::SignedLong: return LongTy; |
| 7120 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 7121 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 7122 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 7123 | } |
| 7124 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7125 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7126 | } |
Ted Kremenek | 77c51b2 | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 7127 | |
| 7128 | //===----------------------------------------------------------------------===// |
| 7129 | // Type Predicates. |
| 7130 | //===----------------------------------------------------------------------===// |
| 7131 | |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 7132 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 7133 | /// garbage collection attribute. |
| 7134 | /// |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7135 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7136 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7137 | return Qualifiers::GCNone; |
| 7138 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7139 | assert(getLangOpts().ObjC1); |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7140 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 7141 | |
| 7142 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 7143 | // (or pointers to them) be treated as though they were declared |
| 7144 | // as __strong. |
| 7145 | if (GCAttrs == Qualifiers::GCNone) { |
| 7146 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 7147 | return Qualifiers::Strong; |
| 7148 | else if (Ty->isPointerType()) |
| 7149 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 7150 | } else { |
| 7151 | // It's not valid to set GC attributes on anything that isn't a |
| 7152 | // pointer. |
| 7153 | #ifndef NDEBUG |
| 7154 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 7155 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 7156 | CT = AT->getElementType(); |
| 7157 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 7158 | #endif |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 7159 | } |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 7160 | return GCAttrs; |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 7161 | } |
| 7162 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7163 | //===----------------------------------------------------------------------===// |
| 7164 | // Type Compatibility Testing |
| 7165 | //===----------------------------------------------------------------------===// |
Chris Lattner | b338a6b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 7166 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7167 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7168 | /// compatible. |
| 7169 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 7170 | const VectorType *RHS) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 7171 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7172 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7173 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7174 | } |
| 7175 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7176 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 7177 | QualType SecondVec) { |
| 7178 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 7179 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 7180 | |
| 7181 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 7182 | return true; |
| 7183 | |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 7184 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 7185 | // equivalent GCC vector types. |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7186 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 7187 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 7188 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7189 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 7190 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 7191 | First->getVectorKind() != VectorType::AltiVecBool && |
| 7192 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 7193 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7194 | return true; |
| 7195 | |
| 7196 | return false; |
| 7197 | } |
| 7198 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7199 | //===----------------------------------------------------------------------===// |
| 7200 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 7201 | //===----------------------------------------------------------------------===// |
| 7202 | |
| 7203 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 7204 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7205 | bool |
| 7206 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 7207 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 7208 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7209 | return true; |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 7210 | for (auto *PI : rProto->protocols()) |
| 7211 | if (ProtocolCompatibleWithProtocol(lProto, PI)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7212 | return true; |
| 7213 | return false; |
| 7214 | } |
| 7215 | |
Dmitri Gribenko | 4364fcf | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 7216 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and |
| 7217 | /// Class<pr1, ...>. |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 7218 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 7219 | QualType rhs) { |
| 7220 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 7221 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 7222 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 7223 | |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7224 | for (auto *lhsProto : lhsQID->quals()) { |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 7225 | bool match = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7226 | for (auto *rhsProto : rhsOPT->quals()) { |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 7227 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 7228 | match = true; |
| 7229 | break; |
| 7230 | } |
| 7231 | } |
| 7232 | if (!match) |
| 7233 | return false; |
| 7234 | } |
| 7235 | return true; |
| 7236 | } |
| 7237 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7238 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 7239 | /// ObjCQualifiedIDType. |
| 7240 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 7241 | bool compare) { |
| 7242 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7243 | if (lhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7244 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 7245 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7246 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7247 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 7248 | return true; |
| 7249 | |
| 7250 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7251 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7252 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7253 | if (!rhsOPT) return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7254 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7255 | if (rhsOPT->qual_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7256 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7257 | // make sure we check the class hierarchy. |
| 7258 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7259 | for (auto *I : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7260 | // when comparing an id<P> on lhs with a static type on rhs, |
| 7261 | // see if static class implements all of id's protocols, directly or |
| 7262 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7263 | if (!rhsID->ClassImplementsProtocol(I, true)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7264 | return false; |
| 7265 | } |
| 7266 | } |
| 7267 | // If there are no qualifiers and no interface, we have an 'id'. |
| 7268 | return true; |
| 7269 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7270 | // Both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7271 | for (auto *lhsProto : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7272 | bool match = false; |
| 7273 | |
| 7274 | // when comparing an id<P> on lhs with a static type on rhs, |
| 7275 | // see if static class implements all of id's protocols, directly or |
| 7276 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7277 | for (auto *rhsProto : rhsOPT->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7278 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 7279 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 7280 | match = true; |
| 7281 | break; |
| 7282 | } |
| 7283 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7284 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7285 | // make sure we check the class hierarchy. |
| 7286 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7287 | for (auto *I : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7288 | // when comparing an id<P> on lhs with a static type on rhs, |
| 7289 | // see if static class implements all of id's protocols, directly or |
| 7290 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7291 | if (rhsID->ClassImplementsProtocol(I, true)) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7292 | match = true; |
| 7293 | break; |
| 7294 | } |
| 7295 | } |
| 7296 | } |
| 7297 | if (!match) |
| 7298 | return false; |
| 7299 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7300 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7301 | return true; |
| 7302 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7303 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7304 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 7305 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 7306 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7307 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7308 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7309 | // If both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7310 | for (auto *lhsProto : lhsOPT->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7311 | bool match = false; |
| 7312 | |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7313 | // 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] | 7314 | // see if static class implements all of id's protocols, directly or |
| 7315 | // through its super class and categories. |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7316 | // First, lhs protocols in the qualifier list must be found, direct |
| 7317 | // or indirect in rhs's qualifier list or it is a mismatch. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7318 | for (auto *rhsProto : rhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7319 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 7320 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 7321 | match = true; |
| 7322 | break; |
| 7323 | } |
| 7324 | } |
| 7325 | if (!match) |
| 7326 | return false; |
| 7327 | } |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7328 | |
| 7329 | // Static class's protocols, or its super class or category protocols |
| 7330 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 7331 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 7332 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 7333 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 7334 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 7335 | // no type qualifier and its class has no static protocol(s) |
| 7336 | // assume that it is mismatch. |
| 7337 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 7338 | return false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7339 | for (auto *lhsProto : LHSInheritedProtocols) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7340 | bool match = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7341 | for (auto *rhsProto : rhsQID->quals()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7342 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 7343 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 7344 | match = true; |
| 7345 | break; |
| 7346 | } |
| 7347 | } |
| 7348 | if (!match) |
| 7349 | return false; |
| 7350 | } |
| 7351 | } |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7352 | return true; |
| 7353 | } |
| 7354 | return false; |
| 7355 | } |
| 7356 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7357 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7358 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 7359 | /// protocol qualifiers on the LHS or RHS. |
| 7360 | /// |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7361 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 7362 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7363 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 7364 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 7365 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 7366 | // 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] | 7367 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 7368 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7369 | return true; |
| 7370 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7371 | // Function object that propagates a successful result or handles |
| 7372 | // __kindof types. |
| 7373 | auto finish = [&](bool succeeded) -> bool { |
| 7374 | if (succeeded) |
| 7375 | return true; |
| 7376 | |
| 7377 | if (!RHS->isKindOfType()) |
| 7378 | return false; |
| 7379 | |
| 7380 | // Strip off __kindof and protocol qualifiers, then check whether |
| 7381 | // we can assign the other way. |
| 7382 | return canAssignObjCInterfaces(RHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 7383 | LHSOPT->stripObjCKindOfTypeAndQuals(*this)); |
| 7384 | }; |
| 7385 | |
| 7386 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) { |
| 7387 | return finish(ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 7388 | QualType(RHSOPT,0), |
| 7389 | false)); |
| 7390 | } |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 7391 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7392 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) { |
| 7393 | return finish(ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 7394 | QualType(RHSOPT,0))); |
| 7395 | } |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 7396 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7397 | // If we have 2 user-defined types, fall into that path. |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7398 | if (LHS->getInterface() && RHS->getInterface()) { |
| 7399 | return finish(canAssignObjCInterfaces(LHS, RHS)); |
| 7400 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7401 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7402 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7403 | } |
| 7404 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7405 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 7406 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7407 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 7408 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 7409 | /// not OK. For the return type, the opposite is not OK. |
| 7410 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 7411 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7412 | const ObjCObjectPointerType *RHSOPT, |
| 7413 | bool BlockReturnType) { |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7414 | |
| 7415 | // Function object that propagates a successful result or handles |
| 7416 | // __kindof types. |
| 7417 | auto finish = [&](bool succeeded) -> bool { |
| 7418 | if (succeeded) |
| 7419 | return true; |
| 7420 | |
| 7421 | const ObjCObjectPointerType *Expected = BlockReturnType ? RHSOPT : LHSOPT; |
| 7422 | if (!Expected->isKindOfType()) |
| 7423 | return false; |
| 7424 | |
| 7425 | // Strip off __kindof and protocol qualifiers, then check whether |
| 7426 | // we can assign the other way. |
| 7427 | return canAssignObjCInterfacesInBlockPointer( |
| 7428 | RHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 7429 | LHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 7430 | BlockReturnType); |
| 7431 | }; |
| 7432 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 7433 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7434 | return true; |
| 7435 | |
| 7436 | if (LHSOPT->isObjCBuiltinType()) { |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7437 | return finish(RHSOPT->isObjCBuiltinType() || |
| 7438 | RHSOPT->isObjCQualifiedIdType()); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7439 | } |
| 7440 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 7441 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7442 | return finish(ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 7443 | QualType(RHSOPT,0), |
| 7444 | false)); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7445 | |
| 7446 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 7447 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 7448 | if (LHS && RHS) { // We have 2 user-defined types. |
| 7449 | if (LHS != RHS) { |
| 7450 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7451 | return finish(BlockReturnType); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7452 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7453 | return finish(!BlockReturnType); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7454 | } |
| 7455 | else |
| 7456 | return true; |
| 7457 | } |
| 7458 | return false; |
| 7459 | } |
| 7460 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7461 | /// Comparison routine for Objective-C protocols to be used with |
| 7462 | /// llvm::array_pod_sort. |
| 7463 | static int compareObjCProtocolsByName(ObjCProtocolDecl * const *lhs, |
| 7464 | ObjCProtocolDecl * const *rhs) { |
| 7465 | return (*lhs)->getName().compare((*rhs)->getName()); |
| 7466 | |
| 7467 | } |
| 7468 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7469 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7470 | /// of protocols inherited from two distinct objective-c pointer objects with |
| 7471 | /// the given common base. |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7472 | /// It is used to build composite qualifier list of the composite type of |
| 7473 | /// the conditional expression involving two objective-c pointer objects. |
| 7474 | static |
| 7475 | void getIntersectionOfProtocols(ASTContext &Context, |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7476 | const ObjCInterfaceDecl *CommonBase, |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7477 | const ObjCObjectPointerType *LHSOPT, |
| 7478 | const ObjCObjectPointerType *RHSOPT, |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7479 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionSet) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7480 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7481 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 7482 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 7483 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 7484 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7485 | |
| 7486 | // Add all of the protocols for the LHS. |
| 7487 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSProtocolSet; |
| 7488 | |
| 7489 | // Start with the protocol qualifiers. |
| 7490 | for (auto proto : LHS->quals()) { |
| 7491 | Context.CollectInheritedProtocols(proto, LHSProtocolSet); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7492 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7493 | |
| 7494 | // Also add the protocols associated with the LHS interface. |
| 7495 | Context.CollectInheritedProtocols(LHS->getInterface(), LHSProtocolSet); |
| 7496 | |
| 7497 | // Add all of the protocls for the RHS. |
| 7498 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSProtocolSet; |
| 7499 | |
| 7500 | // Start with the protocol qualifiers. |
| 7501 | for (auto proto : RHS->quals()) { |
| 7502 | Context.CollectInheritedProtocols(proto, RHSProtocolSet); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7503 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7504 | |
| 7505 | // Also add the protocols associated with the RHS interface. |
| 7506 | Context.CollectInheritedProtocols(RHS->getInterface(), RHSProtocolSet); |
| 7507 | |
| 7508 | // Compute the intersection of the collected protocol sets. |
| 7509 | for (auto proto : LHSProtocolSet) { |
| 7510 | if (RHSProtocolSet.count(proto)) |
| 7511 | IntersectionSet.push_back(proto); |
| 7512 | } |
| 7513 | |
| 7514 | // Compute the set of protocols that is implied by either the common type or |
| 7515 | // the protocols within the intersection. |
| 7516 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ImpliedProtocols; |
| 7517 | Context.CollectInheritedProtocols(CommonBase, ImpliedProtocols); |
| 7518 | |
| 7519 | // Remove any implied protocols from the list of inherited protocols. |
| 7520 | if (!ImpliedProtocols.empty()) { |
| 7521 | IntersectionSet.erase( |
| 7522 | std::remove_if(IntersectionSet.begin(), |
| 7523 | IntersectionSet.end(), |
| 7524 | [&](ObjCProtocolDecl *proto) -> bool { |
| 7525 | return ImpliedProtocols.count(proto) > 0; |
| 7526 | }), |
| 7527 | IntersectionSet.end()); |
| 7528 | } |
| 7529 | |
| 7530 | // Sort the remaining protocols by name. |
| 7531 | llvm::array_pod_sort(IntersectionSet.begin(), IntersectionSet.end(), |
| 7532 | compareObjCProtocolsByName); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7533 | } |
| 7534 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7535 | /// Determine whether the first type is a subtype of the second. |
| 7536 | static bool canAssignObjCObjectTypes(ASTContext &ctx, QualType lhs, |
| 7537 | QualType rhs) { |
| 7538 | // Common case: two object pointers. |
| 7539 | const ObjCObjectPointerType *lhsOPT = lhs->getAs<ObjCObjectPointerType>(); |
| 7540 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 7541 | if (lhsOPT && rhsOPT) |
| 7542 | return ctx.canAssignObjCInterfaces(lhsOPT, rhsOPT); |
| 7543 | |
| 7544 | // Two block pointers. |
| 7545 | const BlockPointerType *lhsBlock = lhs->getAs<BlockPointerType>(); |
| 7546 | const BlockPointerType *rhsBlock = rhs->getAs<BlockPointerType>(); |
| 7547 | if (lhsBlock && rhsBlock) |
| 7548 | return ctx.typesAreBlockPointerCompatible(lhs, rhs); |
| 7549 | |
| 7550 | // If either is an unqualified 'id' and the other is a block, it's |
| 7551 | // acceptable. |
| 7552 | if ((lhsOPT && lhsOPT->isObjCIdType() && rhsBlock) || |
| 7553 | (rhsOPT && rhsOPT->isObjCIdType() && lhsBlock)) |
| 7554 | return true; |
| 7555 | |
| 7556 | return false; |
| 7557 | } |
| 7558 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7559 | // Check that the given Objective-C type argument lists are equivalent. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7560 | static bool sameObjCTypeArgs(ASTContext &ctx, |
| 7561 | const ObjCInterfaceDecl *iface, |
| 7562 | ArrayRef<QualType> lhsArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7563 | ArrayRef<QualType> rhsArgs, |
| 7564 | bool stripKindOf) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7565 | if (lhsArgs.size() != rhsArgs.size()) |
| 7566 | return false; |
| 7567 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7568 | ObjCTypeParamList *typeParams = iface->getTypeParamList(); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7569 | for (unsigned i = 0, n = lhsArgs.size(); i != n; ++i) { |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7570 | if (ctx.hasSameType(lhsArgs[i], rhsArgs[i])) |
| 7571 | continue; |
| 7572 | |
| 7573 | switch (typeParams->begin()[i]->getVariance()) { |
| 7574 | case ObjCTypeParamVariance::Invariant: |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7575 | if (!stripKindOf || |
| 7576 | !ctx.hasSameType(lhsArgs[i].stripObjCKindOfType(ctx), |
| 7577 | rhsArgs[i].stripObjCKindOfType(ctx))) { |
| 7578 | return false; |
| 7579 | } |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7580 | break; |
| 7581 | |
| 7582 | case ObjCTypeParamVariance::Covariant: |
| 7583 | if (!canAssignObjCObjectTypes(ctx, lhsArgs[i], rhsArgs[i])) |
| 7584 | return false; |
| 7585 | break; |
| 7586 | |
| 7587 | case ObjCTypeParamVariance::Contravariant: |
| 7588 | if (!canAssignObjCObjectTypes(ctx, rhsArgs[i], lhsArgs[i])) |
| 7589 | return false; |
| 7590 | break; |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7591 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7592 | } |
| 7593 | |
| 7594 | return true; |
| 7595 | } |
| 7596 | |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 7597 | QualType ASTContext::areCommonBaseCompatible( |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7598 | const ObjCObjectPointerType *Lptr, |
| 7599 | const ObjCObjectPointerType *Rptr) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7600 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 7601 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 7602 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 7603 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7604 | |
| 7605 | if (!LDecl || !RDecl) |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 7606 | return QualType(); |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7607 | |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 7608 | // When either LHS or RHS is a kindof type, we should return a kindof type. |
| 7609 | // For example, for common base of kindof(ASub1) and kindof(ASub2), we return |
| 7610 | // kindof(A). |
| 7611 | bool anyKindOf = LHS->isKindOfType() || RHS->isKindOfType(); |
| 7612 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7613 | // Follow the left-hand side up the class hierarchy until we either hit a |
| 7614 | // root or find the RHS. Record the ancestors in case we don't find it. |
| 7615 | llvm::SmallDenseMap<const ObjCInterfaceDecl *, const ObjCObjectType *, 4> |
| 7616 | LHSAncestors; |
| 7617 | while (true) { |
| 7618 | // Record this ancestor. We'll need this if the common type isn't in the |
| 7619 | // path from the LHS to the root. |
| 7620 | LHSAncestors[LHS->getInterface()->getCanonicalDecl()] = LHS; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7621 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7622 | if (declaresSameEntity(LHS->getInterface(), RDecl)) { |
| 7623 | // Get the type arguments. |
| 7624 | ArrayRef<QualType> LHSTypeArgs = LHS->getTypeArgsAsWritten(); |
| 7625 | bool anyChanges = false; |
| 7626 | if (LHS->isSpecialized() && RHS->isSpecialized()) { |
| 7627 | // Both have type arguments, compare them. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7628 | if (!sameObjCTypeArgs(*this, LHS->getInterface(), |
| 7629 | LHS->getTypeArgs(), RHS->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7630 | /*stripKindOf=*/true)) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7631 | return QualType(); |
| 7632 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { |
| 7633 | // If only one has type arguments, the result will not have type |
| 7634 | // arguments. |
| 7635 | LHSTypeArgs = { }; |
| 7636 | anyChanges = true; |
| 7637 | } |
| 7638 | |
| 7639 | // Compute the intersection of protocols. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7640 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7641 | getIntersectionOfProtocols(*this, LHS->getInterface(), Lptr, Rptr, |
| 7642 | Protocols); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7643 | if (!Protocols.empty()) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7644 | anyChanges = true; |
| 7645 | |
| 7646 | // If anything in the LHS will have changed, build a new result type. |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 7647 | // If we need to return a kindof type but LHS is not a kindof type, we |
| 7648 | // build a new result type. |
| 7649 | if (anyChanges || LHS->isKindOfType() != anyKindOf) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7650 | QualType Result = getObjCInterfaceType(LHS->getInterface()); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7651 | Result = getObjCObjectType(Result, LHSTypeArgs, Protocols, |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 7652 | anyKindOf || LHS->isKindOfType()); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7653 | return getObjCObjectPointerType(Result); |
| 7654 | } |
| 7655 | |
| 7656 | return getObjCObjectPointerType(QualType(LHS, 0)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7657 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7658 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7659 | // Find the superclass. |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7660 | QualType LHSSuperType = LHS->getSuperClassType(); |
| 7661 | if (LHSSuperType.isNull()) |
| 7662 | break; |
| 7663 | |
| 7664 | LHS = LHSSuperType->castAs<ObjCObjectType>(); |
| 7665 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7666 | |
| 7667 | // We didn't find anything by following the LHS to its root; now check |
| 7668 | // the RHS against the cached set of ancestors. |
| 7669 | while (true) { |
| 7670 | auto KnownLHS = LHSAncestors.find(RHS->getInterface()->getCanonicalDecl()); |
| 7671 | if (KnownLHS != LHSAncestors.end()) { |
| 7672 | LHS = KnownLHS->second; |
| 7673 | |
| 7674 | // Get the type arguments. |
| 7675 | ArrayRef<QualType> RHSTypeArgs = RHS->getTypeArgsAsWritten(); |
| 7676 | bool anyChanges = false; |
| 7677 | if (LHS->isSpecialized() && RHS->isSpecialized()) { |
| 7678 | // Both have type arguments, compare them. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7679 | if (!sameObjCTypeArgs(*this, LHS->getInterface(), |
| 7680 | LHS->getTypeArgs(), RHS->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7681 | /*stripKindOf=*/true)) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7682 | return QualType(); |
| 7683 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { |
| 7684 | // If only one has type arguments, the result will not have type |
| 7685 | // arguments. |
| 7686 | RHSTypeArgs = { }; |
| 7687 | anyChanges = true; |
| 7688 | } |
| 7689 | |
| 7690 | // Compute the intersection of protocols. |
| 7691 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 7692 | getIntersectionOfProtocols(*this, RHS->getInterface(), Lptr, Rptr, |
| 7693 | Protocols); |
| 7694 | if (!Protocols.empty()) |
| 7695 | anyChanges = true; |
| 7696 | |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 7697 | // If we need to return a kindof type but RHS is not a kindof type, we |
| 7698 | // build a new result type. |
| 7699 | if (anyChanges || RHS->isKindOfType() != anyKindOf) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7700 | QualType Result = getObjCInterfaceType(RHS->getInterface()); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7701 | Result = getObjCObjectType(Result, RHSTypeArgs, Protocols, |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 7702 | anyKindOf || RHS->isKindOfType()); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7703 | return getObjCObjectPointerType(Result); |
| 7704 | } |
| 7705 | |
| 7706 | return getObjCObjectPointerType(QualType(RHS, 0)); |
| 7707 | } |
| 7708 | |
| 7709 | // Find the superclass of the RHS. |
| 7710 | QualType RHSSuperType = RHS->getSuperClassType(); |
| 7711 | if (RHSSuperType.isNull()) |
| 7712 | break; |
| 7713 | |
| 7714 | RHS = RHSSuperType->castAs<ObjCObjectType>(); |
| 7715 | } |
| 7716 | |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 7717 | return QualType(); |
| 7718 | } |
| 7719 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7720 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 7721 | const ObjCObjectType *RHS) { |
| 7722 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 7723 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 7724 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7725 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 7726 | // the LHS. |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7727 | ObjCInterfaceDecl *LHSInterface = LHS->getInterface(); |
| 7728 | bool IsSuperClass = LHSInterface->isSuperClassOf(RHS->getInterface()); |
| 7729 | if (!IsSuperClass) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7730 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7731 | |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7732 | // If the LHS has protocol qualifiers, determine whether all of them are |
| 7733 | // satisfied by the RHS (i.e., the RHS has a superset of the protocols in the |
| 7734 | // LHS). |
| 7735 | if (LHS->getNumProtocols() > 0) { |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 7736 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 7737 | // ; i.e., SuperClass may implement at least one of the protocols |
| 7738 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 7739 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 7740 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
| 7741 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
| 7742 | // Also, if RHS has explicit quelifiers, include them for comparing with LHS's |
| 7743 | // qualifiers. |
| 7744 | for (auto *RHSPI : RHS->quals()) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7745 | CollectInheritedProtocols(RHSPI, SuperClassInheritedProtocols); |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 7746 | // If there is no protocols associated with RHS, it is not a match. |
| 7747 | if (SuperClassInheritedProtocols.empty()) |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 7748 | return false; |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 7749 | |
| 7750 | for (const auto *LHSProto : LHS->quals()) { |
| 7751 | bool SuperImplementsProtocol = false; |
| 7752 | for (auto *SuperClassProto : SuperClassInheritedProtocols) |
| 7753 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 7754 | SuperImplementsProtocol = true; |
| 7755 | break; |
| 7756 | } |
| 7757 | if (!SuperImplementsProtocol) |
| 7758 | return false; |
| 7759 | } |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7760 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7761 | |
| 7762 | // If the LHS is specialized, we may need to check type arguments. |
| 7763 | if (LHS->isSpecialized()) { |
| 7764 | // Follow the superclass chain until we've matched the LHS class in the |
| 7765 | // hierarchy. This substitutes type arguments through. |
| 7766 | const ObjCObjectType *RHSSuper = RHS; |
| 7767 | while (!declaresSameEntity(RHSSuper->getInterface(), LHSInterface)) |
| 7768 | RHSSuper = RHSSuper->getSuperClassType()->castAs<ObjCObjectType>(); |
| 7769 | |
| 7770 | // If the RHS is specializd, compare type arguments. |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7771 | if (RHSSuper->isSpecialized() && |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7772 | !sameObjCTypeArgs(*this, LHS->getInterface(), |
| 7773 | LHS->getTypeArgs(), RHSSuper->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7774 | /*stripKindOf=*/true)) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7775 | return false; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 7776 | } |
| 7777 | } |
| 7778 | |
| 7779 | return true; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7780 | } |
| 7781 | |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 7782 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 7783 | // get the "pointed to" types |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7784 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 7785 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7786 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7787 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 7788 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7789 | |
| 7790 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 7791 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 7792 | } |
| 7793 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 7794 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 7795 | return canAssignObjCInterfaces( |
| 7796 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 7797 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 7798 | } |
| 7799 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7800 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7801 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7802 | /// 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] | 7803 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7804 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 7805 | bool CompareUnqualified) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7806 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 7807 | return hasSameType(LHS, RHS); |
Joey Gouly | 5788b78 | 2016-11-18 14:10:54 +0000 | [diff] [blame] | 7808 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7809 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7810 | } |
| 7811 | |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 7812 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | c87c879 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 7813 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 7814 | } |
| 7815 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7816 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 7817 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 7818 | } |
| 7819 | |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 7820 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 7821 | /// of T is compatible with SubType, return the merged type, else return |
| 7822 | /// QualType() |
| 7823 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 7824 | bool OfBlockPointer, |
| 7825 | bool Unqualified) { |
| 7826 | if (const RecordType *UT = T->getAsUnionType()) { |
| 7827 | RecordDecl *UD = UT->getDecl(); |
| 7828 | if (UD->hasAttr<TransparentUnionAttr>()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 7829 | for (const auto *I : UD->fields()) { |
| 7830 | QualType ET = I->getType().getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 7831 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 7832 | if (!MT.isNull()) |
| 7833 | return MT; |
| 7834 | } |
| 7835 | } |
| 7836 | } |
| 7837 | |
| 7838 | return QualType(); |
| 7839 | } |
| 7840 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7841 | /// mergeFunctionParameterTypes - merge two types which appear as function |
| 7842 | /// parameter types |
| 7843 | QualType ASTContext::mergeFunctionParameterTypes(QualType lhs, QualType rhs, |
| 7844 | bool OfBlockPointer, |
| 7845 | bool Unqualified) { |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 7846 | // GNU extension: two types are compatible if they appear as a function |
| 7847 | // argument, one of the types is a transparent union type and the other |
| 7848 | // type is compatible with a union member |
| 7849 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 7850 | Unqualified); |
| 7851 | if (!lmerge.isNull()) |
| 7852 | return lmerge; |
| 7853 | |
| 7854 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 7855 | Unqualified); |
| 7856 | if (!rmerge.isNull()) |
| 7857 | return rmerge; |
| 7858 | |
| 7859 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 7860 | } |
| 7861 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7862 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7863 | bool OfBlockPointer, |
| 7864 | bool Unqualified) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7865 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 7866 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7867 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 7868 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7869 | bool allLTypes = true; |
| 7870 | bool allRTypes = true; |
| 7871 | |
| 7872 | // Check return type |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7873 | QualType retType; |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 7874 | if (OfBlockPointer) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7875 | QualType RHS = rbase->getReturnType(); |
| 7876 | QualType LHS = lbase->getReturnType(); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 7877 | bool UnqualifiedResult = Unqualified; |
| 7878 | if (!UnqualifiedResult) |
| 7879 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7880 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 7881 | } |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7882 | else |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7883 | retType = mergeTypes(lbase->getReturnType(), rbase->getReturnType(), false, |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7884 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7885 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7886 | |
| 7887 | if (Unqualified) |
| 7888 | retType = retType.getUnqualifiedType(); |
| 7889 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7890 | CanQualType LRetType = getCanonicalType(lbase->getReturnType()); |
| 7891 | CanQualType RRetType = getCanonicalType(rbase->getReturnType()); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7892 | if (Unqualified) { |
| 7893 | LRetType = LRetType.getUnqualifiedType(); |
| 7894 | RRetType = RRetType.getUnqualifiedType(); |
| 7895 | } |
| 7896 | |
| 7897 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7898 | allLTypes = false; |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7899 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7900 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7901 | |
Daniel Dunbar | edd5bae | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 7902 | // FIXME: double check this |
| 7903 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 7904 | // rbase->getRegParmAttr() != 0 && |
| 7905 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 7906 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 7907 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7908 | |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 7909 | // Compatible functions must have compatible calling conventions |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 7910 | if (lbaseInfo.getCC() != rbaseInfo.getCC()) |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 7911 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7912 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7913 | // Regparm is part of the calling convention. |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 7914 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 7915 | return QualType(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7916 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 7917 | return QualType(); |
| 7918 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7919 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 7920 | return QualType(); |
| 7921 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7922 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 7923 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7924 | |
Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 7925 | if (lbaseInfo.getNoReturn() != NoReturn) |
| 7926 | allLTypes = false; |
| 7927 | if (rbaseInfo.getNoReturn() != NoReturn) |
| 7928 | allRTypes = false; |
| 7929 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7930 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7931 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7932 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 7933 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 7934 | "C++ shouldn't be here"); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7935 | // Compatible functions must have the same number of parameters |
| 7936 | if (lproto->getNumParams() != rproto->getNumParams()) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7937 | return QualType(); |
| 7938 | |
| 7939 | // Variadic and non-variadic functions aren't compatible |
| 7940 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 7941 | return QualType(); |
| 7942 | |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 7943 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 7944 | return QualType(); |
| 7945 | |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 7946 | if (!doFunctionTypesMatchOnExtParameterInfos(rproto, lproto)) |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7947 | return QualType(); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7948 | |
| 7949 | // Check parameter type compatibility |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7950 | SmallVector<QualType, 10> types; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7951 | for (unsigned i = 0, n = lproto->getNumParams(); i < n; i++) { |
| 7952 | QualType lParamType = lproto->getParamType(i).getUnqualifiedType(); |
| 7953 | QualType rParamType = rproto->getParamType(i).getUnqualifiedType(); |
| 7954 | QualType paramType = mergeFunctionParameterTypes( |
| 7955 | lParamType, rParamType, OfBlockPointer, Unqualified); |
| 7956 | if (paramType.isNull()) |
| 7957 | return QualType(); |
| 7958 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7959 | if (Unqualified) |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7960 | paramType = paramType.getUnqualifiedType(); |
| 7961 | |
| 7962 | types.push_back(paramType); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7963 | if (Unqualified) { |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7964 | lParamType = lParamType.getUnqualifiedType(); |
| 7965 | rParamType = rParamType.getUnqualifiedType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7966 | } |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7967 | |
| 7968 | if (getCanonicalType(paramType) != getCanonicalType(lParamType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7969 | allLTypes = false; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7970 | if (getCanonicalType(paramType) != getCanonicalType(rParamType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7971 | allRTypes = false; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7972 | } |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7973 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7974 | if (allLTypes) return lhs; |
| 7975 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7976 | |
| 7977 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 7978 | EPI.ExtInfo = einfo; |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 7979 | return getFunctionType(retType, types, EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7980 | } |
| 7981 | |
| 7982 | if (lproto) allRTypes = false; |
| 7983 | if (rproto) allLTypes = false; |
| 7984 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7985 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7986 | if (proto) { |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 7987 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7988 | if (proto->isVariadic()) return QualType(); |
| 7989 | // Check that the types are compatible with the types that |
| 7990 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 7991 | // The only types actually affected are promotable integer |
| 7992 | // types and floats, which would be passed as a different |
| 7993 | // type depending on whether the prototype is visible. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7994 | for (unsigned i = 0, n = proto->getNumParams(); i < n; ++i) { |
| 7995 | QualType paramTy = proto->getParamType(i); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7996 | |
Eli Friedman | 448ce40 | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 7997 | // 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] | 7998 | // to pass enum values. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7999 | if (const EnumType *Enum = paramTy->getAs<EnumType>()) { |
| 8000 | paramTy = Enum->getDecl()->getIntegerType(); |
| 8001 | if (paramTy.isNull()) |
Eli Friedman | 448ce40 | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 8002 | return QualType(); |
| 8003 | } |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8004 | |
| 8005 | if (paramTy->isPromotableIntegerType() || |
| 8006 | getCanonicalType(paramTy).getUnqualifiedType() == FloatTy) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8007 | return QualType(); |
| 8008 | } |
| 8009 | |
| 8010 | if (allLTypes) return lhs; |
| 8011 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8012 | |
| 8013 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 8014 | EPI.ExtInfo = einfo; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8015 | return getFunctionType(retType, proto->getParamTypes(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8016 | } |
| 8017 | |
| 8018 | if (allLTypes) return lhs; |
| 8019 | if (allRTypes) return rhs; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8020 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8021 | } |
| 8022 | |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8023 | /// Given that we have an enum type and a non-enum type, try to merge them. |
| 8024 | static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET, |
| 8025 | QualType other, bool isBlockReturnType) { |
| 8026 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 8027 | // a signed integer type, or an unsigned integer type. |
| 8028 | // Compatibility is based on the underlying type, not the promotion |
| 8029 | // type. |
| 8030 | QualType underlyingType = ET->getDecl()->getIntegerType(); |
| 8031 | if (underlyingType.isNull()) return QualType(); |
| 8032 | if (Context.hasSameType(underlyingType, other)) |
| 8033 | return other; |
| 8034 | |
| 8035 | // In block return types, we're more permissive and accept any |
| 8036 | // integral type of the same size. |
| 8037 | if (isBlockReturnType && other->isIntegerType() && |
| 8038 | Context.getTypeSize(underlyingType) == Context.getTypeSize(other)) |
| 8039 | return other; |
| 8040 | |
| 8041 | return QualType(); |
| 8042 | } |
| 8043 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8044 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8045 | bool OfBlockPointer, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 8046 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | db4e349 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 8047 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 8048 | // type is adjusted to "T" prior to any further analysis, the expression |
| 8049 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 8050 | // expression is an lvalue unless the reference is an rvalue reference and |
| 8051 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 8052 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 8053 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8054 | |
| 8055 | if (Unqualified) { |
| 8056 | LHS = LHS.getUnqualifiedType(); |
| 8057 | RHS = RHS.getUnqualifiedType(); |
| 8058 | } |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 8059 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8060 | QualType LHSCan = getCanonicalType(LHS), |
| 8061 | RHSCan = getCanonicalType(RHS); |
| 8062 | |
| 8063 | // If two types are identical, they are compatible. |
| 8064 | if (LHSCan == RHSCan) |
| 8065 | return LHS; |
| 8066 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8067 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 8068 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 8069 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8070 | if (LQuals != RQuals) { |
| 8071 | // If any of these qualifiers are different, we have a type |
| 8072 | // mismatch. |
| 8073 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8074 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
Roger Ferrer Ibanez | d93add3 | 2017-02-24 08:41:09 +0000 | [diff] [blame] | 8075 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime() || |
| 8076 | LQuals.hasUnaligned() != RQuals.hasUnaligned()) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8077 | return QualType(); |
| 8078 | |
| 8079 | // Exactly one GC qualifier difference is allowed: __strong is |
| 8080 | // okay if the other type has no GC qualifier but is an Objective |
| 8081 | // C object pointer (i.e. implicitly strong by default). We fix |
| 8082 | // this by pretending that the unqualified type was actually |
| 8083 | // qualified __strong. |
| 8084 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 8085 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 8086 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 8087 | |
| 8088 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 8089 | return QualType(); |
| 8090 | |
| 8091 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 8092 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 8093 | } |
| 8094 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 8095 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 8096 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8097 | return QualType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8098 | } |
| 8099 | |
| 8100 | // Okay, qualifiers are equal. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8101 | |
Eli Friedman | dcca633 | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 8102 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 8103 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8104 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8105 | // We want to consider the two function types to be the same for these |
| 8106 | // comparisons, just force one to the other. |
| 8107 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 8108 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 16f9096 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 8109 | |
| 8110 | // Same as above for arrays |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 8111 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 8112 | LHSClass = Type::ConstantArray; |
| 8113 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 8114 | RHSClass = Type::ConstantArray; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8115 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8116 | // ObjCInterfaces are just specialized ObjCObjects. |
| 8117 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 8118 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 8119 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 8120 | // Canonicalize ExtVector -> Vector. |
| 8121 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 8122 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8123 | |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 8124 | // If the canonical type classes don't match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8125 | if (LHSClass != RHSClass) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8126 | // Note that we only have special rules for turning block enum |
| 8127 | // returns into block int returns, not vice-versa. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 8128 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8129 | return mergeEnumWithInteger(*this, ETy, RHS, false); |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 8130 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 8131 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8132 | return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType); |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 8133 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 8134 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 194904e | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 8135 | if (OfBlockPointer && !BlockReturnType) { |
| 8136 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 8137 | return LHS; |
| 8138 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 8139 | return RHS; |
| 8140 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 8141 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8142 | return QualType(); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 8143 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8144 | |
Steve Naroff | c6edcbd | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 8145 | // The canonical type classes match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8146 | switch (LHSClass) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8147 | #define TYPE(Class, Base) |
| 8148 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 8149 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8150 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 8151 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 8152 | #include "clang/AST/TypeNodes.def" |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8153 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8154 | |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 8155 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 8156 | case Type::DeducedTemplateSpecialization: |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 8157 | case Type::LValueReference: |
| 8158 | case Type::RValueReference: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8159 | case Type::MemberPointer: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8160 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8161 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8162 | case Type::ObjCInterface: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8163 | case Type::IncompleteArray: |
| 8164 | case Type::VariableArray: |
| 8165 | case Type::FunctionProto: |
| 8166 | case Type::ExtVector: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8167 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8168 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8169 | case Type::Pointer: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8170 | { |
| 8171 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 8172 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 8173 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8174 | if (Unqualified) { |
| 8175 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 8176 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 8177 | } |
| 8178 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 8179 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8180 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 8181 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8182 | return LHS; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 8183 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8184 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8185 | return getPointerType(ResultType); |
| 8186 | } |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 8187 | case Type::BlockPointer: |
| 8188 | { |
| 8189 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 8190 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 8191 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8192 | if (Unqualified) { |
| 8193 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 8194 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 8195 | } |
Anastasia Stulova | 81a25e35 | 2017-03-10 15:23:07 +0000 | [diff] [blame] | 8196 | if (getLangOpts().OpenCL) { |
| 8197 | Qualifiers LHSPteeQual = LHSPointee.getQualifiers(); |
| 8198 | Qualifiers RHSPteeQual = RHSPointee.getQualifiers(); |
| 8199 | // Blocks can't be an expression in a ternary operator (OpenCL v2.0 |
| 8200 | // 6.12.5) thus the following check is asymmetric. |
| 8201 | if (!LHSPteeQual.isAddressSpaceSupersetOf(RHSPteeQual)) |
| 8202 | return QualType(); |
| 8203 | LHSPteeQual.removeAddressSpace(); |
| 8204 | RHSPteeQual.removeAddressSpace(); |
| 8205 | LHSPointee = |
| 8206 | QualType(LHSPointee.getTypePtr(), LHSPteeQual.getAsOpaqueValue()); |
| 8207 | RHSPointee = |
| 8208 | QualType(RHSPointee.getTypePtr(), RHSPteeQual.getAsOpaqueValue()); |
| 8209 | } |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8210 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 8211 | Unqualified); |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 8212 | if (ResultType.isNull()) return QualType(); |
| 8213 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 8214 | return LHS; |
| 8215 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 8216 | return RHS; |
| 8217 | return getBlockPointerType(ResultType); |
| 8218 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 8219 | case Type::Atomic: |
| 8220 | { |
| 8221 | // Merge two pointer types, while trying to preserve typedef info |
| 8222 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 8223 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 8224 | if (Unqualified) { |
| 8225 | LHSValue = LHSValue.getUnqualifiedType(); |
| 8226 | RHSValue = RHSValue.getUnqualifiedType(); |
| 8227 | } |
| 8228 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 8229 | Unqualified); |
| 8230 | if (ResultType.isNull()) return QualType(); |
| 8231 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 8232 | return LHS; |
| 8233 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 8234 | return RHS; |
| 8235 | return getAtomicType(ResultType); |
| 8236 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8237 | case Type::ConstantArray: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8238 | { |
| 8239 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 8240 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 8241 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 8242 | return QualType(); |
| 8243 | |
| 8244 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 8245 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8246 | if (Unqualified) { |
| 8247 | LHSElem = LHSElem.getUnqualifiedType(); |
| 8248 | RHSElem = RHSElem.getUnqualifiedType(); |
| 8249 | } |
| 8250 | |
| 8251 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8252 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8253 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 8254 | return LHS; |
| 8255 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 8256 | return RHS; |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 8257 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 8258 | ArrayType::ArraySizeModifier(), 0); |
| 8259 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 8260 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8261 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 8262 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8263 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 8264 | return LHS; |
| 8265 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 8266 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8267 | if (LVAT) { |
| 8268 | // FIXME: This isn't correct! But tricky to implement because |
| 8269 | // the array's size has to be the size of LHS, but the type |
| 8270 | // has to be different. |
| 8271 | return LHS; |
| 8272 | } |
| 8273 | if (RVAT) { |
| 8274 | // FIXME: This isn't correct! But tricky to implement because |
| 8275 | // the array's size has to be the size of RHS, but the type |
| 8276 | // has to be different. |
| 8277 | return RHS; |
| 8278 | } |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 8279 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 8280 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 8281 | return getIncompleteArrayType(ResultType, |
| 8282 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8283 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8284 | case Type::FunctionNoProto: |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8285 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8286 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8287 | case Type::Enum: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8288 | return QualType(); |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8289 | case Type::Builtin: |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 8290 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8291 | return QualType(); |
Daniel Dunbar | 804c044 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 8292 | case Type::Complex: |
| 8293 | // Distinct complex types are incompatible. |
| 8294 | return QualType(); |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 8295 | case Type::Vector: |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 8296 | // FIXME: The merged type should be an ExtVector! |
John McCall | 44c064b | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 8297 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 8298 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8299 | return LHS; |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8300 | return QualType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8301 | case Type::ObjCObject: { |
| 8302 | // Check if the types are assignment compatible. |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 8303 | // FIXME: This should be type compatibility, e.g. whether |
| 8304 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8305 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 8306 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 8307 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 7a7814c | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 8308 | return LHS; |
| 8309 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8310 | return QualType(); |
Cedric Venet | 4fc88b7 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 8311 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8312 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8313 | if (OfBlockPointer) { |
| 8314 | if (canAssignObjCInterfacesInBlockPointer( |
| 8315 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 8316 | RHS->getAs<ObjCObjectPointerType>(), |
| 8317 | BlockReturnType)) |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 8318 | return LHS; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8319 | return QualType(); |
| 8320 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 8321 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 8322 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8323 | return LHS; |
| 8324 | |
Steve Naroff | c68cfcf | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 8325 | return QualType(); |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 8326 | } |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 8327 | case Type::Pipe: |
| 8328 | { |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 8329 | assert(LHS != RHS && |
| 8330 | "Equivalent pipe types should have already been handled!"); |
| 8331 | return QualType(); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 8332 | } |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 8333 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8334 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 8335 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 8336 | } |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 8337 | |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 8338 | bool ASTContext::doFunctionTypesMatchOnExtParameterInfos( |
| 8339 | const FunctionProtoType *firstFnType, |
| 8340 | const FunctionProtoType *secondFnType) { |
| 8341 | // Fast path: if the first type doesn't have ext parameter infos, |
| 8342 | // we match if and only if they second type also doesn't have them. |
| 8343 | if (!firstFnType->hasExtParameterInfos()) |
| 8344 | return !secondFnType->hasExtParameterInfos(); |
| 8345 | |
| 8346 | // Otherwise, we can only match if the second type has them. |
| 8347 | if (!secondFnType->hasExtParameterInfos()) |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 8348 | return false; |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 8349 | |
| 8350 | auto firstEPI = firstFnType->getExtParameterInfos(); |
| 8351 | auto secondEPI = secondFnType->getExtParameterInfos(); |
| 8352 | assert(firstEPI.size() == secondEPI.size()); |
| 8353 | |
| 8354 | for (size_t i = 0, n = firstEPI.size(); i != n; ++i) { |
| 8355 | if (firstEPI[i] != secondEPI[i]) |
| 8356 | return false; |
| 8357 | } |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 8358 | return true; |
| 8359 | } |
| 8360 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 8361 | void ASTContext::ResetObjCLayout(const ObjCContainerDecl *CD) { |
| 8362 | ObjCLayouts[CD] = nullptr; |
| 8363 | } |
| 8364 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 8365 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 8366 | /// 'RHS' attributes and returns the merged version; including for function |
| 8367 | /// return types. |
| 8368 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 8369 | QualType LHSCan = getCanonicalType(LHS), |
| 8370 | RHSCan = getCanonicalType(RHS); |
| 8371 | // If two types are identical, they are compatible. |
| 8372 | if (LHSCan == RHSCan) |
| 8373 | return LHS; |
| 8374 | if (RHSCan->isFunctionType()) { |
| 8375 | if (!LHSCan->isFunctionType()) |
| 8376 | return QualType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8377 | QualType OldReturnType = |
| 8378 | cast<FunctionType>(RHSCan.getTypePtr())->getReturnType(); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 8379 | QualType NewReturnType = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8380 | cast<FunctionType>(LHSCan.getTypePtr())->getReturnType(); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 8381 | QualType ResReturnType = |
| 8382 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 8383 | if (ResReturnType.isNull()) |
| 8384 | return QualType(); |
| 8385 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 8386 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 8387 | // In either case, use OldReturnType to build the new function type. |
| 8388 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 8389 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8390 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8391 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Reid Kleckner | 896b32f | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 8392 | QualType ResultType = |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8393 | getFunctionType(OldReturnType, FPT->getParamTypes(), EPI); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 8394 | return ResultType; |
| 8395 | } |
| 8396 | } |
| 8397 | return QualType(); |
| 8398 | } |
| 8399 | |
| 8400 | // If the qualifiers are different, the types can still be merged. |
| 8401 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 8402 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 8403 | if (LQuals != RQuals) { |
| 8404 | // If any of these qualifiers are different, we have a type mismatch. |
| 8405 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 8406 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 8407 | return QualType(); |
| 8408 | |
| 8409 | // Exactly one GC qualifier difference is allowed: __strong is |
| 8410 | // okay if the other type has no GC qualifier but is an Objective |
| 8411 | // C object pointer (i.e. implicitly strong by default). We fix |
| 8412 | // this by pretending that the unqualified type was actually |
| 8413 | // qualified __strong. |
| 8414 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 8415 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 8416 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 8417 | |
| 8418 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 8419 | return QualType(); |
| 8420 | |
| 8421 | if (GC_L == Qualifiers::Strong) |
| 8422 | return LHS; |
| 8423 | if (GC_R == Qualifiers::Strong) |
| 8424 | return RHS; |
| 8425 | return QualType(); |
| 8426 | } |
| 8427 | |
| 8428 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 8429 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 8430 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 8431 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 8432 | if (ResQT == LHSBaseQT) |
| 8433 | return LHS; |
| 8434 | if (ResQT == RHSBaseQT) |
| 8435 | return RHS; |
| 8436 | } |
| 8437 | return QualType(); |
| 8438 | } |
| 8439 | |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 8440 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 8441 | // Integer Predicates |
| 8442 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3c91971 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 8443 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8444 | unsigned ASTContext::getIntWidth(QualType T) const { |
Richard Smith | e952106 | 2013-10-15 04:56:17 +0000 | [diff] [blame] | 8445 | if (const EnumType *ET = T->getAs<EnumType>()) |
Eli Friedman | ee275c8 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 8446 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 8447 | if (T->isBooleanType()) |
| 8448 | return 1; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 8449 | // For builtin types, just use the standard type sizing method |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 8450 | return (unsigned)getTypeSize(T); |
| 8451 | } |
| 8452 | |
Abramo Bagnara | 1364049 | 2012-09-09 10:21:24 +0000 | [diff] [blame] | 8453 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) const { |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 8454 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 8455 | |
| 8456 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 8457 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 8458 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 8459 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 8460 | |
| 8461 | // For enums, we return the unsigned version of the base type. |
| 8462 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 8463 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 8464 | |
| 8465 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 8466 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 8467 | switch (BTy->getKind()) { |
| 8468 | case BuiltinType::Char_S: |
| 8469 | case BuiltinType::SChar: |
| 8470 | return UnsignedCharTy; |
| 8471 | case BuiltinType::Short: |
| 8472 | return UnsignedShortTy; |
| 8473 | case BuiltinType::Int: |
| 8474 | return UnsignedIntTy; |
| 8475 | case BuiltinType::Long: |
| 8476 | return UnsignedLongTy; |
| 8477 | case BuiltinType::LongLong: |
| 8478 | return UnsignedLongLongTy; |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 8479 | case BuiltinType::Int128: |
| 8480 | return UnsignedInt128Ty; |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 8481 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8482 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 8483 | } |
| 8484 | } |
| 8485 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 8486 | ASTMutationListener::~ASTMutationListener() { } |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 8487 | |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 8488 | void ASTMutationListener::DeducedReturnType(const FunctionDecl *FD, |
| 8489 | QualType ReturnType) {} |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8490 | |
| 8491 | //===----------------------------------------------------------------------===// |
| 8492 | // Builtin Type Computation |
| 8493 | //===----------------------------------------------------------------------===// |
| 8494 | |
| 8495 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 8496 | /// pointer over the consumed characters. This returns the resultant type. If |
| 8497 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 8498 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 8499 | /// a vector of "i*". |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8500 | /// |
| 8501 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 8502 | /// to be an Integer Constant Expression. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8503 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8504 | ASTContext::GetBuiltinTypeError &Error, |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 8505 | bool &RequiresICE, |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 8506 | bool AllowTypeModifiers) { |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8507 | // Modifiers. |
| 8508 | int HowLong = 0; |
| 8509 | bool Signed = false, Unsigned = false; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8510 | RequiresICE = false; |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 8511 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 8512 | // Read the prefixed modifiers first. |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8513 | bool Done = false; |
| 8514 | while (!Done) { |
| 8515 | switch (*Str++) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8516 | default: Done = true; --Str; break; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 8517 | case 'I': |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8518 | RequiresICE = true; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 8519 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8520 | case 'S': |
| 8521 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 8522 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 8523 | Signed = true; |
| 8524 | break; |
| 8525 | case 'U': |
| 8526 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
Sean Silva | 2a99514 | 2015-01-16 21:44:26 +0000 | [diff] [blame] | 8527 | assert(!Unsigned && "Can't use 'U' modifier multiple times!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8528 | Unsigned = true; |
| 8529 | break; |
| 8530 | case 'L': |
| 8531 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 8532 | ++HowLong; |
| 8533 | break; |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 8534 | case 'W': |
| 8535 | // This modifier represents int64 type. |
| 8536 | assert(HowLong == 0 && "Can't use both 'L' and 'W' modifiers!"); |
| 8537 | switch (Context.getTargetInfo().getInt64Type()) { |
| 8538 | default: |
| 8539 | llvm_unreachable("Unexpected integer type"); |
| 8540 | case TargetInfo::SignedLong: |
| 8541 | HowLong = 1; |
| 8542 | break; |
| 8543 | case TargetInfo::SignedLongLong: |
| 8544 | HowLong = 2; |
| 8545 | break; |
| 8546 | } |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8547 | } |
| 8548 | } |
| 8549 | |
| 8550 | QualType Type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8551 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8552 | // Read the base type. |
| 8553 | switch (*Str++) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8554 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8555 | case 'v': |
| 8556 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 8557 | "Bad modifiers used with 'v'!"); |
| 8558 | Type = Context.VoidTy; |
| 8559 | break; |
Jack Carter | 24bef98 | 2013-08-15 15:16:57 +0000 | [diff] [blame] | 8560 | case 'h': |
| 8561 | assert(HowLong == 0 && !Signed && !Unsigned && |
Sean Silva | 2a99514 | 2015-01-16 21:44:26 +0000 | [diff] [blame] | 8562 | "Bad modifiers used with 'h'!"); |
Jack Carter | 24bef98 | 2013-08-15 15:16:57 +0000 | [diff] [blame] | 8563 | Type = Context.HalfTy; |
| 8564 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8565 | case 'f': |
| 8566 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 8567 | "Bad modifiers used with 'f'!"); |
| 8568 | Type = Context.FloatTy; |
| 8569 | break; |
| 8570 | case 'd': |
| 8571 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 8572 | "Bad modifiers used with 'd'!"); |
| 8573 | if (HowLong) |
| 8574 | Type = Context.LongDoubleTy; |
| 8575 | else |
| 8576 | Type = Context.DoubleTy; |
| 8577 | break; |
| 8578 | case 's': |
| 8579 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 8580 | if (Unsigned) |
| 8581 | Type = Context.UnsignedShortTy; |
| 8582 | else |
| 8583 | Type = Context.ShortTy; |
| 8584 | break; |
| 8585 | case 'i': |
| 8586 | if (HowLong == 3) |
| 8587 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 8588 | else if (HowLong == 2) |
| 8589 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 8590 | else if (HowLong == 1) |
| 8591 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 8592 | else |
| 8593 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 8594 | break; |
| 8595 | case 'c': |
| 8596 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 8597 | if (Signed) |
| 8598 | Type = Context.SignedCharTy; |
| 8599 | else if (Unsigned) |
| 8600 | Type = Context.UnsignedCharTy; |
| 8601 | else |
| 8602 | Type = Context.CharTy; |
| 8603 | break; |
| 8604 | case 'b': // boolean |
| 8605 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 8606 | Type = Context.BoolTy; |
| 8607 | break; |
| 8608 | case 'z': // size_t. |
| 8609 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 8610 | Type = Context.getSizeType(); |
| 8611 | break; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 8612 | case 'w': // wchar_t. |
| 8613 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'w'!"); |
| 8614 | Type = Context.getWideCharType(); |
| 8615 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8616 | case 'F': |
| 8617 | Type = Context.getCFConstantStringType(); |
| 8618 | break; |
Fariborz Jahanian | d11da7e | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 8619 | case 'G': |
| 8620 | Type = Context.getObjCIdType(); |
| 8621 | break; |
| 8622 | case 'H': |
| 8623 | Type = Context.getObjCSelType(); |
| 8624 | break; |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 8625 | case 'M': |
| 8626 | Type = Context.getObjCSuperType(); |
| 8627 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8628 | case 'a': |
| 8629 | Type = Context.getBuiltinVaListType(); |
| 8630 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 8631 | break; |
| 8632 | case 'A': |
| 8633 | // This is a "reference" to a va_list; however, what exactly |
| 8634 | // this means depends on how va_list is defined. There are two |
| 8635 | // different kinds of va_list: ones passed by value, and ones |
| 8636 | // passed by reference. An example of a by-value va_list is |
| 8637 | // x86, where va_list is a char*. An example of by-ref va_list |
| 8638 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 8639 | // we want this argument to be a char*&; for x86-64, we want |
| 8640 | // it to be a __va_list_tag*. |
| 8641 | Type = Context.getBuiltinVaListType(); |
| 8642 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8643 | if (Type->isArrayType()) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8644 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8645 | else |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8646 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8647 | break; |
| 8648 | case 'V': { |
| 8649 | char *End; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8650 | unsigned NumElements = strtoul(Str, &End, 10); |
| 8651 | assert(End != Str && "Missing vector size"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8652 | Str = End; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8653 | |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 8654 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 8655 | RequiresICE, false); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8656 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 8657 | |
| 8658 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 8659 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 8660 | VectorType::GenericVector); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8661 | break; |
| 8662 | } |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 8663 | case 'E': { |
| 8664 | char *End; |
| 8665 | |
| 8666 | unsigned NumElements = strtoul(Str, &End, 10); |
| 8667 | assert(End != Str && "Missing vector size"); |
| 8668 | |
| 8669 | Str = End; |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 8670 | |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 8671 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 8672 | false); |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 8673 | Type = Context.getExtVectorType(ElementType, NumElements); |
| 8674 | break; |
| 8675 | } |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 8676 | case 'X': { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8677 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 8678 | false); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8679 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 8680 | Type = Context.getComplexType(ElementType); |
| 8681 | break; |
Fariborz Jahanian | 73952fc | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 8682 | } |
| 8683 | case 'Y' : { |
| 8684 | Type = Context.getPointerDiffType(); |
| 8685 | break; |
| 8686 | } |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 8687 | case 'P': |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 8688 | Type = Context.getFILEType(); |
| 8689 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 8690 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8691 | return QualType(); |
| 8692 | } |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 8693 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 8694 | case 'J': |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 8695 | if (Signed) |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 8696 | Type = Context.getsigjmp_bufType(); |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 8697 | else |
| 8698 | Type = Context.getjmp_bufType(); |
| 8699 | |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 8700 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 8701 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 8702 | return QualType(); |
| 8703 | } |
| 8704 | break; |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 8705 | case 'K': |
| 8706 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 8707 | Type = Context.getucontext_tType(); |
| 8708 | |
| 8709 | if (Type.isNull()) { |
| 8710 | Error = ASTContext::GE_Missing_ucontext; |
| 8711 | return QualType(); |
| 8712 | } |
| 8713 | break; |
Eli Friedman | 4e91899e | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 8714 | case 'p': |
| 8715 | Type = Context.getProcessIDType(); |
| 8716 | break; |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 8717 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8718 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 8719 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 8720 | Done = !AllowTypeModifiers; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8721 | while (!Done) { |
John McCall | b8b9466 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 8722 | switch (char c = *Str++) { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 8723 | default: Done = true; --Str; break; |
| 8724 | case '*': |
| 8725 | case '&': { |
| 8726 | // Both pointers and references can have their pointee types |
| 8727 | // qualified with an address space. |
| 8728 | char *End; |
| 8729 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 8730 | if (End != Str && AddrSpace != 0) { |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame^] | 8731 | Type = Context.getAddrSpaceQualType(Type, AddrSpace + |
| 8732 | LangAS::Count); |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 8733 | Str = End; |
| 8734 | } |
| 8735 | if (c == '*') |
| 8736 | Type = Context.getPointerType(Type); |
| 8737 | else |
| 8738 | Type = Context.getLValueReferenceType(Type); |
| 8739 | break; |
| 8740 | } |
| 8741 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 8742 | case 'C': |
| 8743 | Type = Type.withConst(); |
| 8744 | break; |
| 8745 | case 'D': |
| 8746 | Type = Context.getVolatileType(Type); |
| 8747 | break; |
Ted Kremenek | f2a2f5f | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 8748 | case 'R': |
| 8749 | Type = Type.withRestrict(); |
| 8750 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8751 | } |
| 8752 | } |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 8753 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8754 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 8755 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8756 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8757 | return Type; |
| 8758 | } |
| 8759 | |
| 8760 | /// GetBuiltinType - Return the type for the specified builtin. |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 8761 | QualType ASTContext::GetBuiltinType(unsigned Id, |
| 8762 | GetBuiltinTypeError &Error, |
| 8763 | unsigned *IntegerConstantArgs) const { |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 8764 | const char *TypeStr = BuiltinInfo.getTypeString(Id); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8765 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8766 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8767 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8768 | bool RequiresICE = false; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8769 | Error = GE_None; |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 8770 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 8771 | RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8772 | if (Error != GE_None) |
| 8773 | return QualType(); |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 8774 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8775 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 8776 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8777 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 8778 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8779 | if (Error != GE_None) |
| 8780 | return QualType(); |
| 8781 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8782 | // If this argument is required to be an IntegerConstantExpression and the |
| 8783 | // caller cares, fill in the bitmask we return. |
| 8784 | if (RequiresICE && IntegerConstantArgs) |
| 8785 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 8786 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8787 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 8788 | if (Ty->isArrayType()) |
| 8789 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8790 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8791 | ArgTypes.push_back(Ty); |
| 8792 | } |
| 8793 | |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 8794 | if (Id == Builtin::BI__GetExceptionInfo) |
| 8795 | return QualType(); |
| 8796 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8797 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 8798 | "'.' should only occur at end of builtin type list!"); |
| 8799 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 8800 | FunctionType::ExtInfo EI(CC_C); |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 8801 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 8802 | |
| 8803 | bool Variadic = (TypeStr[0] == '.'); |
| 8804 | |
Richard Smith | 836de6b | 2016-12-19 23:59:34 +0000 | [diff] [blame] | 8805 | // We really shouldn't be making a no-proto type here. |
| 8806 | if (ArgTypes.empty() && Variadic && !getLangOpts().CPlusPlus) |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 8807 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 8808 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8809 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 8810 | EPI.ExtInfo = EI; |
| 8811 | EPI.Variadic = Variadic; |
Richard Smith | 391fb86 | 2016-10-18 07:13:55 +0000 | [diff] [blame] | 8812 | if (getLangOpts().CPlusPlus && BuiltinInfo.isNoThrow(Id)) |
| 8813 | EPI.ExceptionSpec.Type = |
| 8814 | getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8815 | |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 8816 | return getFunctionType(ResType, ArgTypes, EPI); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8817 | } |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 8818 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8819 | static GVALinkage basicGVALinkageForFunction(const ASTContext &Context, |
| 8820 | const FunctionDecl *FD) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8821 | if (!FD->isExternallyVisible()) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8822 | return GVA_Internal; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8823 | |
Yaron Keren | 4cd211b | 2017-02-22 14:32:39 +0000 | [diff] [blame] | 8824 | GVALinkage External; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8825 | switch (FD->getTemplateSpecializationKind()) { |
| 8826 | case TSK_Undeclared: |
| 8827 | case TSK_ExplicitSpecialization: |
| 8828 | External = GVA_StrongExternal; |
| 8829 | break; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8830 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8831 | case TSK_ExplicitInstantiationDefinition: |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 8832 | return GVA_StrongODR; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8833 | |
David Majnemer | c3d0733 | 2014-05-15 06:25:57 +0000 | [diff] [blame] | 8834 | // C++11 [temp.explicit]p10: |
| 8835 | // [ Note: The intent is that an inline function that is the subject of |
| 8836 | // an explicit instantiation declaration will still be implicitly |
| 8837 | // instantiated when used so that the body can be considered for |
| 8838 | // inlining, but that no out-of-line copy of the inline function would be |
| 8839 | // generated in the translation unit. -- end note ] |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8840 | case TSK_ExplicitInstantiationDeclaration: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8841 | return GVA_AvailableExternally; |
| 8842 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8843 | case TSK_ImplicitInstantiation: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8844 | External = GVA_DiscardableODR; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8845 | break; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8846 | } |
| 8847 | |
| 8848 | if (!FD->isInlined()) |
| 8849 | return External; |
David Majnemer | 62f0ffd | 2013-08-01 17:26:42 +0000 | [diff] [blame] | 8850 | |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 8851 | if ((!Context.getLangOpts().CPlusPlus && |
| 8852 | !Context.getTargetInfo().getCXXABI().isMicrosoft() && |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8853 | !FD->hasAttr<DLLExportAttr>()) || |
David Majnemer | 62f0ffd | 2013-08-01 17:26:42 +0000 | [diff] [blame] | 8854 | FD->hasAttr<GNUInlineAttr>()) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8855 | // FIXME: This doesn't match gcc's behavior for dllexport inline functions. |
| 8856 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8857 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 8858 | // externally visible. |
| 8859 | if (FD->isInlineDefinitionExternallyVisible()) |
| 8860 | return External; |
| 8861 | |
| 8862 | // C99 inline semantics, where the symbol is not externally visible. |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8863 | return GVA_AvailableExternally; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8864 | } |
| 8865 | |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 8866 | // Functions specified with extern and inline in -fms-compatibility mode |
| 8867 | // forcibly get emitted. While the body of the function cannot be later |
| 8868 | // replaced, the function definition cannot be discarded. |
David Majnemer | 7376870 | 2015-03-20 00:02:27 +0000 | [diff] [blame] | 8869 | if (FD->isMSExternInline()) |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 8870 | return GVA_StrongODR; |
| 8871 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8872 | return GVA_DiscardableODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8873 | } |
| 8874 | |
Artem Belevich | ca2b951 | 2016-05-02 20:30:03 +0000 | [diff] [blame] | 8875 | static GVALinkage adjustGVALinkageForAttributes(const ASTContext &Context, |
| 8876 | GVALinkage L, const Decl *D) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8877 | // See http://msdn.microsoft.com/en-us/library/xa0d9ste.aspx |
| 8878 | // dllexport/dllimport on inline functions. |
| 8879 | if (D->hasAttr<DLLImportAttr>()) { |
| 8880 | if (L == GVA_DiscardableODR || L == GVA_StrongODR) |
| 8881 | return GVA_AvailableExternally; |
Artem Belevich | ca2b951 | 2016-05-02 20:30:03 +0000 | [diff] [blame] | 8882 | } else if (D->hasAttr<DLLExportAttr>()) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8883 | if (L == GVA_DiscardableODR) |
| 8884 | return GVA_StrongODR; |
Artem Belevich | ca2b951 | 2016-05-02 20:30:03 +0000 | [diff] [blame] | 8885 | } else if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice && |
| 8886 | D->hasAttr<CUDAGlobalAttr>()) { |
| 8887 | // Device-side functions with __global__ attribute must always be |
| 8888 | // visible externally so they can be launched from host. |
| 8889 | if (L == GVA_DiscardableODR || L == GVA_Internal) |
| 8890 | return GVA_StrongODR; |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8891 | } |
| 8892 | return L; |
| 8893 | } |
| 8894 | |
| 8895 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const { |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 8896 | auto L = adjustGVALinkageForAttributes( |
Artem Belevich | ca2b951 | 2016-05-02 20:30:03 +0000 | [diff] [blame] | 8897 | *this, basicGVALinkageForFunction(*this, FD), FD); |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 8898 | auto EK = ExternalASTSource::EK_ReplyHazy; |
| 8899 | if (auto *Ext = getExternalSource()) |
| 8900 | EK = Ext->hasExternalDefinitions(FD->getOwningModuleID()); |
| 8901 | switch (EK) { |
| 8902 | case ExternalASTSource::EK_Never: |
| 8903 | if (L == GVA_DiscardableODR) |
| 8904 | return GVA_StrongODR; |
| 8905 | break; |
| 8906 | case ExternalASTSource::EK_Always: |
| 8907 | return GVA_AvailableExternally; |
| 8908 | case ExternalASTSource::EK_ReplyHazy: |
| 8909 | break; |
| 8910 | } |
| 8911 | return L; |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8912 | } |
| 8913 | |
| 8914 | static GVALinkage basicGVALinkageForVariable(const ASTContext &Context, |
| 8915 | const VarDecl *VD) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8916 | if (!VD->isExternallyVisible()) |
| 8917 | return GVA_Internal; |
| 8918 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8919 | if (VD->isStaticLocal()) { |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8920 | const DeclContext *LexicalContext = VD->getParentFunctionOrMethod(); |
| 8921 | while (LexicalContext && !isa<FunctionDecl>(LexicalContext)) |
| 8922 | LexicalContext = LexicalContext->getLexicalParent(); |
| 8923 | |
David Blaikie | eb21001 | 2017-01-27 23:11:10 +0000 | [diff] [blame] | 8924 | // ObjC Blocks can create local variables that don't have a FunctionDecl |
| 8925 | // LexicalContext. |
| 8926 | if (!LexicalContext) |
| 8927 | return GVA_DiscardableODR; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8928 | |
David Blaikie | eb21001 | 2017-01-27 23:11:10 +0000 | [diff] [blame] | 8929 | // Otherwise, let the static local variable inherit its linkage from the |
| 8930 | // nearest enclosing function. |
| 8931 | auto StaticLocalLinkage = |
| 8932 | Context.GetGVALinkageForFunction(cast<FunctionDecl>(LexicalContext)); |
| 8933 | |
| 8934 | // Itanium ABI 5.2.2: "Each COMDAT group [for a static local variable] must |
| 8935 | // be emitted in any object with references to the symbol for the object it |
| 8936 | // contains, whether inline or out-of-line." |
| 8937 | // Similar behavior is observed with MSVC. An alternative ABI could use |
| 8938 | // StrongODR/AvailableExternally to match the function, but none are |
| 8939 | // known/supported currently. |
| 8940 | if (StaticLocalLinkage == GVA_StrongODR || |
| 8941 | StaticLocalLinkage == GVA_AvailableExternally) |
| 8942 | return GVA_DiscardableODR; |
| 8943 | return StaticLocalLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8944 | } |
| 8945 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 8946 | // MSVC treats in-class initialized static data members as definitions. |
| 8947 | // By giving them non-strong linkage, out-of-line definitions won't |
| 8948 | // cause link errors. |
| 8949 | if (Context.isMSStaticDataMemberInlineDefinition(VD)) |
| 8950 | return GVA_DiscardableODR; |
| 8951 | |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 8952 | // Most non-template variables have strong linkage; inline variables are |
| 8953 | // linkonce_odr or (occasionally, for compatibility) weak_odr. |
| 8954 | GVALinkage StrongLinkage; |
| 8955 | switch (Context.getInlineVariableDefinitionKind(VD)) { |
| 8956 | case ASTContext::InlineVariableDefinitionKind::None: |
| 8957 | StrongLinkage = GVA_StrongExternal; |
| 8958 | break; |
| 8959 | case ASTContext::InlineVariableDefinitionKind::Weak: |
| 8960 | case ASTContext::InlineVariableDefinitionKind::WeakUnknown: |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 8961 | StrongLinkage = GVA_DiscardableODR; |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 8962 | break; |
| 8963 | case ASTContext::InlineVariableDefinitionKind::Strong: |
| 8964 | StrongLinkage = GVA_StrongODR; |
| 8965 | break; |
| 8966 | } |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 8967 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 8968 | switch (VD->getTemplateSpecializationKind()) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8969 | case TSK_Undeclared: |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 8970 | return StrongLinkage; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8971 | |
David Majnemer | 6d1780c | 2015-07-17 23:36:49 +0000 | [diff] [blame] | 8972 | case TSK_ExplicitSpecialization: |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 8973 | return Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 8974 | VD->isStaticDataMember() |
David Majnemer | 6d1780c | 2015-07-17 23:36:49 +0000 | [diff] [blame] | 8975 | ? GVA_StrongODR |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 8976 | : StrongLinkage; |
David Majnemer | 6d1780c | 2015-07-17 23:36:49 +0000 | [diff] [blame] | 8977 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8978 | case TSK_ExplicitInstantiationDefinition: |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 8979 | return GVA_StrongODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8980 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8981 | case TSK_ExplicitInstantiationDeclaration: |
| 8982 | return GVA_AvailableExternally; |
| 8983 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 8984 | case TSK_ImplicitInstantiation: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8985 | return GVA_DiscardableODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8986 | } |
Rafael Espindola | 27699c8 | 2013-05-13 14:05:53 +0000 | [diff] [blame] | 8987 | |
| 8988 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8989 | } |
| 8990 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8991 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
Artem Belevich | ca2b951 | 2016-05-02 20:30:03 +0000 | [diff] [blame] | 8992 | return adjustGVALinkageForAttributes( |
| 8993 | *this, basicGVALinkageForVariable(*this, VD), VD); |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 8994 | } |
| 8995 | |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 8996 | bool ASTContext::DeclMustBeEmitted(const Decl *D, bool ForModularCodegen) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8997 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8998 | if (!VD->isFileVarDecl()) |
| 8999 | return false; |
Renato Golin | 9258aa5 | 2014-05-21 10:40:27 +0000 | [diff] [blame] | 9000 | // Global named register variables (GNU extension) are never emitted. |
| 9001 | if (VD->getStorageClass() == SC_Register) |
| 9002 | return false; |
Richard Smith | 7747ce2 | 2015-08-19 20:49:38 +0000 | [diff] [blame] | 9003 | if (VD->getDescribedVarTemplate() || |
| 9004 | isa<VarTemplatePartialSpecializationDecl>(VD)) |
| 9005 | return false; |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 9006 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 9007 | // We never need to emit an uninstantiated function template. |
| 9008 | if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) |
| 9009 | return false; |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 9010 | } else if (isa<PragmaCommentDecl>(D)) |
| 9011 | return true; |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 9012 | else if (isa<OMPThreadPrivateDecl>(D) || |
| 9013 | D->hasAttr<OMPDeclareTargetDeclAttr>()) |
| 9014 | return true; |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 9015 | else if (isa<PragmaDetectMismatchDecl>(D)) |
| 9016 | return true; |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 9017 | else if (isa<OMPThreadPrivateDecl>(D)) |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 9018 | return !D->getDeclContext()->isDependentContext(); |
| 9019 | else if (isa<OMPDeclareReductionDecl>(D)) |
| 9020 | return !D->getDeclContext()->isDependentContext(); |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 9021 | else if (isa<ImportDecl>(D)) |
| 9022 | return true; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 9023 | else |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 9024 | return false; |
| 9025 | |
| 9026 | // If this is a member of a class template, we do not need to emit it. |
| 9027 | if (D->getDeclContext()->isDependentContext()) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9028 | return false; |
| 9029 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 9030 | // Weak references don't produce any output by themselves. |
| 9031 | if (D->hasAttr<WeakRefAttr>()) |
| 9032 | return false; |
| 9033 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9034 | // Aliases and used decls are required. |
| 9035 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 9036 | return true; |
| 9037 | |
| 9038 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 9039 | // Forward declarations aren't required. |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 9040 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 9041 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9042 | |
| 9043 | // Constructors and destructors are required. |
| 9044 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 9045 | return true; |
| 9046 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 9047 | // The key function for a class is required. This rule only comes |
| 9048 | // into play when inline functions can be key functions, though. |
| 9049 | if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
| 9050 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 9051 | const CXXRecordDecl *RD = MD->getParent(); |
| 9052 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 9053 | const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD); |
| 9054 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 9055 | return true; |
| 9056 | } |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9057 | } |
| 9058 | } |
| 9059 | |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9060 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 9061 | |
| 9062 | if (Linkage == GVA_DiscardableODR && ForModularCodegen) |
| 9063 | return true; |
| 9064 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9065 | // static, static inline, always_inline, and extern inline functions can |
| 9066 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 9067 | // Implicit template instantiations can also be deferred in C++. |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9068 | return !isDiscardableGVALinkage(Linkage); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9069 | } |
Douglas Gregor | 87d8124 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 9070 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9071 | const VarDecl *VD = cast<VarDecl>(D); |
| 9072 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 9073 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 9074 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly && |
| 9075 | !isMSStaticDataMemberInlineDefinition(VD)) |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 9076 | return false; |
| 9077 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9078 | // Variables that can be needed in other TUs are required. |
Justin Lebar | 606f01f | 2016-10-13 20:52:17 +0000 | [diff] [blame] | 9079 | if (!isDiscardableGVALinkage(GetGVALinkageForVariable(VD))) |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9080 | return true; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9081 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9082 | // Variables that have destruction with side-effects are required. |
| 9083 | if (VD->getType().isDestructedType()) |
| 9084 | return true; |
| 9085 | |
| 9086 | // Variables that have initialization with side-effects are required. |
Richard Smith | 7747ce2 | 2015-08-19 20:49:38 +0000 | [diff] [blame] | 9087 | if (VD->getInit() && VD->getInit()->HasSideEffects(*this) && |
Richard Smith | 187ffb4 | 2017-01-20 01:19:46 +0000 | [diff] [blame] | 9088 | // We can get a value-dependent initializer during error recovery. |
| 9089 | (VD->getInit()->isValueDependent() || !VD->evaluateValue())) |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9090 | return true; |
| 9091 | |
Richard Smith | da38363 | 2016-08-15 01:33:41 +0000 | [diff] [blame] | 9092 | // Likewise, variables with tuple-like bindings are required if their |
| 9093 | // bindings have side-effects. |
| 9094 | if (auto *DD = dyn_cast<DecompositionDecl>(VD)) |
| 9095 | for (auto *BD : DD->bindings()) |
| 9096 | if (auto *BindingVD = BD->getHoldingVar()) |
| 9097 | if (DeclMustBeEmitted(BindingVD)) |
| 9098 | return true; |
| 9099 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9100 | return false; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9101 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 9102 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 9103 | CallingConv ASTContext::getDefaultCallingConvention(bool IsVariadic, |
| 9104 | bool IsCXXMethod) const { |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 9105 | // Pass through to the C++ ABI object |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 9106 | if (IsCXXMethod) |
| 9107 | return ABI->getDefaultMethodCallConv(IsVariadic); |
Timur Iskhodzhanov | c5098ad | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 9108 | |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 9109 | switch (LangOpts.getDefaultCallingConv()) { |
| 9110 | case LangOptions::DCC_None: |
| 9111 | break; |
| 9112 | case LangOptions::DCC_CDecl: |
| 9113 | return CC_C; |
| 9114 | case LangOptions::DCC_FastCall: |
| 9115 | if (getTargetInfo().hasFeature("sse2")) |
| 9116 | return CC_X86FastCall; |
| 9117 | break; |
| 9118 | case LangOptions::DCC_StdCall: |
| 9119 | if (!IsVariadic) |
| 9120 | return CC_X86StdCall; |
| 9121 | break; |
| 9122 | case LangOptions::DCC_VectorCall: |
| 9123 | // __vectorcall cannot be applied to variadic functions. |
| 9124 | if (!IsVariadic) |
| 9125 | return CC_X86VectorCall; |
| 9126 | break; |
| 9127 | } |
Alexander Kornienko | 21de0ae | 2015-01-20 11:20:41 +0000 | [diff] [blame] | 9128 | return Target->getDefaultCallingConv(TargetInfo::CCMT_Unknown); |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 9129 | } |
| 9130 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 9131 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 9132 | // Pass through to the C++ ABI object |
| 9133 | return ABI->isNearlyEmpty(RD); |
| 9134 | } |
| 9135 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 9136 | VTableContextBase *ASTContext::getVTableContext() { |
| 9137 | if (!VTContext.get()) { |
| 9138 | if (Target->getCXXABI().isMicrosoft()) |
| 9139 | VTContext.reset(new MicrosoftVTableContext(*this)); |
| 9140 | else |
| 9141 | VTContext.reset(new ItaniumVTableContext(*this)); |
| 9142 | } |
| 9143 | return VTContext.get(); |
| 9144 | } |
| 9145 | |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 9146 | MangleContext *ASTContext::createMangleContext() { |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 9147 | switch (Target->getCXXABI().getKind()) { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 9148 | case TargetCXXABI::GenericAArch64: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 9149 | case TargetCXXABI::GenericItanium: |
| 9150 | case TargetCXXABI::GenericARM: |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 9151 | case TargetCXXABI::GenericMIPS: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 9152 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9153 | case TargetCXXABI::iOS64: |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9154 | case TargetCXXABI::WebAssembly: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 9155 | case TargetCXXABI::WatchOS: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 9156 | return ItaniumMangleContext::create(*this, getDiagnostics()); |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 9157 | case TargetCXXABI::Microsoft: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 9158 | return MicrosoftMangleContext::create(*this, getDiagnostics()); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 9159 | } |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 9160 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 9161 | } |
| 9162 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 9163 | CXXABI::~CXXABI() {} |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 9164 | |
| 9165 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9166 | return ASTRecordLayouts.getMemorySize() + |
| 9167 | llvm::capacity_in_bytes(ObjCLayouts) + |
| 9168 | llvm::capacity_in_bytes(KeyFunctions) + |
| 9169 | llvm::capacity_in_bytes(ObjCImpls) + |
| 9170 | llvm::capacity_in_bytes(BlockVarCopyInits) + |
| 9171 | llvm::capacity_in_bytes(DeclAttrs) + |
| 9172 | llvm::capacity_in_bytes(TemplateOrInstantiation) + |
| 9173 | llvm::capacity_in_bytes(InstantiatedFromUsingDecl) + |
| 9174 | llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) + |
| 9175 | llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) + |
| 9176 | llvm::capacity_in_bytes(OverriddenMethods) + |
| 9177 | llvm::capacity_in_bytes(Types) + |
| 9178 | llvm::capacity_in_bytes(VariableArrayTypes) + |
| 9179 | llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 9180 | } |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 9181 | |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 9182 | /// getIntTypeForBitwidth - |
| 9183 | /// sets integer QualTy according to specified details: |
| 9184 | /// bitwidth, signed/unsigned. |
| 9185 | /// Returns empty type if there is no appropriate target types. |
| 9186 | QualType ASTContext::getIntTypeForBitwidth(unsigned DestWidth, |
| 9187 | unsigned Signed) const { |
| 9188 | TargetInfo::IntType Ty = getTargetInfo().getIntTypeByWidth(DestWidth, Signed); |
| 9189 | CanQualType QualTy = getFromTargetType(Ty); |
| 9190 | if (!QualTy && DestWidth == 128) |
| 9191 | return Signed ? Int128Ty : UnsignedInt128Ty; |
| 9192 | return QualTy; |
| 9193 | } |
| 9194 | |
| 9195 | /// getRealTypeForBitwidth - |
| 9196 | /// sets floating point QualTy according to specified bitwidth. |
| 9197 | /// Returns empty type if there is no appropriate target types. |
| 9198 | QualType ASTContext::getRealTypeForBitwidth(unsigned DestWidth) const { |
| 9199 | TargetInfo::RealType Ty = getTargetInfo().getRealTypeByWidth(DestWidth); |
| 9200 | switch (Ty) { |
| 9201 | case TargetInfo::Float: |
| 9202 | return FloatTy; |
| 9203 | case TargetInfo::Double: |
| 9204 | return DoubleTy; |
| 9205 | case TargetInfo::LongDouble: |
| 9206 | return LongDoubleTy; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 9207 | case TargetInfo::Float128: |
| 9208 | return Float128Ty; |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 9209 | case TargetInfo::NoFloat: |
| 9210 | return QualType(); |
| 9211 | } |
| 9212 | |
| 9213 | llvm_unreachable("Unhandled TargetInfo::RealType value"); |
| 9214 | } |
| 9215 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 9216 | void ASTContext::setManglingNumber(const NamedDecl *ND, unsigned Number) { |
| 9217 | if (Number > 1) |
| 9218 | MangleNumbers[ND] = Number; |
David Blaikie | 095deba | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 9219 | } |
| 9220 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 9221 | unsigned ASTContext::getManglingNumber(const NamedDecl *ND) const { |
Richard Smith | e9b02d6 | 2016-03-21 22:33:02 +0000 | [diff] [blame] | 9222 | auto I = MangleNumbers.find(ND); |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 9223 | return I != MangleNumbers.end() ? I->second : 1; |
David Blaikie | 095deba | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 9224 | } |
| 9225 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 9226 | void ASTContext::setStaticLocalNumber(const VarDecl *VD, unsigned Number) { |
| 9227 | if (Number > 1) |
| 9228 | StaticLocalNumbers[VD] = Number; |
| 9229 | } |
| 9230 | |
| 9231 | unsigned ASTContext::getStaticLocalNumber(const VarDecl *VD) const { |
Richard Smith | e9b02d6 | 2016-03-21 22:33:02 +0000 | [diff] [blame] | 9232 | auto I = StaticLocalNumbers.find(VD); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 9233 | return I != StaticLocalNumbers.end() ? I->second : 1; |
| 9234 | } |
| 9235 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 9236 | MangleNumberingContext & |
| 9237 | ASTContext::getManglingNumberContext(const DeclContext *DC) { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 9238 | assert(LangOpts.CPlusPlus); // We don't need mangling numbers for plain C. |
Justin Lebar | 20ebffc | 2016-10-10 16:26:19 +0000 | [diff] [blame] | 9239 | std::unique_ptr<MangleNumberingContext> &MCtx = MangleNumberingContexts[DC]; |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 9240 | if (!MCtx) |
| 9241 | MCtx = createMangleNumberingContext(); |
| 9242 | return *MCtx; |
| 9243 | } |
| 9244 | |
Justin Lebar | 20ebffc | 2016-10-10 16:26:19 +0000 | [diff] [blame] | 9245 | std::unique_ptr<MangleNumberingContext> |
| 9246 | ASTContext::createMangleNumberingContext() const { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 9247 | return ABI->createMangleNumberingContext(); |
Douglas Gregor | 6379854 | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 9248 | } |
| 9249 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 9250 | const CXXConstructorDecl * |
| 9251 | ASTContext::getCopyConstructorForExceptionObject(CXXRecordDecl *RD) { |
| 9252 | return ABI->getCopyConstructorForExceptionObject( |
| 9253 | cast<CXXRecordDecl>(RD->getFirstDecl())); |
| 9254 | } |
| 9255 | |
| 9256 | void ASTContext::addCopyConstructorForExceptionObject(CXXRecordDecl *RD, |
| 9257 | CXXConstructorDecl *CD) { |
| 9258 | return ABI->addCopyConstructorForExceptionObject( |
| 9259 | cast<CXXRecordDecl>(RD->getFirstDecl()), |
| 9260 | cast<CXXConstructorDecl>(CD->getFirstDecl())); |
| 9261 | } |
| 9262 | |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 9263 | void ASTContext::addTypedefNameForUnnamedTagDecl(TagDecl *TD, |
| 9264 | TypedefNameDecl *DD) { |
| 9265 | return ABI->addTypedefNameForUnnamedTagDecl(TD, DD); |
| 9266 | } |
| 9267 | |
| 9268 | TypedefNameDecl * |
| 9269 | ASTContext::getTypedefNameForUnnamedTagDecl(const TagDecl *TD) { |
| 9270 | return ABI->getTypedefNameForUnnamedTagDecl(TD); |
| 9271 | } |
| 9272 | |
| 9273 | void ASTContext::addDeclaratorForUnnamedTagDecl(TagDecl *TD, |
| 9274 | DeclaratorDecl *DD) { |
| 9275 | return ABI->addDeclaratorForUnnamedTagDecl(TD, DD); |
| 9276 | } |
| 9277 | |
| 9278 | DeclaratorDecl *ASTContext::getDeclaratorForUnnamedTagDecl(const TagDecl *TD) { |
| 9279 | return ABI->getDeclaratorForUnnamedTagDecl(TD); |
| 9280 | } |
| 9281 | |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 9282 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 9283 | ParamIndices[D] = index; |
| 9284 | } |
| 9285 | |
| 9286 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 9287 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 9288 | assert(I != ParamIndices.end() && |
| 9289 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 9290 | return I->second; |
| 9291 | } |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 9292 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 9293 | APValue * |
| 9294 | ASTContext::getMaterializedTemporaryValue(const MaterializeTemporaryExpr *E, |
| 9295 | bool MayCreate) { |
| 9296 | assert(E && E->getStorageDuration() == SD_Static && |
| 9297 | "don't need to cache the computed value for this temporary"); |
David Majnemer | 2dcef9e | 2015-08-13 23:50:15 +0000 | [diff] [blame] | 9298 | if (MayCreate) { |
| 9299 | APValue *&MTVI = MaterializedTemporaryValues[E]; |
| 9300 | if (!MTVI) |
| 9301 | MTVI = new (*this) APValue; |
| 9302 | return MTVI; |
| 9303 | } |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 9304 | |
David Majnemer | 2dcef9e | 2015-08-13 23:50:15 +0000 | [diff] [blame] | 9305 | return MaterializedTemporaryValues.lookup(E); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 9306 | } |
| 9307 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 9308 | bool ASTContext::AtomicUsesUnsupportedLibcall(const AtomicExpr *E) const { |
| 9309 | const llvm::Triple &T = getTargetInfo().getTriple(); |
| 9310 | if (!T.isOSDarwin()) |
| 9311 | return false; |
| 9312 | |
Bob Wilson | 2c82c3d | 2013-11-02 23:27:49 +0000 | [diff] [blame] | 9313 | if (!(T.isiOS() && T.isOSVersionLT(7)) && |
| 9314 | !(T.isMacOSX() && T.isOSVersionLT(10, 9))) |
| 9315 | return false; |
| 9316 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 9317 | QualType AtomicTy = E->getPtr()->getType()->getPointeeType(); |
| 9318 | CharUnits sizeChars = getTypeSizeInChars(AtomicTy); |
| 9319 | uint64_t Size = sizeChars.getQuantity(); |
| 9320 | CharUnits alignChars = getTypeAlignInChars(AtomicTy); |
| 9321 | unsigned Align = alignChars.getQuantity(); |
| 9322 | unsigned MaxInlineWidthInBits = getTargetInfo().getMaxAtomicInlineWidth(); |
| 9323 | return (Size != Align || toBits(sizeChars) > MaxInlineWidthInBits); |
| 9324 | } |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9325 | |
| 9326 | namespace { |
| 9327 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9328 | ast_type_traits::DynTypedNode getSingleDynTypedNodeFromParentMap( |
| 9329 | ASTContext::ParentMapPointers::mapped_type U) { |
Benjamin Kramer | fbfa7a1 | 2015-10-22 11:26:35 +0000 | [diff] [blame] | 9330 | if (const auto *D = U.dyn_cast<const Decl *>()) |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 9331 | return ast_type_traits::DynTypedNode::create(*D); |
Benjamin Kramer | fbfa7a1 | 2015-10-22 11:26:35 +0000 | [diff] [blame] | 9332 | if (const auto *S = U.dyn_cast<const Stmt *>()) |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 9333 | return ast_type_traits::DynTypedNode::create(*S); |
Benjamin Kramer | fbfa7a1 | 2015-10-22 11:26:35 +0000 | [diff] [blame] | 9334 | return *U.get<ast_type_traits::DynTypedNode *>(); |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 9335 | } |
| 9336 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9337 | /// Template specializations to abstract away from pointers and TypeLocs. |
| 9338 | /// @{ |
| 9339 | template <typename T> |
| 9340 | ast_type_traits::DynTypedNode createDynTypedNode(const T &Node) { |
| 9341 | return ast_type_traits::DynTypedNode::create(*Node); |
| 9342 | } |
| 9343 | template <> |
| 9344 | ast_type_traits::DynTypedNode createDynTypedNode(const TypeLoc &Node) { |
| 9345 | return ast_type_traits::DynTypedNode::create(Node); |
| 9346 | } |
| 9347 | template <> |
| 9348 | ast_type_traits::DynTypedNode |
| 9349 | createDynTypedNode(const NestedNameSpecifierLoc &Node) { |
| 9350 | return ast_type_traits::DynTypedNode::create(Node); |
| 9351 | } |
| 9352 | /// @} |
| 9353 | |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9354 | /// \brief A \c RecursiveASTVisitor that builds a map from nodes to their |
| 9355 | /// parents as defined by the \c RecursiveASTVisitor. |
| 9356 | /// |
| 9357 | /// Note that the relationship described here is purely in terms of AST |
| 9358 | /// traversal - there are other relationships (for example declaration context) |
| 9359 | /// in the AST that are better modeled by special matchers. |
| 9360 | /// |
Benjamin Kramer | e8c51fd | 2015-10-21 10:07:26 +0000 | [diff] [blame] | 9361 | /// 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] | 9362 | class ParentMapASTVisitor : public RecursiveASTVisitor<ParentMapASTVisitor> { |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9363 | public: |
| 9364 | /// \brief Builds and returns the translation unit's parent map. |
| 9365 | /// |
| 9366 | /// The caller takes ownership of the returned \c ParentMap. |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9367 | static std::pair<ASTContext::ParentMapPointers *, |
| 9368 | ASTContext::ParentMapOtherNodes *> |
| 9369 | buildMap(TranslationUnitDecl &TU) { |
| 9370 | ParentMapASTVisitor Visitor(new ASTContext::ParentMapPointers, |
| 9371 | new ASTContext::ParentMapOtherNodes); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9372 | Visitor.TraverseDecl(&TU); |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9373 | return std::make_pair(Visitor.Parents, Visitor.OtherParents); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9374 | } |
| 9375 | |
| 9376 | private: |
| 9377 | typedef RecursiveASTVisitor<ParentMapASTVisitor> VisitorBase; |
| 9378 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9379 | ParentMapASTVisitor(ASTContext::ParentMapPointers *Parents, |
| 9380 | ASTContext::ParentMapOtherNodes *OtherParents) |
| 9381 | : Parents(Parents), OtherParents(OtherParents) {} |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9382 | |
| 9383 | bool shouldVisitTemplateInstantiations() const { |
| 9384 | return true; |
| 9385 | } |
| 9386 | bool shouldVisitImplicitCode() const { |
| 9387 | return true; |
| 9388 | } |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9389 | |
Richard Smith | 8583872 | 2015-11-24 03:09:01 +0000 | [diff] [blame] | 9390 | template <typename T, typename MapNodeTy, typename BaseTraverseFn, |
| 9391 | typename MapTy> |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9392 | bool TraverseNode(T Node, MapNodeTy MapNode, |
Richard Smith | 8583872 | 2015-11-24 03:09:01 +0000 | [diff] [blame] | 9393 | BaseTraverseFn BaseTraverse, MapTy *Parents) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 9394 | if (!Node) |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9395 | return true; |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 9396 | if (ParentStack.size() > 0) { |
Benjamin Kramer | e8c51fd | 2015-10-21 10:07:26 +0000 | [diff] [blame] | 9397 | // FIXME: Currently we add the same parent multiple times, but only |
| 9398 | // when no memoization data is available for the type. |
| 9399 | // For example when we visit all subexpressions of template |
| 9400 | // instantiations; this is suboptimal, but benign: the only way to |
| 9401 | // visit those is with hasAncestor / hasParent, and those do not create |
| 9402 | // new matches. |
| 9403 | // The plan is to enable DynTypedNode to be storable in a map or hash |
| 9404 | // map. The main problem there is to implement hash functions / |
| 9405 | // comparison operators for all types that DynTypedNode supports that |
| 9406 | // do not have pointer identity. |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9407 | auto &NodeOrVector = (*Parents)[MapNode]; |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 9408 | if (NodeOrVector.isNull()) { |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 9409 | if (const auto *D = ParentStack.back().get<Decl>()) |
| 9410 | NodeOrVector = D; |
| 9411 | else if (const auto *S = ParentStack.back().get<Stmt>()) |
| 9412 | NodeOrVector = S; |
| 9413 | else |
| 9414 | NodeOrVector = |
| 9415 | new ast_type_traits::DynTypedNode(ParentStack.back()); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 9416 | } else { |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 9417 | if (!NodeOrVector.template is<ASTContext::ParentVector *>()) { |
| 9418 | auto *Vector = new ASTContext::ParentVector( |
| 9419 | 1, getSingleDynTypedNodeFromParentMap(NodeOrVector)); |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 9420 | if (auto *Node = |
| 9421 | NodeOrVector |
| 9422 | .template dyn_cast<ast_type_traits::DynTypedNode *>()) |
| 9423 | delete Node; |
Benjamin Kramer | 422b3ff | 2015-10-23 13:24:18 +0000 | [diff] [blame] | 9424 | NodeOrVector = Vector; |
Samuel Benzaquen | 3ca0a7b | 2014-06-13 13:31:40 +0000 | [diff] [blame] | 9425 | } |
Samuel Benzaquen | 3ca0a7b | 2014-06-13 13:31:40 +0000 | [diff] [blame] | 9426 | |
| 9427 | auto *Vector = |
| 9428 | NodeOrVector.template get<ASTContext::ParentVector *>(); |
| 9429 | // Skip duplicates for types that have memoization data. |
| 9430 | // We must check that the type has memoization data before calling |
| 9431 | // std::find() because DynTypedNode::operator== can't compare all |
| 9432 | // types. |
| 9433 | bool Found = ParentStack.back().getMemoizationData() && |
| 9434 | std::find(Vector->begin(), Vector->end(), |
| 9435 | ParentStack.back()) != Vector->end(); |
| 9436 | if (!Found) |
| 9437 | Vector->push_back(ParentStack.back()); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 9438 | } |
| 9439 | } |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9440 | ParentStack.push_back(createDynTypedNode(Node)); |
Richard Smith | 8583872 | 2015-11-24 03:09:01 +0000 | [diff] [blame] | 9441 | bool Result = BaseTraverse(); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9442 | ParentStack.pop_back(); |
| 9443 | return Result; |
| 9444 | } |
| 9445 | |
| 9446 | bool TraverseDecl(Decl *DeclNode) { |
Richard Smith | 8583872 | 2015-11-24 03:09:01 +0000 | [diff] [blame] | 9447 | return TraverseNode(DeclNode, DeclNode, |
| 9448 | [&] { return VisitorBase::TraverseDecl(DeclNode); }, |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9449 | Parents); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9450 | } |
| 9451 | |
| 9452 | bool TraverseStmt(Stmt *StmtNode) { |
Richard Smith | 8583872 | 2015-11-24 03:09:01 +0000 | [diff] [blame] | 9453 | return TraverseNode(StmtNode, StmtNode, |
| 9454 | [&] { return VisitorBase::TraverseStmt(StmtNode); }, |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9455 | Parents); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9456 | } |
| 9457 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9458 | bool TraverseTypeLoc(TypeLoc TypeLocNode) { |
Richard Smith | 8583872 | 2015-11-24 03:09:01 +0000 | [diff] [blame] | 9459 | return TraverseNode( |
| 9460 | TypeLocNode, ast_type_traits::DynTypedNode::create(TypeLocNode), |
| 9461 | [&] { return VisitorBase::TraverseTypeLoc(TypeLocNode); }, |
| 9462 | OtherParents); |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9463 | } |
| 9464 | |
| 9465 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSLocNode) { |
| 9466 | return TraverseNode( |
| 9467 | NNSLocNode, ast_type_traits::DynTypedNode::create(NNSLocNode), |
Richard Smith | 8583872 | 2015-11-24 03:09:01 +0000 | [diff] [blame] | 9468 | [&] { |
| 9469 | return VisitorBase::TraverseNestedNameSpecifierLoc(NNSLocNode); |
| 9470 | }, |
| 9471 | OtherParents); |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9472 | } |
| 9473 | |
| 9474 | ASTContext::ParentMapPointers *Parents; |
| 9475 | ASTContext::ParentMapOtherNodes *OtherParents; |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9476 | llvm::SmallVector<ast_type_traits::DynTypedNode, 16> ParentStack; |
| 9477 | |
| 9478 | friend class RecursiveASTVisitor<ParentMapASTVisitor>; |
| 9479 | }; |
| 9480 | |
Eugene Zelenko | d4304d2 | 2015-11-04 21:37:17 +0000 | [diff] [blame] | 9481 | } // anonymous namespace |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9482 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9483 | template <typename NodeTy, typename MapTy> |
| 9484 | static ASTContext::DynTypedNodeList getDynNodeFromMap(const NodeTy &Node, |
| 9485 | const MapTy &Map) { |
| 9486 | auto I = Map.find(Node); |
| 9487 | if (I == Map.end()) { |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 9488 | return llvm::ArrayRef<ast_type_traits::DynTypedNode>(); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9489 | } |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9490 | if (auto *V = I->second.template dyn_cast<ASTContext::ParentVector *>()) { |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 9491 | return llvm::makeArrayRef(*V); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 9492 | } |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 9493 | return getSingleDynTypedNodeFromParentMap(I->second); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9494 | } |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 9495 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9496 | ASTContext::DynTypedNodeList |
| 9497 | ASTContext::getParents(const ast_type_traits::DynTypedNode &Node) { |
| 9498 | if (!PointerParents) { |
| 9499 | // We always need to run over the whole translation unit, as |
| 9500 | // hasAncestor can escape any subtree. |
| 9501 | auto Maps = ParentMapASTVisitor::buildMap(*getTranslationUnitDecl()); |
| 9502 | PointerParents.reset(Maps.first); |
| 9503 | OtherParents.reset(Maps.second); |
| 9504 | } |
| 9505 | if (Node.getNodeKind().hasPointerIdentity()) |
| 9506 | return getDynNodeFromMap(Node.getMemoizationData(), *PointerParents); |
| 9507 | return getDynNodeFromMap(Node, *OtherParents); |
| 9508 | } |
| 9509 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 9510 | bool |
| 9511 | ASTContext::ObjCMethodsAreEqual(const ObjCMethodDecl *MethodDecl, |
| 9512 | const ObjCMethodDecl *MethodImpl) { |
| 9513 | // No point trying to match an unavailable/deprecated mothod. |
| 9514 | if (MethodDecl->hasAttr<UnavailableAttr>() |
| 9515 | || MethodDecl->hasAttr<DeprecatedAttr>()) |
| 9516 | return false; |
| 9517 | if (MethodDecl->getObjCDeclQualifier() != |
| 9518 | MethodImpl->getObjCDeclQualifier()) |
| 9519 | return false; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9520 | if (!hasSameType(MethodDecl->getReturnType(), MethodImpl->getReturnType())) |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 9521 | return false; |
| 9522 | |
| 9523 | if (MethodDecl->param_size() != MethodImpl->param_size()) |
| 9524 | return false; |
| 9525 | |
| 9526 | for (ObjCMethodDecl::param_const_iterator IM = MethodImpl->param_begin(), |
| 9527 | IF = MethodDecl->param_begin(), EM = MethodImpl->param_end(), |
| 9528 | EF = MethodDecl->param_end(); |
| 9529 | IM != EM && IF != EF; ++IM, ++IF) { |
| 9530 | const ParmVarDecl *DeclVar = (*IF); |
| 9531 | const ParmVarDecl *ImplVar = (*IM); |
| 9532 | if (ImplVar->getObjCDeclQualifier() != DeclVar->getObjCDeclQualifier()) |
| 9533 | return false; |
| 9534 | if (!hasSameType(DeclVar->getType(), ImplVar->getType())) |
| 9535 | return false; |
| 9536 | } |
| 9537 | return (MethodDecl->isVariadic() == MethodImpl->isVariadic()); |
| 9538 | |
| 9539 | } |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 9540 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 9541 | uint64_t ASTContext::getTargetNullPointerValue(QualType QT) const { |
| 9542 | unsigned AS; |
| 9543 | if (QT->getUnqualifiedDesugaredType()->isNullPtrType()) |
| 9544 | AS = 0; |
| 9545 | else |
| 9546 | AS = QT->getPointeeType().getAddressSpace(); |
| 9547 | |
| 9548 | return getTargetInfo().getNullPointerValue(AS); |
| 9549 | } |
| 9550 | |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame^] | 9551 | unsigned ASTContext::getTargetAddressSpace(unsigned AS) const { |
| 9552 | // For OpenCL, only function local variables are not explicitly marked with |
| 9553 | // an address space in the AST, and these need to be the address space of |
| 9554 | // alloca. |
| 9555 | if (!AS && LangOpts.OpenCL) |
| 9556 | return getTargetInfo().getDataLayout().getAllocaAddrSpace(); |
| 9557 | if (AS >= LangAS::Count) |
| 9558 | return AS - LangAS::Count; |
| 9559 | else |
| 9560 | return (*AddrSpaceMap)[AS]; |
| 9561 | } |
| 9562 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 9563 | // Explicitly instantiate this in case a Redeclarable<T> is used from a TU that |
| 9564 | // doesn't include ASTContext.h |
| 9565 | template |
| 9566 | clang::LazyGenerationalUpdatePtr< |
| 9567 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::ValueType |
| 9568 | clang::LazyGenerationalUpdatePtr< |
| 9569 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::makeValue( |
| 9570 | const clang::ASTContext &Ctx, Decl *Value); |