Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 1 | //===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ASTContext interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTContext.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "CXXABI.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTMutationListener.h" |
| 17 | #include "clang/AST/Attr.h" |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 18 | #include "clang/AST/CharUnits.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/Comment.h" |
Dmitri Gribenko | ca7f80a | 2012-08-09 00:03:17 +0000 | [diff] [blame] | 20 | #include "clang/AST/CommentCommandTraits.h" |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Steve Naroff | 67391b8 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExternalASTSource.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 27 | #include "clang/AST/Mangle.h" |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 28 | #include "clang/AST/MangleNumberingContext.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 29 | #include "clang/AST/RecordLayout.h" |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 30 | #include "clang/AST/RecursiveASTVisitor.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 31 | #include "clang/AST/TypeLoc.h" |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 32 | #include "clang/AST/VTableBuilder.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 33 | #include "clang/Basic/Builtins.h" |
Chris Lattner | d286851 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 34 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 35 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallString.h" |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/StringExtras.h" |
Robert Lytton | eaf6f36 | 2013-11-12 10:09:34 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/Triple.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Capacity.h" |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 40 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 42 | #include <map> |
Anders Carlsson | a4267a6 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 43 | |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 44 | using namespace clang; |
| 45 | |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 46 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 47 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 48 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 49 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 50 | unsigned ASTContext::NumImplicitMoveConstructors; |
| 51 | unsigned ASTContext::NumImplicitMoveConstructorsDeclared; |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 52 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 53 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 54 | unsigned ASTContext::NumImplicitMoveAssignmentOperators; |
| 55 | unsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 56 | unsigned ASTContext::NumImplicitDestructors; |
| 57 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 58 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 59 | enum FloatingRank { |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 60 | HalfRank, FloatRank, DoubleRank, LongDoubleRank |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 63 | RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 64 | if (!CommentsLoaded && ExternalSource) { |
| 65 | ExternalSource->ReadComments(); |
Dmitri Gribenko | 9ee0e30 | 2014-03-27 15:40:39 +0000 | [diff] [blame] | 66 | |
| 67 | #ifndef NDEBUG |
| 68 | ArrayRef<RawComment *> RawComments = Comments.getComments(); |
| 69 | assert(std::is_sorted(RawComments.begin(), RawComments.end(), |
| 70 | BeforeThanCompare<RawComment>(SourceMgr))); |
| 71 | #endif |
| 72 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 73 | CommentsLoaded = true; |
| 74 | } |
| 75 | |
| 76 | assert(D); |
| 77 | |
Dmitri Gribenko | df17d64 | 2012-06-28 16:19:39 +0000 | [diff] [blame] | 78 | // User can not attach documentation to implicit declarations. |
| 79 | if (D->isImplicit()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 80 | return nullptr; |
Dmitri Gribenko | df17d64 | 2012-06-28 16:19:39 +0000 | [diff] [blame] | 81 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 82 | // User can not attach documentation to implicit instantiations. |
| 83 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 84 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 85 | return nullptr; |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 88 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 89 | if (VD->isStaticDataMember() && |
| 90 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 91 | return nullptr; |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) { |
| 95 | if (CRD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 96 | return nullptr; |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 99 | if (const ClassTemplateSpecializationDecl *CTSD = |
| 100 | dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
| 101 | TemplateSpecializationKind TSK = CTSD->getSpecializationKind(); |
| 102 | if (TSK == TSK_ImplicitInstantiation || |
| 103 | TSK == TSK_Undeclared) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 104 | return nullptr; |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 107 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 108 | if (ED->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 109 | return nullptr; |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 110 | } |
Fariborz Jahanian | 799a403 | 2013-04-17 21:05:20 +0000 | [diff] [blame] | 111 | if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 112 | // When tag declaration (but not definition!) is part of the |
| 113 | // decl-specifier-seq of some other declaration, it doesn't get comment |
| 114 | if (TD->isEmbeddedInDeclarator() && !TD->isCompleteDefinition()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 115 | return nullptr; |
Fariborz Jahanian | 799a403 | 2013-04-17 21:05:20 +0000 | [diff] [blame] | 116 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 117 | // TODO: handle comments for function parameters properly. |
| 118 | if (isa<ParmVarDecl>(D)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 119 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 120 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 121 | // TODO: we could look up template parameter documentation in the template |
| 122 | // documentation. |
| 123 | if (isa<TemplateTypeParmDecl>(D) || |
| 124 | isa<NonTypeTemplateParmDecl>(D) || |
| 125 | isa<TemplateTemplateParmDecl>(D)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 126 | return nullptr; |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 127 | |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 128 | ArrayRef<RawComment *> RawComments = Comments.getComments(); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 129 | |
| 130 | // If there are no comments anywhere, we won't find anything. |
| 131 | if (RawComments.empty()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 132 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 133 | |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 134 | // Find declaration location. |
| 135 | // For Objective-C declarations we generally don't expect to have multiple |
| 136 | // declarators, thus use declaration starting location as the "declaration |
| 137 | // location". |
| 138 | // For all other declarations multiple declarators are used quite frequently, |
| 139 | // so we use the location of the identifier as the "declaration location". |
| 140 | SourceLocation DeclLoc; |
| 141 | if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) || |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 142 | isa<ObjCPropertyDecl>(D) || |
Dmitri Gribenko | 7f4b377 | 2012-08-02 20:49:51 +0000 | [diff] [blame] | 143 | isa<RedeclarableTemplateDecl>(D) || |
| 144 | isa<ClassTemplateSpecializationDecl>(D)) |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 145 | DeclLoc = D->getLocStart(); |
Fariborz Jahanian | b64e95f | 2013-07-24 22:58:51 +0000 | [diff] [blame] | 146 | else { |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 147 | DeclLoc = D->getLocation(); |
Dmitri Gribenko | ef099dc | 2014-03-27 16:40:51 +0000 | [diff] [blame] | 148 | if (DeclLoc.isMacroID()) { |
| 149 | if (isa<TypedefDecl>(D)) { |
| 150 | // If location of the typedef name is in a macro, it is because being |
| 151 | // declared via a macro. Try using declaration's starting location as |
| 152 | // the "declaration location". |
| 153 | DeclLoc = D->getLocStart(); |
| 154 | } else if (const TagDecl *TD = dyn_cast<TagDecl>(D)) { |
| 155 | // If location of the tag decl is inside a macro, but the spelling of |
| 156 | // the tag name comes from a macro argument, it looks like a special |
| 157 | // macro like NS_ENUM is being used to define the tag decl. In that |
| 158 | // case, adjust the source location to the expansion loc so that we can |
| 159 | // attach the comment to the tag decl. |
| 160 | if (SourceMgr.isMacroArgExpansion(DeclLoc) && |
| 161 | TD->isCompleteDefinition()) |
| 162 | DeclLoc = SourceMgr.getExpansionLoc(DeclLoc); |
| 163 | } |
| 164 | } |
Fariborz Jahanian | b64e95f | 2013-07-24 22:58:51 +0000 | [diff] [blame] | 165 | } |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 166 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 167 | // If the declaration doesn't map directly to a location in a file, we |
| 168 | // can't find the comment. |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 169 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 170 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 171 | |
| 172 | // Find the comment that occurs just after this declaration. |
Dmitri Gribenko | 82ea947 | 2012-07-17 22:01:09 +0000 | [diff] [blame] | 173 | ArrayRef<RawComment *>::iterator Comment; |
| 174 | { |
| 175 | // When searching for comments during parsing, the comment we are looking |
| 176 | // for is usually among the last two comments we parsed -- check them |
| 177 | // first. |
Dmitri Gribenko | a7d16ce | 2013-04-10 15:35:17 +0000 | [diff] [blame] | 178 | RawComment CommentAtDeclLoc( |
| 179 | SourceMgr, SourceRange(DeclLoc), false, |
| 180 | LangOpts.CommentOpts.ParseAllComments); |
Dmitri Gribenko | 82ea947 | 2012-07-17 22:01:09 +0000 | [diff] [blame] | 181 | BeforeThanCompare<RawComment> Compare(SourceMgr); |
| 182 | ArrayRef<RawComment *>::iterator MaybeBeforeDecl = RawComments.end() - 1; |
| 183 | bool Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); |
| 184 | if (!Found && RawComments.size() >= 2) { |
| 185 | MaybeBeforeDecl--; |
| 186 | Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); |
| 187 | } |
| 188 | |
| 189 | if (Found) { |
| 190 | Comment = MaybeBeforeDecl + 1; |
| 191 | assert(Comment == std::lower_bound(RawComments.begin(), RawComments.end(), |
| 192 | &CommentAtDeclLoc, Compare)); |
| 193 | } else { |
| 194 | // Slow path. |
| 195 | Comment = std::lower_bound(RawComments.begin(), RawComments.end(), |
| 196 | &CommentAtDeclLoc, Compare); |
| 197 | } |
| 198 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 199 | |
| 200 | // Decompose the location for the declaration and find the beginning of the |
| 201 | // file buffer. |
| 202 | std::pair<FileID, unsigned> DeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLoc); |
| 203 | |
| 204 | // First check whether we have a trailing comment. |
| 205 | if (Comment != RawComments.end() && |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 206 | (*Comment)->isDocumentation() && (*Comment)->isTrailingComment() && |
Fariborz Jahanian | fad2854 | 2013-08-06 23:29:00 +0000 | [diff] [blame] | 207 | (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D) || |
Fariborz Jahanian | 3ab6222 | 2013-08-07 16:40:29 +0000 | [diff] [blame] | 208 | isa<ObjCMethodDecl>(D) || isa<ObjCPropertyDecl>(D))) { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 209 | std::pair<FileID, unsigned> CommentBeginDecomp |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 210 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getBegin()); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 211 | // Check that Doxygen trailing comment comes after the declaration, starts |
| 212 | // on the same line and in the same file as the declaration. |
| 213 | if (DeclLocDecomp.first == CommentBeginDecomp.first && |
| 214 | SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second) |
| 215 | == SourceMgr.getLineNumber(CommentBeginDecomp.first, |
| 216 | CommentBeginDecomp.second)) { |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 217 | return *Comment; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | |
| 221 | // The comment just after the declaration was not a trailing comment. |
| 222 | // Let's look at the previous comment. |
| 223 | if (Comment == RawComments.begin()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 224 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 225 | --Comment; |
| 226 | |
| 227 | // Check that we actually have a non-member Doxygen comment. |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 228 | if (!(*Comment)->isDocumentation() || (*Comment)->isTrailingComment()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 229 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 230 | |
| 231 | // Decompose the end of the comment. |
| 232 | std::pair<FileID, unsigned> CommentEndDecomp |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 233 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getEnd()); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 234 | |
| 235 | // If the comment and the declaration aren't in the same file, then they |
| 236 | // aren't related. |
| 237 | if (DeclLocDecomp.first != CommentEndDecomp.first) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 238 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 239 | |
| 240 | // Get the corresponding buffer. |
| 241 | bool Invalid = false; |
| 242 | const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first, |
| 243 | &Invalid).data(); |
| 244 | if (Invalid) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 245 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 246 | |
| 247 | // Extract text between the comment and declaration. |
| 248 | StringRef Text(Buffer + CommentEndDecomp.second, |
| 249 | DeclLocDecomp.second - CommentEndDecomp.second); |
| 250 | |
Dmitri Gribenko | 7e8729b | 2012-06-27 23:43:37 +0000 | [diff] [blame] | 251 | // There should be no other declarations or preprocessor directives between |
| 252 | // comment and declaration. |
Argyrios Kyrtzidis | b534d3a | 2013-07-26 18:38:12 +0000 | [diff] [blame] | 253 | if (Text.find_first_of(";{}#@") != StringRef::npos) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 254 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 255 | |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 256 | return *Comment; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 259 | namespace { |
| 260 | /// If we have a 'templated' declaration for a template, adjust 'D' to |
| 261 | /// refer to the actual template. |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 262 | /// If we have an implicit instantiation, adjust 'D' to refer to template. |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 263 | const Decl *adjustDeclToTemplate(const Decl *D) { |
Douglas Gregor | 35ceb27 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 264 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 265 | // Is this function declaration part of a function template? |
Douglas Gregor | 35ceb27 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 266 | if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate()) |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 267 | return FTD; |
| 268 | |
| 269 | // Nothing to do if function is not an implicit instantiation. |
| 270 | if (FD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
| 271 | return D; |
| 272 | |
| 273 | // Function is an implicit instantiation of a function template? |
| 274 | if (const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate()) |
| 275 | return FTD; |
| 276 | |
| 277 | // Function is instantiated from a member definition of a class template? |
| 278 | if (const FunctionDecl *MemberDecl = |
| 279 | FD->getInstantiatedFromMemberFunction()) |
| 280 | return MemberDecl; |
| 281 | |
| 282 | return D; |
Douglas Gregor | 35ceb27 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 283 | } |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 284 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 285 | // Static data member is instantiated from a member definition of a class |
| 286 | // template? |
| 287 | if (VD->isStaticDataMember()) |
| 288 | if (const VarDecl *MemberDecl = VD->getInstantiatedFromStaticDataMember()) |
| 289 | return MemberDecl; |
| 290 | |
| 291 | return D; |
| 292 | } |
| 293 | if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) { |
| 294 | // Is this class declaration part of a class template? |
| 295 | if (const ClassTemplateDecl *CTD = CRD->getDescribedClassTemplate()) |
| 296 | return CTD; |
| 297 | |
| 298 | // Class is an implicit instantiation of a class template or partial |
| 299 | // specialization? |
| 300 | if (const ClassTemplateSpecializationDecl *CTSD = |
| 301 | dyn_cast<ClassTemplateSpecializationDecl>(CRD)) { |
| 302 | if (CTSD->getSpecializationKind() != TSK_ImplicitInstantiation) |
| 303 | return D; |
| 304 | llvm::PointerUnion<ClassTemplateDecl *, |
| 305 | ClassTemplatePartialSpecializationDecl *> |
| 306 | PU = CTSD->getSpecializedTemplateOrPartial(); |
| 307 | return PU.is<ClassTemplateDecl*>() ? |
| 308 | static_cast<const Decl*>(PU.get<ClassTemplateDecl *>()) : |
| 309 | static_cast<const Decl*>( |
| 310 | PU.get<ClassTemplatePartialSpecializationDecl *>()); |
| 311 | } |
| 312 | |
| 313 | // Class is instantiated from a member definition of a class template? |
| 314 | if (const MemberSpecializationInfo *Info = |
| 315 | CRD->getMemberSpecializationInfo()) |
| 316 | return Info->getInstantiatedFrom(); |
| 317 | |
| 318 | return D; |
| 319 | } |
| 320 | if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 321 | // Enum is instantiated from a member definition of a class template? |
| 322 | if (const EnumDecl *MemberDecl = ED->getInstantiatedFromMemberEnum()) |
| 323 | return MemberDecl; |
| 324 | |
| 325 | return D; |
| 326 | } |
| 327 | // FIXME: Adjust alias templates? |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 328 | return D; |
| 329 | } |
| 330 | } // unnamed namespace |
| 331 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 332 | const RawComment *ASTContext::getRawCommentForAnyRedecl( |
| 333 | const Decl *D, |
| 334 | const Decl **OriginalDecl) const { |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 335 | D = adjustDeclToTemplate(D); |
Douglas Gregor | 35ceb27 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 336 | |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 337 | // Check whether we have cached a comment for this declaration already. |
| 338 | { |
| 339 | llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos = |
| 340 | RedeclComments.find(D); |
| 341 | if (Pos != RedeclComments.end()) { |
| 342 | const RawCommentAndCacheFlags &Raw = Pos->second; |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 343 | if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) { |
| 344 | if (OriginalDecl) |
| 345 | *OriginalDecl = Raw.getOriginalDecl(); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 346 | return Raw.getRaw(); |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 347 | } |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 348 | } |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 349 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 350 | |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 351 | // Search for comments attached to declarations in the redeclaration chain. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 352 | const RawComment *RC = nullptr; |
| 353 | const Decl *OriginalDeclForRC = nullptr; |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 354 | for (auto I : D->redecls()) { |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 355 | llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos = |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 356 | RedeclComments.find(I); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 357 | if (Pos != RedeclComments.end()) { |
| 358 | const RawCommentAndCacheFlags &Raw = Pos->second; |
| 359 | if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) { |
| 360 | RC = Raw.getRaw(); |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 361 | OriginalDeclForRC = Raw.getOriginalDecl(); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 362 | break; |
| 363 | } |
| 364 | } else { |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 365 | RC = getRawCommentForDeclNoCache(I); |
| 366 | OriginalDeclForRC = I; |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 367 | RawCommentAndCacheFlags Raw; |
| 368 | if (RC) { |
| 369 | Raw.setRaw(RC); |
| 370 | Raw.setKind(RawCommentAndCacheFlags::FromDecl); |
| 371 | } else |
| 372 | Raw.setKind(RawCommentAndCacheFlags::NoCommentInDecl); |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 373 | Raw.setOriginalDecl(I); |
| 374 | RedeclComments[I] = Raw; |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 375 | if (RC) |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | |
Dmitri Gribenko | 5c8897d | 2012-06-28 16:25:36 +0000 | [diff] [blame] | 380 | // If we found a comment, it should be a documentation comment. |
| 381 | assert(!RC || RC->isDocumentation()); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 382 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 383 | if (OriginalDecl) |
| 384 | *OriginalDecl = OriginalDeclForRC; |
| 385 | |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 386 | // Update cache for every declaration in the redeclaration chain. |
| 387 | RawCommentAndCacheFlags Raw; |
| 388 | Raw.setRaw(RC); |
| 389 | Raw.setKind(RawCommentAndCacheFlags::FromRedecl); |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 390 | Raw.setOriginalDecl(OriginalDeclForRC); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 391 | |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 392 | for (auto I : D->redecls()) { |
| 393 | RawCommentAndCacheFlags &R = RedeclComments[I]; |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 394 | if (R.getKind() == RawCommentAndCacheFlags::NoCommentInDecl) |
| 395 | R = Raw; |
| 396 | } |
| 397 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 398 | return RC; |
| 399 | } |
| 400 | |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 401 | static void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod, |
| 402 | SmallVectorImpl<const NamedDecl *> &Redeclared) { |
| 403 | const DeclContext *DC = ObjCMethod->getDeclContext(); |
| 404 | if (const ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(DC)) { |
| 405 | const ObjCInterfaceDecl *ID = IMD->getClassInterface(); |
| 406 | if (!ID) |
| 407 | return; |
| 408 | // Add redeclared method here. |
Aaron Ballman | b4a5345 | 2014-03-13 21:57:01 +0000 | [diff] [blame] | 409 | for (const auto *Ext : ID->known_extensions()) { |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 410 | if (ObjCMethodDecl *RedeclaredMethod = |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 411 | Ext->getMethod(ObjCMethod->getSelector(), |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 412 | ObjCMethod->isInstanceMethod())) |
| 413 | Redeclared.push_back(RedeclaredMethod); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 418 | comments::FullComment *ASTContext::cloneFullComment(comments::FullComment *FC, |
| 419 | const Decl *D) const { |
| 420 | comments::DeclInfo *ThisDeclInfo = new (*this) comments::DeclInfo; |
| 421 | ThisDeclInfo->CommentDecl = D; |
| 422 | ThisDeclInfo->IsFilled = false; |
| 423 | ThisDeclInfo->fill(); |
| 424 | ThisDeclInfo->CommentDecl = FC->getDecl(); |
Argyrios Kyrtzidis | 7daabbd | 2014-04-27 22:53:03 +0000 | [diff] [blame] | 425 | if (!ThisDeclInfo->TemplateParameters) |
| 426 | ThisDeclInfo->TemplateParameters = FC->getDeclInfo()->TemplateParameters; |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 427 | comments::FullComment *CFC = |
| 428 | new (*this) comments::FullComment(FC->getBlocks(), |
| 429 | ThisDeclInfo); |
| 430 | return CFC; |
| 431 | |
| 432 | } |
| 433 | |
Richard Smith | b39b9d5 | 2013-05-21 05:24:00 +0000 | [diff] [blame] | 434 | comments::FullComment *ASTContext::getLocalCommentForDeclUncached(const Decl *D) const { |
| 435 | const RawComment *RC = getRawCommentForDeclNoCache(D); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 436 | return RC ? RC->parse(*this, nullptr, D) : nullptr; |
Richard Smith | b39b9d5 | 2013-05-21 05:24:00 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 439 | comments::FullComment *ASTContext::getCommentForDecl( |
| 440 | const Decl *D, |
| 441 | const Preprocessor *PP) const { |
Fariborz Jahanian | 096f7c1 | 2013-05-13 17:27:00 +0000 | [diff] [blame] | 442 | if (D->isInvalidDecl()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 443 | return nullptr; |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 444 | D = adjustDeclToTemplate(D); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 445 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 446 | const Decl *Canonical = D->getCanonicalDecl(); |
| 447 | llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos = |
| 448 | ParsedComments.find(Canonical); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 449 | |
| 450 | if (Pos != ParsedComments.end()) { |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 451 | if (Canonical != D) { |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 452 | comments::FullComment *FC = Pos->second; |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 453 | comments::FullComment *CFC = cloneFullComment(FC, D); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 454 | return CFC; |
| 455 | } |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 456 | return Pos->second; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 459 | const Decl *OriginalDecl; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 460 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 461 | const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 462 | if (!RC) { |
| 463 | if (isa<ObjCMethodDecl>(D) || isa<FunctionDecl>(D)) { |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 464 | SmallVector<const NamedDecl*, 8> Overridden; |
Fariborz Jahanian | 37494a1 | 2013-01-12 00:28:34 +0000 | [diff] [blame] | 465 | const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 466 | if (OMD && OMD->isPropertyAccessor()) |
| 467 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) |
| 468 | if (comments::FullComment *FC = getCommentForDecl(PDecl, PP)) |
| 469 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 37494a1 | 2013-01-12 00:28:34 +0000 | [diff] [blame] | 470 | if (OMD) |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 471 | addRedeclaredMethods(OMD, Overridden); |
| 472 | getOverriddenMethods(dyn_cast<NamedDecl>(D), Overridden); |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 473 | for (unsigned i = 0, e = Overridden.size(); i < e; i++) |
| 474 | if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP)) |
| 475 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 476 | } |
Fariborz Jahanian | 6384fbb | 2013-05-02 15:44:16 +0000 | [diff] [blame] | 477 | else if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 478 | // Attach any tag type's documentation to its typedef if latter |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 479 | // does not have one of its own. |
Fariborz Jahanian | 40abf34 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 480 | QualType QT = TD->getUnderlyingType(); |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 481 | if (const TagType *TT = QT->getAs<TagType>()) |
| 482 | if (const Decl *TD = TT->getDecl()) |
| 483 | if (comments::FullComment *FC = getCommentForDecl(TD, PP)) |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 484 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 40abf34 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 485 | } |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 486 | else if (const ObjCInterfaceDecl *IC = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 487 | while (IC->getSuperClass()) { |
| 488 | IC = IC->getSuperClass(); |
| 489 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
| 490 | return cloneFullComment(FC, D); |
| 491 | } |
| 492 | } |
| 493 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
| 494 | if (const ObjCInterfaceDecl *IC = CD->getClassInterface()) |
| 495 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
| 496 | return cloneFullComment(FC, D); |
| 497 | } |
| 498 | else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 499 | if (!(RD = RD->getDefinition())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 500 | return nullptr; |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 501 | // Check non-virtual bases. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 502 | for (const auto &I : RD->bases()) { |
| 503 | if (I.isVirtual() || (I.getAccessSpecifier() != AS_public)) |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 504 | continue; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 505 | QualType Ty = I.getType(); |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 506 | if (Ty.isNull()) |
| 507 | continue; |
| 508 | if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) { |
| 509 | if (!(NonVirtualBase= NonVirtualBase->getDefinition())) |
| 510 | continue; |
| 511 | |
| 512 | if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP)) |
| 513 | return cloneFullComment(FC, D); |
| 514 | } |
| 515 | } |
| 516 | // Check virtual bases. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 517 | for (const auto &I : RD->vbases()) { |
| 518 | if (I.getAccessSpecifier() != AS_public) |
Fariborz Jahanian | 5a2e4a2 | 2013-04-26 23:34:36 +0000 | [diff] [blame] | 519 | continue; |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 520 | QualType Ty = I.getType(); |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 521 | if (Ty.isNull()) |
| 522 | continue; |
| 523 | if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) { |
| 524 | if (!(VirtualBase= VirtualBase->getDefinition())) |
| 525 | continue; |
| 526 | if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP)) |
| 527 | return cloneFullComment(FC, D); |
| 528 | } |
| 529 | } |
| 530 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 531 | return nullptr; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Dmitri Gribenko | bfda9f7 | 2012-08-22 18:12:19 +0000 | [diff] [blame] | 534 | // If the RawComment was attached to other redeclaration of this Decl, we |
| 535 | // should parse the comment in context of that other Decl. This is important |
| 536 | // because comments can contain references to parameter names which can be |
| 537 | // different across redeclarations. |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 538 | if (D != OriginalDecl) |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 539 | return getCommentForDecl(OriginalDecl, PP); |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 540 | |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 541 | comments::FullComment *FC = RC->parse(*this, PP, D); |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 542 | ParsedComments[Canonical] = FC; |
| 543 | return FC; |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 546 | void |
| 547 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 548 | TemplateTemplateParmDecl *Parm) { |
| 549 | ID.AddInteger(Parm->getDepth()); |
| 550 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 551 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 552 | |
| 553 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 554 | ID.AddInteger(Params->size()); |
| 555 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 556 | PEnd = Params->end(); |
| 557 | P != PEnd; ++P) { |
| 558 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
| 559 | ID.AddInteger(0); |
| 560 | ID.AddBoolean(TTP->isParameterPack()); |
| 561 | continue; |
| 562 | } |
| 563 | |
| 564 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 565 | ID.AddInteger(1); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 566 | ID.AddBoolean(NTTP->isParameterPack()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 567 | ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 568 | if (NTTP->isExpandedParameterPack()) { |
| 569 | ID.AddBoolean(true); |
| 570 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 571 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 572 | QualType T = NTTP->getExpansionType(I); |
| 573 | ID.AddPointer(T.getCanonicalType().getAsOpaquePtr()); |
| 574 | } |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 575 | } else |
| 576 | ID.AddBoolean(false); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 577 | continue; |
| 578 | } |
| 579 | |
| 580 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); |
| 581 | ID.AddInteger(2); |
| 582 | Profile(ID, TTP); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | TemplateTemplateParmDecl * |
| 587 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 588 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 589 | // Check if we already have a canonical template template parameter. |
| 590 | llvm::FoldingSetNodeID ID; |
| 591 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 592 | void *InsertPos = nullptr; |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 593 | CanonicalTemplateTemplateParm *Canonical |
| 594 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 595 | if (Canonical) |
| 596 | return Canonical->getParam(); |
| 597 | |
| 598 | // Build a canonical template parameter list. |
| 599 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 600 | SmallVector<NamedDecl *, 4> CanonParams; |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 601 | CanonParams.reserve(Params->size()); |
| 602 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 603 | PEnd = Params->end(); |
| 604 | P != PEnd; ++P) { |
| 605 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
| 606 | CanonParams.push_back( |
| 607 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 608 | SourceLocation(), |
| 609 | SourceLocation(), |
| 610 | TTP->getDepth(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 611 | TTP->getIndex(), nullptr, false, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 612 | TTP->isParameterPack())); |
| 613 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 614 | = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 615 | QualType T = getCanonicalType(NTTP->getType()); |
| 616 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 617 | NonTypeTemplateParmDecl *Param; |
| 618 | if (NTTP->isExpandedParameterPack()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 619 | SmallVector<QualType, 2> ExpandedTypes; |
| 620 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 621 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 622 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 623 | ExpandedTInfos.push_back( |
| 624 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 625 | } |
| 626 | |
| 627 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 628 | SourceLocation(), |
| 629 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 630 | NTTP->getDepth(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 631 | NTTP->getPosition(), nullptr, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 632 | T, |
| 633 | TInfo, |
| 634 | ExpandedTypes.data(), |
| 635 | ExpandedTypes.size(), |
| 636 | ExpandedTInfos.data()); |
| 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 | |
| 654 | TemplateTemplateParmDecl *CanonTTP |
| 655 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 656 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 657 | TTP->getPosition(), |
| 658 | TTP->isParameterPack(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 659 | nullptr, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 660 | TemplateParameterList::Create(*this, SourceLocation(), |
| 661 | SourceLocation(), |
| 662 | CanonParams.data(), |
| 663 | CanonParams.size(), |
| 664 | SourceLocation())); |
| 665 | |
| 666 | // Get the new insert position for the node we care about. |
| 667 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 668 | assert(!Canonical && "Shouldn't be in the map!"); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 669 | (void)Canonical; |
| 670 | |
| 671 | // Create the canonical template template parameter entry. |
| 672 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 673 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 674 | return CanonTTP; |
| 675 | } |
| 676 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 677 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 678 | if (!LangOpts.CPlusPlus) return nullptr; |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 679 | |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 680 | switch (T.getCXXABI().getKind()) { |
Joerg Sonnenberger | daa13aa | 2014-05-13 11:20:16 +0000 | [diff] [blame] | 681 | case TargetCXXABI::GenericARM: // Same as Itanium at this level |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 682 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 683 | case TargetCXXABI::iOS64: |
Joerg Sonnenberger | daa13aa | 2014-05-13 11:20:16 +0000 | [diff] [blame] | 684 | case TargetCXXABI::GenericAArch64: |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 685 | case TargetCXXABI::GenericMIPS: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 686 | case TargetCXXABI::GenericItanium: |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 687 | return CreateItaniumCXXABI(*this); |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 688 | case TargetCXXABI::Microsoft: |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 689 | return CreateMicrosoftCXXABI(*this); |
| 690 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 691 | llvm_unreachable("Invalid CXXABI type!"); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 694 | static const LangAS::Map *getAddressSpaceMap(const TargetInfo &T, |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 695 | const LangOptions &LOpts) { |
| 696 | if (LOpts.FakeAddressSpaceMap) { |
| 697 | // The fake address space map must have a distinct entry for each |
| 698 | // language-specific address space. |
| 699 | static const unsigned FakeAddrSpaceMap[] = { |
| 700 | 1, // opencl_global |
| 701 | 2, // opencl_local |
Peter Collingbourne | f44bdf9 | 2012-05-20 21:08:35 +0000 | [diff] [blame] | 702 | 3, // opencl_constant |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 703 | 4, // opencl_generic |
| 704 | 5, // cuda_device |
| 705 | 6, // cuda_constant |
| 706 | 7 // cuda_shared |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 707 | }; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 708 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 709 | } else { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 710 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 711 | } |
| 712 | } |
| 713 | |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 714 | static bool isAddrSpaceMapManglingEnabled(const TargetInfo &TI, |
| 715 | const LangOptions &LangOpts) { |
| 716 | switch (LangOpts.getAddressSpaceMapMangling()) { |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 717 | case LangOptions::ASMM_Target: |
| 718 | return TI.useAddressSpaceMapMangling(); |
| 719 | case LangOptions::ASMM_On: |
| 720 | return true; |
| 721 | case LangOptions::ASMM_Off: |
| 722 | return false; |
| 723 | } |
NAKAMURA Takumi | 5c81ca4 | 2013-09-13 17:12:09 +0000 | [diff] [blame] | 724 | llvm_unreachable("getAddressSpaceMapMangling() doesn't cover anything."); |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Alexey Samsonov | 0b15e34 | 2014-10-15 22:17:27 +0000 | [diff] [blame] | 727 | ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM, |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 728 | IdentifierTable &idents, SelectorTable &sels, |
Alp Toker | 0804343 | 2014-05-03 03:46:04 +0000 | [diff] [blame] | 729 | Builtin::Context &builtins) |
Alexey Samsonov | 0b15e34 | 2014-10-15 22:17:27 +0000 | [diff] [blame] | 730 | : FunctionProtoTypes(this_()), TemplateSpecializationTypes(this_()), |
| 731 | DependentTemplateSpecializationTypes(this_()), |
| 732 | SubstTemplateTemplateParmPacks(this_()), |
| 733 | GlobalNestedNameSpecifier(nullptr), Int128Decl(nullptr), |
| 734 | UInt128Decl(nullptr), Float128StubDecl(nullptr), |
| 735 | BuiltinVaListDecl(nullptr), ObjCIdDecl(nullptr), ObjCSelDecl(nullptr), |
| 736 | ObjCClassDecl(nullptr), ObjCProtocolClassDecl(nullptr), BOOLDecl(nullptr), |
| 737 | CFConstantStringTypeDecl(nullptr), ObjCInstanceTypeDecl(nullptr), |
| 738 | FILEDecl(nullptr), jmp_bufDecl(nullptr), sigjmp_bufDecl(nullptr), |
| 739 | ucontext_tDecl(nullptr), BlockDescriptorType(nullptr), |
| 740 | BlockDescriptorExtendedType(nullptr), cudaConfigureCallDecl(nullptr), |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 741 | FirstLocalImport(), LastLocalImport(), ExternCContext(nullptr), |
| 742 | SourceMgr(SM), LangOpts(LOpts), |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 743 | SanitizerBL(new SanitizerBlacklist(LangOpts.SanitizerBlacklistFiles, SM)), |
Alexey Samsonov | 0b15e34 | 2014-10-15 22:17:27 +0000 | [diff] [blame] | 744 | AddrSpaceMap(nullptr), Target(nullptr), PrintingPolicy(LOpts), |
| 745 | Idents(idents), Selectors(sels), BuiltinInfo(builtins), |
| 746 | DeclarationNames(*this), ExternalSource(nullptr), Listener(nullptr), |
| 747 | Comments(SM), CommentsLoaded(false), |
| 748 | CommentCommandTraits(BumpAlloc, LOpts.CommentOpts), LastSDM(nullptr, 0) { |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 749 | TUDecl = TranslationUnitDecl::Create(*this); |
| 750 | } |
| 751 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 752 | ASTContext::~ASTContext() { |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 753 | ReleaseParentMapEntries(); |
| 754 | |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 755 | // Release the DenseMaps associated with DeclContext objects. |
| 756 | // FIXME: Is this the ideal solution? |
| 757 | ReleaseDeclContextMaps(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 758 | |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 759 | // Call all of the deallocation functions on all of their targets. |
| 760 | for (DeallocationMap::const_iterator I = Deallocations.begin(), |
| 761 | E = Deallocations.end(); I != E; ++I) |
| 762 | for (unsigned J = 0, N = I->second.size(); J != N; ++J) |
| 763 | (I->first)((I->second)[J]); |
| 764 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 765 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 766 | // because they can contain DenseMaps. |
| 767 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 768 | const ASTRecordLayout*>::iterator |
| 769 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 770 | // Increment in loop to prevent using deallocated memory. |
| 771 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 772 | R->Destroy(*this); |
| 773 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 774 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 775 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 776 | // Increment in loop to prevent using deallocated memory. |
| 777 | if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second)) |
| 778 | R->Destroy(*this); |
| 779 | } |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 780 | |
| 781 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 782 | AEnd = DeclAttrs.end(); |
| 783 | A != AEnd; ++A) |
| 784 | A->second->~AttrVec(); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 785 | |
Reid Kleckner | 588c937 | 2014-02-19 23:44:52 +0000 | [diff] [blame] | 786 | llvm::DeleteContainerSeconds(MangleNumberingContexts); |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 787 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 788 | |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 789 | void ASTContext::ReleaseParentMapEntries() { |
| 790 | if (!AllParents) return; |
| 791 | for (const auto &Entry : *AllParents) { |
| 792 | if (Entry.second.is<ast_type_traits::DynTypedNode *>()) { |
| 793 | delete Entry.second.get<ast_type_traits::DynTypedNode *>(); |
| 794 | } else { |
| 795 | assert(Entry.second.is<ParentVector *>()); |
| 796 | delete Entry.second.get<ParentVector *>(); |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 801 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 802 | Deallocations[Callback].push_back(Data); |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | void |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 806 | ASTContext::setExternalSource(IntrusiveRefCntPtr<ExternalASTSource> Source) { |
| 807 | ExternalSource = Source; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 810 | void ASTContext::PrintStats() const { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 811 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 812 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 813 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 814 | unsigned counts[] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 815 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 816 | #define ABSTRACT_TYPE(Name, Parent) |
| 817 | #include "clang/AST/TypeNodes.def" |
| 818 | 0 // Extra |
| 819 | }; |
Douglas Gregor | b1fe2c9 | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 820 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 821 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 822 | Type *T = Types[i]; |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 823 | counts[(unsigned)T->getTypeClass()]++; |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 826 | unsigned Idx = 0; |
| 827 | unsigned TotalBytes = 0; |
| 828 | #define TYPE(Name, Parent) \ |
| 829 | if (counts[Idx]) \ |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 830 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 831 | << " types\n"; \ |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 832 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 833 | ++Idx; |
| 834 | #define ABSTRACT_TYPE(Name, Parent) |
| 835 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 837 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 838 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 839 | // Implicit special member functions. |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 840 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 841 | << NumImplicitDefaultConstructors |
| 842 | << " implicit default constructors created\n"; |
| 843 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 844 | << NumImplicitCopyConstructors |
| 845 | << " implicit copy constructors created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 846 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 847 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 848 | << NumImplicitMoveConstructors |
| 849 | << " implicit move constructors created\n"; |
| 850 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 851 | << NumImplicitCopyAssignmentOperators |
| 852 | << " implicit copy assignment operators created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 853 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 854 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 855 | << NumImplicitMoveAssignmentOperators |
| 856 | << " implicit move assignment operators created\n"; |
| 857 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 858 | << NumImplicitDestructors |
| 859 | << " implicit destructors created\n"; |
| 860 | |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 861 | if (ExternalSource) { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 862 | llvm::errs() << "\n"; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 863 | ExternalSource->PrintStats(); |
| 864 | } |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 865 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 866 | BumpAlloc.PrintStats(); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 869 | ExternCContextDecl *ASTContext::getExternCContextDecl() const { |
| 870 | if (!ExternCContext) |
| 871 | ExternCContext = ExternCContextDecl::Create(*this, getTranslationUnitDecl()); |
| 872 | |
| 873 | return ExternCContext; |
| 874 | } |
| 875 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 876 | RecordDecl *ASTContext::buildImplicitRecord(StringRef Name, |
| 877 | RecordDecl::TagKind TK) const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 878 | SourceLocation Loc; |
| 879 | RecordDecl *NewDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 880 | if (getLangOpts().CPlusPlus) |
| 881 | NewDecl = CXXRecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, |
| 882 | Loc, &Idents.get(Name)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 883 | else |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 884 | NewDecl = RecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, Loc, |
| 885 | &Idents.get(Name)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 886 | NewDecl->setImplicit(); |
David Majnemer | f863736 | 2015-01-15 08:41:25 +0000 | [diff] [blame] | 887 | NewDecl->addAttr(TypeVisibilityAttr::CreateImplicit( |
| 888 | const_cast<ASTContext &>(*this), TypeVisibilityAttr::Default)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 889 | return NewDecl; |
| 890 | } |
| 891 | |
| 892 | TypedefDecl *ASTContext::buildImplicitTypedef(QualType T, |
| 893 | StringRef Name) const { |
| 894 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 895 | TypedefDecl *NewDecl = TypedefDecl::Create( |
| 896 | const_cast<ASTContext &>(*this), getTranslationUnitDecl(), |
| 897 | SourceLocation(), SourceLocation(), &Idents.get(Name), TInfo); |
| 898 | NewDecl->setImplicit(); |
| 899 | return NewDecl; |
| 900 | } |
| 901 | |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 902 | TypedefDecl *ASTContext::getInt128Decl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 903 | if (!Int128Decl) |
| 904 | Int128Decl = buildImplicitTypedef(Int128Ty, "__int128_t"); |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 905 | return Int128Decl; |
| 906 | } |
| 907 | |
| 908 | TypedefDecl *ASTContext::getUInt128Decl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 909 | if (!UInt128Decl) |
| 910 | UInt128Decl = buildImplicitTypedef(UnsignedInt128Ty, "__uint128_t"); |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 911 | return UInt128Decl; |
| 912 | } |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 913 | |
Nico Weber | e1687c5 | 2013-06-20 21:44:55 +0000 | [diff] [blame] | 914 | TypeDecl *ASTContext::getFloat128StubType() const { |
Nico Weber | 5b0b46f | 2013-06-21 01:29:36 +0000 | [diff] [blame] | 915 | assert(LangOpts.CPlusPlus && "should only be called for c++"); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 916 | if (!Float128StubDecl) |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 917 | Float128StubDecl = buildImplicitRecord("__float128"); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 918 | |
Nico Weber | e1687c5 | 2013-06-20 21:44:55 +0000 | [diff] [blame] | 919 | return Float128StubDecl; |
| 920 | } |
| 921 | |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 922 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 923 | BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 924 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 925 | Types.push_back(Ty); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 928 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target) { |
| 929 | assert((!this->Target || this->Target == &Target) && |
| 930 | "Incorrect target reinitialization"); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 931 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 932 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 933 | this->Target = &Target; |
| 934 | |
| 935 | ABI.reset(createCXXABI(Target)); |
| 936 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 937 | AddrSpaceMapMangling = isAddrSpaceMapManglingEnabled(Target, LangOpts); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 938 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 939 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 940 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 941 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 942 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 943 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 944 | // C99 6.2.5p3. |
Eli Friedman | 9ffd4a9 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 945 | if (LangOpts.CharIsSigned) |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 946 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 947 | else |
| 948 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 949 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 950 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 951 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 952 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 953 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 954 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 955 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 956 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 957 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 958 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 959 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 960 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 961 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 963 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 964 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 965 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 966 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 967 | |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 968 | // GNU extension, 128-bit integers. |
| 969 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 970 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 971 | |
Hans Wennborg | 0d81e01 | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 972 | // C++ 3.9.1p5 |
| 973 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
| 974 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 975 | else // -fshort-wchar makes wchar_t be unsigned. |
| 976 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 977 | if (LangOpts.CPlusPlus && LangOpts.WChar) |
| 978 | WideCharTy = WCharTy; |
| 979 | else { |
| 980 | // C99 (or C++ using -fno-wchar). |
| 981 | WideCharTy = getFromTargetType(Target.getWCharType()); |
| 982 | } |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 983 | |
James Molloy | 3636554 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 984 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 985 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 986 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 987 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 988 | else // C99 |
| 989 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 990 | |
| 991 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 992 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 993 | else // C99 |
| 994 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 995 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 996 | // Placeholder type for type-dependent expressions whose type is |
| 997 | // completely unknown. No code should ever check a type against |
| 998 | // DependentTy and users should never see it; however, it is here to |
| 999 | // help diagnose failures to properly check for type-dependent |
| 1000 | // expressions. |
| 1001 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1002 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 1003 | // Placeholder type for functions. |
| 1004 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 1005 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1006 | // Placeholder type for bound members. |
| 1007 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 1008 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1009 | // Placeholder type for pseudo-objects. |
| 1010 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 1011 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 1012 | // "any" type; useful for debugger-like clients. |
| 1013 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 1014 | |
John McCall | 8a6b59a | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 1015 | // Placeholder type for unbridged ARC casts. |
| 1016 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 1017 | |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 1018 | // Placeholder type for builtin functions. |
| 1019 | InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn); |
| 1020 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1021 | // C99 6.2.5p11. |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1022 | FloatComplexTy = getComplexType(FloatTy); |
| 1023 | DoubleComplexTy = getComplexType(DoubleTy); |
| 1024 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1025 | |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1026 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1027 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 1028 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1029 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1030 | |
| 1031 | if (LangOpts.OpenCL) { |
| 1032 | InitBuiltinType(OCLImage1dTy, BuiltinType::OCLImage1d); |
| 1033 | InitBuiltinType(OCLImage1dArrayTy, BuiltinType::OCLImage1dArray); |
| 1034 | InitBuiltinType(OCLImage1dBufferTy, BuiltinType::OCLImage1dBuffer); |
| 1035 | InitBuiltinType(OCLImage2dTy, BuiltinType::OCLImage2d); |
| 1036 | InitBuiltinType(OCLImage2dArrayTy, BuiltinType::OCLImage2dArray); |
| 1037 | InitBuiltinType(OCLImage3dTy, BuiltinType::OCLImage3d); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1038 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1039 | InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1040 | InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent); |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1041 | } |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1042 | |
| 1043 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 29898f4 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 1044 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 1045 | SignedCharTy : BoolTy); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1046 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1047 | ObjCConstantStringType = QualType(); |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1048 | |
| 1049 | ObjCSuperType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1051 | // void * type |
| 1052 | VoidPtrTy = getPointerType(VoidTy); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1053 | |
| 1054 | // nullptr type (C++0x 2.14.7) |
| 1055 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1056 | |
| 1057 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 1058 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 1059 | |
| 1060 | // Builtin type used to help define __builtin_va_list. |
| 1061 | VaListTagTy = QualType(); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1064 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1065 | return SourceMgr.getDiagnostics(); |
| 1066 | } |
| 1067 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 1068 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 1069 | AttrVec *&Result = DeclAttrs[D]; |
| 1070 | if (!Result) { |
| 1071 | void *Mem = Allocate(sizeof(AttrVec)); |
| 1072 | Result = new (Mem) AttrVec; |
| 1073 | } |
| 1074 | |
| 1075 | return *Result; |
| 1076 | } |
| 1077 | |
| 1078 | /// \brief Erase the attributes corresponding to the given declaration. |
| 1079 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
| 1080 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 1081 | if (Pos != DeclAttrs.end()) { |
| 1082 | Pos->second->~AttrVec(); |
| 1083 | DeclAttrs.erase(Pos); |
| 1084 | } |
| 1085 | } |
| 1086 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1087 | // FIXME: Remove ? |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1088 | MemberSpecializationInfo * |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1089 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1090 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1091 | return getTemplateOrSpecializationInfo(Var) |
| 1092 | .dyn_cast<MemberSpecializationInfo *>(); |
| 1093 | } |
| 1094 | |
| 1095 | ASTContext::TemplateOrSpecializationInfo |
| 1096 | ASTContext::getTemplateOrSpecializationInfo(const VarDecl *Var) { |
| 1097 | llvm::DenseMap<const VarDecl *, TemplateOrSpecializationInfo>::iterator Pos = |
| 1098 | TemplateOrInstantiation.find(Var); |
| 1099 | if (Pos == TemplateOrInstantiation.end()) |
| 1100 | return TemplateOrSpecializationInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1102 | return Pos->second; |
| 1103 | } |
| 1104 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | void |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1106 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 1107 | TemplateSpecializationKind TSK, |
| 1108 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1109 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 1110 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1111 | setTemplateOrSpecializationInfo(Inst, new (*this) MemberSpecializationInfo( |
| 1112 | Tmpl, TSK, PointOfInstantiation)); |
| 1113 | } |
| 1114 | |
| 1115 | void |
| 1116 | ASTContext::setTemplateOrSpecializationInfo(VarDecl *Inst, |
| 1117 | TemplateOrSpecializationInfo TSI) { |
| 1118 | assert(!TemplateOrInstantiation[Inst] && |
| 1119 | "Already noted what the variable was instantiated from"); |
| 1120 | TemplateOrInstantiation[Inst] = TSI; |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1123 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 1124 | const FunctionDecl *FD){ |
| 1125 | assert(FD && "Specialization is 0"); |
| 1126 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1127 | = ClassScopeSpecializationPattern.find(FD); |
| 1128 | if (Pos == ClassScopeSpecializationPattern.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1129 | return nullptr; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1130 | |
| 1131 | return Pos->second; |
| 1132 | } |
| 1133 | |
| 1134 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 1135 | FunctionDecl *Pattern) { |
| 1136 | assert(FD && "Specialization is 0"); |
| 1137 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1138 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1141 | NamedDecl * |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1142 | ASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) { |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1143 | llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1144 | = InstantiatedFromUsingDecl.find(UUD); |
| 1145 | if (Pos == InstantiatedFromUsingDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1146 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1147 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1148 | return Pos->second; |
| 1149 | } |
| 1150 | |
| 1151 | void |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1152 | ASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) { |
| 1153 | assert((isa<UsingDecl>(Pattern) || |
| 1154 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 1155 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 1156 | "pattern decl is not a using decl"); |
| 1157 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 1158 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 1159 | } |
| 1160 | |
| 1161 | UsingShadowDecl * |
| 1162 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 1163 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 1164 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 1165 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1166 | return nullptr; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1167 | |
| 1168 | return Pos->second; |
| 1169 | } |
| 1170 | |
| 1171 | void |
| 1172 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 1173 | UsingShadowDecl *Pattern) { |
| 1174 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 1175 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1178 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 1179 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 1180 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 1181 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1182 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1183 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1184 | return Pos->second; |
| 1185 | } |
| 1186 | |
| 1187 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 1188 | FieldDecl *Tmpl) { |
| 1189 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 1190 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 1191 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 1192 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1194 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 1195 | } |
| 1196 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1197 | ASTContext::overridden_cxx_method_iterator |
| 1198 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
| 1199 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | cc4ca0a | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1200 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1201 | if (Pos == OverriddenMethods.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1202 | return nullptr; |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1203 | |
| 1204 | return Pos->second.begin(); |
| 1205 | } |
| 1206 | |
| 1207 | ASTContext::overridden_cxx_method_iterator |
| 1208 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
| 1209 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | cc4ca0a | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1210 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1211 | if (Pos == OverriddenMethods.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1212 | return nullptr; |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1213 | |
| 1214 | return Pos->second.end(); |
| 1215 | } |
| 1216 | |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1217 | unsigned |
| 1218 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
| 1219 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos |
Argyrios Kyrtzidis | cc4ca0a | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1220 | = OverriddenMethods.find(Method->getCanonicalDecl()); |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1221 | if (Pos == OverriddenMethods.end()) |
| 1222 | return 0; |
| 1223 | |
| 1224 | return Pos->second.size(); |
| 1225 | } |
| 1226 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1227 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 1228 | const CXXMethodDecl *Overridden) { |
Argyrios Kyrtzidis | cc4ca0a | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1229 | assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl()); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1230 | OverriddenMethods[Method].push_back(Overridden); |
| 1231 | } |
| 1232 | |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 1233 | void ASTContext::getOverriddenMethods( |
| 1234 | const NamedDecl *D, |
| 1235 | SmallVectorImpl<const NamedDecl *> &Overridden) const { |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1236 | assert(D); |
| 1237 | |
| 1238 | if (const CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | c8e7008 | 2013-04-17 00:09:03 +0000 | [diff] [blame] | 1239 | Overridden.append(overridden_methods_begin(CXXMethod), |
| 1240 | overridden_methods_end(CXXMethod)); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1241 | return; |
| 1242 | } |
| 1243 | |
| 1244 | const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D); |
| 1245 | if (!Method) |
| 1246 | return; |
| 1247 | |
Argyrios Kyrtzidis | 353f6a4 | 2012-10-09 18:19:01 +0000 | [diff] [blame] | 1248 | SmallVector<const ObjCMethodDecl *, 8> OverDecls; |
| 1249 | Method->getOverriddenMethods(OverDecls); |
Argyrios Kyrtzidis | a7a1081 | 2012-10-09 20:08:43 +0000 | [diff] [blame] | 1250 | Overridden.append(OverDecls.begin(), OverDecls.end()); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 1253 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 1254 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 1255 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 1256 | if (!FirstLocalImport) { |
| 1257 | FirstLocalImport = Import; |
| 1258 | LastLocalImport = Import; |
| 1259 | return; |
| 1260 | } |
| 1261 | |
| 1262 | LastLocalImport->NextLocalImport = Import; |
| 1263 | LastLocalImport = Import; |
| 1264 | } |
| 1265 | |
Chris Lattner | 53cfe80 | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1266 | //===----------------------------------------------------------------------===// |
| 1267 | // Type Sizing and Analysis |
| 1268 | //===----------------------------------------------------------------------===// |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1269 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1270 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 1271 | /// scalar floating point type. |
| 1272 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1273 | const BuiltinType *BT = T->getAs<BuiltinType>(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1274 | assert(BT && "Not a floating point type!"); |
| 1275 | switch (BT->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1276 | default: llvm_unreachable("Not a floating point type!"); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1277 | case BuiltinType::Half: return Target->getHalfFormat(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1278 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 1279 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 1280 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1284 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1285 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1286 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1287 | bool UseAlignAttrOnly = false; |
| 1288 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 1289 | Align = AlignFromAttr; |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1290 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1291 | // __attribute__((aligned)) can increase or decrease alignment |
| 1292 | // *except* on a struct or struct member, where it only increases |
| 1293 | // alignment unless 'packed' is also specified. |
| 1294 | // |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1295 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1296 | // ignore that possibility; Sema should diagnose it. |
| 1297 | if (isa<FieldDecl>(D)) { |
| 1298 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 1299 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 1300 | } else { |
| 1301 | UseAlignAttrOnly = true; |
| 1302 | } |
| 1303 | } |
Fariborz Jahanian | 9f10718 | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 1304 | else if (isa<FieldDecl>(D)) |
| 1305 | UseAlignAttrOnly = |
| 1306 | D->hasAttr<PackedAttr>() || |
| 1307 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1308 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1309 | // If we're using the align attribute only, just ignore everything |
| 1310 | // else about the declaration and its type. |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1311 | if (UseAlignAttrOnly) { |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1312 | // do nothing |
| 1313 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1314 | } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1315 | QualType T = VD->getType(); |
Richard Smith | f6d7030 | 2014-06-10 23:34:28 +0000 | [diff] [blame] | 1316 | if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1317 | if (ForAlignof) |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1318 | T = RT->getPointeeType(); |
| 1319 | else |
| 1320 | T = getPointerType(RT->getPointeeType()); |
| 1321 | } |
Richard Smith | f6d7030 | 2014-06-10 23:34:28 +0000 | [diff] [blame] | 1322 | QualType BaseT = getBaseElementType(T); |
| 1323 | if (!BaseT->isIncompleteType() && !T->isFunctionType()) { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1324 | // Adjust alignments of declarations with array type by the |
| 1325 | // large-array alignment on the target. |
Rafael Espindola | 8857275 | 2013-08-07 18:08:19 +0000 | [diff] [blame] | 1326 | if (const ArrayType *arrayType = getAsArrayType(T)) { |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1327 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
| 1328 | if (!ForAlignof && MinWidth) { |
Rafael Espindola | 8857275 | 2013-08-07 18:08:19 +0000 | [diff] [blame] | 1329 | if (isa<VariableArrayType>(arrayType)) |
| 1330 | Align = std::max(Align, Target->getLargeArrayAlign()); |
| 1331 | else if (isa<ConstantArrayType>(arrayType) && |
| 1332 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
| 1333 | Align = std::max(Align, Target->getLargeArrayAlign()); |
| 1334 | } |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1335 | } |
Chad Rosier | 99ee782 | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 1336 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1337 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Ulrich Weigand | b63f779 | 2015-04-21 17:26:18 +0000 | [diff] [blame] | 1338 | if (VD->hasGlobalStorage() && !ForAlignof) |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1339 | Align = std::max(Align, getTargetInfo().getMinGlobalAlign()); |
| 1340 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1341 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1342 | |
| 1343 | // Fields can be subject to extra alignment constraints, like if |
| 1344 | // the field is packed, the struct is packed, or the struct has a |
| 1345 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 1346 | // the actual alignment of the field within the struct, and then |
| 1347 | // (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] | 1348 | if (const FieldDecl *Field = dyn_cast<FieldDecl>(VD)) { |
| 1349 | const RecordDecl *Parent = Field->getParent(); |
| 1350 | // We can only produce a sensible answer if the record is valid. |
| 1351 | if (!Parent->isInvalidDecl()) { |
| 1352 | const ASTRecordLayout &Layout = getASTRecordLayout(Parent); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1353 | |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1354 | // Start with the record's overall alignment. |
| 1355 | unsigned FieldAlign = toBits(Layout.getAlignment()); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1356 | |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1357 | // Use the GCD of that and the offset within the record. |
| 1358 | uint64_t Offset = Layout.getFieldOffset(Field->getFieldIndex()); |
| 1359 | if (Offset > 0) { |
| 1360 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 1361 | // which means we get to do this crazy thing instead of Euclid's. |
| 1362 | uint64_t LowBitOfOffset = Offset & (~Offset + 1); |
| 1363 | if (LowBitOfOffset < FieldAlign) |
| 1364 | FieldAlign = static_cast<unsigned>(LowBitOfOffset); |
| 1365 | } |
| 1366 | |
| 1367 | Align = std::min(Align, FieldAlign); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1368 | } |
Charles Davis | 3fc5107 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 1369 | } |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1370 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1371 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1372 | return toCharUnitsFromBits(Align); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1373 | } |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1374 | |
John McCall | f124992 | 2012-08-21 04:10:00 +0000 | [diff] [blame] | 1375 | // getTypeInfoDataSizeInChars - Return the size of a type, in |
| 1376 | // chars. If the type is a record, its data size is returned. This is |
| 1377 | // the size of the memcpy that's performed when assigning this type |
| 1378 | // using a trivial copy/move assignment operator. |
| 1379 | std::pair<CharUnits, CharUnits> |
| 1380 | ASTContext::getTypeInfoDataSizeInChars(QualType T) const { |
| 1381 | std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T); |
| 1382 | |
| 1383 | // In C++, objects can sometimes be allocated into the tail padding |
| 1384 | // of a base-class subobject. We decide whether that's possible |
| 1385 | // during class layout, so here we can just trust the layout results. |
| 1386 | if (getLangOpts().CPlusPlus) { |
| 1387 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 1388 | const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl()); |
| 1389 | sizeAndAlign.first = layout.getDataSize(); |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | return sizeAndAlign; |
| 1394 | } |
| 1395 | |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1396 | /// getConstantArrayInfoInChars - Performing the computation in CharUnits |
| 1397 | /// instead of in bits prevents overflowing the uint64_t for some large arrays. |
| 1398 | std::pair<CharUnits, CharUnits> |
| 1399 | static getConstantArrayInfoInChars(const ASTContext &Context, |
| 1400 | const ConstantArrayType *CAT) { |
| 1401 | std::pair<CharUnits, CharUnits> EltInfo = |
| 1402 | Context.getTypeInfoInChars(CAT->getElementType()); |
| 1403 | uint64_t Size = CAT->getSize().getZExtValue(); |
Richard Trieu | c750907 | 2013-05-14 23:41:50 +0000 | [diff] [blame] | 1404 | assert((Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity()) <= |
| 1405 | (uint64_t)(-1)/Size) && |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1406 | "Overflow in array type char size evaluation"); |
| 1407 | uint64_t Width = EltInfo.first.getQuantity() * Size; |
| 1408 | unsigned Align = EltInfo.second.getQuantity(); |
Warren Hunt | 5ae586a | 2013-11-01 23:59:41 +0000 | [diff] [blame] | 1409 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 1410 | Context.getTargetInfo().getPointerWidth(0) == 64) |
| 1411 | Width = llvm::RoundUpToAlignment(Width, Align); |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1412 | return std::make_pair(CharUnits::fromQuantity(Width), |
| 1413 | CharUnits::fromQuantity(Align)); |
| 1414 | } |
| 1415 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1416 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1417 | ASTContext::getTypeInfoInChars(const Type *T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1418 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) |
| 1419 | return getConstantArrayInfoInChars(*this, CAT); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1420 | TypeInfo Info = getTypeInfo(T); |
| 1421 | return std::make_pair(toCharUnitsFromBits(Info.Width), |
| 1422 | toCharUnitsFromBits(Info.Align)); |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1426 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1427 | return getTypeInfoInChars(T.getTypePtr()); |
| 1428 | } |
| 1429 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1430 | bool ASTContext::isAlignmentRequired(const Type *T) const { |
| 1431 | return getTypeInfo(T).AlignIsRequired; |
| 1432 | } |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1433 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1434 | bool ASTContext::isAlignmentRequired(QualType T) const { |
| 1435 | return isAlignmentRequired(T.getTypePtr()); |
| 1436 | } |
| 1437 | |
| 1438 | TypeInfo ASTContext::getTypeInfo(const Type *T) const { |
David Majnemer | f8d3864 | 2014-07-30 08:42:33 +0000 | [diff] [blame] | 1439 | TypeInfoMap::iterator I = MemoizedTypeInfo.find(T); |
| 1440 | if (I != MemoizedTypeInfo.end()) |
| 1441 | return I->second; |
| 1442 | |
| 1443 | // This call can invalidate MemoizedTypeInfo[T], so we need a second lookup. |
| 1444 | TypeInfo TI = getTypeInfoImpl(T); |
| 1445 | MemoizedTypeInfo[T] = TI; |
Rafael Espindola | eaa88c1 | 2014-07-30 04:40:23 +0000 | [diff] [blame] | 1446 | return TI; |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 1450 | /// method does not work on incomplete types. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1451 | /// |
| 1452 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 1453 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 1454 | /// should take a QualType, &c. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1455 | TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const { |
| 1456 | uint64_t Width = 0; |
| 1457 | unsigned Align = 8; |
| 1458 | bool AlignIsRequired = false; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1459 | switch (T->getTypeClass()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1460 | #define TYPE(Class, Base) |
| 1461 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1462 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1463 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
David Blaikie | ab277d6 | 2013-07-13 21:08:03 +0000 | [diff] [blame] | 1464 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) \ |
| 1465 | case Type::Class: \ |
| 1466 | assert(!T->isDependentType() && "should not see dependent types here"); \ |
| 1467 | return getTypeInfo(cast<Class##Type>(T)->desugar().getTypePtr()); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1468 | #include "clang/AST/TypeNodes.def" |
John McCall | 15547bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 1469 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1470 | |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1471 | case Type::FunctionNoProto: |
| 1472 | case Type::FunctionProto: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1473 | // GCC extension: alignof(function) = 32 bits |
| 1474 | Width = 0; |
| 1475 | Align = 32; |
| 1476 | break; |
| 1477 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1478 | case Type::IncompleteArray: |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1479 | case Type::VariableArray: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1480 | Width = 0; |
| 1481 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 1482 | break; |
| 1483 | |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1484 | case Type::ConstantArray: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1485 | const ConstantArrayType *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1487 | TypeInfo EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | d541a4c | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1488 | uint64_t Size = CAT->getSize().getZExtValue(); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1489 | assert((Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) && |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1490 | "Overflow in array type bit size evaluation"); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1491 | Width = EltInfo.Width * Size; |
| 1492 | Align = EltInfo.Align; |
Warren Hunt | 5ae586a | 2013-11-01 23:59:41 +0000 | [diff] [blame] | 1493 | if (!getTargetInfo().getCXXABI().isMicrosoft() || |
| 1494 | getTargetInfo().getPointerWidth(0) == 64) |
| 1495 | Width = llvm::RoundUpToAlignment(Width, Align); |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1496 | break; |
Christopher Lamb | c5fafa2 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 1497 | } |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1498 | case Type::ExtVector: |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1499 | case Type::Vector: { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1500 | const VectorType *VT = cast<VectorType>(T); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1501 | TypeInfo EltInfo = getTypeInfo(VT->getElementType()); |
| 1502 | Width = EltInfo.Width * VT->getNumElements(); |
Eli Friedman | 3df5efe | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1503 | Align = Width; |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1504 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 1505 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | ef78c8e | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 1506 | if (Align & (Align-1)) { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1507 | Align = llvm::NextPowerOf2(Align); |
| 1508 | Width = llvm::RoundUpToAlignment(Width, Align); |
| 1509 | } |
Chad Rosier | cc40ea7 | 2012-07-13 23:57:43 +0000 | [diff] [blame] | 1510 | // Adjust the alignment based on the target max. |
| 1511 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
| 1512 | if (TargetVectorAlign && TargetVectorAlign < Align) |
| 1513 | Align = TargetVectorAlign; |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1514 | break; |
| 1515 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1516 | |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1517 | case Type::Builtin: |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1518 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1519 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1520 | case BuiltinType::Void: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1521 | // GCC extension: alignof(void) = 8 bits. |
| 1522 | Width = 0; |
| 1523 | Align = 8; |
| 1524 | break; |
| 1525 | |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1526 | case BuiltinType::Bool: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1527 | Width = Target->getBoolWidth(); |
| 1528 | Align = Target->getBoolAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1529 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1530 | case BuiltinType::Char_S: |
| 1531 | case BuiltinType::Char_U: |
| 1532 | case BuiltinType::UChar: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1533 | case BuiltinType::SChar: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1534 | Width = Target->getCharWidth(); |
| 1535 | Align = Target->getCharAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1536 | break; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1537 | case BuiltinType::WChar_S: |
| 1538 | case BuiltinType::WChar_U: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1539 | Width = Target->getWCharWidth(); |
| 1540 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1541 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1542 | case BuiltinType::Char16: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1543 | Width = Target->getChar16Width(); |
| 1544 | Align = Target->getChar16Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1545 | break; |
| 1546 | case BuiltinType::Char32: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1547 | Width = Target->getChar32Width(); |
| 1548 | Align = Target->getChar32Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1549 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1550 | case BuiltinType::UShort: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1551 | case BuiltinType::Short: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1552 | Width = Target->getShortWidth(); |
| 1553 | Align = Target->getShortAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1554 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1555 | case BuiltinType::UInt: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1556 | case BuiltinType::Int: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1557 | Width = Target->getIntWidth(); |
| 1558 | Align = Target->getIntAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1559 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1560 | case BuiltinType::ULong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1561 | case BuiltinType::Long: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1562 | Width = Target->getLongWidth(); |
| 1563 | Align = Target->getLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1564 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1565 | case BuiltinType::ULongLong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1566 | case BuiltinType::LongLong: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1567 | Width = Target->getLongLongWidth(); |
| 1568 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1569 | break; |
Chris Lattner | 0a415ec | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 1570 | case BuiltinType::Int128: |
| 1571 | case BuiltinType::UInt128: |
| 1572 | Width = 128; |
| 1573 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 1574 | break; |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1575 | case BuiltinType::Half: |
| 1576 | Width = Target->getHalfWidth(); |
| 1577 | Align = Target->getHalfAlign(); |
| 1578 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1579 | case BuiltinType::Float: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1580 | Width = Target->getFloatWidth(); |
| 1581 | Align = Target->getFloatAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1582 | break; |
| 1583 | case BuiltinType::Double: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1584 | Width = Target->getDoubleWidth(); |
| 1585 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1586 | break; |
| 1587 | case BuiltinType::LongDouble: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1588 | Width = Target->getLongDoubleWidth(); |
| 1589 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1590 | break; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1591 | case BuiltinType::NullPtr: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1592 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 1593 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | a81b0b7 | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 1594 | break; |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1595 | case BuiltinType::ObjCId: |
| 1596 | case BuiltinType::ObjCClass: |
| 1597 | case BuiltinType::ObjCSel: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1598 | Width = Target->getPointerWidth(0); |
| 1599 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1600 | break; |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1601 | case BuiltinType::OCLSampler: |
| 1602 | // Samplers are modeled as integers. |
| 1603 | Width = Target->getIntWidth(); |
| 1604 | Align = Target->getIntAlign(); |
| 1605 | break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1606 | case BuiltinType::OCLEvent: |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1607 | case BuiltinType::OCLImage1d: |
| 1608 | case BuiltinType::OCLImage1dArray: |
| 1609 | case BuiltinType::OCLImage1dBuffer: |
| 1610 | case BuiltinType::OCLImage2d: |
| 1611 | case BuiltinType::OCLImage2dArray: |
| 1612 | case BuiltinType::OCLImage3d: |
| 1613 | // Currently these types are pointers to opaque types. |
| 1614 | Width = Target->getPointerWidth(0); |
| 1615 | Align = Target->getPointerAlign(0); |
| 1616 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1617 | } |
Chris Lattner | 48f84b8 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 1618 | break; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1619 | case Type::ObjCObjectPointer: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1620 | Width = Target->getPointerWidth(0); |
| 1621 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1622 | break; |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1623 | case Type::BlockPointer: { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1624 | unsigned AS = getTargetAddressSpace( |
| 1625 | cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1626 | Width = Target->getPointerWidth(AS); |
| 1627 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1628 | break; |
| 1629 | } |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1630 | case Type::LValueReference: |
| 1631 | case Type::RValueReference: { |
| 1632 | // alignof and sizeof should never enter this code path here, so we go |
| 1633 | // the pointer route. |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1634 | unsigned AS = getTargetAddressSpace( |
| 1635 | cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1636 | Width = Target->getPointerWidth(AS); |
| 1637 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1638 | break; |
| 1639 | } |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1640 | case Type::Pointer: { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 1641 | unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1642 | Width = Target->getPointerWidth(AS); |
| 1643 | Align = Target->getPointerAlign(AS); |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1644 | break; |
| 1645 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1646 | case Type::MemberPointer: { |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 1647 | const MemberPointerType *MPT = cast<MemberPointerType>(T); |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 1648 | std::tie(Width, Align) = ABI->getMemberPointerWidthAndAlign(MPT); |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1649 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1650 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1651 | case Type::Complex: { |
| 1652 | // Complex types have the same alignment as their elements, but twice the |
| 1653 | // size. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1654 | TypeInfo EltInfo = getTypeInfo(cast<ComplexType>(T)->getElementType()); |
| 1655 | Width = EltInfo.Width * 2; |
| 1656 | Align = EltInfo.Align; |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1657 | break; |
| 1658 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1659 | case Type::ObjCObject: |
| 1660 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1661 | case Type::Adjusted: |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 1662 | case Type::Decayed: |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1663 | return getTypeInfo(cast<AdjustedType>(T)->getAdjustedType().getTypePtr()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1664 | case Type::ObjCInterface: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1665 | const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1666 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1667 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1668 | Align = toBits(Layout.getAlignment()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1669 | break; |
| 1670 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1671 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1672 | case Type::Enum: { |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1673 | const TagType *TT = cast<TagType>(T); |
| 1674 | |
| 1675 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 7f97189 | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1676 | Width = 8; |
| 1677 | Align = 8; |
Chris Lattner | 572100b | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1678 | break; |
| 1679 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 1681 | if (const EnumType *ET = dyn_cast<EnumType>(TT)) { |
| 1682 | const EnumDecl *ED = ET->getDecl(); |
| 1683 | TypeInfo Info = |
| 1684 | getTypeInfo(ED->getIntegerType()->getUnqualifiedDesugaredType()); |
| 1685 | if (unsigned AttrAlign = ED->getMaxAlignment()) { |
| 1686 | Info.Align = AttrAlign; |
| 1687 | Info.AlignIsRequired = true; |
| 1688 | } |
| 1689 | return Info; |
| 1690 | } |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1691 | |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1692 | const RecordType *RT = cast<RecordType>(TT); |
David Majnemer | 5821ff7 | 2015-02-03 08:49:29 +0000 | [diff] [blame] | 1693 | const RecordDecl *RD = RT->getDecl(); |
| 1694 | const ASTRecordLayout &Layout = getASTRecordLayout(RD); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1695 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1696 | Align = toBits(Layout.getAlignment()); |
David Majnemer | 5821ff7 | 2015-02-03 08:49:29 +0000 | [diff] [blame] | 1697 | AlignIsRequired = RD->hasAttr<AlignedAttr>(); |
Chris Lattner | 49a953a | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1698 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1699 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1700 | |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1701 | case Type::SubstTemplateTypeParm: |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1702 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1703 | getReplacementType().getTypePtr()); |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1704 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1705 | case Type::Auto: { |
| 1706 | const AutoType *A = cast<AutoType>(T); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 1707 | assert(!A->getDeducedType().isNull() && |
| 1708 | "cannot request the size of an undeduced or dependent auto type"); |
Matt Beaumont-Gay | 7a24210e | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1709 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1712 | case Type::Paren: |
| 1713 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1714 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1715 | case Type::Typedef: { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1716 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1717 | TypeInfo Info = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1718 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 1719 | // alignment we have. This violates the GCC documentation (which says that |
| 1720 | // attribute(aligned) can only round up) but matches its implementation. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1721 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) { |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 1722 | Align = AttrAlign; |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1723 | AlignIsRequired = true; |
David Majnemer | 37bffb6 | 2014-08-04 05:11:01 +0000 | [diff] [blame] | 1724 | } else { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1725 | Align = Info.Align; |
David Majnemer | 37bffb6 | 2014-08-04 05:11:01 +0000 | [diff] [blame] | 1726 | AlignIsRequired = Info.AlignIsRequired; |
| 1727 | } |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1728 | Width = Info.Width; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1729 | break; |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1730 | } |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1731 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 1732 | case Type::Elaborated: |
| 1733 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1734 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 1735 | case Type::Attributed: |
| 1736 | return getTypeInfo( |
| 1737 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 1738 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1739 | case Type::Atomic: { |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1740 | // Start with the base type information. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1741 | TypeInfo Info = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 1742 | Width = Info.Width; |
| 1743 | Align = Info.Align; |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 1744 | |
| 1745 | // If the size of the type doesn't exceed the platform's max |
| 1746 | // atomic promotion width, make the size and alignment more |
| 1747 | // favorable to atomic operations: |
| 1748 | if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth()) { |
| 1749 | // Round the size up to a power of 2. |
| 1750 | if (!llvm::isPowerOf2_64(Width)) |
| 1751 | Width = llvm::NextPowerOf2(Width); |
| 1752 | |
| 1753 | // Set the alignment equal to the size. |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1754 | Align = static_cast<unsigned>(Width); |
| 1755 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1758 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1759 | |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 1760 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1761 | return TypeInfo(Width, Align, AlignIsRequired); |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1764 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 1765 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 1766 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 1767 | } |
| 1768 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1769 | /// toBits - Convert a size in characters to a size in characters. |
| 1770 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 1771 | return CharSize.getQuantity() * getCharWidth(); |
| 1772 | } |
| 1773 | |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1774 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 1775 | /// This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1776 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1777 | return getTypeInfoInChars(T).first; |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1778 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1779 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1780 | return getTypeInfoInChars(T).first; |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
Ken Dyck | a6046ab | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 1783 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1784 | /// characters. This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1785 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1786 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1787 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1788 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1789 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1792 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 1793 | /// type for the current target in bits. This can be different than the ABI |
| 1794 | /// alignment in cases where it is beneficial for performance to overalign |
| 1795 | /// a data type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1796 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1797 | TypeInfo TI = getTypeInfo(T); |
| 1798 | unsigned ABIAlign = TI.Align; |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1799 | |
David Majnemer | e154456 | 2015-04-24 01:25:05 +0000 | [diff] [blame^] | 1800 | T = T->getBaseElementTypeUnsafe(); |
| 1801 | |
| 1802 | // The preferred alignment of member pointers is that of a pointer. |
| 1803 | if (T->isMemberPointerType()) |
| 1804 | return getPreferredTypeAlign(getPointerDiffType().getTypePtr()); |
| 1805 | |
Robert Lytton | eaf6f36 | 2013-11-12 10:09:34 +0000 | [diff] [blame] | 1806 | if (Target->getTriple().getArch() == llvm::Triple::xcore) |
| 1807 | return ABIAlign; // Never overalign on XCore. |
| 1808 | |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1809 | // Double and long long should be naturally aligned if possible. |
David Majnemer | 1d4db8f | 2014-02-24 23:43:27 +0000 | [diff] [blame] | 1810 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1811 | T = CT->getElementType().getTypePtr(); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 1812 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 1813 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1814 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | b57321a | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 1815 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 1816 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
David Majnemer | 8b6bd57 | 2014-02-24 23:34:17 +0000 | [diff] [blame] | 1817 | // Don't increase the alignment if an alignment attribute was specified on a |
| 1818 | // typedef declaration. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1819 | if (!TI.AlignIsRequired) |
David Majnemer | 8b6bd57 | 2014-02-24 23:34:17 +0000 | [diff] [blame] | 1820 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 1821 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 1822 | return ABIAlign; |
| 1823 | } |
| 1824 | |
Ulrich Weigand | ca3cb7f | 2015-04-21 17:29:35 +0000 | [diff] [blame] | 1825 | /// getTargetDefaultAlignForAttributeAligned - Return the default alignment |
| 1826 | /// for __attribute__((aligned)) on this target, to be used if no alignment |
| 1827 | /// value is specified. |
| 1828 | unsigned ASTContext::getTargetDefaultAlignForAttributeAligned(void) const { |
| 1829 | return getTargetInfo().getDefaultAlignForAttributeAligned(); |
| 1830 | } |
| 1831 | |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1832 | /// getAlignOfGlobalVar - Return the alignment in bits that should be given |
| 1833 | /// to a global variable of the specified type. |
| 1834 | unsigned ASTContext::getAlignOfGlobalVar(QualType T) const { |
| 1835 | return std::max(getTypeAlign(T), getTargetInfo().getMinGlobalAlign()); |
| 1836 | } |
| 1837 | |
| 1838 | /// getAlignOfGlobalVarInChars - Return the alignment in characters that |
| 1839 | /// should be given to a global variable of the specified type. |
| 1840 | CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T) const { |
| 1841 | return toCharUnitsFromBits(getAlignOfGlobalVar(T)); |
| 1842 | } |
| 1843 | |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1844 | /// DeepCollectObjCIvars - |
| 1845 | /// This routine first collects all declared, but not synthesized, ivars in |
| 1846 | /// super class and then collects all ivars, including those synthesized for |
| 1847 | /// current class. This routine is used for implementation of current class |
| 1848 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1849 | /// |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1850 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 1851 | bool leafClass, |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1852 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 1853 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 1854 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 1855 | if (!leafClass) { |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 1856 | for (const auto *I : OI->ivars()) |
| 1857 | Ivars.push_back(I); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1858 | } else { |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1859 | ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 1860 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 1861 | Iv= Iv->getNextIvar()) |
| 1862 | Ivars.push_back(Iv); |
| 1863 | } |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1866 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 1867 | /// those inherited by it. |
| 1868 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 1869 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1870 | if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 1871 | // We can use protocol_iterator here instead of |
| 1872 | // all_referenced_protocol_iterator since we are walking all categories. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 1873 | for (auto *Proto : OI->all_referenced_protocols()) { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1874 | Protocols.insert(Proto->getCanonicalDecl()); |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 1875 | for (auto *P : Proto->protocols()) { |
| 1876 | Protocols.insert(P->getCanonicalDecl()); |
| 1877 | CollectInheritedProtocols(P, Protocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1878 | } |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 1879 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1880 | |
| 1881 | // Categories of this Interface. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 1882 | for (const auto *Cat : OI->visible_categories()) |
| 1883 | CollectInheritedProtocols(Cat, Protocols); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1884 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1885 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 1886 | while (SD) { |
| 1887 | CollectInheritedProtocols(SD, Protocols); |
| 1888 | SD = SD->getSuperClass(); |
| 1889 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1890 | } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 1891 | for (auto *Proto : OC->protocols()) { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1892 | Protocols.insert(Proto->getCanonicalDecl()); |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 1893 | for (const auto *P : Proto->protocols()) |
| 1894 | CollectInheritedProtocols(P, Protocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1895 | } |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1896 | } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 1897 | for (auto *Proto : OP->protocols()) { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 1898 | Protocols.insert(Proto->getCanonicalDecl()); |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 1899 | for (const auto *P : Proto->protocols()) |
| 1900 | CollectInheritedProtocols(P, Protocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1901 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 1902 | } |
| 1903 | } |
| 1904 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 1905 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1906 | unsigned count = 0; |
| 1907 | // Count ivars declared in class extension. |
Aaron Ballman | b4a5345 | 2014-03-13 21:57:01 +0000 | [diff] [blame] | 1908 | for (const auto *Ext : OI->known_extensions()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1909 | count += Ext->ivar_size(); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 1910 | |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 1911 | // Count ivar defined in this class's implementation. This |
| 1912 | // includes synthesized ivars. |
| 1913 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 1914 | count += ImplDecl->ivar_size(); |
| 1915 | |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1916 | return count; |
| 1917 | } |
| 1918 | |
Argyrios Kyrtzidis | 2e809ce | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 1919 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 1920 | if (!E) |
| 1921 | return false; |
| 1922 | |
| 1923 | // nullptr_t is always treated as null. |
| 1924 | if (E->getType()->isNullPtrType()) return true; |
| 1925 | |
| 1926 | if (E->getType()->isAnyPointerType() && |
| 1927 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 1928 | Expr::NPC_ValueDependentIsNull)) |
| 1929 | return true; |
| 1930 | |
| 1931 | // Unfortunately, __null has type 'int'. |
| 1932 | if (isa<GNUNullExpr>(E)) return true; |
| 1933 | |
| 1934 | return false; |
| 1935 | } |
| 1936 | |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1937 | /// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists. |
| 1938 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 1939 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1940 | I = ObjCImpls.find(D); |
| 1941 | if (I != ObjCImpls.end()) |
| 1942 | return cast<ObjCImplementationDecl>(I->second); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1943 | return nullptr; |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1944 | } |
| 1945 | /// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists. |
| 1946 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 1947 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 1948 | I = ObjCImpls.find(D); |
| 1949 | if (I != ObjCImpls.end()) |
| 1950 | return cast<ObjCCategoryImplDecl>(I->second); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1951 | return nullptr; |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | /// \brief Set the implementation of ObjCInterfaceDecl. |
| 1955 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 1956 | ObjCImplementationDecl *ImplD) { |
| 1957 | assert(IFaceD && ImplD && "Passed null params"); |
| 1958 | ObjCImpls[IFaceD] = ImplD; |
| 1959 | } |
| 1960 | /// \brief Set the implementation of ObjCCategoryDecl. |
| 1961 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 1962 | ObjCCategoryImplDecl *ImplD) { |
| 1963 | assert(CatD && ImplD && "Passed null params"); |
| 1964 | ObjCImpls[CatD] = ImplD; |
| 1965 | } |
| 1966 | |
Dmitri Gribenko | 37527c2 | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1967 | const ObjCInterfaceDecl *ASTContext::getObjContainingInterface( |
| 1968 | const NamedDecl *ND) const { |
| 1969 | if (const ObjCInterfaceDecl *ID = |
| 1970 | dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1971 | return ID; |
Dmitri Gribenko | 37527c2 | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1972 | if (const ObjCCategoryDecl *CD = |
| 1973 | dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1974 | return CD->getClassInterface(); |
Dmitri Gribenko | 37527c2 | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 1975 | if (const ObjCImplDecl *IMD = |
| 1976 | dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1977 | return IMD->getClassInterface(); |
| 1978 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1979 | return nullptr; |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1982 | /// \brief Get the copy initialization expression of VarDecl,or NULL if |
| 1983 | /// none exists. |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1984 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1985 | assert(VD && "Passed null params"); |
| 1986 | assert(VD->hasAttr<BlocksAttr>() && |
| 1987 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 1988 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1989 | I = BlockVarCopyInits.find(VD); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1990 | return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : nullptr; |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | /// \brief Set the copy inialization expression of a block var decl. |
| 1994 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 1995 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 1996 | assert(VD->hasAttr<BlocksAttr>() && |
| 1997 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 1998 | BlockVarCopyInits[VD] = Init; |
| 1999 | } |
| 2000 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2001 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2002 | unsigned DataSize) const { |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 2003 | if (!DataSize) |
| 2004 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 2005 | else |
| 2006 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2007 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 2008 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2009 | TypeSourceInfo *TInfo = |
| 2010 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 2011 | new (TInfo) TypeSourceInfo(T); |
| 2012 | return TInfo; |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2015 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2016 | SourceLocation L) const { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2017 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | 2d525f0 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2018 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | 3665e00 | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 2019 | return DI; |
| 2020 | } |
| 2021 | |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2022 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2023 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2024 | return getObjCLayout(D, nullptr); |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2028 | ASTContext::getASTObjCImplementationLayout( |
| 2029 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2030 | return getObjCLayout(D->getClassInterface(), D); |
| 2031 | } |
| 2032 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2033 | //===----------------------------------------------------------------------===// |
| 2034 | // Type creation/memoization methods |
| 2035 | //===----------------------------------------------------------------------===// |
| 2036 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2037 | QualType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2038 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 2039 | unsigned fastQuals = quals.getFastQualifiers(); |
| 2040 | quals.removeFastQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2041 | |
| 2042 | // Check if we've already instantiated this type. |
| 2043 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2044 | ExtQuals::Profile(ID, baseType, quals); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2045 | void *insertPos = nullptr; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2046 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 2047 | assert(eq->getQualifiers() == quals); |
| 2048 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2051 | // If the base type is not canonical, make the appropriate canonical type. |
| 2052 | QualType canon; |
| 2053 | if (!baseType->isCanonicalUnqualified()) { |
| 2054 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2055 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 2056 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2057 | |
| 2058 | // Re-find the insert position. |
| 2059 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 2060 | } |
| 2061 | |
| 2062 | ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
| 2063 | ExtQualNodes.InsertNode(eq, insertPos); |
| 2064 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2067 | QualType |
| 2068 | ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2069 | QualType CanT = getCanonicalType(T); |
| 2070 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | 445fcab | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2071 | return T; |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2072 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2073 | // If we are composing extended qualifiers together, merge together |
| 2074 | // into one ExtQuals node. |
| 2075 | QualifierCollector Quals; |
| 2076 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2077 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2078 | // If this type already has an address space specified, it cannot get |
| 2079 | // another one. |
| 2080 | assert(!Quals.hasAddressSpace() && |
| 2081 | "Type cannot be in multiple addr spaces!"); |
| 2082 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2083 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2084 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | 025b5fb | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2087 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2088 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2089 | QualType CanT = getCanonicalType(T); |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2090 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2091 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2093 | if (const PointerType *ptr = T->getAs<PointerType>()) { |
| 2094 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 2095 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | b68215c | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 2096 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 2097 | return getPointerType(ResultType); |
| 2098 | } |
| 2099 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2100 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2101 | // If we are composing extended qualifiers together, merge together |
| 2102 | // into one ExtQuals node. |
| 2103 | QualifierCollector Quals; |
| 2104 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2105 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2106 | // If this type already has an ObjCGC specified, it cannot get |
| 2107 | // another one. |
| 2108 | assert(!Quals.hasObjCGCAttr() && |
| 2109 | "Type cannot have multiple ObjCGCs!"); |
| 2110 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2111 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2112 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2113 | } |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2114 | |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2115 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 2116 | FunctionType::ExtInfo Info) { |
| 2117 | if (T->getExtInfo() == Info) |
| 2118 | return T; |
| 2119 | |
| 2120 | QualType Result; |
| 2121 | if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2122 | Result = getFunctionNoProtoType(FNPT->getReturnType(), Info); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2123 | } else { |
| 2124 | const FunctionProtoType *FPT = cast<FunctionProtoType>(T); |
| 2125 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 2126 | EPI.ExtInfo = Info; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2127 | Result = getFunctionType(FPT->getReturnType(), FPT->getParamTypes(), EPI); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
| 2130 | return cast<FunctionType>(Result.getTypePtr()); |
| 2131 | } |
| 2132 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2133 | void ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD, |
| 2134 | QualType ResultType) { |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2135 | FD = FD->getMostRecentDecl(); |
| 2136 | while (true) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2137 | const FunctionProtoType *FPT = FD->getType()->castAs<FunctionProtoType>(); |
| 2138 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2139 | FD->setType(getFunctionType(ResultType, FPT->getParamTypes(), EPI)); |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2140 | if (FunctionDecl *Next = FD->getPreviousDecl()) |
| 2141 | FD = Next; |
| 2142 | else |
| 2143 | break; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2144 | } |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2145 | if (ASTMutationListener *L = getASTMutationListener()) |
| 2146 | L->DeducedReturnType(FD, ResultType); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2149 | /// Get a function type and produce the equivalent function type with the |
| 2150 | /// specified exception specification. Type sugar that can be present on a |
| 2151 | /// declaration of a function with an exception specification is permitted |
| 2152 | /// and preserved. Other type sugar (for instance, typedefs) is not. |
| 2153 | static QualType getFunctionTypeWithExceptionSpec( |
| 2154 | ASTContext &Context, QualType Orig, |
| 2155 | const FunctionProtoType::ExceptionSpecInfo &ESI) { |
| 2156 | // Might have some parens. |
| 2157 | if (auto *PT = dyn_cast<ParenType>(Orig)) |
| 2158 | return Context.getParenType( |
| 2159 | getFunctionTypeWithExceptionSpec(Context, PT->getInnerType(), ESI)); |
| 2160 | |
| 2161 | // Might have a calling-convention attribute. |
| 2162 | if (auto *AT = dyn_cast<AttributedType>(Orig)) |
| 2163 | return Context.getAttributedType( |
| 2164 | AT->getAttrKind(), |
| 2165 | getFunctionTypeWithExceptionSpec(Context, AT->getModifiedType(), ESI), |
| 2166 | getFunctionTypeWithExceptionSpec(Context, AT->getEquivalentType(), |
| 2167 | ESI)); |
| 2168 | |
| 2169 | // Anything else must be a function type. Rebuild it with the new exception |
| 2170 | // specification. |
| 2171 | const FunctionProtoType *Proto = cast<FunctionProtoType>(Orig); |
| 2172 | return Context.getFunctionType( |
| 2173 | Proto->getReturnType(), Proto->getParamTypes(), |
| 2174 | Proto->getExtProtoInfo().withExceptionSpec(ESI)); |
| 2175 | } |
| 2176 | |
| 2177 | void ASTContext::adjustExceptionSpec( |
| 2178 | FunctionDecl *FD, const FunctionProtoType::ExceptionSpecInfo &ESI, |
| 2179 | bool AsWritten) { |
| 2180 | // Update the type. |
| 2181 | QualType Updated = |
| 2182 | getFunctionTypeWithExceptionSpec(*this, FD->getType(), ESI); |
| 2183 | FD->setType(Updated); |
| 2184 | |
| 2185 | if (!AsWritten) |
| 2186 | return; |
| 2187 | |
| 2188 | // Update the type in the type source information too. |
| 2189 | if (TypeSourceInfo *TSInfo = FD->getTypeSourceInfo()) { |
| 2190 | // If the type and the type-as-written differ, we may need to update |
| 2191 | // the type-as-written too. |
| 2192 | if (TSInfo->getType() != FD->getType()) |
| 2193 | Updated = getFunctionTypeWithExceptionSpec(*this, TSInfo->getType(), ESI); |
| 2194 | |
| 2195 | // FIXME: When we get proper type location information for exceptions, |
| 2196 | // we'll also have to rebuild the TypeSourceInfo. For now, we just patch |
| 2197 | // up the TypeSourceInfo; |
| 2198 | assert(TypeLoc::getFullDataSizeForType(Updated) == |
| 2199 | TypeLoc::getFullDataSizeForType(TSInfo->getType()) && |
| 2200 | "TypeLoc size mismatch from updating exception specification"); |
| 2201 | TSInfo->overrideType(Updated); |
| 2202 | } |
| 2203 | } |
| 2204 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2205 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 2206 | /// number with the specified element type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2207 | QualType ASTContext::getComplexType(QualType T) const { |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2208 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2209 | // structure. |
| 2210 | llvm::FoldingSetNodeID ID; |
| 2211 | ComplexType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2212 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2213 | void *InsertPos = nullptr; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2214 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2215 | return QualType(CT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2216 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2217 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2218 | // so fill in the canonical type field. |
| 2219 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2220 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2221 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2222 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2223 | // Get the new insert position for the node we care about. |
| 2224 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2225 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2226 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2227 | ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2228 | Types.push_back(New); |
| 2229 | ComplexTypes.InsertNode(New, InsertPos); |
| 2230 | return QualType(New, 0); |
| 2231 | } |
| 2232 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 2233 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 2234 | /// the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2235 | QualType ASTContext::getPointerType(QualType T) const { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 2236 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2237 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2238 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2239 | PointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2240 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2241 | void *InsertPos = nullptr; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2242 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2243 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2244 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 2245 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2246 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2247 | QualType Canonical; |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2248 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2249 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2250 | |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2251 | // Get the new insert position for the node we care about. |
| 2252 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2253 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2254 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2255 | PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2256 | Types.push_back(New); |
| 2257 | PointerTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2258 | return QualType(New, 0); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2261 | QualType ASTContext::getAdjustedType(QualType Orig, QualType New) const { |
| 2262 | llvm::FoldingSetNodeID ID; |
| 2263 | AdjustedType::Profile(ID, Orig, New); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2264 | void *InsertPos = nullptr; |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2265 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2266 | if (AT) |
| 2267 | return QualType(AT, 0); |
| 2268 | |
| 2269 | QualType Canonical = getCanonicalType(New); |
| 2270 | |
| 2271 | // Get the new insert position for the node we care about. |
| 2272 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2273 | assert(!AT && "Shouldn't be in the map!"); |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2274 | |
| 2275 | AT = new (*this, TypeAlignment) |
| 2276 | AdjustedType(Type::Adjusted, Orig, New, Canonical); |
| 2277 | Types.push_back(AT); |
| 2278 | AdjustedTypes.InsertNode(AT, InsertPos); |
| 2279 | return QualType(AT, 0); |
| 2280 | } |
| 2281 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2282 | QualType ASTContext::getDecayedType(QualType T) const { |
| 2283 | assert((T->isArrayType() || T->isFunctionType()) && "T does not decay"); |
| 2284 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2285 | QualType Decayed; |
| 2286 | |
| 2287 | // C99 6.7.5.3p7: |
| 2288 | // A declaration of a parameter as "array of type" shall be |
| 2289 | // adjusted to "qualified pointer to type", where the type |
| 2290 | // qualifiers (if any) are those specified within the [ and ] of |
| 2291 | // the array type derivation. |
| 2292 | if (T->isArrayType()) |
| 2293 | Decayed = getArrayDecayedType(T); |
| 2294 | |
| 2295 | // C99 6.7.5.3p8: |
| 2296 | // A declaration of a parameter as "function returning type" |
| 2297 | // shall be adjusted to "pointer to function returning type", as |
| 2298 | // in 6.3.2.1. |
| 2299 | if (T->isFunctionType()) |
| 2300 | Decayed = getPointerType(T); |
| 2301 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2302 | llvm::FoldingSetNodeID ID; |
| 2303 | AdjustedType::Profile(ID, T, Decayed); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2304 | void *InsertPos = nullptr; |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2305 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2306 | if (AT) |
| 2307 | return QualType(AT, 0); |
| 2308 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2309 | QualType Canonical = getCanonicalType(Decayed); |
| 2310 | |
| 2311 | // Get the new insert position for the node we care about. |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2312 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2313 | assert(!AT && "Shouldn't be in the map!"); |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2314 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2315 | AT = new (*this, TypeAlignment) DecayedType(T, Decayed, Canonical); |
| 2316 | Types.push_back(AT); |
| 2317 | AdjustedTypes.InsertNode(AT, InsertPos); |
| 2318 | return QualType(AT, 0); |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2321 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2322 | /// a pointer to the specified block. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2323 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 0ac01283 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2324 | assert(T->isFunctionType() && "block of function types only"); |
| 2325 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2326 | // structure. |
| 2327 | llvm::FoldingSetNodeID ID; |
| 2328 | BlockPointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2329 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2330 | void *InsertPos = nullptr; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2331 | if (BlockPointerType *PT = |
| 2332 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2333 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | |
| 2335 | // 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] | 2336 | // type either so fill in the canonical type field. |
| 2337 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2338 | if (!T.isCanonical()) { |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2339 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2341 | // Get the new insert position for the node we care about. |
| 2342 | BlockPointerType *NewIP = |
| 2343 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2344 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2345 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2346 | BlockPointerType *New |
| 2347 | = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2348 | Types.push_back(New); |
| 2349 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 2350 | return QualType(New, 0); |
| 2351 | } |
| 2352 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2353 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 2354 | /// lvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2355 | QualType |
| 2356 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 291e8ee | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 2357 | assert(getCanonicalType(T) != OverloadTy && |
| 2358 | "Unresolved overloaded function type"); |
| 2359 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2360 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2361 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2362 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2363 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2364 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2365 | void *InsertPos = nullptr; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2366 | if (LValueReferenceType *RT = |
| 2367 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2368 | return QualType(RT, 0); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2369 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2370 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2371 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2372 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2373 | // either, so fill in the canonical type field. |
| 2374 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2375 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 2376 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2377 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2378 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2379 | // Get the new insert position for the node we care about. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2380 | LValueReferenceType *NewIP = |
| 2381 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2382 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2385 | LValueReferenceType *New |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2386 | = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 2387 | SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2388 | Types.push_back(New); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2389 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2390 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2391 | return QualType(New, 0); |
| 2392 | } |
| 2393 | |
| 2394 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 2395 | /// rvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2396 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2397 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2398 | // structure. |
| 2399 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2400 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2401 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2402 | void *InsertPos = nullptr; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2403 | if (RValueReferenceType *RT = |
| 2404 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2405 | return QualType(RT, 0); |
| 2406 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2407 | const ReferenceType *InnerRef = T->getAs<ReferenceType>(); |
| 2408 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2409 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2410 | // either, so fill in the canonical type field. |
| 2411 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2412 | if (InnerRef || !T.isCanonical()) { |
| 2413 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2414 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2415 | |
| 2416 | // Get the new insert position for the node we care about. |
| 2417 | RValueReferenceType *NewIP = |
| 2418 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2419 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2422 | RValueReferenceType *New |
| 2423 | = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2424 | Types.push_back(New); |
| 2425 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2426 | return QualType(New, 0); |
| 2427 | } |
| 2428 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2429 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 2430 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2431 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2432 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2433 | // structure. |
| 2434 | llvm::FoldingSetNodeID ID; |
| 2435 | MemberPointerType::Profile(ID, T, Cls); |
| 2436 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2437 | void *InsertPos = nullptr; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2438 | if (MemberPointerType *PT = |
| 2439 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2440 | return QualType(PT, 0); |
| 2441 | |
| 2442 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 2443 | // type either, so fill in the canonical type field. |
| 2444 | QualType Canonical; |
Douglas Gregor | 615ac67 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2445 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2446 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 2447 | |
| 2448 | // Get the new insert position for the node we care about. |
| 2449 | MemberPointerType *NewIP = |
| 2450 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2451 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2452 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2453 | MemberPointerType *New |
| 2454 | = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2455 | Types.push_back(New); |
| 2456 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 2457 | return QualType(New, 0); |
| 2458 | } |
| 2459 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2460 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2461 | /// array of the specified element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2462 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2463 | const llvm::APInt &ArySizeIn, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2464 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2465 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 2dfdb82 | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 2466 | assert((EltTy->isDependentType() || |
| 2467 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 2468 | "Constant array of VLAs is illegal!"); |
| 2469 | |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2470 | // Convert the array size into a canonical width matching the pointer size for |
| 2471 | // the target. |
| 2472 | llvm::APInt ArySize(ArySizeIn); |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2473 | ArySize = |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2474 | ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2475 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2476 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2477 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2478 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2479 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2480 | if (ConstantArrayType *ATP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2481 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2482 | return QualType(ATP, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2483 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2484 | // If the element type isn't canonical or has qualifiers, this won't |
| 2485 | // be a canonical type either, so fill in the canonical type field. |
| 2486 | QualType Canon; |
| 2487 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2488 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2489 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2490 | ASM, IndexTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2491 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2492 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 2493 | // Get the new insert position for the node we care about. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2494 | ConstantArrayType *NewIP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2495 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2496 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 2497 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2498 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2499 | ConstantArrayType *New = new(*this,TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2500 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2501 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 2502 | Types.push_back(New); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2503 | return QualType(New, 0); |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2506 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 2507 | /// variably-modified, into the corresponding type with all the known |
| 2508 | /// sizes replaced with [*]. |
| 2509 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 2510 | // Vastly most common case. |
| 2511 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2512 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2513 | QualType result; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2514 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2515 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2516 | const Type *ty = split.Ty; |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2517 | switch (ty->getTypeClass()) { |
| 2518 | #define TYPE(Class, Base) |
| 2519 | #define ABSTRACT_TYPE(Class, Base) |
| 2520 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2521 | #include "clang/AST/TypeNodes.def" |
| 2522 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 2523 | |
| 2524 | // These types should never be variably-modified. |
| 2525 | case Type::Builtin: |
| 2526 | case Type::Complex: |
| 2527 | case Type::Vector: |
| 2528 | case Type::ExtVector: |
| 2529 | case Type::DependentSizedExtVector: |
| 2530 | case Type::ObjCObject: |
| 2531 | case Type::ObjCInterface: |
| 2532 | case Type::ObjCObjectPointer: |
| 2533 | case Type::Record: |
| 2534 | case Type::Enum: |
| 2535 | case Type::UnresolvedUsing: |
| 2536 | case Type::TypeOfExpr: |
| 2537 | case Type::TypeOf: |
| 2538 | case Type::Decltype: |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 2539 | case Type::UnaryTransform: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2540 | case Type::DependentName: |
| 2541 | case Type::InjectedClassName: |
| 2542 | case Type::TemplateSpecialization: |
| 2543 | case Type::DependentTemplateSpecialization: |
| 2544 | case Type::TemplateTypeParm: |
| 2545 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2546 | case Type::Auto: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2547 | case Type::PackExpansion: |
| 2548 | llvm_unreachable("type should never be variably-modified"); |
| 2549 | |
| 2550 | // These types can be variably-modified but should never need to |
| 2551 | // further decay. |
| 2552 | case Type::FunctionNoProto: |
| 2553 | case Type::FunctionProto: |
| 2554 | case Type::BlockPointer: |
| 2555 | case Type::MemberPointer: |
| 2556 | return type; |
| 2557 | |
| 2558 | // These types can be variably-modified. All these modifications |
| 2559 | // preserve structure except as noted by comments. |
| 2560 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 2561 | // optimizations available here. |
| 2562 | case Type::Pointer: |
| 2563 | result = getPointerType(getVariableArrayDecayedType( |
| 2564 | cast<PointerType>(ty)->getPointeeType())); |
| 2565 | break; |
| 2566 | |
| 2567 | case Type::LValueReference: { |
| 2568 | const LValueReferenceType *lv = cast<LValueReferenceType>(ty); |
| 2569 | result = getLValueReferenceType( |
| 2570 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 2571 | lv->isSpelledAsLValue()); |
| 2572 | break; |
| 2573 | } |
| 2574 | |
| 2575 | case Type::RValueReference: { |
| 2576 | const RValueReferenceType *lv = cast<RValueReferenceType>(ty); |
| 2577 | result = getRValueReferenceType( |
| 2578 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 2579 | break; |
| 2580 | } |
| 2581 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2582 | case Type::Atomic: { |
| 2583 | const AtomicType *at = cast<AtomicType>(ty); |
| 2584 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 2585 | break; |
| 2586 | } |
| 2587 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2588 | case Type::ConstantArray: { |
| 2589 | const ConstantArrayType *cat = cast<ConstantArrayType>(ty); |
| 2590 | result = getConstantArrayType( |
| 2591 | getVariableArrayDecayedType(cat->getElementType()), |
| 2592 | cat->getSize(), |
| 2593 | cat->getSizeModifier(), |
| 2594 | cat->getIndexTypeCVRQualifiers()); |
| 2595 | break; |
| 2596 | } |
| 2597 | |
| 2598 | case Type::DependentSizedArray: { |
| 2599 | const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty); |
| 2600 | result = getDependentSizedArrayType( |
| 2601 | getVariableArrayDecayedType(dat->getElementType()), |
| 2602 | dat->getSizeExpr(), |
| 2603 | dat->getSizeModifier(), |
| 2604 | dat->getIndexTypeCVRQualifiers(), |
| 2605 | dat->getBracketsRange()); |
| 2606 | break; |
| 2607 | } |
| 2608 | |
| 2609 | // Turn incomplete types into [*] types. |
| 2610 | case Type::IncompleteArray: { |
| 2611 | const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty); |
| 2612 | result = getVariableArrayType( |
| 2613 | getVariableArrayDecayedType(iat->getElementType()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2614 | /*size*/ nullptr, |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2615 | ArrayType::Normal, |
| 2616 | iat->getIndexTypeCVRQualifiers(), |
| 2617 | SourceRange()); |
| 2618 | break; |
| 2619 | } |
| 2620 | |
| 2621 | // Turn VLA types into [*] types. |
| 2622 | case Type::VariableArray: { |
| 2623 | const VariableArrayType *vat = cast<VariableArrayType>(ty); |
| 2624 | result = getVariableArrayType( |
| 2625 | getVariableArrayDecayedType(vat->getElementType()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2626 | /*size*/ nullptr, |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2627 | ArrayType::Star, |
| 2628 | vat->getIndexTypeCVRQualifiers(), |
| 2629 | vat->getBracketsRange()); |
| 2630 | break; |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | // Apply the top-level qualifiers from the original. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2635 | return getQualifiedType(result, split.Quals); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 2636 | } |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 2637 | |
Steve Naroff | cadebd0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 2638 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 2639 | /// variable array of the specified element type. |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 2640 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 2641 | Expr *NumElts, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2642 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2643 | unsigned IndexTypeQuals, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2644 | SourceRange Brackets) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2645 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 2646 | // that have an expression provided for their size. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2647 | QualType Canon; |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2648 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2649 | // Be sure to pull qualifiers off the element type. |
| 2650 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2651 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2652 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2653 | IndexTypeQuals, Brackets); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2654 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 2655 | } |
| 2656 | |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2657 | VariableArrayType *New = new(*this, TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2658 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2659 | |
| 2660 | VariableArrayTypes.push_back(New); |
| 2661 | Types.push_back(New); |
| 2662 | return QualType(New, 0); |
| 2663 | } |
| 2664 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2665 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 2666 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 2667 | /// type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2668 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 2669 | Expr *numElements, |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2670 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2671 | unsigned elementTypeQuals, |
| 2672 | SourceRange brackets) const { |
| 2673 | assert((!numElements || numElements->isTypeDependent() || |
| 2674 | numElements->isValueDependent()) && |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2675 | "Size must be type- or value-dependent!"); |
| 2676 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2677 | // Dependently-sized array types that do not have a specified number |
| 2678 | // of elements will have their sizes deduced from a dependent |
| 2679 | // initializer. We do no canonicalization here at all, which is okay |
| 2680 | // because they can't be used in most locations. |
| 2681 | if (!numElements) { |
| 2682 | DependentSizedArrayType *newType |
| 2683 | = new (*this, TypeAlignment) |
| 2684 | DependentSizedArrayType(*this, elementType, QualType(), |
| 2685 | numElements, ASM, elementTypeQuals, |
| 2686 | brackets); |
| 2687 | Types.push_back(newType); |
| 2688 | return QualType(newType, 0); |
| 2689 | } |
| 2690 | |
| 2691 | // Otherwise, we actually build a new type every time, but we |
| 2692 | // also build a canonical type. |
| 2693 | |
| 2694 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 2695 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2696 | void *insertPos = nullptr; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2697 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2698 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2699 | QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2700 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2701 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2702 | // Look for an existing type with these properties. |
| 2703 | DependentSizedArrayType *canonTy = |
| 2704 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2705 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2706 | // If we don't have one, build one. |
| 2707 | if (!canonTy) { |
| 2708 | canonTy = new (*this, TypeAlignment) |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2709 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2710 | QualType(), numElements, ASM, elementTypeQuals, |
| 2711 | brackets); |
| 2712 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 2713 | Types.push_back(canonTy); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2716 | // Apply qualifiers from the element type to the array. |
| 2717 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2718 | canonElementType.Quals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2719 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2720 | // If we didn't need extra canonicalization for the element type, |
| 2721 | // then just use that as our result. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2722 | if (QualType(canonElementType.Ty, 0) == elementType) |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2723 | return canon; |
| 2724 | |
| 2725 | // Otherwise, we need to build a type which follows the spelling |
| 2726 | // of the element type. |
| 2727 | DependentSizedArrayType *sugaredType |
| 2728 | = new (*this, TypeAlignment) |
| 2729 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 2730 | ASM, elementTypeQuals, brackets); |
| 2731 | Types.push_back(sugaredType); |
| 2732 | return QualType(sugaredType, 0); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2735 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2736 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2737 | unsigned elementTypeQuals) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2738 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2739 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2740 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2741 | void *insertPos = nullptr; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2742 | if (IncompleteArrayType *iat = |
| 2743 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 2744 | return QualType(iat, 0); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2745 | |
| 2746 | // 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] | 2747 | // either, so fill in the canonical type field. We also have to pull |
| 2748 | // qualifiers off the element type. |
| 2749 | QualType canon; |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2750 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2751 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 2752 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2753 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2754 | ASM, elementTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2755 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2756 | |
| 2757 | // Get the new insert position for the node we care about. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2758 | IncompleteArrayType *existing = |
| 2759 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 2760 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 843ebedd | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 2761 | } |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2762 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2763 | IncompleteArrayType *newType = new (*this, TypeAlignment) |
| 2764 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 2765 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2766 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 2767 | Types.push_back(newType); |
| 2768 | return QualType(newType, 0); |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2771 | /// getVectorType - Return the unique reference to a vector type of |
| 2772 | /// 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] | 2773 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2774 | VectorType::VectorKind VecKind) const { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2775 | assert(vecType->isBuiltinType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2776 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2777 | // Check if we've already instantiated a vector of this type. |
| 2778 | llvm::FoldingSetNodeID ID; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2779 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2780 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2781 | void *InsertPos = nullptr; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2782 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2783 | return QualType(VTP, 0); |
| 2784 | |
| 2785 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2786 | // so fill in the canonical type field. |
| 2787 | QualType Canonical; |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 2788 | if (!vecType.isCanonical()) { |
Bob Wilson | 7795480 | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 2789 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2791 | // Get the new insert position for the node we care about. |
| 2792 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2793 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2794 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2795 | VectorType *New = new (*this, TypeAlignment) |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2796 | VectorType(vecType, NumElts, Canonical, VecKind); |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 2797 | VectorTypes.InsertNode(New, InsertPos); |
| 2798 | Types.push_back(New); |
| 2799 | return QualType(New, 0); |
| 2800 | } |
| 2801 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2802 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2803 | /// 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] | 2804 | QualType |
| 2805 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2806 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2807 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2808 | // Check if we've already instantiated a vector of this type. |
| 2809 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 2810 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 2811 | VectorType::GenericVector); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2812 | void *InsertPos = nullptr; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2813 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2814 | return QualType(VTP, 0); |
| 2815 | |
| 2816 | // If the element type isn't canonical, this won't be a canonical type either, |
| 2817 | // so fill in the canonical type field. |
| 2818 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2819 | if (!vecType.isCanonical()) { |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2820 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2821 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2822 | // Get the new insert position for the node we care about. |
| 2823 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2824 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2825 | } |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2826 | ExtVectorType *New = new (*this, TypeAlignment) |
| 2827 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 2828 | VectorTypes.InsertNode(New, InsertPos); |
| 2829 | Types.push_back(New); |
| 2830 | return QualType(New, 0); |
| 2831 | } |
| 2832 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2833 | QualType |
| 2834 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 2835 | Expr *SizeExpr, |
| 2836 | SourceLocation AttrLoc) const { |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2837 | llvm::FoldingSetNodeID ID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2838 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2839 | SizeExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2840 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2841 | void *InsertPos = nullptr; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2842 | DependentSizedExtVectorType *Canon |
| 2843 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2844 | DependentSizedExtVectorType *New; |
| 2845 | if (Canon) { |
| 2846 | // We already have a canonical version of this array type; use it as |
| 2847 | // the canonical type for a newly-built type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2848 | New = new (*this, TypeAlignment) |
| 2849 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 2850 | SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2851 | } else { |
| 2852 | QualType CanonVecTy = getCanonicalType(vecType); |
| 2853 | if (CanonVecTy == vecType) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2854 | New = new (*this, TypeAlignment) |
| 2855 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 2856 | AttrLoc); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 2857 | |
| 2858 | DependentSizedExtVectorType *CanonCheck |
| 2859 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2860 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 2861 | (void)CanonCheck; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2862 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 2863 | } else { |
| 2864 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 2865 | SourceLocation()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2866 | New = new (*this, TypeAlignment) |
| 2867 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 2868 | } |
| 2869 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2870 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2871 | Types.push_back(New); |
| 2872 | return QualType(New, 0); |
| 2873 | } |
| 2874 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2875 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2876 | /// |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2877 | QualType |
| 2878 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 2879 | const FunctionType::ExtInfo &Info) const { |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2880 | const CallingConv CallConv = Info.getCC(); |
| 2881 | |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2882 | // Unique functions, to guarantee there is only one function of a particular |
| 2883 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2884 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 2885 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2886 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2887 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2888 | if (FunctionNoProtoType *FT = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2889 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2890 | return QualType(FT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2891 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2892 | QualType Canonical; |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2893 | if (!ResultTy.isCanonical()) { |
| 2894 | Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy), Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2895 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2896 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2897 | FunctionNoProtoType *NewIP = |
| 2898 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2899 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2900 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2901 | |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2902 | FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 2903 | FunctionNoProtoType *New = new (*this, TypeAlignment) |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2904 | FunctionNoProtoType(ResultTy, Canonical, newInfo); |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 2905 | Types.push_back(New); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2906 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2907 | return QualType(New, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
Douglas Gregor | cd78037 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2910 | /// \brief Determine whether \p T is canonical as the result type of a function. |
| 2911 | static bool isCanonicalResultType(QualType T) { |
| 2912 | return T.isCanonical() && |
| 2913 | (T.getObjCLifetime() == Qualifiers::OCL_None || |
| 2914 | T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone); |
| 2915 | } |
| 2916 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2917 | QualType |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2918 | ASTContext::getFunctionType(QualType ResultTy, ArrayRef<QualType> ArgArray, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2919 | const FunctionProtoType::ExtProtoInfo &EPI) const { |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2920 | size_t NumArgs = ArgArray.size(); |
| 2921 | |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2922 | // Unique functions, to guarantee there is only one function of a particular |
| 2923 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2924 | llvm::FoldingSetNodeID ID; |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2925 | FunctionProtoType::Profile(ID, ResultTy, ArgArray.begin(), NumArgs, EPI, |
| 2926 | *this); |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 2927 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2928 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2929 | if (FunctionProtoType *FTP = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2930 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2931 | return QualType(FTP, 0); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2932 | |
| 2933 | // Determine whether the type being created is already canonical or not. |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2934 | bool isCanonical = |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2935 | EPI.ExceptionSpec.Type == EST_None && isCanonicalResultType(ResultTy) && |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2936 | !EPI.HasTrailingReturn; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2937 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2938 | if (!ArgArray[i].isCanonicalAsParam()) |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2939 | isCanonical = false; |
| 2940 | |
| 2941 | // If this type isn't canonical, get the canonical version of it. |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2942 | // The exception spec is not part of the canonical type. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2943 | QualType Canonical; |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 2944 | if (!isCanonical) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2945 | SmallVector<QualType, 16> CanonicalArgs; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2946 | CanonicalArgs.reserve(NumArgs); |
| 2947 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2948 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2949 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2950 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2951 | CanonicalEPI.HasTrailingReturn = false; |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2952 | CanonicalEPI.ExceptionSpec = FunctionProtoType::ExceptionSpecInfo(); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2953 | |
Douglas Gregor | cd78037 | 2013-01-17 23:36:45 +0000 | [diff] [blame] | 2954 | // Result types do not have ARC lifetime qualifiers. |
| 2955 | QualType CanResultTy = getCanonicalType(ResultTy); |
| 2956 | if (ResultTy.getQualifiers().hasObjCLifetime()) { |
| 2957 | Qualifiers Qs = CanResultTy.getQualifiers(); |
| 2958 | Qs.removeObjCLifetime(); |
| 2959 | CanResultTy = getQualifiedType(CanResultTy.getUnqualifiedType(), Qs); |
| 2960 | } |
| 2961 | |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2962 | Canonical = getFunctionType(CanResultTy, CanonicalArgs, CanonicalEPI); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2963 | |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 2964 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2965 | FunctionProtoType *NewIP = |
| 2966 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2967 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2968 | } |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 2969 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2970 | // FunctionProtoType objects are allocated with extra bytes after |
| 2971 | // them for three variable size arrays at the end: |
| 2972 | // - parameter types |
| 2973 | // - exception types |
| 2974 | // - consumed-arguments flags |
| 2975 | // Instead of the exception types, there could be a noexcept |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2976 | // expression, or information used to resolve the exception |
| 2977 | // specification. |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2978 | size_t Size = sizeof(FunctionProtoType) + |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2979 | NumArgs * sizeof(QualType); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2980 | if (EPI.ExceptionSpec.Type == EST_Dynamic) { |
| 2981 | Size += EPI.ExceptionSpec.Exceptions.size() * sizeof(QualType); |
| 2982 | } else if (EPI.ExceptionSpec.Type == EST_ComputedNoexcept) { |
Sebastian Redl | 31ad754 | 2011-03-13 17:09:40 +0000 | [diff] [blame] | 2983 | Size += sizeof(Expr*); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2984 | } else if (EPI.ExceptionSpec.Type == EST_Uninstantiated) { |
Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 2985 | Size += 2 * sizeof(FunctionDecl*); |
Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 2986 | } else if (EPI.ExceptionSpec.Type == EST_Unevaluated) { |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 2987 | Size += sizeof(FunctionDecl*); |
Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2988 | } |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 2989 | if (EPI.ConsumedParameters) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2990 | Size += NumArgs * sizeof(bool); |
| 2991 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2992 | FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment); |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 2993 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2994 | new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2995 | Types.push_back(FTP); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2996 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2997 | return QualType(FTP, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 2998 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 2999 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3000 | #ifndef NDEBUG |
| 3001 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 3002 | if (!isa<CXXRecordDecl>(D)) return false; |
| 3003 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 3004 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 3005 | return true; |
| 3006 | if (RD->getDescribedClassTemplate() && |
| 3007 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 3008 | return true; |
| 3009 | return false; |
| 3010 | } |
| 3011 | #endif |
| 3012 | |
| 3013 | /// getInjectedClassNameType - Return the unique reference to the |
| 3014 | /// injected class name type for the specified templated declaration. |
| 3015 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3016 | QualType TST) const { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3017 | assert(NeedsInjectedClassNameType(Decl)); |
| 3018 | if (Decl->TypeForDecl) { |
| 3019 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3020 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3021 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 3022 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3023 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 3024 | } else { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3025 | Type *newType = |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3026 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3027 | Decl->TypeForDecl = newType; |
| 3028 | Types.push_back(newType); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3029 | } |
| 3030 | return QualType(Decl->TypeForDecl, 0); |
| 3031 | } |
| 3032 | |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3033 | /// getTypeDeclType - Return the unique reference to the type for the |
| 3034 | /// specified type declaration. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3035 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 89656d2 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 3036 | assert(Decl && "Passed null for Decl param"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3037 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3038 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3039 | if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3040 | return getTypedefType(Typedef); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3041 | |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3042 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 3043 | "Template type parameter types are always available."); |
| 3044 | |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 3045 | if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) { |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 3046 | assert(Record->isFirstDecl() && "struct/union has previous declaration"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3047 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3048 | return getRecordType(Record); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 3049 | } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) { |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 3050 | assert(Enum->isFirstDecl() && "enum has previous declaration"); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3051 | return getEnumType(Enum); |
John McCall | 81e3850 | 2010-02-16 03:57:14 +0000 | [diff] [blame] | 3052 | } else if (const UnresolvedUsingTypenameDecl *Using = |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3053 | dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3054 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 3055 | Decl->TypeForDecl = newType; |
| 3056 | Types.push_back(newType); |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3057 | } else |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3058 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 3059 | |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 3060 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3061 | } |
| 3062 | |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 3063 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3064 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3065 | QualType |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3066 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 3067 | QualType Canonical) const { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3068 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3069 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3070 | if (Canonical.isNull()) |
| 3071 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3072 | TypedefType *newType = new(*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3073 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3074 | Decl->TypeForDecl = newType; |
| 3075 | Types.push_back(newType); |
| 3076 | return QualType(newType, 0); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3079 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3080 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 3081 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3082 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3083 | if (PrevDecl->TypeForDecl) |
| 3084 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 3085 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3086 | RecordType *newType = new (*this, TypeAlignment) RecordType(Decl); |
| 3087 | Decl->TypeForDecl = newType; |
| 3088 | Types.push_back(newType); |
| 3089 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3092 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3093 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 3094 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3095 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3096 | if (PrevDecl->TypeForDecl) |
| 3097 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 3098 | |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3099 | EnumType *newType = new (*this, TypeAlignment) EnumType(Decl); |
| 3100 | Decl->TypeForDecl = newType; |
| 3101 | Types.push_back(newType); |
| 3102 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 3105 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 3106 | QualType modifiedType, |
| 3107 | QualType equivalentType) { |
| 3108 | llvm::FoldingSetNodeID id; |
| 3109 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 3110 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3111 | void *insertPos = nullptr; |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 3112 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 3113 | if (type) return QualType(type, 0); |
| 3114 | |
| 3115 | QualType canon = getCanonicalType(equivalentType); |
| 3116 | type = new (*this, TypeAlignment) |
| 3117 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 3118 | |
| 3119 | Types.push_back(type); |
| 3120 | AttributedTypes.InsertNode(type, insertPos); |
| 3121 | |
| 3122 | return QualType(type, 0); |
| 3123 | } |
| 3124 | |
| 3125 | |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3126 | /// \brief Retrieve a substitution-result type. |
| 3127 | QualType |
| 3128 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3129 | QualType Replacement) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3130 | assert(Replacement.isCanonical() |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3131 | && "replacement types must always be canonical"); |
| 3132 | |
| 3133 | llvm::FoldingSetNodeID ID; |
| 3134 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3135 | void *InsertPos = nullptr; |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3136 | SubstTemplateTypeParmType *SubstParm |
| 3137 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3138 | |
| 3139 | if (!SubstParm) { |
| 3140 | SubstParm = new (*this, TypeAlignment) |
| 3141 | SubstTemplateTypeParmType(Parm, Replacement); |
| 3142 | Types.push_back(SubstParm); |
| 3143 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 3144 | } |
| 3145 | |
| 3146 | return QualType(SubstParm, 0); |
| 3147 | } |
| 3148 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3149 | /// \brief Retrieve a |
| 3150 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 3151 | const TemplateTypeParmType *Parm, |
| 3152 | const TemplateArgument &ArgPack) { |
| 3153 | #ifndef NDEBUG |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 3154 | for (const auto &P : ArgPack.pack_elements()) { |
| 3155 | assert(P.getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 3156 | assert(P.getAsType().isCanonical() && "Pack contains non-canonical type"); |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3157 | } |
| 3158 | #endif |
| 3159 | |
| 3160 | llvm::FoldingSetNodeID ID; |
| 3161 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3162 | void *InsertPos = nullptr; |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3163 | if (SubstTemplateTypeParmPackType *SubstParm |
| 3164 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3165 | return QualType(SubstParm, 0); |
| 3166 | |
| 3167 | QualType Canon; |
| 3168 | if (!Parm->isCanonicalUnqualified()) { |
| 3169 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 3170 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 3171 | ArgPack); |
| 3172 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3173 | } |
| 3174 | |
| 3175 | SubstTemplateTypeParmPackType *SubstParm |
| 3176 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 3177 | ArgPack); |
| 3178 | Types.push_back(SubstParm); |
| 3179 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 3180 | return QualType(SubstParm, 0); |
| 3181 | } |
| 3182 | |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3183 | /// \brief Retrieve the template type parameter type for a template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3184 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3185 | /// name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3186 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3187 | bool ParameterPack, |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3188 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3189 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3190 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3191 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3192 | TemplateTypeParmType *TypeParm |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3193 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3194 | |
| 3195 | if (TypeParm) |
| 3196 | return QualType(TypeParm, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3197 | |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3198 | if (TTPDecl) { |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3199 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3200 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3201 | |
| 3202 | TemplateTypeParmType *TypeCheck |
| 3203 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3204 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 3205 | (void)TypeCheck; |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3206 | } else |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3207 | TypeParm = new (*this, TypeAlignment) |
| 3208 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3209 | |
| 3210 | Types.push_back(TypeParm); |
| 3211 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 3212 | |
| 3213 | return QualType(TypeParm, 0); |
| 3214 | } |
| 3215 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3216 | TypeSourceInfo * |
| 3217 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 3218 | SourceLocation NameLoc, |
| 3219 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3220 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3221 | assert(!Name.getAsDependentTemplateName() && |
| 3222 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3223 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3224 | |
| 3225 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3226 | TemplateSpecializationTypeLoc TL = |
| 3227 | DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>(); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3228 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3229 | TL.setTemplateNameLoc(NameLoc); |
| 3230 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 3231 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 3232 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 3233 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 3234 | return DI; |
| 3235 | } |
| 3236 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3237 | QualType |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3238 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3239 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3240 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3241 | assert(!Template.getAsDependentTemplateName() && |
| 3242 | "No dependent template names here!"); |
| 3243 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3244 | unsigned NumArgs = Args.size(); |
| 3245 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3246 | SmallVector<TemplateArgument, 4> ArgVec; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3247 | ArgVec.reserve(NumArgs); |
| 3248 | for (unsigned i = 0; i != NumArgs; ++i) |
| 3249 | ArgVec.push_back(Args[i].getArgument()); |
| 3250 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3251 | return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3252 | Underlying); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3255 | #ifndef NDEBUG |
| 3256 | static bool hasAnyPackExpansions(const TemplateArgument *Args, |
| 3257 | unsigned NumArgs) { |
| 3258 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3259 | if (Args[I].isPackExpansion()) |
| 3260 | return true; |
| 3261 | |
| 3262 | return true; |
| 3263 | } |
| 3264 | #endif |
| 3265 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3266 | QualType |
| 3267 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3268 | const TemplateArgument *Args, |
| 3269 | unsigned NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3270 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3271 | assert(!Template.getAsDependentTemplateName() && |
| 3272 | "No dependent template names here!"); |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3273 | // Look through qualified template names. |
| 3274 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3275 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3276 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3277 | bool IsTypeAlias = |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3278 | Template.getAsTemplateDecl() && |
| 3279 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3280 | QualType CanonType; |
| 3281 | if (!Underlying.isNull()) |
| 3282 | CanonType = getCanonicalType(Underlying); |
| 3283 | else { |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3284 | // We can get here with an alias template when the specialization contains |
| 3285 | // a pack expansion that does not match up with a parameter pack. |
| 3286 | assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) && |
| 3287 | "Caller must compute aliased type"); |
| 3288 | IsTypeAlias = false; |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3289 | CanonType = getCanonicalTemplateSpecializationType(Template, Args, |
| 3290 | NumArgs); |
| 3291 | } |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3292 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3293 | // Allocate the (non-canonical) template specialization type, but don't |
| 3294 | // try to unique it: these types typically have location information that |
| 3295 | // we don't unique and don't want to lose. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3296 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
| 3297 | sizeof(TemplateArgument) * NumArgs + |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3298 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3299 | TypeAlignment); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3300 | TemplateSpecializationType *Spec |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3301 | = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType, |
| 3302 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3303 | |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3304 | Types.push_back(Spec); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3305 | return QualType(Spec, 0); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3306 | } |
| 3307 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3308 | QualType |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3309 | ASTContext::getCanonicalTemplateSpecializationType(TemplateName Template, |
| 3310 | const TemplateArgument *Args, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3311 | unsigned NumArgs) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3312 | assert(!Template.getAsDependentTemplateName() && |
| 3313 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3314 | |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3315 | // Look through qualified template names. |
| 3316 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3317 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3318 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3319 | // Build the canonical template specialization type. |
| 3320 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3321 | SmallVector<TemplateArgument, 4> CanonArgs; |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3322 | CanonArgs.reserve(NumArgs); |
| 3323 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3324 | CanonArgs.push_back(getCanonicalTemplateArgument(Args[I])); |
| 3325 | |
| 3326 | // Determine whether this canonical template specialization type already |
| 3327 | // exists. |
| 3328 | llvm::FoldingSetNodeID ID; |
| 3329 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
| 3330 | CanonArgs.data(), NumArgs, *this); |
| 3331 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3332 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3333 | TemplateSpecializationType *Spec |
| 3334 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3335 | |
| 3336 | if (!Spec) { |
| 3337 | // Allocate a new canonical template specialization type. |
| 3338 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 3339 | sizeof(TemplateArgument) * NumArgs), |
| 3340 | TypeAlignment); |
| 3341 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
| 3342 | CanonArgs.data(), NumArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3343 | QualType(), QualType()); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3344 | Types.push_back(Spec); |
| 3345 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 3346 | } |
| 3347 | |
| 3348 | assert(Spec->isDependentType() && |
| 3349 | "Non-dependent template-id type must have a canonical type"); |
| 3350 | return QualType(Spec, 0); |
| 3351 | } |
| 3352 | |
| 3353 | QualType |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3354 | ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 3355 | NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3356 | QualType NamedType) const { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3357 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3358 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3359 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3360 | void *InsertPos = nullptr; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3361 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3362 | if (T) |
| 3363 | return QualType(T, 0); |
| 3364 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3365 | QualType Canon = NamedType; |
| 3366 | if (!Canon.isCanonical()) { |
| 3367 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3368 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3369 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3370 | (void)CheckT; |
| 3371 | } |
| 3372 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 3373 | T = new (*this, TypeAlignment) ElaboratedType(Keyword, NNS, NamedType, Canon); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3374 | Types.push_back(T); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 3375 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 3376 | return QualType(T, 0); |
| 3377 | } |
| 3378 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3379 | QualType |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3380 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3381 | llvm::FoldingSetNodeID ID; |
| 3382 | ParenType::Profile(ID, InnerType); |
| 3383 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3384 | void *InsertPos = nullptr; |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3385 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3386 | if (T) |
| 3387 | return QualType(T, 0); |
| 3388 | |
| 3389 | QualType Canon = InnerType; |
| 3390 | if (!Canon.isCanonical()) { |
| 3391 | Canon = getCanonicalType(InnerType); |
| 3392 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3393 | assert(!CheckT && "Paren canonical type broken"); |
| 3394 | (void)CheckT; |
| 3395 | } |
| 3396 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 3397 | T = new (*this, TypeAlignment) ParenType(InnerType, Canon); |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 3398 | Types.push_back(T); |
| 3399 | ParenTypes.InsertNode(T, InsertPos); |
| 3400 | return QualType(T, 0); |
| 3401 | } |
| 3402 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3403 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 3404 | NestedNameSpecifier *NNS, |
| 3405 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3406 | QualType Canon) const { |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3407 | if (Canon.isNull()) { |
| 3408 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3409 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3410 | if (Keyword == ETK_None) |
| 3411 | CanonKeyword = ETK_Typename; |
| 3412 | |
| 3413 | if (CanonNNS != NNS || CanonKeyword != Keyword) |
| 3414 | Canon = getDependentNameType(CanonKeyword, CanonNNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3418 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3419 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3420 | void *InsertPos = nullptr; |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3421 | DependentNameType *T |
| 3422 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3423 | if (T) |
| 3424 | return QualType(T, 0); |
| 3425 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 3426 | T = new (*this, TypeAlignment) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3427 | Types.push_back(T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3428 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3429 | return QualType(T, 0); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | QualType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3433 | ASTContext::getDependentTemplateSpecializationType( |
| 3434 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 3435 | NestedNameSpecifier *NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3436 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3437 | const TemplateArgumentListInfo &Args) const { |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3438 | // TODO: avoid this copy |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3439 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3440 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 3441 | ArgCopy.push_back(Args[I].getArgument()); |
| 3442 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, |
| 3443 | ArgCopy.size(), |
| 3444 | ArgCopy.data()); |
| 3445 | } |
| 3446 | |
| 3447 | QualType |
| 3448 | ASTContext::getDependentTemplateSpecializationType( |
| 3449 | ElaboratedTypeKeyword Keyword, |
| 3450 | NestedNameSpecifier *NNS, |
| 3451 | const IdentifierInfo *Name, |
| 3452 | unsigned NumArgs, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3453 | const TemplateArgument *Args) const { |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 3454 | assert((!NNS || NNS->isDependent()) && |
| 3455 | "nested-name-specifier must be dependent"); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3456 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3457 | llvm::FoldingSetNodeID ID; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3458 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
| 3459 | Name, NumArgs, Args); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3460 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3461 | void *InsertPos = nullptr; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3462 | DependentTemplateSpecializationType *T |
| 3463 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3464 | if (T) |
| 3465 | return QualType(T, 0); |
| 3466 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3467 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3468 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3469 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 3470 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 3471 | |
| 3472 | bool AnyNonCanonArgs = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3473 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3474 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 3475 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 3476 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 3477 | AnyNonCanonArgs = true; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3480 | QualType Canon; |
| 3481 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 3482 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
| 3483 | Name, NumArgs, |
| 3484 | CanonArgs.data()); |
| 3485 | |
| 3486 | // Find the insert position again. |
| 3487 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3488 | } |
| 3489 | |
| 3490 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 3491 | sizeof(TemplateArgument) * NumArgs), |
| 3492 | TypeAlignment); |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 3493 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3494 | Name, NumArgs, Args, Canon); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3495 | Types.push_back(T); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3496 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3497 | return QualType(T, 0); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3500 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3501 | Optional<unsigned> NumExpansions) { |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3502 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 3503 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3504 | |
| 3505 | assert(Pattern->containsUnexpandedParameterPack() && |
| 3506 | "Pack expansions must expand one or more parameter packs"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3507 | void *InsertPos = nullptr; |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3508 | PackExpansionType *T |
| 3509 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3510 | if (T) |
| 3511 | return QualType(T, 0); |
| 3512 | |
| 3513 | QualType Canon; |
| 3514 | if (!Pattern.isCanonical()) { |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3515 | Canon = getCanonicalType(Pattern); |
| 3516 | // The canonical type might not contain an unexpanded parameter pack, if it |
| 3517 | // contains an alias template specialization which ignores one of its |
| 3518 | // parameters. |
| 3519 | if (Canon->containsUnexpandedParameterPack()) { |
Richard Smith | 8b4e1e2 | 2014-07-10 01:20:17 +0000 | [diff] [blame] | 3520 | Canon = getPackExpansionType(Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3521 | |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 3522 | // Find the insert position again, in case we inserted an element into |
| 3523 | // PackExpansionTypes and invalidated our insert position. |
| 3524 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3525 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3526 | } |
| 3527 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 3528 | T = new (*this, TypeAlignment) |
| 3529 | PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3530 | Types.push_back(T); |
| 3531 | PackExpansionTypes.InsertNode(T, InsertPos); |
Richard Smith | 8b4e1e2 | 2014-07-10 01:20:17 +0000 | [diff] [blame] | 3532 | return QualType(T, 0); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3535 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 3536 | /// alphabetically. |
Benjamin Kramer | 0eb262f | 2015-03-14 13:32:49 +0000 | [diff] [blame] | 3537 | static int CmpProtocolNames(ObjCProtocolDecl *const *LHS, |
| 3538 | ObjCProtocolDecl *const *RHS) { |
| 3539 | return DeclarationName::compare((*LHS)->getDeclName(), (*RHS)->getDeclName()); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3542 | static bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols, |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3543 | unsigned NumProtocols) { |
| 3544 | if (NumProtocols == 0) return true; |
| 3545 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3546 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 3547 | return false; |
| 3548 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3549 | for (unsigned i = 1; i != NumProtocols; ++i) |
Benjamin Kramer | 0eb262f | 2015-03-14 13:32:49 +0000 | [diff] [blame] | 3550 | if (CmpProtocolNames(&Protocols[i - 1], &Protocols[i]) >= 0 || |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3551 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3552 | return false; |
| 3553 | return true; |
| 3554 | } |
| 3555 | |
| 3556 | static void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols, |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3557 | unsigned &NumProtocols) { |
| 3558 | ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3559 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3560 | // Sort protocols, keyed by name. |
Benjamin Kramer | 0eb262f | 2015-03-14 13:32:49 +0000 | [diff] [blame] | 3561 | llvm::array_pod_sort(Protocols, ProtocolsEnd, CmpProtocolNames); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3562 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 3563 | // Canonicalize. |
| 3564 | for (unsigned I = 0, N = NumProtocols; I != N; ++I) |
| 3565 | Protocols[I] = Protocols[I]->getCanonicalDecl(); |
| 3566 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3567 | // Remove duplicates. |
| 3568 | ProtocolsEnd = std::unique(Protocols, ProtocolsEnd); |
| 3569 | NumProtocols = ProtocolsEnd-Protocols; |
| 3570 | } |
| 3571 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3572 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 3573 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3574 | unsigned NumProtocols) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3575 | // If the base type is an interface and there aren't any protocols |
| 3576 | // to add, then the interface type will do just fine. |
| 3577 | if (!NumProtocols && isa<ObjCInterfaceType>(BaseType)) |
| 3578 | return BaseType; |
| 3579 | |
| 3580 | // Look in the folding set for an existing type. |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3581 | llvm::FoldingSetNodeID ID; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3582 | ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3583 | void *InsertPos = nullptr; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3584 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3585 | return QualType(QT, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3586 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3587 | // Build the canonical type, which has the canonical base type and |
| 3588 | // a sorted-and-uniqued list of protocols. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3589 | QualType Canonical; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3590 | bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols); |
| 3591 | if (!ProtocolsSorted || !BaseType.isCanonical()) { |
| 3592 | if (!ProtocolsSorted) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3593 | SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols, |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 3594 | Protocols + NumProtocols); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3595 | unsigned UniqueCount = NumProtocols; |
| 3596 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3597 | SortAndUniqueProtocols(&Sorted[0], UniqueCount); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3598 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 3599 | &Sorted[0], UniqueCount); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3600 | } else { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3601 | Canonical = getObjCObjectType(getCanonicalType(BaseType), |
| 3602 | Protocols, NumProtocols); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | // Regenerate InsertPos. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3606 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3607 | } |
| 3608 | |
| 3609 | unsigned Size = sizeof(ObjCObjectTypeImpl); |
| 3610 | Size += NumProtocols * sizeof(ObjCProtocolDecl *); |
| 3611 | void *Mem = Allocate(Size, TypeAlignment); |
| 3612 | ObjCObjectTypeImpl *T = |
| 3613 | new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols); |
| 3614 | |
| 3615 | Types.push_back(T); |
| 3616 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 3617 | return QualType(T, 0); |
| 3618 | } |
| 3619 | |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3620 | /// ObjCObjectAdoptsQTypeProtocols - Checks that protocols in IC's |
| 3621 | /// protocol list adopt all protocols in QT's qualified-id protocol |
| 3622 | /// list. |
| 3623 | bool ASTContext::ObjCObjectAdoptsQTypeProtocols(QualType QT, |
| 3624 | ObjCInterfaceDecl *IC) { |
| 3625 | if (!QT->isObjCQualifiedIdType()) |
| 3626 | return false; |
| 3627 | |
| 3628 | if (const ObjCObjectPointerType *OPT = QT->getAs<ObjCObjectPointerType>()) { |
| 3629 | // If both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 3630 | for (auto *Proto : OPT->quals()) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3631 | if (!IC->ClassImplementsProtocol(Proto, false)) |
| 3632 | return false; |
| 3633 | } |
| 3634 | return true; |
| 3635 | } |
| 3636 | return false; |
| 3637 | } |
| 3638 | |
| 3639 | /// QIdProtocolsAdoptObjCObjectProtocols - Checks that protocols in |
| 3640 | /// QT's qualified-id protocol list adopt all protocols in IDecl's list |
| 3641 | /// of protocols. |
| 3642 | bool ASTContext::QIdProtocolsAdoptObjCObjectProtocols(QualType QT, |
| 3643 | ObjCInterfaceDecl *IDecl) { |
| 3644 | if (!QT->isObjCQualifiedIdType()) |
| 3645 | return false; |
| 3646 | const ObjCObjectPointerType *OPT = QT->getAs<ObjCObjectPointerType>(); |
| 3647 | if (!OPT) |
| 3648 | return false; |
| 3649 | if (!IDecl->hasDefinition()) |
| 3650 | return false; |
Fariborz Jahanian | 91fb0be | 2013-11-20 19:01:50 +0000 | [diff] [blame] | 3651 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocols; |
| 3652 | CollectInheritedProtocols(IDecl, InheritedProtocols); |
| 3653 | if (InheritedProtocols.empty()) |
| 3654 | return false; |
Fariborz Jahanian | 48a01cb | 2014-04-04 23:53:45 +0000 | [diff] [blame] | 3655 | // Check that if every protocol in list of id<plist> conforms to a protcol |
| 3656 | // of IDecl's, then bridge casting is ok. |
| 3657 | bool Conforms = false; |
| 3658 | for (auto *Proto : OPT->quals()) { |
| 3659 | Conforms = false; |
| 3660 | for (auto *PI : InheritedProtocols) { |
| 3661 | if (ProtocolCompatibleWithProtocol(Proto, PI)) { |
| 3662 | Conforms = true; |
| 3663 | break; |
| 3664 | } |
| 3665 | } |
| 3666 | if (!Conforms) |
| 3667 | break; |
| 3668 | } |
| 3669 | if (Conforms) |
| 3670 | return true; |
| 3671 | |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 3672 | for (auto *PI : InheritedProtocols) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3673 | // If both the right and left sides have qualifiers. |
| 3674 | bool Adopts = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 3675 | for (auto *Proto : OPT->quals()) { |
Fariborz Jahanian | 48a01cb | 2014-04-04 23:53:45 +0000 | [diff] [blame] | 3676 | // return 'true' if 'PI' is in the inheritance hierarchy of Proto |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 3677 | if ((Adopts = ProtocolCompatibleWithProtocol(PI, Proto))) |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3678 | break; |
| 3679 | } |
| 3680 | if (!Adopts) |
Fariborz Jahanian | 91fb0be | 2013-11-20 19:01:50 +0000 | [diff] [blame] | 3681 | return false; |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3682 | } |
| 3683 | return true; |
| 3684 | } |
| 3685 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3686 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 3687 | /// the given object type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3688 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3689 | llvm::FoldingSetNodeID ID; |
| 3690 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 3691 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3692 | void *InsertPos = nullptr; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3693 | if (ObjCObjectPointerType *QT = |
| 3694 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3695 | return QualType(QT, 0); |
| 3696 | |
| 3697 | // Find the canonical object type. |
| 3698 | QualType Canonical; |
| 3699 | if (!ObjectT.isCanonical()) { |
| 3700 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 3701 | |
| 3702 | // Regenerate InsertPos. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3703 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3704 | } |
| 3705 | |
Douglas Gregor | f85bee6 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 3706 | // No match. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3707 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
| 3708 | ObjCObjectPointerType *QType = |
| 3709 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3710 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3711 | Types.push_back(QType); |
| 3712 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3713 | return QualType(QType, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3714 | } |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 3715 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3716 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 3717 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3718 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 3719 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3720 | if (Decl->TypeForDecl) |
| 3721 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3722 | |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 3723 | if (PrevDecl) { |
| 3724 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 3725 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3726 | return QualType(PrevDecl->TypeForDecl, 0); |
| 3727 | } |
| 3728 | |
Douglas Gregor | 7671e53 | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 3729 | // Prefer the definition, if there is one. |
| 3730 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 3731 | Decl = Def; |
| 3732 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 3733 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
| 3734 | ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl); |
| 3735 | Decl->TypeForDecl = T; |
| 3736 | Types.push_back(T); |
| 3737 | return QualType(T, 0); |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3740 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 3741 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3742 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3743 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3744 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3745 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3746 | TypeOfExprType *toe; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3747 | if (tofExpr->isTypeDependent()) { |
| 3748 | llvm::FoldingSetNodeID ID; |
| 3749 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3750 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3751 | void *InsertPos = nullptr; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3752 | DependentTypeOfExprType *Canon |
| 3753 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3754 | if (Canon) { |
| 3755 | // We already have a "canonical" version of an identical, dependent |
| 3756 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3757 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3758 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3759 | } else { |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3760 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3761 | Canon |
| 3762 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 3763 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 3764 | toe = Canon; |
| 3765 | } |
| 3766 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3767 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3768 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3769 | } |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3770 | Types.push_back(toe); |
| 3771 | return QualType(toe, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3774 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3775 | /// TypeOfType nodes. The only motivation to unique these nodes would be |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3776 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3777 | /// an issue. This doesn't affect the type checker, since it operates |
| 3778 | /// on canonical types (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3779 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 3780 | QualType Canonical = getCanonicalType(tofType); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3781 | TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 3782 | Types.push_back(tot); |
| 3783 | return QualType(tot, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Anders Carlsson | ad6bd35 | 2009-06-24 21:24:56 +0000 | [diff] [blame] | 3786 | |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3787 | /// \brief Unlike many "get<Type>" functions, we don't unique DecltypeType |
| 3788 | /// nodes. This would never be helpful, since each such type has its own |
| 3789 | /// expression, and would not give a significant memory saving, since there |
| 3790 | /// is an Expr tree under each such type. |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 3791 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3792 | DecltypeType *dt; |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3793 | |
| 3794 | // C++11 [temp.type]p2: |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3795 | // If an expression e involves a template parameter, decltype(e) denotes a |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3796 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 3797 | // type only if their expressions are equivalent (14.5.6.1). |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3798 | if (e->isInstantiationDependent()) { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3799 | llvm::FoldingSetNodeID ID; |
| 3800 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3801 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3802 | void *InsertPos = nullptr; |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3803 | DependentDecltypeType *Canon |
| 3804 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3805 | if (!Canon) { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3806 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3807 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3808 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3809 | } |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3810 | dt = new (*this, TypeAlignment) |
| 3811 | DecltypeType(e, UnderlyingType, QualType((DecltypeType *)Canon, 0)); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 3812 | } else { |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 3813 | dt = new (*this, TypeAlignment) |
| 3814 | DecltypeType(e, UnderlyingType, getCanonicalType(UnderlyingType)); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 3815 | } |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 3816 | Types.push_back(dt); |
| 3817 | return QualType(dt, 0); |
| 3818 | } |
| 3819 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3820 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 3821 | /// savings are minimal and these are rare. |
| 3822 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 3823 | QualType UnderlyingType, |
| 3824 | UnaryTransformType::UTTKind Kind) |
| 3825 | const { |
| 3826 | UnaryTransformType *Ty = |
Douglas Gregor | 6c6e676 | 2011-05-25 17:51:54 +0000 | [diff] [blame] | 3827 | new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType, |
| 3828 | Kind, |
| 3829 | UnderlyingType->isDependentType() ? |
Peter Collingbourne | 15d48ec | 2012-03-05 16:02:06 +0000 | [diff] [blame] | 3830 | QualType() : getCanonicalType(UnderlyingType)); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3831 | Types.push_back(Ty); |
| 3832 | return QualType(Ty, 0); |
| 3833 | } |
| 3834 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3835 | /// getAutoType - Return the uniqued reference to the 'auto' type which has been |
| 3836 | /// deduced to the given type, or to the canonical undeduced 'auto' type, or the |
| 3837 | /// canonical deduced-but-dependent 'auto' type. |
| 3838 | QualType ASTContext::getAutoType(QualType DeducedType, bool IsDecltypeAuto, |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3839 | bool IsDependent) const { |
| 3840 | if (DeducedType.isNull() && !IsDecltypeAuto && !IsDependent) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3841 | return getAutoDeductType(); |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3842 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3843 | // Look in the folding set for an existing type. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3844 | void *InsertPos = nullptr; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3845 | llvm::FoldingSetNodeID ID; |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3846 | AutoType::Profile(ID, DeducedType, IsDecltypeAuto, IsDependent); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3847 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3848 | return QualType(AT, 0); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3849 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3850 | AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType, |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3851 | IsDecltypeAuto, |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3852 | IsDependent); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3853 | Types.push_back(AT); |
| 3854 | if (InsertPos) |
| 3855 | AutoTypes.InsertNode(AT, InsertPos); |
| 3856 | return QualType(AT, 0); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3859 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 3860 | /// the given value type. |
| 3861 | QualType ASTContext::getAtomicType(QualType T) const { |
| 3862 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3863 | // structure. |
| 3864 | llvm::FoldingSetNodeID ID; |
| 3865 | AtomicType::Profile(ID, T); |
| 3866 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3867 | void *InsertPos = nullptr; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3868 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3869 | return QualType(AT, 0); |
| 3870 | |
| 3871 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 3872 | // either, so fill in the canonical type field. |
| 3873 | QualType Canonical; |
| 3874 | if (!T.isCanonical()) { |
| 3875 | Canonical = getAtomicType(getCanonicalType(T)); |
| 3876 | |
| 3877 | // Get the new insert position for the node we care about. |
| 3878 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3879 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3880 | } |
| 3881 | AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
| 3882 | Types.push_back(New); |
| 3883 | AtomicTypes.InsertNode(New, InsertPos); |
| 3884 | return QualType(New, 0); |
| 3885 | } |
| 3886 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3887 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 3888 | QualType ASTContext::getAutoDeductType() const { |
| 3889 | if (AutoDeductTy.isNull()) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3890 | AutoDeductTy = QualType( |
| 3891 | new (*this, TypeAlignment) AutoType(QualType(), /*decltype(auto)*/false, |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3892 | /*dependent*/false), |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3893 | 0); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 3894 | return AutoDeductTy; |
| 3895 | } |
| 3896 | |
| 3897 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 3898 | QualType ASTContext::getAutoRRefDeductType() const { |
| 3899 | if (AutoRRefDeductTy.isNull()) |
| 3900 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 3901 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 3902 | return AutoRRefDeductTy; |
| 3903 | } |
| 3904 | |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 3905 | /// getTagDeclType - Return the unique reference to the type for the |
| 3906 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3907 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Ted Kremenek | 2b0ce11 | 2007-11-26 21:16:01 +0000 | [diff] [blame] | 3908 | assert (Decl); |
Mike Stump | b93185d | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 3909 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 3910 | // away const? mutable? |
| 3911 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 3912 | } |
| 3913 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3914 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 3915 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 3916 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 3917 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3918 | return getFromTargetType(Target->getSizeType()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 3919 | } |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 3920 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3921 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 3922 | CanQualType ASTContext::getIntMaxType() const { |
| 3923 | return getFromTargetType(Target->getIntMaxType()); |
| 3924 | } |
| 3925 | |
| 3926 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 3927 | CanQualType ASTContext::getUIntMaxType() const { |
| 3928 | return getFromTargetType(Target->getUIntMaxType()); |
| 3929 | } |
| 3930 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3931 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 3932 | /// Used when in C++, as a GCC extension. |
| 3933 | QualType ASTContext::getSignedWCharType() const { |
| 3934 | // FIXME: derive from "Target" ? |
| 3935 | return WCharTy; |
| 3936 | } |
| 3937 | |
| 3938 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 3939 | /// Used when in C++, as a GCC extension. |
| 3940 | QualType ASTContext::getUnsignedWCharType() const { |
| 3941 | // FIXME: derive from "Target" ? |
| 3942 | return UnsignedIntTy; |
| 3943 | } |
| 3944 | |
Enea Zaffanella | f11ceb6 | 2013-01-26 17:08:37 +0000 | [diff] [blame] | 3945 | QualType ASTContext::getIntPtrType() const { |
| 3946 | return getFromTargetType(Target->getIntPtrType()); |
| 3947 | } |
| 3948 | |
| 3949 | QualType ASTContext::getUIntPtrType() const { |
| 3950 | return getCorrespondingUnsignedType(getIntPtrType()); |
| 3951 | } |
| 3952 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 3953 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3954 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 3955 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3956 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 3957 | } |
| 3958 | |
Eli Friedman | 4e91899e | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 3959 | /// \brief Return the unique type for "pid_t" defined in |
| 3960 | /// <sys/types.h>. We need this to compute the correct type for vfork(). |
| 3961 | QualType ASTContext::getProcessIDType() const { |
| 3962 | return getFromTargetType(Target->getProcessIDType()); |
| 3963 | } |
| 3964 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 3965 | //===----------------------------------------------------------------------===// |
| 3966 | // Type Operators |
| 3967 | //===----------------------------------------------------------------------===// |
| 3968 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3969 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3970 | // Push qualifiers into arrays, and then discard any remaining |
| 3971 | // qualifiers. |
| 3972 | T = getCanonicalType(T); |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3973 | T = getVariableArrayDecayedType(T); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3974 | const Type *Ty = T.getTypePtr(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3975 | QualType Result; |
| 3976 | if (isa<ArrayType>(Ty)) { |
| 3977 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 3978 | } else if (isa<FunctionType>(Ty)) { |
| 3979 | Result = getPointerType(QualType(Ty, 0)); |
| 3980 | } else { |
| 3981 | Result = QualType(Ty, 0); |
| 3982 | } |
| 3983 | |
| 3984 | return CanQualType::CreateUnsafe(Result); |
| 3985 | } |
| 3986 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3987 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 3988 | Qualifiers &quals) { |
| 3989 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 3990 | |
| 3991 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 3992 | // the unqualified desugared type and then drops it on the floor. |
| 3993 | // We then have to strip that sugar back off with |
| 3994 | // getUnqualifiedDesugaredType(), which is silly. |
| 3995 | const ArrayType *AT = |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3996 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 3997 | |
| 3998 | // 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] | 3999 | if (!AT) { |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4000 | quals = splitType.Quals; |
| 4001 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4004 | // Otherwise, recurse on the array's element type. |
| 4005 | QualType elementType = AT->getElementType(); |
| 4006 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 4007 | |
| 4008 | // If that didn't change the element type, AT has no qualifiers, so we |
| 4009 | // can just use the results in splitType. |
| 4010 | if (elementType == unqualElementType) { |
| 4011 | assert(quals.empty()); // from the recursive call |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4012 | quals = splitType.Quals; |
| 4013 | return QualType(splitType.Ty, 0); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | // Otherwise, add in the qualifiers from the outermost type, then |
| 4017 | // build the type back up. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4018 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4019 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 4020 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4021 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4022 | CAT->getSizeModifier(), 0); |
| 4023 | } |
| 4024 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 4025 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4026 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4027 | } |
| 4028 | |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 4029 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4030 | return getVariableArrayType(unqualElementType, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4031 | VAT->getSizeExpr(), |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 4032 | VAT->getSizeModifier(), |
| 4033 | VAT->getIndexTypeCVRQualifiers(), |
| 4034 | VAT->getBracketsRange()); |
| 4035 | } |
| 4036 | |
| 4037 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4038 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4039 | DSAT->getSizeModifier(), 0, |
| 4040 | SourceRange()); |
| 4041 | } |
| 4042 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 4043 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 4044 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 4045 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 4046 | /// or pointer-to-member types, or if they are not similar at this |
| 4047 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 4048 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 4049 | /// be called in a loop that successively "unwraps" pointer and |
| 4050 | /// pointer-to-member types to compare them at each level. |
| 4051 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
| 4052 | const PointerType *T1PtrType = T1->getAs<PointerType>(), |
| 4053 | *T2PtrType = T2->getAs<PointerType>(); |
| 4054 | if (T1PtrType && T2PtrType) { |
| 4055 | T1 = T1PtrType->getPointeeType(); |
| 4056 | T2 = T2PtrType->getPointeeType(); |
| 4057 | return true; |
| 4058 | } |
| 4059 | |
| 4060 | const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(), |
| 4061 | *T2MPType = T2->getAs<MemberPointerType>(); |
| 4062 | if (T1MPType && T2MPType && |
| 4063 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 4064 | QualType(T2MPType->getClass(), 0))) { |
| 4065 | T1 = T1MPType->getPointeeType(); |
| 4066 | T2 = T2MPType->getPointeeType(); |
| 4067 | return true; |
| 4068 | } |
| 4069 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4070 | if (getLangOpts().ObjC1) { |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 4071 | const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(), |
| 4072 | *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
| 4073 | if (T1OPType && T2OPType) { |
| 4074 | T1 = T1OPType->getPointeeType(); |
| 4075 | T2 = T2OPType->getPointeeType(); |
| 4076 | return true; |
| 4077 | } |
| 4078 | } |
| 4079 | |
| 4080 | // FIXME: Block pointers, too? |
| 4081 | |
| 4082 | return false; |
| 4083 | } |
| 4084 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4085 | DeclarationNameInfo |
| 4086 | ASTContext::getNameForTemplate(TemplateName Name, |
| 4087 | SourceLocation NameLoc) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4088 | switch (Name.getKind()) { |
| 4089 | case TemplateName::QualifiedTemplate: |
| 4090 | case TemplateName::Template: |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4091 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4092 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 4093 | NameLoc); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4094 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4095 | case TemplateName::OverloadedTemplate: { |
| 4096 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 4097 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 4098 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 4099 | } |
| 4100 | |
| 4101 | case TemplateName::DependentTemplate: { |
| 4102 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4103 | DeclarationName DName; |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 4104 | if (DTN->isIdentifier()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4105 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 4106 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 4107 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4108 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 4109 | // DNInfo work in progress: FIXME: source locations? |
| 4110 | DeclarationNameLoc DNLoc; |
| 4111 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 4112 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 4113 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 4114 | } |
| 4115 | } |
| 4116 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4117 | case TemplateName::SubstTemplateTemplateParm: { |
| 4118 | SubstTemplateTemplateParmStorage *subst |
| 4119 | = Name.getAsSubstTemplateTemplateParm(); |
| 4120 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 4121 | NameLoc); |
| 4122 | } |
| 4123 | |
| 4124 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 4125 | SubstTemplateTemplateParmPackStorage *subst |
| 4126 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 4127 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 4128 | NameLoc); |
| 4129 | } |
| 4130 | } |
| 4131 | |
| 4132 | llvm_unreachable("bad template name kind!"); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4135 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4136 | switch (Name.getKind()) { |
| 4137 | case TemplateName::QualifiedTemplate: |
| 4138 | case TemplateName::Template: { |
| 4139 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 4140 | if (TemplateTemplateParmDecl *TTP |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4141 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 4142 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 4143 | |
| 4144 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 4145 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 4146 | } |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 4147 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4148 | case TemplateName::OverloadedTemplate: |
| 4149 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4150 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 4151 | case TemplateName::DependentTemplate: { |
| 4152 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 4153 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 4154 | return DTN->CanonicalTemplateName; |
| 4155 | } |
| 4156 | |
| 4157 | case TemplateName::SubstTemplateTemplateParm: { |
| 4158 | SubstTemplateTemplateParmStorage *subst |
| 4159 | = Name.getAsSubstTemplateTemplateParm(); |
| 4160 | return getCanonicalTemplateName(subst->getReplacement()); |
| 4161 | } |
| 4162 | |
| 4163 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 4164 | SubstTemplateTemplateParmPackStorage *subst |
| 4165 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 4166 | TemplateTemplateParmDecl *canonParameter |
| 4167 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 4168 | TemplateArgument canonArgPack |
| 4169 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 4170 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 4171 | } |
| 4172 | } |
| 4173 | |
| 4174 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 4177 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 4178 | X = getCanonicalTemplateName(X); |
| 4179 | Y = getCanonicalTemplateName(Y); |
| 4180 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 4181 | } |
| 4182 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4183 | TemplateArgument |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4184 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4185 | switch (Arg.getKind()) { |
| 4186 | case TemplateArgument::Null: |
| 4187 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4188 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4189 | case TemplateArgument::Expression: |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4190 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4191 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4192 | case TemplateArgument::Declaration: { |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4193 | ValueDecl *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl()); |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 4194 | return TemplateArgument(D, Arg.getParamTypeForDecl()); |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4195 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4196 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4197 | case TemplateArgument::NullPtr: |
| 4198 | return TemplateArgument(getCanonicalType(Arg.getNullPtrType()), |
| 4199 | /*isNullPtr*/true); |
| 4200 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4201 | case TemplateArgument::Template: |
| 4202 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4203 | |
| 4204 | case TemplateArgument::TemplateExpansion: |
| 4205 | return TemplateArgument(getCanonicalTemplateName( |
| 4206 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 4207 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4208 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4209 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 4210 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4211 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4212 | case TemplateArgument::Type: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4213 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4214 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4215 | case TemplateArgument::Pack: { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 4216 | if (Arg.pack_size() == 0) |
| 4217 | return Arg; |
| 4218 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4219 | TemplateArgument *CanonArgs |
| 4220 | = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4221 | unsigned Idx = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4222 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4223 | AEnd = Arg.pack_end(); |
| 4224 | A != AEnd; (void)++A, ++Idx) |
| 4225 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4226 | |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 4227 | return TemplateArgument(CanonArgs, Arg.pack_size()); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4228 | } |
| 4229 | } |
| 4230 | |
| 4231 | // Silence GCC warning |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4232 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4235 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4236 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4237 | if (!NNS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4238 | return nullptr; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4239 | |
| 4240 | switch (NNS->getKind()) { |
| 4241 | case NestedNameSpecifier::Identifier: |
| 4242 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4243 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4244 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 4245 | NNS->getAsIdentifier()); |
| 4246 | |
| 4247 | case NestedNameSpecifier::Namespace: |
| 4248 | // A namespace is canonical; build a nested-name-specifier with |
| 4249 | // this namespace and no prefix. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4250 | return NestedNameSpecifier::Create(*this, nullptr, |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4251 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 4252 | |
| 4253 | case NestedNameSpecifier::NamespaceAlias: |
| 4254 | // A namespace is canonical; build a nested-name-specifier with |
| 4255 | // this namespace and no prefix. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4256 | return NestedNameSpecifier::Create(*this, nullptr, |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 4257 | NNS->getAsNamespaceAlias()->getNamespace() |
| 4258 | ->getOriginalNamespace()); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4259 | |
| 4260 | case NestedNameSpecifier::TypeSpec: |
| 4261 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 4262 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4263 | |
| 4264 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 4265 | // break it apart into its prefix and identifier, then reconsititute those |
| 4266 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 4267 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 4268 | // types, e.g., |
| 4269 | // typedef typename T::type T1; |
| 4270 | // typedef typename T1::type T2; |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 4271 | if (const DependentNameType *DNT = T->getAs<DependentNameType>()) |
| 4272 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4273 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 4274 | |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 4275 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 4276 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 4277 | // first place? |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4278 | return NestedNameSpecifier::Create(*this, nullptr, false, |
| 4279 | const_cast<Type *>(T.getTypePtr())); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4280 | } |
| 4281 | |
| 4282 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 4283 | case NestedNameSpecifier::Super: |
| 4284 | // The global specifier and __super specifer are canonical and unique. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4285 | return NNS; |
| 4286 | } |
| 4287 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4288 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4289 | } |
| 4290 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4291 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4292 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4293 | // Handle the non-qualified case efficiently. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4294 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4295 | // Handle the common positive case fast. |
| 4296 | if (const ArrayType *AT = dyn_cast<ArrayType>(T)) |
| 4297 | return AT; |
| 4298 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4299 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4300 | // Handle the common negative case fast. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4301 | if (!isa<ArrayType>(T.getCanonicalType())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4302 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4303 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4304 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4305 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 4306 | // 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] | 4307 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4308 | // If we get here, we either have type qualifiers on the type, or we have |
| 4309 | // 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] | 4310 | // we must propagate them down into the element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4311 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4312 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4313 | Qualifiers qs = split.Quals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4314 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4315 | // If we have a simple case, just return now. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4316 | const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4317 | if (!ATy || qs.empty()) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4318 | return ATy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4319 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4320 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 4321 | // qualifiers into the array element type and return a new array type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4322 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4323 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4324 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy)) |
| 4325 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 4326 | CAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4327 | CAT->getIndexTypeCVRQualifiers())); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4328 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
| 4329 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 4330 | IAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4331 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4332 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4333 | if (const DependentSizedArrayType *DSAT |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4334 | = dyn_cast<DependentSizedArrayType>(ATy)) |
| 4335 | return cast<ArrayType>( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4336 | getDependentSizedArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4337 | DSAT->getSizeExpr(), |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4338 | DSAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4339 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4340 | DSAT->getBracketsRange())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4341 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4342 | const VariableArrayType *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4343 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4344 | VAT->getSizeExpr(), |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4345 | VAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4346 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4347 | VAT->getBracketsRange())); |
Chris Lattner | ed0d079 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4350 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 4351 | if (T->isArrayType() || T->isFunctionType()) |
| 4352 | return getDecayedType(T); |
| 4353 | return T; |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 4356 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 4357 | T = getVariableArrayDecayedType(T); |
| 4358 | T = getAdjustedParameterType(T); |
| 4359 | return T.getUnqualifiedType(); |
| 4360 | } |
| 4361 | |
David Majnemer | d09a51c | 2015-03-03 01:50:05 +0000 | [diff] [blame] | 4362 | QualType ASTContext::getExceptionObjectType(QualType T) const { |
| 4363 | // C++ [except.throw]p3: |
| 4364 | // A throw-expression initializes a temporary object, called the exception |
| 4365 | // object, the type of which is determined by removing any top-level |
| 4366 | // cv-qualifiers from the static type of the operand of throw and adjusting |
| 4367 | // the type from "array of T" or "function returning T" to "pointer to T" |
| 4368 | // or "pointer to function returning T", [...] |
| 4369 | T = getVariableArrayDecayedType(T); |
| 4370 | if (T->isArrayType() || T->isFunctionType()) |
| 4371 | T = getDecayedType(T); |
| 4372 | return T.getUnqualifiedType(); |
| 4373 | } |
| 4374 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4375 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 4376 | /// specified array type to a pointer. This operation is non-trivial when |
| 4377 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 4378 | /// this returns a pointer to a properly qualified element of the array. |
| 4379 | /// |
| 4380 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4381 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4382 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 4383 | // typedefs in the element type of the array. This also handles propagation |
| 4384 | // of type qualifiers from the array type into the element type if present |
| 4385 | // (C99 6.7.3p8). |
| 4386 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 4387 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4388 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4389 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4390 | |
| 4391 | // int x[restrict 4] -> int *restrict |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4392 | return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4395 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 4396 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 4397 | } |
| 4398 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4399 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 4400 | Qualifiers qs; |
| 4401 | while (true) { |
| 4402 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4403 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4404 | if (!array) break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4405 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4406 | type = array->getElementType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4407 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4408 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4409 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 4410 | return getQualifiedType(type, qs); |
Anders Carlsson | e0808df | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 4411 | } |
| 4412 | |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4413 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4414 | uint64_t |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4415 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 4416 | uint64_t ElementCount = 1; |
| 4417 | do { |
| 4418 | ElementCount *= CA->getSize().getZExtValue(); |
Richard Smith | 7808c6a | 2012-12-06 03:04:50 +0000 | [diff] [blame] | 4419 | CA = dyn_cast_or_null<ConstantArrayType>( |
| 4420 | CA->getElementType()->getAsArrayTypeUnsafe()); |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 4421 | } while (CA); |
| 4422 | return ElementCount; |
| 4423 | } |
| 4424 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 4425 | /// getFloatingRank - Return a relative rank for floating point types. |
| 4426 | /// 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] | 4427 | static FloatingRank getFloatingRank(QualType T) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4428 | if (const ComplexType *CT = T->getAs<ComplexType>()) |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 4429 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4430 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4431 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 4432 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4433 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4434 | case BuiltinType::Half: return HalfRank; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 4435 | case BuiltinType::Float: return FloatRank; |
| 4436 | case BuiltinType::Double: return DoubleRank; |
| 4437 | case BuiltinType::LongDouble: return LongDoubleRank; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 4438 | } |
| 4439 | } |
| 4440 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4441 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 4442 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | fc6ffa2 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 4443 | /// 'typeDomain' is a real floating point or complex type. |
| 4444 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4445 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 4446 | QualType Domain) const { |
| 4447 | FloatingRank EltRank = getFloatingRank(Size); |
| 4448 | if (Domain->isComplexType()) { |
| 4449 | switch (EltRank) { |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 4450 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 4451 | case FloatRank: return FloatComplexTy; |
| 4452 | case DoubleRank: return DoubleComplexTy; |
| 4453 | case LongDoubleRank: return LongDoubleComplexTy; |
| 4454 | } |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 4455 | } |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4456 | |
| 4457 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 4458 | switch (EltRank) { |
Joey Gouly | dd7f456 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 4459 | case HalfRank: return HalfTy; |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 4460 | case FloatRank: return FloatTy; |
| 4461 | case DoubleRank: return DoubleTy; |
| 4462 | case LongDoubleRank: return LongDoubleTy; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 4463 | } |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 4464 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 4465 | } |
| 4466 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4467 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 4468 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 4469 | /// '_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] | 4470 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4471 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4472 | FloatingRank LHSR = getFloatingRank(LHS); |
| 4473 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4474 | |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4475 | if (LHSR == RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 4476 | return 0; |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 4477 | if (LHSR > RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 4478 | return 1; |
| 4479 | return -1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4482 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 4483 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 4484 | /// or if it is not canonicalized. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4485 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4486 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4487 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4488 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4489 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4490 | case BuiltinType::Bool: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4491 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4492 | case BuiltinType::Char_S: |
| 4493 | case BuiltinType::Char_U: |
| 4494 | case BuiltinType::SChar: |
| 4495 | case BuiltinType::UChar: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4496 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4497 | case BuiltinType::Short: |
| 4498 | case BuiltinType::UShort: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4499 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4500 | case BuiltinType::Int: |
| 4501 | case BuiltinType::UInt: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4502 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4503 | case BuiltinType::Long: |
| 4504 | case BuiltinType::ULong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4505 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4506 | case BuiltinType::LongLong: |
| 4507 | case BuiltinType::ULongLong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 4508 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 4509 | case BuiltinType::Int128: |
| 4510 | case BuiltinType::UInt128: |
| 4511 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4512 | } |
| 4513 | } |
| 4514 | |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4515 | /// \brief Whether this is a promotable bitfield reference according |
| 4516 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 4517 | /// |
| 4518 | /// \returns the type this bit-field will promote to, or NULL if no |
| 4519 | /// promotion occurs. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4520 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | e05d3cb | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 4521 | if (E->isTypeDependent() || E->isValueDependent()) |
| 4522 | return QualType(); |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 4523 | |
| 4524 | // FIXME: We should not do this unless E->refersToBitField() is true. This |
| 4525 | // matters in C where getSourceBitField() will find bit-fields for various |
| 4526 | // cases where the source expression is not a bit-field designator. |
| 4527 | |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 4528 | FieldDecl *Field = E->getSourceBitField(); // FIXME: conditional bit-fields? |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4529 | if (!Field) |
| 4530 | return QualType(); |
| 4531 | |
| 4532 | QualType FT = Field->getType(); |
| 4533 | |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4534 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4535 | uint64_t IntSize = getTypeSize(IntTy); |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 4536 | // C++ [conv.prom]p5: |
| 4537 | // A prvalue for an integral bit-field can be converted to a prvalue of type |
| 4538 | // int if int can represent all the values of the bit-field; otherwise, it |
| 4539 | // can be converted to unsigned int if unsigned int can represent all the |
| 4540 | // values of the bit-field. If the bit-field is larger yet, no integral |
| 4541 | // promotion applies to it. |
| 4542 | // C11 6.3.1.1/2: |
| 4543 | // [For a bit-field of type _Bool, int, signed int, or unsigned int:] |
| 4544 | // If an int can represent all values of the original type (as restricted by |
| 4545 | // the width, for a bit-field), the value is converted to an int; otherwise, |
| 4546 | // it is converted to an unsigned int. |
| 4547 | // |
| 4548 | // FIXME: C does not permit promotion of a 'long : 3' bitfield to int. |
| 4549 | // We perform that promotion here to match GCC and C++. |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4550 | if (BitWidth < IntSize) |
| 4551 | return IntTy; |
| 4552 | |
| 4553 | if (BitWidth == IntSize) |
| 4554 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 4555 | |
| 4556 | // Types bigger than int are not subject to promotions, and therefore act |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 4557 | // like the base type. GCC has some weird bugs in this area that we |
| 4558 | // deliberately do not follow (GCC follows a pre-standard resolution to |
| 4559 | // C's DR315 which treats bit-width as being part of the type, and this leaks |
| 4560 | // into their semantics in some cases). |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4561 | return QualType(); |
| 4562 | } |
| 4563 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4564 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 4565 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 4566 | /// integer type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4567 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4568 | assert(!Promotable.isNull()); |
| 4569 | assert(Promotable->isPromotableIntegerType()); |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 4570 | if (const EnumType *ET = Promotable->getAs<EnumType>()) |
| 4571 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 4572 | |
| 4573 | if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) { |
| 4574 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 4575 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 4576 | // types that can represent all the values of its underlying type: |
| 4577 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 4578 | // unsigned long long int [...] |
| 4579 | // FIXME: Is there some better way to compute this? |
| 4580 | if (BT->getKind() == BuiltinType::WChar_S || |
| 4581 | BT->getKind() == BuiltinType::WChar_U || |
| 4582 | BT->getKind() == BuiltinType::Char16 || |
| 4583 | BT->getKind() == BuiltinType::Char32) { |
| 4584 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 4585 | uint64_t FromSize = getTypeSize(BT); |
| 4586 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 4587 | LongLongTy, UnsignedLongLongTy }; |
| 4588 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 4589 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 4590 | if (FromSize < ToSize || |
| 4591 | (FromSize == ToSize && |
| 4592 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 4593 | return PromoteTypes[Idx]; |
| 4594 | } |
| 4595 | llvm_unreachable("char type should fit into long long"); |
| 4596 | } |
| 4597 | } |
| 4598 | |
| 4599 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4600 | if (Promotable->isSignedIntegerType()) |
| 4601 | return IntTy; |
Eli Friedman | 6745c3b | 2012-11-15 01:21:59 +0000 | [diff] [blame] | 4602 | uint64_t PromotableSize = getIntWidth(Promotable); |
| 4603 | uint64_t IntSize = getIntWidth(IntTy); |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 4604 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 4605 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 4606 | } |
| 4607 | |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 4608 | /// \brief Recurses in pointer/array types until it finds an objc retainable |
| 4609 | /// type and returns its ownership. |
| 4610 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 4611 | while (!T.isNull()) { |
| 4612 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 4613 | return T.getObjCLifetime(); |
| 4614 | if (T->isArrayType()) |
| 4615 | T = getBaseElementType(T); |
| 4616 | else if (const PointerType *PT = T->getAs<PointerType>()) |
| 4617 | T = PT->getPointeeType(); |
| 4618 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 8e25253 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 4619 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 4620 | else |
| 4621 | break; |
| 4622 | } |
| 4623 | |
| 4624 | return Qualifiers::OCL_None; |
| 4625 | } |
| 4626 | |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 4627 | static const Type *getIntegerTypeForEnum(const EnumType *ET) { |
| 4628 | // Incomplete enum types are not treated as integer types. |
| 4629 | // FIXME: In C++, enum types are never integer types. |
| 4630 | if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped()) |
| 4631 | return ET->getDecl()->getIntegerType().getTypePtr(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4632 | return nullptr; |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 4633 | } |
| 4634 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4635 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4636 | /// 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] | 4637 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4638 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4639 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 4640 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 4641 | |
| 4642 | // Unwrap enums to their underlying type. |
| 4643 | if (const EnumType *ET = dyn_cast<EnumType>(LHSC)) |
| 4644 | LHSC = getIntegerTypeForEnum(ET); |
| 4645 | if (const EnumType *ET = dyn_cast<EnumType>(RHSC)) |
| 4646 | RHSC = getIntegerTypeForEnum(ET); |
| 4647 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4648 | if (LHSC == RHSC) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4649 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4650 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 4651 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4652 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4653 | unsigned LHSRank = getIntegerRank(LHSC); |
| 4654 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4655 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4656 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 4657 | if (LHSRank == RHSRank) return 0; |
| 4658 | return LHSRank > RHSRank ? 1 : -1; |
| 4659 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4660 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4661 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 4662 | if (LHSUnsigned) { |
| 4663 | // If the unsigned [LHS] type is larger, return it. |
| 4664 | if (LHSRank >= RHSRank) |
| 4665 | return 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4666 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4667 | // If the signed type can represent all values of the unsigned type, it |
| 4668 | // 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] | 4669 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4670 | return -1; |
| 4671 | } |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4672 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4673 | // If the unsigned [RHS] type is larger, return it. |
| 4674 | if (RHSRank >= LHSRank) |
| 4675 | return -1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4676 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4677 | // If the signed type can represent all values of the unsigned type, it |
| 4678 | // 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] | 4679 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 4680 | return 1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 4681 | } |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4682 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4683 | // getCFConstantStringType - Return the type used for constant CFStrings. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4684 | QualType ASTContext::getCFConstantStringType() const { |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4685 | if (!CFConstantStringTypeDecl) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4686 | CFConstantStringTypeDecl = buildImplicitRecord("NSConstantString"); |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4687 | CFConstantStringTypeDecl->startDefinition(); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 4688 | |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 4689 | QualType FieldTypes[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4690 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4691 | // const int *isa; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4692 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 4693 | // int flags; |
| 4694 | FieldTypes[1] = IntTy; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4695 | // const char *str; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4696 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4697 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4698 | FieldTypes[3] = LongTy; |
| 4699 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4700 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4701 | for (unsigned i = 0; i < 4; ++i) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4702 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4703 | SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4704 | SourceLocation(), nullptr, |
| 4705 | FieldTypes[i], /*TInfo=*/nullptr, |
| 4706 | /*BitWidth=*/nullptr, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4707 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4708 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4709 | Field->setAccess(AS_public); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4710 | CFConstantStringTypeDecl->addDecl(Field); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 4711 | } |
| 4712 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4713 | CFConstantStringTypeDecl->completeDefinition(); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4714 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4715 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 4716 | return getTagDeclType(CFConstantStringTypeDecl); |
Gabor Greif | 412af03 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 4717 | } |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 4718 | |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 4719 | QualType ASTContext::getObjCSuperType() const { |
| 4720 | if (ObjCSuperType.isNull()) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4721 | RecordDecl *ObjCSuperTypeDecl = buildImplicitRecord("objc_super"); |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 4722 | TUDecl->addDecl(ObjCSuperTypeDecl); |
| 4723 | ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); |
| 4724 | } |
| 4725 | return ObjCSuperType; |
| 4726 | } |
| 4727 | |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4728 | void ASTContext::setCFConstantStringType(QualType T) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4729 | const RecordType *Rec = T->getAs<RecordType>(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 4730 | assert(Rec && "Invalid CFConstantStringType"); |
| 4731 | CFConstantStringTypeDecl = Rec->getDecl(); |
| 4732 | } |
| 4733 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4734 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4735 | if (BlockDescriptorType) |
| 4736 | return getTagDeclType(BlockDescriptorType); |
| 4737 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4738 | RecordDecl *RD; |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4739 | // FIXME: Needs the FlagAppleBlock bit. |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4740 | RD = buildImplicitRecord("__block_descriptor"); |
| 4741 | RD->startDefinition(); |
| 4742 | |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4743 | QualType FieldTypes[] = { |
| 4744 | UnsignedLongTy, |
| 4745 | UnsignedLongTy, |
| 4746 | }; |
| 4747 | |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 4748 | static const char *const FieldNames[] = { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4749 | "reserved", |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4750 | "Size" |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4751 | }; |
| 4752 | |
| 4753 | for (size_t i = 0; i < 2; ++i) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4754 | FieldDecl *Field = FieldDecl::Create( |
| 4755 | *this, RD, SourceLocation(), SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4756 | &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
| 4757 | /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4758 | Field->setAccess(AS_public); |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4759 | RD->addDecl(Field); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4760 | } |
| 4761 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4762 | RD->completeDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4763 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4764 | BlockDescriptorType = RD; |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 4765 | |
| 4766 | return getTagDeclType(BlockDescriptorType); |
| 4767 | } |
| 4768 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4769 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4770 | if (BlockDescriptorExtendedType) |
| 4771 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4772 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4773 | RecordDecl *RD; |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4774 | // FIXME: Needs the FlagAppleBlock bit. |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4775 | RD = buildImplicitRecord("__block_descriptor_withcopydispose"); |
| 4776 | RD->startDefinition(); |
| 4777 | |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4778 | QualType FieldTypes[] = { |
| 4779 | UnsignedLongTy, |
| 4780 | UnsignedLongTy, |
| 4781 | getPointerType(VoidPtrTy), |
| 4782 | getPointerType(VoidPtrTy) |
| 4783 | }; |
| 4784 | |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 4785 | static const char *const FieldNames[] = { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4786 | "reserved", |
| 4787 | "Size", |
| 4788 | "CopyFuncPtr", |
| 4789 | "DestroyFuncPtr" |
| 4790 | }; |
| 4791 | |
| 4792 | for (size_t i = 0; i < 4; ++i) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4793 | FieldDecl *Field = FieldDecl::Create( |
| 4794 | *this, RD, SourceLocation(), SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4795 | &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
| 4796 | /*BitWidth=*/nullptr, |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4797 | /*Mutable=*/false, ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 4798 | Field->setAccess(AS_public); |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4799 | RD->addDecl(Field); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4800 | } |
| 4801 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4802 | RD->completeDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4803 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 4804 | BlockDescriptorExtendedType = RD; |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 4805 | return getTagDeclType(BlockDescriptorExtendedType); |
| 4806 | } |
| 4807 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4808 | /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty" |
| 4809 | /// requires copy/dispose. Note that this must match the logic |
| 4810 | /// in buildByrefHelpers. |
| 4811 | bool ASTContext::BlockRequiresCopying(QualType Ty, |
| 4812 | const VarDecl *D) { |
| 4813 | if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) { |
| 4814 | const Expr *copyExpr = getBlockVarCopyInits(D); |
| 4815 | if (!copyExpr && record->hasTrivialDestructor()) return false; |
| 4816 | |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4817 | return true; |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 4818 | } |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 4819 | |
| 4820 | if (!Ty->isObjCRetainableType()) return false; |
| 4821 | |
| 4822 | Qualifiers qs = Ty.getQualifiers(); |
| 4823 | |
| 4824 | // If we have lifetime, that dominates. |
| 4825 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
| 4826 | assert(getLangOpts().ObjCAutoRefCount); |
| 4827 | |
| 4828 | switch (lifetime) { |
| 4829 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
| 4830 | |
| 4831 | // These are just bits as far as the runtime is concerned. |
| 4832 | case Qualifiers::OCL_ExplicitNone: |
| 4833 | case Qualifiers::OCL_Autoreleasing: |
| 4834 | return false; |
| 4835 | |
| 4836 | // Tell the runtime that this is ARC __weak, called by the |
| 4837 | // byref routines. |
| 4838 | case Qualifiers::OCL_Weak: |
| 4839 | // ARC __strong __block variables need to be retained. |
| 4840 | case Qualifiers::OCL_Strong: |
| 4841 | return true; |
| 4842 | } |
| 4843 | llvm_unreachable("fell out of lifetime switch!"); |
| 4844 | } |
| 4845 | return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) || |
| 4846 | Ty->isObjCObjectPointerType()); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 4847 | } |
| 4848 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4849 | bool ASTContext::getByrefLifetime(QualType Ty, |
| 4850 | Qualifiers::ObjCLifetime &LifeTime, |
| 4851 | bool &HasByrefExtendedLayout) const { |
| 4852 | |
| 4853 | if (!getLangOpts().ObjC1 || |
| 4854 | getLangOpts().getGC() != LangOptions::NonGC) |
| 4855 | return false; |
| 4856 | |
| 4857 | HasByrefExtendedLayout = false; |
Fariborz Jahanian | f762b72 | 2012-12-11 19:58:01 +0000 | [diff] [blame] | 4858 | if (Ty->isRecordType()) { |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 4859 | HasByrefExtendedLayout = true; |
| 4860 | LifeTime = Qualifiers::OCL_None; |
| 4861 | } |
| 4862 | else if (getLangOpts().ObjCAutoRefCount) |
| 4863 | LifeTime = Ty.getObjCLifetime(); |
| 4864 | // MRR. |
| 4865 | else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 4866 | LifeTime = Qualifiers::OCL_ExplicitNone; |
| 4867 | else |
| 4868 | LifeTime = Qualifiers::OCL_None; |
| 4869 | return true; |
| 4870 | } |
| 4871 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4872 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 4873 | if (!ObjCInstanceTypeDecl) |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 4874 | ObjCInstanceTypeDecl = |
| 4875 | buildImplicitTypedef(getObjCIdType(), "instancetype"); |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 4876 | return ObjCInstanceTypeDecl; |
| 4877 | } |
| 4878 | |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4879 | // This returns true if a type has been typedefed to BOOL: |
| 4880 | // typedef <type> BOOL; |
Chris Lattner | e021899 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 4881 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 4882 | if (const TypedefType *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 9b1f279 | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 4883 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 4884 | return II->isStr("BOOL"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4885 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 4886 | return false; |
| 4887 | } |
| 4888 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 4889 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4890 | /// purpose. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4891 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4892 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 4893 | return CharUnits::Zero(); |
| 4894 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4895 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4896 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4897 | // 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] | 4898 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4899 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4900 | // Treat arrays as pointers, since that's how they're passed in. |
| 4901 | else if (type->isArrayType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4902 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4903 | return sz; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 4906 | bool ASTContext::isMSStaticDataMemberInlineDefinition(const VarDecl *VD) const { |
| 4907 | return getLangOpts().MSVCCompat && VD->isStaticDataMember() && |
| 4908 | VD->getType()->isIntegralOrEnumerationType() && |
| 4909 | !VD->getFirstDecl()->isOutOfLine() && VD->getFirstDecl()->hasInit(); |
| 4910 | } |
| 4911 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4912 | static inline |
| 4913 | std::string charUnitsToString(const CharUnits &CU) { |
| 4914 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 4915 | } |
| 4916 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4917 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4918 | /// declaration. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4919 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 4920 | std::string S; |
| 4921 | |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4922 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 4923 | QualType BlockTy = |
| 4924 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 4925 | // Encode result type. |
Fariborz Jahanian | 0e3043b | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 4926 | if (getLangOpts().EncodeExtendedBlockSig) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4927 | getObjCEncodingForMethodParameter( |
| 4928 | Decl::OBJC_TQ_None, BlockTy->getAs<FunctionType>()->getReturnType(), S, |
| 4929 | true /*Extended*/); |
Fariborz Jahanian | 64223e6 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 4930 | else |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4931 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getReturnType(), S); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4932 | // Compute size of all parameters. |
| 4933 | // Start with computing size of a pointer in number of bytes. |
| 4934 | // FIXME: There might(should) be a better way of doing this computation! |
| 4935 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4936 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 4937 | CharUnits ParmOffset = PtrSize; |
Aaron Ballman | b2b8b1d | 2014-03-07 16:09:59 +0000 | [diff] [blame] | 4938 | for (auto PI : Decl->params()) { |
| 4939 | QualType PType = PI->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4940 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | d487941 | 2012-06-30 00:48:59 +0000 | [diff] [blame] | 4941 | if (sz.isZero()) |
| 4942 | continue; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4943 | assert (sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4944 | ParmOffset += sz; |
| 4945 | } |
| 4946 | // Size of the argument frame |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4947 | S += charUnitsToString(ParmOffset); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4948 | // Block pointer and offset. |
| 4949 | S += "@?0"; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4950 | |
| 4951 | // Argument types. |
| 4952 | ParmOffset = PtrSize; |
Aaron Ballman | b2b8b1d | 2014-03-07 16:09:59 +0000 | [diff] [blame] | 4953 | for (auto PVDecl : Decl->params()) { |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4954 | QualType PType = PVDecl->getOriginalType(); |
| 4955 | if (const ArrayType *AT = |
| 4956 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4957 | // Use array's original type only if it has known number of |
| 4958 | // elements. |
| 4959 | if (!isa<ConstantArrayType>(AT)) |
| 4960 | PType = PVDecl->getType(); |
| 4961 | } else if (PType->isFunctionType()) |
| 4962 | PType = PVDecl->getType(); |
Fariborz Jahanian | 0e3043b | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 4963 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 64223e6 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 4964 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType, |
| 4965 | S, true /*Extended*/); |
| 4966 | else |
| 4967 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 4968 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 4969 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4970 | } |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 4971 | |
| 4972 | return S; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 4973 | } |
| 4974 | |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4975 | bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4976 | std::string& S) { |
| 4977 | // Encode result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4978 | getObjCEncodingForType(Decl->getReturnType(), S); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4979 | CharUnits ParmOffset; |
| 4980 | // Compute size of all parameters. |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 4981 | for (auto PI : Decl->params()) { |
| 4982 | QualType PType = PI->getType(); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4983 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4984 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 4985 | continue; |
| 4986 | |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4987 | assert (sz.isPositive() && |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4988 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4989 | ParmOffset += sz; |
| 4990 | } |
| 4991 | S += charUnitsToString(ParmOffset); |
| 4992 | ParmOffset = CharUnits::Zero(); |
| 4993 | |
| 4994 | // Argument types. |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 4995 | for (auto PVDecl : Decl->params()) { |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 4996 | QualType PType = PVDecl->getOriginalType(); |
| 4997 | if (const ArrayType *AT = |
| 4998 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 4999 | // Use array's original type only if it has known number of |
| 5000 | // elements. |
| 5001 | if (!isa<ConstantArrayType>(AT)) |
| 5002 | PType = PVDecl->getType(); |
| 5003 | } else if (PType->isFunctionType()) |
| 5004 | PType = PVDecl->getType(); |
| 5005 | getObjCEncodingForType(PType, S); |
| 5006 | S += charUnitsToString(ParmOffset); |
| 5007 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 5008 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5009 | |
| 5010 | return false; |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 5011 | } |
| 5012 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5013 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 5014 | /// method parameter or return type. If Extended, include class names and |
| 5015 | /// block object types. |
| 5016 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 5017 | QualType T, std::string& S, |
| 5018 | bool Extended) const { |
| 5019 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 5020 | getObjCEncodingForTypeQualifier(QT, S); |
| 5021 | // Encode parameter type. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5022 | getObjCEncodingForTypeImpl(T, S, true, true, nullptr, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5023 | true /*OutermostType*/, |
| 5024 | false /*EncodingProperty*/, |
| 5025 | false /*StructField*/, |
| 5026 | Extended /*EncodeBlockParameters*/, |
| 5027 | Extended /*EncodeClassNames*/); |
| 5028 | } |
| 5029 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5030 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5031 | /// declaration. |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5032 | bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5033 | std::string& S, |
| 5034 | bool Extended) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5035 | // FIXME: This is not very efficient. |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5036 | // Encode return type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5037 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 5038 | Decl->getReturnType(), S, Extended); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5039 | // Compute size of all parameters. |
| 5040 | // Start with computing size of a pointer in number of bytes. |
| 5041 | // FIXME: There might(should) be a better way of doing this computation! |
| 5042 | SourceLocation Loc; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5043 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5044 | // The first two arguments (self and _cmd) are pointers; account for |
| 5045 | // their size. |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5046 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 5047 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 5048 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 5049 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5050 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5051 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 5052 | continue; |
| 5053 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5054 | assert (sz.isPositive() && |
| 5055 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5056 | ParmOffset += sz; |
| 5057 | } |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5058 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5059 | S += "@0:"; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5060 | S += charUnitsToString(PtrSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5061 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5062 | // Argument types. |
| 5063 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 5064 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 5065 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 5066 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5067 | QualType PType = PVDecl->getOriginalType(); |
Fariborz Jahanian | a0befc0 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 5068 | if (const ArrayType *AT = |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 5069 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
| 5070 | // Use array's original type only if it has known number of |
| 5071 | // elements. |
Steve Naroff | 323827e | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 5072 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 5073 | PType = PVDecl->getType(); |
| 5074 | } else if (PType->isFunctionType()) |
| 5075 | PType = PVDecl->getType(); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5076 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 5077 | PType, S, Extended); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5078 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5079 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5080 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5081 | |
| 5082 | return false; |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 5085 | ObjCPropertyImplDecl * |
| 5086 | ASTContext::getObjCPropertyImplDeclForPropertyDecl( |
| 5087 | const ObjCPropertyDecl *PD, |
| 5088 | const Decl *Container) const { |
| 5089 | if (!Container) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5090 | return nullptr; |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 5091 | if (const ObjCCategoryImplDecl *CID = |
| 5092 | dyn_cast<ObjCCategoryImplDecl>(Container)) { |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 5093 | for (auto *PID : CID->property_impls()) |
| 5094 | if (PID->getPropertyDecl() == PD) |
| 5095 | return PID; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5096 | } else { |
| 5097 | const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container); |
| 5098 | for (auto *PID : OID->property_impls()) |
| 5099 | if (PID->getPropertyDecl() == PD) |
| 5100 | return PID; |
| 5101 | } |
| 5102 | return nullptr; |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 5103 | } |
| 5104 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5105 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 5106 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5107 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 5108 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5109 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 5110 | /// attributes readonly and bycopy are encoded as single characters. The |
| 5111 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 5112 | /// encoded as single characters, followed by an identifier. Property types |
| 5113 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 5114 | /// these attributes are defined by the following enumeration: |
| 5115 | /// @code |
| 5116 | /// enum PropertyAttributes { |
| 5117 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 5118 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 5119 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 5120 | /// kPropertyDynamic = 'D', // property is dynamic |
| 5121 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 5122 | /// kPropertySetter = 'S', // followed by setter selector name |
| 5123 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 8cf6185 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 5124 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 5125 | /// kPropertyWeak = 'W' // 'weak' property |
| 5126 | /// kPropertyStrong = 'P' // property GC'able |
| 5127 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 5128 | /// }; |
| 5129 | /// @endcode |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5130 | void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5131 | const Decl *Container, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5132 | std::string& S) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5133 | // Collect information from the property implementation decl(s). |
| 5134 | bool Dynamic = false; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5135 | ObjCPropertyImplDecl *SynthesizePID = nullptr; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5136 | |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 5137 | if (ObjCPropertyImplDecl *PropertyImpDecl = |
| 5138 | getObjCPropertyImplDeclForPropertyDecl(PD, Container)) { |
| 5139 | if (PropertyImpDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 5140 | Dynamic = true; |
| 5141 | else |
| 5142 | SynthesizePID = PropertyImpDecl; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5143 | } |
| 5144 | |
| 5145 | // FIXME: This is not very efficient. |
| 5146 | S = "T"; |
| 5147 | |
| 5148 | // Encode result type. |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 5149 | // GCC has some special rules regarding encoding of properties which |
| 5150 | // closely resembles encoding of ivars. |
Joe Groff | 98ac7d2 | 2014-07-07 22:25:15 +0000 | [diff] [blame] | 5151 | getObjCEncodingForPropertyType(PD->getType(), S); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5152 | |
| 5153 | if (PD->isReadOnly()) { |
| 5154 | S += ",R"; |
Nico Weber | 4e8626f | 2013-05-08 23:47:40 +0000 | [diff] [blame] | 5155 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) |
| 5156 | S += ",C"; |
| 5157 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) |
| 5158 | S += ",&"; |
Fariborz Jahanian | 33079ee | 2014-04-02 22:49:42 +0000 | [diff] [blame] | 5159 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) |
| 5160 | S += ",W"; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5161 | } else { |
| 5162 | switch (PD->getSetterKind()) { |
| 5163 | case ObjCPropertyDecl::Assign: break; |
| 5164 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5165 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 70a315c | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 5166 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5167 | } |
| 5168 | } |
| 5169 | |
| 5170 | // It really isn't clear at all what this means, since properties |
| 5171 | // are "dynamic by default". |
| 5172 | if (Dynamic) |
| 5173 | S += ",D"; |
| 5174 | |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 5175 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 5176 | S += ",N"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5177 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5178 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 5179 | S += ",G"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5180 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5181 | } |
| 5182 | |
| 5183 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 5184 | S += ",S"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 5185 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5186 | } |
| 5187 | |
| 5188 | if (SynthesizePID) { |
| 5189 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 5190 | S += ",V"; |
Chris Lattner | 1cbaacc | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 5191 | S += OID->getNameAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 5192 | } |
| 5193 | |
| 5194 | // FIXME: OBJCGC: weak & strong |
| 5195 | } |
| 5196 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5197 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5198 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 5199 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5200 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
| 5201 | /// |
| 5202 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 5203 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5204 | if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5205 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5206 | PointeeTy = UnsignedIntTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5207 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5208 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5209 | PointeeTy = IntTy; |
| 5210 | } |
| 5211 | } |
| 5212 | } |
| 5213 | |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5214 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5215 | const FieldDecl *Field, |
| 5216 | QualType *NotEncodedT) const { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 5217 | // We follow the behavior of gcc, expanding structures which are |
| 5218 | // directly pointed to, and expanding embedded structures. Note that |
| 5219 | // these rules are sufficient to prevent recursive encoding of the |
| 5220 | // same type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5221 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5222 | true /* outermost type */, false, false, |
| 5223 | false, false, false, NotEncodedT); |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
Joe Groff | 98ac7d2 | 2014-07-07 22:25:15 +0000 | [diff] [blame] | 5226 | void ASTContext::getObjCEncodingForPropertyType(QualType T, |
| 5227 | std::string& S) const { |
| 5228 | // Encode result type. |
| 5229 | // GCC has some special rules regarding encoding of properties which |
| 5230 | // closely resembles encoding of ivars. |
| 5231 | getObjCEncodingForTypeImpl(T, S, true, true, nullptr, |
| 5232 | true /* outermost type */, |
| 5233 | true /* encoding property */); |
| 5234 | } |
| 5235 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5236 | static char getObjCEncodingForPrimitiveKind(const ASTContext *C, |
| 5237 | BuiltinType::Kind kind) { |
| 5238 | switch (kind) { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5239 | case BuiltinType::Void: return 'v'; |
| 5240 | case BuiltinType::Bool: return 'B'; |
| 5241 | case BuiltinType::Char_U: |
| 5242 | case BuiltinType::UChar: return 'C'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5243 | case BuiltinType::Char16: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5244 | case BuiltinType::UShort: return 'S'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5245 | case BuiltinType::Char32: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5246 | case BuiltinType::UInt: return 'I'; |
| 5247 | case BuiltinType::ULong: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5248 | return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5249 | case BuiltinType::UInt128: return 'T'; |
| 5250 | case BuiltinType::ULongLong: return 'Q'; |
| 5251 | case BuiltinType::Char_S: |
| 5252 | case BuiltinType::SChar: return 'c'; |
| 5253 | case BuiltinType::Short: return 's'; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 5254 | case BuiltinType::WChar_S: |
| 5255 | case BuiltinType::WChar_U: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5256 | case BuiltinType::Int: return 'i'; |
| 5257 | case BuiltinType::Long: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5258 | return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5259 | case BuiltinType::LongLong: return 'q'; |
| 5260 | case BuiltinType::Int128: return 't'; |
| 5261 | case BuiltinType::Float: return 'f'; |
| 5262 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 7cba5a7 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 5263 | case BuiltinType::LongDouble: return 'D'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5264 | case BuiltinType::NullPtr: return '*'; // like char* |
| 5265 | |
| 5266 | case BuiltinType::Half: |
| 5267 | // FIXME: potentially need @encodes for these! |
| 5268 | return ' '; |
| 5269 | |
| 5270 | case BuiltinType::ObjCId: |
| 5271 | case BuiltinType::ObjCClass: |
| 5272 | case BuiltinType::ObjCSel: |
| 5273 | llvm_unreachable("@encoding ObjC primitive type"); |
| 5274 | |
| 5275 | // OpenCL and placeholder types don't need @encodings. |
| 5276 | case BuiltinType::OCLImage1d: |
| 5277 | case BuiltinType::OCLImage1dArray: |
| 5278 | case BuiltinType::OCLImage1dBuffer: |
| 5279 | case BuiltinType::OCLImage2d: |
| 5280 | case BuiltinType::OCLImage2dArray: |
| 5281 | case BuiltinType::OCLImage3d: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5282 | case BuiltinType::OCLEvent: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5283 | case BuiltinType::OCLSampler: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5284 | case BuiltinType::Dependent: |
| 5285 | #define BUILTIN_TYPE(KIND, ID) |
| 5286 | #define PLACEHOLDER_TYPE(KIND, ID) \ |
| 5287 | case BuiltinType::KIND: |
| 5288 | #include "clang/AST/BuiltinTypes.def" |
| 5289 | llvm_unreachable("invalid builtin type for @encode"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5290 | } |
David Blaikie | 5a6a020 | 2013-01-09 17:48:41 +0000 | [diff] [blame] | 5291 | llvm_unreachable("invalid BuiltinType::Kind value"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5292 | } |
| 5293 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5294 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 5295 | EnumDecl *Enum = ET->getDecl(); |
| 5296 | |
| 5297 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 5298 | if (!Enum->isFixed()) |
| 5299 | return 'i'; |
| 5300 | |
| 5301 | // The encoding of a fixed enum type matches its fixed underlying type. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5302 | const BuiltinType *BT = Enum->getIntegerType()->castAs<BuiltinType>(); |
| 5303 | return getObjCEncodingForPrimitiveKind(C, BT->getKind()); |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5304 | } |
| 5305 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5306 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5307 | QualType T, const FieldDecl *FD) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5308 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 5309 | S += 'b'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5310 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 5311 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 5312 | // then the offset (in bits) of the first element, then the type of the |
| 5313 | // bitfield, then the size in bits. For example, in this structure: |
| 5314 | // |
| 5315 | // struct |
| 5316 | // { |
| 5317 | // int integer; |
| 5318 | // int flags:2; |
| 5319 | // }; |
| 5320 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 5321 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 5322 | // information is not especially sensible, but we're stuck with it for |
| 5323 | // compatibility with GCC, although providing it breaks anything that |
| 5324 | // actually uses runtime introspection and wants to work on both runtimes... |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 5325 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5326 | const RecordDecl *RD = FD->getParent(); |
| 5327 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
Eli Friedman | a3c122d | 2011-07-07 01:54:01 +0000 | [diff] [blame] | 5328 | S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex())); |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 5329 | if (const EnumType *ET = T->getAs<EnumType>()) |
| 5330 | S += ObjCEncodingForEnumType(Ctx, ET); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5331 | else { |
| 5332 | const BuiltinType *BT = T->castAs<BuiltinType>(); |
| 5333 | S += getObjCEncodingForPrimitiveKind(Ctx, BT->getKind()); |
| 5334 | } |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5335 | } |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5336 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 5337 | } |
| 5338 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 5339 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 5340 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 5341 | bool ExpandPointedToStructures, |
| 5342 | bool ExpandStructures, |
Daniel Dunbar | c040ce4 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 5343 | const FieldDecl *FD, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 5344 | bool OutermostType, |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5345 | bool EncodingProperty, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5346 | bool StructField, |
| 5347 | bool EncodeBlockParameters, |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5348 | bool EncodeClassNames, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5349 | bool EncodePointerToObjCTypedef, |
| 5350 | QualType *NotEncodedT) const { |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5351 | CanQualType CT = getCanonicalType(T); |
| 5352 | switch (CT->getTypeClass()) { |
| 5353 | case Type::Builtin: |
| 5354 | case Type::Enum: |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5355 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 5356 | return EncodeBitField(this, S, T, FD); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5357 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(CT)) |
| 5358 | S += getObjCEncodingForPrimitiveKind(this, BT->getKind()); |
| 5359 | else |
| 5360 | S += ObjCEncodingForEnumType(this, cast<EnumType>(CT)); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5361 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5362 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5363 | case Type::Complex: { |
| 5364 | const ComplexType *CT = T->castAs<ComplexType>(); |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 5365 | S += 'j'; |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5366 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, nullptr); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5367 | return; |
| 5368 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5369 | |
| 5370 | case Type::Atomic: { |
| 5371 | const AtomicType *AT = T->castAs<AtomicType>(); |
| 5372 | S += 'A'; |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5373 | getObjCEncodingForTypeImpl(AT->getValueType(), S, false, false, nullptr); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5374 | return; |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 5375 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5376 | |
| 5377 | // encoding for pointer or reference types. |
| 5378 | case Type::Pointer: |
| 5379 | case Type::LValueReference: |
| 5380 | case Type::RValueReference: { |
| 5381 | QualType PointeeTy; |
| 5382 | if (isa<PointerType>(CT)) { |
| 5383 | const PointerType *PT = T->castAs<PointerType>(); |
| 5384 | if (PT->isObjCSelType()) { |
| 5385 | S += ':'; |
| 5386 | return; |
| 5387 | } |
| 5388 | PointeeTy = PT->getPointeeType(); |
| 5389 | } else { |
| 5390 | PointeeTy = T->castAs<ReferenceType>()->getPointeeType(); |
| 5391 | } |
| 5392 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5393 | bool isReadOnly = false; |
| 5394 | // For historical/compatibility reasons, the read-only qualifier of the |
| 5395 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 5396 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5397 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 5398 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5399 | if (OutermostType && T.isConstQualified()) { |
| 5400 | isReadOnly = true; |
| 5401 | S += 'r'; |
| 5402 | } |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 5403 | } else if (OutermostType) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5404 | QualType P = PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5405 | while (P->getAs<PointerType>()) |
| 5406 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5407 | if (P.isConstQualified()) { |
| 5408 | isReadOnly = true; |
| 5409 | S += 'r'; |
| 5410 | } |
| 5411 | } |
| 5412 | if (isReadOnly) { |
| 5413 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 5414 | // combinations need to be rearranged. |
| 5415 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5416 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 5417 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5418 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5419 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5420 | if (PointeeTy->isCharType()) { |
| 5421 | // char pointer types should be encoded as '*' unless it is a |
| 5422 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 5423 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5424 | S += '*'; |
| 5425 | return; |
| 5426 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5427 | } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 3de6b70 | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 5428 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 5429 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 5430 | S += '#'; |
| 5431 | return; |
| 5432 | } |
| 5433 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 5434 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 5435 | S += '@'; |
| 5436 | return; |
| 5437 | } |
| 5438 | // fall through... |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5439 | } |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5440 | S += '^'; |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 5441 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 5442 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5443 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5444 | nullptr, false, false, false, false, false, false, |
| 5445 | NotEncodedT); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5446 | return; |
| 5447 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5448 | |
| 5449 | case Type::ConstantArray: |
| 5450 | case Type::IncompleteArray: |
| 5451 | case Type::VariableArray: { |
| 5452 | const ArrayType *AT = cast<ArrayType>(CT); |
| 5453 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5454 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5455 | // Incomplete arrays are encoded as a pointer to the array element. |
| 5456 | S += '^'; |
| 5457 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5458 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5459 | false, ExpandStructures, FD); |
| 5460 | } else { |
| 5461 | S += '['; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5462 | |
Fariborz Jahanian | f0dc11a | 2013-06-04 16:04:37 +0000 | [diff] [blame] | 5463 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) |
| 5464 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 5465 | else { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5466 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5467 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 5468 | "Unknown array type!"); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5469 | S += '0'; |
| 5470 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5471 | |
| 5472 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5473 | false, ExpandStructures, FD, |
| 5474 | false, false, false, false, false, false, |
| 5475 | NotEncodedT); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 5476 | S += ']'; |
| 5477 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5478 | return; |
| 5479 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5480 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5481 | case Type::FunctionNoProto: |
| 5482 | case Type::FunctionProto: |
Anders Carlsson | df4cc61 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 5483 | S += '?'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5484 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5485 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5486 | case Type::Record: { |
| 5487 | RecordDecl *RDecl = cast<RecordType>(CT)->getDecl(); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5488 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 5489 | // Anonymous structures print as '?' |
| 5490 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 5491 | S += II->getName(); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 5492 | if (ClassTemplateSpecializationDecl *Spec |
| 5493 | = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
| 5494 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 5495 | llvm::raw_string_ostream OS(S); |
| 5496 | TemplateSpecializationType::PrintTemplateArgumentList(OS, |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 5497 | TemplateArgs.data(), |
| 5498 | TemplateArgs.size(), |
Douglas Gregor | c0b0728 | 2011-09-27 22:38:19 +0000 | [diff] [blame] | 5499 | (*this).getPrintingPolicy()); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 5500 | } |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 5501 | } else { |
| 5502 | S += '?'; |
| 5503 | } |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 5504 | if (ExpandStructures) { |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5505 | S += '='; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5506 | if (!RDecl->isUnion()) { |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5507 | getObjCEncodingForStructureImpl(RDecl, S, FD, true, NotEncodedT); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5508 | } else { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5509 | for (const auto *Field : RDecl->fields()) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5510 | if (FD) { |
| 5511 | S += '"'; |
| 5512 | S += Field->getNameAsString(); |
| 5513 | S += '"'; |
| 5514 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5515 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5516 | // Special case bit-fields. |
| 5517 | if (Field->isBitField()) { |
| 5518 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 5519 | Field); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5520 | } else { |
| 5521 | QualType qt = Field->getType(); |
| 5522 | getLegacyIntegralTypeEncoding(qt); |
| 5523 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 5524 | FD, /*OutermostType*/false, |
| 5525 | /*EncodingProperty*/false, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5526 | /*StructField*/true, |
| 5527 | false, false, false, NotEncodedT); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5528 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5529 | } |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 5530 | } |
Fariborz Jahanian | bc92fd7 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 5531 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 5532 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5533 | return; |
| 5534 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5535 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5536 | case Type::BlockPointer: { |
| 5537 | const BlockPointerType *BT = T->castAs<BlockPointerType>(); |
Steve Naroff | 49140cb | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 5538 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5539 | if (EncodeBlockParameters) { |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5540 | const FunctionType *FT = BT->getPointeeType()->castAs<FunctionType>(); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5541 | |
| 5542 | S += '<'; |
| 5543 | // Block return type |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5544 | getObjCEncodingForTypeImpl( |
| 5545 | FT->getReturnType(), S, ExpandPointedToStructures, ExpandStructures, |
| 5546 | FD, false /* OutermostType */, EncodingProperty, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5547 | false /* StructField */, EncodeBlockParameters, EncodeClassNames, false, |
| 5548 | NotEncodedT); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5549 | // Block self |
| 5550 | S += "@?"; |
| 5551 | // Block parameters |
| 5552 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 5553 | for (const auto &I : FPT->param_types()) |
| 5554 | getObjCEncodingForTypeImpl( |
| 5555 | I, S, ExpandPointedToStructures, ExpandStructures, FD, |
| 5556 | false /* OutermostType */, EncodingProperty, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5557 | false /* StructField */, EncodeBlockParameters, EncodeClassNames, |
| 5558 | false, NotEncodedT); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5559 | } |
| 5560 | S += '>'; |
| 5561 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5562 | return; |
| 5563 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5564 | |
Fariborz Jahanian | 33fa962 | 2014-01-28 20:41:15 +0000 | [diff] [blame] | 5565 | case Type::ObjCObject: { |
| 5566 | // hack to match legacy encoding of *id and *Class |
| 5567 | QualType Ty = getObjCObjectPointerType(CT); |
| 5568 | if (Ty->isObjCIdType()) { |
| 5569 | S += "{objc_object=}"; |
| 5570 | return; |
| 5571 | } |
| 5572 | else if (Ty->isObjCClassType()) { |
| 5573 | S += "{objc_class=}"; |
| 5574 | return; |
| 5575 | } |
| 5576 | } |
| 5577 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5578 | case Type::ObjCInterface: { |
| 5579 | // Ignore protocol qualifiers when mangling at this level. |
| 5580 | T = T->castAs<ObjCObjectType>()->getBaseType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5581 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5582 | // The assumption seems to be that this assert will succeed |
| 5583 | // because nested levels will have filtered out 'id' and 'Class'. |
| 5584 | const ObjCInterfaceType *OIT = T->castAs<ObjCInterfaceType>(); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5585 | // @encode(class_name) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5586 | ObjCInterfaceDecl *OI = OIT->getDecl(); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5587 | S += '{'; |
| 5588 | const IdentifierInfo *II = OI->getIdentifier(); |
| 5589 | S += II->getName(); |
| 5590 | S += '='; |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5591 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5592 | DeepCollectObjCIvars(OI, true, Ivars); |
| 5593 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5594 | const FieldDecl *Field = cast<FieldDecl>(Ivars[i]); |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 5595 | if (Field->isBitField()) |
| 5596 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5597 | else |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5598 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD, |
| 5599 | false, false, false, false, false, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5600 | EncodePointerToObjCTypedef, |
| 5601 | NotEncodedT); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5602 | } |
| 5603 | S += '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5604 | return; |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 5605 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5606 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5607 | case Type::ObjCObjectPointer: { |
| 5608 | const ObjCObjectPointerType *OPT = T->castAs<ObjCObjectPointerType>(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5609 | if (OPT->isObjCIdType()) { |
| 5610 | S += '@'; |
| 5611 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5612 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5613 | |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 5614 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 5615 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 5616 | // Since this is a binary compatibility issue, need to consult with runtime |
| 5617 | // folks. Fortunately, this is a *very* obsure construct. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5618 | S += '#'; |
| 5619 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5620 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5621 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5622 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5623 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5624 | ExpandPointedToStructures, |
| 5625 | ExpandStructures, FD); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5626 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5627 | // Note that we do extended encoding of protocol qualifer list |
| 5628 | // Only when doing ivar or property encoding. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5629 | S += '"'; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 5630 | for (const auto *I : OPT->quals()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5631 | S += '<'; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 5632 | S += I->getNameAsString(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5633 | S += '>'; |
| 5634 | } |
| 5635 | S += '"'; |
| 5636 | } |
| 5637 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5638 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5639 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5640 | QualType PointeeTy = OPT->getPointeeType(); |
| 5641 | if (!EncodingProperty && |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5642 | isa<TypedefType>(PointeeTy.getTypePtr()) && |
| 5643 | !EncodePointerToObjCTypedef) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5644 | // Another historical/compatibility reason. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5645 | // We encode the underlying type which comes out as |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5646 | // {...}; |
| 5647 | S += '^'; |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 5648 | if (FD && OPT->getInterfaceDecl()) { |
Fariborz Jahanian | c682ef5 | 2013-07-12 16:41:56 +0000 | [diff] [blame] | 5649 | // Prevent recursive encoding of fields in some rare cases. |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 5650 | ObjCInterfaceDecl *OI = OPT->getInterfaceDecl(); |
| 5651 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
| 5652 | DeepCollectObjCIvars(OI, true, Ivars); |
| 5653 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
| 5654 | if (cast<FieldDecl>(Ivars[i]) == FD) { |
| 5655 | S += '{'; |
| 5656 | S += OI->getIdentifier()->getName(); |
| 5657 | S += '}'; |
| 5658 | return; |
| 5659 | } |
| 5660 | } |
| 5661 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5662 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 5663 | false, ExpandPointedToStructures, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5664 | nullptr, |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 5665 | false, false, false, false, false, |
| 5666 | /*EncodePointerToObjCTypedef*/true); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5667 | return; |
| 5668 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5669 | |
| 5670 | S += '@'; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5671 | if (OPT->getInterfaceDecl() && |
| 5672 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5673 | S += '"'; |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 5674 | S += OPT->getInterfaceDecl()->getIdentifier()->getName(); |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 5675 | for (const auto *I : OPT->quals()) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5676 | S += '<'; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 5677 | S += I->getNameAsString(); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5678 | S += '>'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5679 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 5680 | S += '"'; |
| 5681 | } |
| 5682 | return; |
| 5683 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5684 | |
John McCall | a9e6e8d | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 5685 | // gcc just blithely ignores member pointers. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5686 | // FIXME: we shoul do better than that. 'M' is available. |
| 5687 | case Type::MemberPointer: |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5688 | // This matches gcc's encoding, even though technically it is insufficient. |
| 5689 | //FIXME. We should do a better job than gcc. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5690 | case Type::Vector: |
| 5691 | case Type::ExtVector: |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5692 | // Until we have a coherent encoding of these three types, issue warning. |
| 5693 | { if (NotEncodedT) |
| 5694 | *NotEncodedT = T; |
| 5695 | return; |
| 5696 | } |
| 5697 | |
| 5698 | // We could see an undeduced auto type here during error recovery. |
| 5699 | // Just ignore it. |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5700 | case Type::Auto: |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5701 | return; |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5702 | |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 5703 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5704 | #define ABSTRACT_TYPE(KIND, BASE) |
| 5705 | #define TYPE(KIND, BASE) |
| 5706 | #define DEPENDENT_TYPE(KIND, BASE) \ |
| 5707 | case Type::KIND: |
| 5708 | #define NON_CANONICAL_TYPE(KIND, BASE) \ |
| 5709 | case Type::KIND: |
| 5710 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \ |
| 5711 | case Type::KIND: |
| 5712 | #include "clang/AST/TypeNodes.def" |
| 5713 | llvm_unreachable("@encode for dependent type!"); |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 5714 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 5715 | llvm_unreachable("bad type kind!"); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5716 | } |
| 5717 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5718 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 5719 | std::string &S, |
| 5720 | const FieldDecl *FD, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5721 | bool includeVBases, |
| 5722 | QualType *NotEncodedT) const { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5723 | assert(RDecl && "Expected non-null RecordDecl"); |
| 5724 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
| 5725 | if (!RDecl->getDefinition()) |
| 5726 | return; |
| 5727 | |
| 5728 | CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
| 5729 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 5730 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 5731 | |
| 5732 | if (CXXRec) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 5733 | for (const auto &BI : CXXRec->bases()) { |
| 5734 | if (!BI.isVirtual()) { |
| 5735 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5736 | if (base->isEmpty()) |
| 5737 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 5738 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5739 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 5740 | std::make_pair(offs, base)); |
| 5741 | } |
| 5742 | } |
| 5743 | } |
| 5744 | |
| 5745 | unsigned i = 0; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 5746 | for (auto *Field : RDecl->fields()) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5747 | uint64_t offs = layout.getFieldOffset(i); |
| 5748 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 5749 | std::make_pair(offs, Field)); |
| 5750 | ++i; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5751 | } |
| 5752 | |
| 5753 | if (CXXRec && includeVBases) { |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 5754 | for (const auto &BI : CXXRec->vbases()) { |
| 5755 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5756 | if (base->isEmpty()) |
| 5757 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 5758 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); |
Eli Friedman | 1d24af8 | 2013-09-18 01:59:16 +0000 | [diff] [blame] | 5759 | if (offs >= uint64_t(toBits(layout.getNonVirtualSize())) && |
| 5760 | FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
Argyrios Kyrtzidis | 81c0b5c | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 5761 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 5762 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5763 | } |
| 5764 | } |
| 5765 | |
| 5766 | CharUnits size; |
| 5767 | if (CXXRec) { |
| 5768 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 5769 | } else { |
| 5770 | size = layout.getSize(); |
| 5771 | } |
| 5772 | |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5773 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5774 | uint64_t CurOffs = 0; |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5775 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5776 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 5777 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 5778 | |
Douglas Gregor | f5d6c74 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 5779 | if (CXXRec && CXXRec->isDynamicClass() && |
| 5780 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5781 | if (FD) { |
| 5782 | S += "\"_vptr$"; |
| 5783 | std::string recname = CXXRec->getNameAsString(); |
| 5784 | if (recname.empty()) recname = "?"; |
| 5785 | S += recname; |
| 5786 | S += '"'; |
| 5787 | } |
| 5788 | S += "^^?"; |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5789 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5790 | CurOffs += getTypeSize(VoidPtrTy); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5791 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5792 | } |
| 5793 | |
| 5794 | if (!RDecl->hasFlexibleArrayMember()) { |
| 5795 | // Mark the end of the structure. |
| 5796 | uint64_t offs = toBits(size); |
| 5797 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5798 | std::make_pair(offs, nullptr)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5799 | } |
| 5800 | |
| 5801 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5802 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5803 | assert(CurOffs <= CurLayObj->first); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5804 | if (CurOffs < CurLayObj->first) { |
| 5805 | uint64_t padding = CurLayObj->first - CurOffs; |
| 5806 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 5807 | // packing/alignment of members is different that normal, in which case |
| 5808 | // the encoding will be out-of-sync with the real layout. |
| 5809 | // If the runtime switches to just consider the size of types without |
| 5810 | // taking into account alignment, we could make padding explicit in the |
| 5811 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 5812 | // longer then though. |
| 5813 | CurOffs += padding; |
| 5814 | } |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5815 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5816 | |
| 5817 | NamedDecl *dcl = CurLayObj->second; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5818 | if (!dcl) |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5819 | break; // reached end of structure. |
| 5820 | |
| 5821 | if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) { |
| 5822 | // We expand the bases without their virtual bases since those are going |
| 5823 | // in the initial structure. Note that this differs from gcc which |
| 5824 | // expands virtual bases each time one is encountered in the hierarchy, |
| 5825 | // making the encoding type bigger than it really is. |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5826 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false, |
| 5827 | NotEncodedT); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5828 | assert(!base->isEmpty()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5829 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 5830 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5831 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5832 | } else { |
| 5833 | FieldDecl *field = cast<FieldDecl>(dcl); |
| 5834 | if (FD) { |
| 5835 | S += '"'; |
| 5836 | S += field->getNameAsString(); |
| 5837 | S += '"'; |
| 5838 | } |
| 5839 | |
| 5840 | if (field->isBitField()) { |
| 5841 | EncodeBitField(this, S, field->getType(), field); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5842 | #ifndef NDEBUG |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5843 | CurOffs += field->getBitWidthValue(*this); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5844 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5845 | } else { |
| 5846 | QualType qt = field->getType(); |
| 5847 | getLegacyIntegralTypeEncoding(qt); |
| 5848 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 5849 | /*OutermostType*/false, |
| 5850 | /*EncodingProperty*/false, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 5851 | /*StructField*/true, |
| 5852 | false, false, false, NotEncodedT); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5853 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5854 | CurOffs += getTypeSize(field->getType()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 5855 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 5856 | } |
| 5857 | } |
| 5858 | } |
| 5859 | } |
| 5860 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5861 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 5862 | std::string& S) const { |
| 5863 | if (QT & Decl::OBJC_TQ_In) |
| 5864 | S += 'n'; |
| 5865 | if (QT & Decl::OBJC_TQ_Inout) |
| 5866 | S += 'N'; |
| 5867 | if (QT & Decl::OBJC_TQ_Out) |
| 5868 | S += 'o'; |
| 5869 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 5870 | S += 'O'; |
| 5871 | if (QT & Decl::OBJC_TQ_Byref) |
| 5872 | S += 'R'; |
| 5873 | if (QT & Decl::OBJC_TQ_Oneway) |
| 5874 | S += 'V'; |
| 5875 | } |
| 5876 | |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 5877 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 5878 | if (!ObjCIdDecl) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5879 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, nullptr, 0); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 5880 | T = getObjCObjectPointerType(T); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 5881 | ObjCIdDecl = buildImplicitTypedef(T, "id"); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 5882 | } |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 5883 | return ObjCIdDecl; |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 5884 | } |
| 5885 | |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 5886 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 5887 | if (!ObjCSelDecl) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 5888 | QualType T = getPointerType(ObjCBuiltinSelTy); |
| 5889 | ObjCSelDecl = buildImplicitTypedef(T, "SEL"); |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 5890 | } |
| 5891 | return ObjCSelDecl; |
Fariborz Jahanian | 4bef462 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 5892 | } |
| 5893 | |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 5894 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 5895 | if (!ObjCClassDecl) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5896 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, nullptr, 0); |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 5897 | T = getObjCObjectPointerType(T); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 5898 | ObjCClassDecl = buildImplicitTypedef(T, "Class"); |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 5899 | } |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 5900 | return ObjCClassDecl; |
Anders Carlsson | f56a7ae | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 5901 | } |
| 5902 | |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 5903 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 5904 | if (!ObjCProtocolClassDecl) { |
| 5905 | ObjCProtocolClassDecl |
| 5906 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 5907 | SourceLocation(), |
| 5908 | &Idents.get("Protocol"), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5909 | /*PrevDecl=*/nullptr, |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 5910 | SourceLocation(), true); |
| 5911 | } |
| 5912 | |
| 5913 | return ObjCProtocolClassDecl; |
| 5914 | } |
| 5915 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5916 | //===----------------------------------------------------------------------===// |
| 5917 | // __builtin_va_list Construction Functions |
| 5918 | //===----------------------------------------------------------------------===// |
| 5919 | |
| 5920 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5921 | // typedef char* __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 5922 | QualType T = Context->getPointerType(Context->CharTy); |
| 5923 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5924 | } |
| 5925 | |
| 5926 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 5927 | // typedef void* __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 5928 | QualType T = Context->getPointerType(Context->VoidTy); |
| 5929 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5930 | } |
| 5931 | |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5932 | static TypedefDecl * |
| 5933 | CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 5934 | // struct __va_list |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5935 | RecordDecl *VaListTagDecl = Context->buildImplicitRecord("__va_list"); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5936 | if (Context->getLangOpts().CPlusPlus) { |
| 5937 | // namespace std { struct __va_list { |
| 5938 | NamespaceDecl *NS; |
| 5939 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 5940 | Context->getTranslationUnitDecl(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5941 | /*Inline*/ false, SourceLocation(), |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5942 | SourceLocation(), &Context->Idents.get("std"), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5943 | /*PrevDecl*/ nullptr); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 5944 | NS->setImplicit(); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5945 | VaListTagDecl->setDeclContext(NS); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5946 | } |
| 5947 | |
| 5948 | VaListTagDecl->startDefinition(); |
| 5949 | |
| 5950 | const size_t NumFields = 5; |
| 5951 | QualType FieldTypes[NumFields]; |
| 5952 | const char *FieldNames[NumFields]; |
| 5953 | |
| 5954 | // void *__stack; |
| 5955 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); |
| 5956 | FieldNames[0] = "__stack"; |
| 5957 | |
| 5958 | // void *__gr_top; |
| 5959 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); |
| 5960 | FieldNames[1] = "__gr_top"; |
| 5961 | |
| 5962 | // void *__vr_top; |
| 5963 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 5964 | FieldNames[2] = "__vr_top"; |
| 5965 | |
| 5966 | // int __gr_offs; |
| 5967 | FieldTypes[3] = Context->IntTy; |
| 5968 | FieldNames[3] = "__gr_offs"; |
| 5969 | |
| 5970 | // int __vr_offs; |
| 5971 | FieldTypes[4] = Context->IntTy; |
| 5972 | FieldNames[4] = "__vr_offs"; |
| 5973 | |
| 5974 | // Create fields |
| 5975 | for (unsigned i = 0; i < NumFields; ++i) { |
| 5976 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 5977 | VaListTagDecl, |
| 5978 | SourceLocation(), |
| 5979 | SourceLocation(), |
| 5980 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5981 | FieldTypes[i], /*TInfo=*/nullptr, |
| 5982 | /*BitWidth=*/nullptr, |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5983 | /*Mutable=*/false, |
| 5984 | ICIS_NoInit); |
| 5985 | Field->setAccess(AS_public); |
| 5986 | VaListTagDecl->addDecl(Field); |
| 5987 | } |
| 5988 | VaListTagDecl->completeDefinition(); |
| 5989 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 5990 | Context->VaListTagTy = VaListTagType; |
| 5991 | |
| 5992 | // } __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 5993 | return Context->buildImplicitTypedef(VaListTagType, "__builtin_va_list"); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 5994 | } |
| 5995 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 5996 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
| 5997 | // typedef struct __va_list_tag { |
| 5998 | RecordDecl *VaListTagDecl; |
| 5999 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6000 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6001 | VaListTagDecl->startDefinition(); |
| 6002 | |
| 6003 | const size_t NumFields = 5; |
| 6004 | QualType FieldTypes[NumFields]; |
| 6005 | const char *FieldNames[NumFields]; |
| 6006 | |
| 6007 | // unsigned char gpr; |
| 6008 | FieldTypes[0] = Context->UnsignedCharTy; |
| 6009 | FieldNames[0] = "gpr"; |
| 6010 | |
| 6011 | // unsigned char fpr; |
| 6012 | FieldTypes[1] = Context->UnsignedCharTy; |
| 6013 | FieldNames[1] = "fpr"; |
| 6014 | |
| 6015 | // unsigned short reserved; |
| 6016 | FieldTypes[2] = Context->UnsignedShortTy; |
| 6017 | FieldNames[2] = "reserved"; |
| 6018 | |
| 6019 | // void* overflow_arg_area; |
| 6020 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 6021 | FieldNames[3] = "overflow_arg_area"; |
| 6022 | |
| 6023 | // void* reg_save_area; |
| 6024 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
| 6025 | FieldNames[4] = "reg_save_area"; |
| 6026 | |
| 6027 | // Create fields |
| 6028 | for (unsigned i = 0; i < NumFields; ++i) { |
| 6029 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
| 6030 | SourceLocation(), |
| 6031 | SourceLocation(), |
| 6032 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6033 | FieldTypes[i], /*TInfo=*/nullptr, |
| 6034 | /*BitWidth=*/nullptr, |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6035 | /*Mutable=*/false, |
| 6036 | ICIS_NoInit); |
| 6037 | Field->setAccess(AS_public); |
| 6038 | VaListTagDecl->addDecl(Field); |
| 6039 | } |
| 6040 | VaListTagDecl->completeDefinition(); |
| 6041 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 6042 | Context->VaListTagTy = VaListTagType; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6043 | |
| 6044 | // } __va_list_tag; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6045 | TypedefDecl *VaListTagTypedefDecl = |
| 6046 | Context->buildImplicitTypedef(VaListTagType, "__va_list_tag"); |
| 6047 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6048 | QualType VaListTagTypedefType = |
| 6049 | Context->getTypedefType(VaListTagTypedefDecl); |
| 6050 | |
| 6051 | // typedef __va_list_tag __builtin_va_list[1]; |
| 6052 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 6053 | QualType VaListTagArrayType |
| 6054 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 6055 | Size, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6056 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6057 | } |
| 6058 | |
| 6059 | static TypedefDecl * |
| 6060 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
| 6061 | // typedef struct __va_list_tag { |
| 6062 | RecordDecl *VaListTagDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6063 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6064 | VaListTagDecl->startDefinition(); |
| 6065 | |
| 6066 | const size_t NumFields = 4; |
| 6067 | QualType FieldTypes[NumFields]; |
| 6068 | const char *FieldNames[NumFields]; |
| 6069 | |
| 6070 | // unsigned gp_offset; |
| 6071 | FieldTypes[0] = Context->UnsignedIntTy; |
| 6072 | FieldNames[0] = "gp_offset"; |
| 6073 | |
| 6074 | // unsigned fp_offset; |
| 6075 | FieldTypes[1] = Context->UnsignedIntTy; |
| 6076 | FieldNames[1] = "fp_offset"; |
| 6077 | |
| 6078 | // void* overflow_arg_area; |
| 6079 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 6080 | FieldNames[2] = "overflow_arg_area"; |
| 6081 | |
| 6082 | // void* reg_save_area; |
| 6083 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 6084 | FieldNames[3] = "reg_save_area"; |
| 6085 | |
| 6086 | // Create fields |
| 6087 | for (unsigned i = 0; i < NumFields; ++i) { |
| 6088 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 6089 | VaListTagDecl, |
| 6090 | SourceLocation(), |
| 6091 | SourceLocation(), |
| 6092 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6093 | FieldTypes[i], /*TInfo=*/nullptr, |
| 6094 | /*BitWidth=*/nullptr, |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6095 | /*Mutable=*/false, |
| 6096 | ICIS_NoInit); |
| 6097 | Field->setAccess(AS_public); |
| 6098 | VaListTagDecl->addDecl(Field); |
| 6099 | } |
| 6100 | VaListTagDecl->completeDefinition(); |
| 6101 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 6102 | Context->VaListTagTy = VaListTagType; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6103 | |
| 6104 | // } __va_list_tag; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6105 | TypedefDecl *VaListTagTypedefDecl = |
| 6106 | Context->buildImplicitTypedef(VaListTagType, "__va_list_tag"); |
| 6107 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6108 | QualType VaListTagTypedefType = |
| 6109 | Context->getTypedefType(VaListTagTypedefDecl); |
| 6110 | |
| 6111 | // typedef __va_list_tag __builtin_va_list[1]; |
| 6112 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 6113 | QualType VaListTagArrayType |
| 6114 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 6115 | Size, ArrayType::Normal,0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6116 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6117 | } |
| 6118 | |
| 6119 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
| 6120 | // typedef int __builtin_va_list[4]; |
| 6121 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); |
| 6122 | QualType IntArrayType |
| 6123 | = Context->getConstantArrayType(Context->IntTy, |
| 6124 | Size, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6125 | return Context->buildImplicitTypedef(IntArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6126 | } |
| 6127 | |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6128 | static TypedefDecl * |
| 6129 | CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6130 | // struct __va_list |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6131 | RecordDecl *VaListDecl = Context->buildImplicitRecord("__va_list"); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6132 | if (Context->getLangOpts().CPlusPlus) { |
| 6133 | // namespace std { struct __va_list { |
| 6134 | NamespaceDecl *NS; |
| 6135 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 6136 | Context->getTranslationUnitDecl(), |
| 6137 | /*Inline*/false, SourceLocation(), |
| 6138 | SourceLocation(), &Context->Idents.get("std"), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6139 | /*PrevDecl*/ nullptr); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6140 | NS->setImplicit(); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6141 | VaListDecl->setDeclContext(NS); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6142 | } |
| 6143 | |
| 6144 | VaListDecl->startDefinition(); |
| 6145 | |
| 6146 | // void * __ap; |
| 6147 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 6148 | VaListDecl, |
| 6149 | SourceLocation(), |
| 6150 | SourceLocation(), |
| 6151 | &Context->Idents.get("__ap"), |
| 6152 | Context->getPointerType(Context->VoidTy), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6153 | /*TInfo=*/nullptr, |
| 6154 | /*BitWidth=*/nullptr, |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6155 | /*Mutable=*/false, |
| 6156 | ICIS_NoInit); |
| 6157 | Field->setAccess(AS_public); |
| 6158 | VaListDecl->addDecl(Field); |
| 6159 | |
| 6160 | // }; |
| 6161 | VaListDecl->completeDefinition(); |
| 6162 | |
| 6163 | // typedef struct __va_list __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6164 | QualType T = Context->getRecordType(VaListDecl); |
| 6165 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6166 | } |
| 6167 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6168 | static TypedefDecl * |
| 6169 | CreateSystemZBuiltinVaListDecl(const ASTContext *Context) { |
| 6170 | // typedef struct __va_list_tag { |
| 6171 | RecordDecl *VaListTagDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6172 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6173 | VaListTagDecl->startDefinition(); |
| 6174 | |
| 6175 | const size_t NumFields = 4; |
| 6176 | QualType FieldTypes[NumFields]; |
| 6177 | const char *FieldNames[NumFields]; |
| 6178 | |
| 6179 | // long __gpr; |
| 6180 | FieldTypes[0] = Context->LongTy; |
| 6181 | FieldNames[0] = "__gpr"; |
| 6182 | |
| 6183 | // long __fpr; |
| 6184 | FieldTypes[1] = Context->LongTy; |
| 6185 | FieldNames[1] = "__fpr"; |
| 6186 | |
| 6187 | // void *__overflow_arg_area; |
| 6188 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 6189 | FieldNames[2] = "__overflow_arg_area"; |
| 6190 | |
| 6191 | // void *__reg_save_area; |
| 6192 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 6193 | FieldNames[3] = "__reg_save_area"; |
| 6194 | |
| 6195 | // Create fields |
| 6196 | for (unsigned i = 0; i < NumFields; ++i) { |
| 6197 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 6198 | VaListTagDecl, |
| 6199 | SourceLocation(), |
| 6200 | SourceLocation(), |
| 6201 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6202 | FieldTypes[i], /*TInfo=*/nullptr, |
| 6203 | /*BitWidth=*/nullptr, |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6204 | /*Mutable=*/false, |
| 6205 | ICIS_NoInit); |
| 6206 | Field->setAccess(AS_public); |
| 6207 | VaListTagDecl->addDecl(Field); |
| 6208 | } |
| 6209 | VaListTagDecl->completeDefinition(); |
| 6210 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 6211 | Context->VaListTagTy = VaListTagType; |
| 6212 | |
| 6213 | // } __va_list_tag; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6214 | TypedefDecl *VaListTagTypedefDecl = |
| 6215 | Context->buildImplicitTypedef(VaListTagType, "__va_list_tag"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6216 | QualType VaListTagTypedefType = |
| 6217 | Context->getTypedefType(VaListTagTypedefDecl); |
| 6218 | |
| 6219 | // typedef __va_list_tag __builtin_va_list[1]; |
| 6220 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 6221 | QualType VaListTagArrayType |
| 6222 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 6223 | Size, ArrayType::Normal,0); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6224 | |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6225 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6226 | } |
| 6227 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6228 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
| 6229 | TargetInfo::BuiltinVaListKind Kind) { |
| 6230 | switch (Kind) { |
| 6231 | case TargetInfo::CharPtrBuiltinVaList: |
| 6232 | return CreateCharPtrBuiltinVaListDecl(Context); |
| 6233 | case TargetInfo::VoidPtrBuiltinVaList: |
| 6234 | return CreateVoidPtrBuiltinVaListDecl(Context); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6235 | case TargetInfo::AArch64ABIBuiltinVaList: |
| 6236 | return CreateAArch64ABIBuiltinVaListDecl(Context); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6237 | case TargetInfo::PowerABIBuiltinVaList: |
| 6238 | return CreatePowerABIBuiltinVaListDecl(Context); |
| 6239 | case TargetInfo::X86_64ABIBuiltinVaList: |
| 6240 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
| 6241 | case TargetInfo::PNaClABIBuiltinVaList: |
| 6242 | return CreatePNaClABIBuiltinVaListDecl(Context); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 6243 | case TargetInfo::AAPCSABIBuiltinVaList: |
| 6244 | return CreateAAPCSABIBuiltinVaListDecl(Context); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 6245 | case TargetInfo::SystemZBuiltinVaList: |
| 6246 | return CreateSystemZBuiltinVaListDecl(Context); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6247 | } |
| 6248 | |
| 6249 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
| 6250 | } |
| 6251 | |
| 6252 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6253 | if (!BuiltinVaListDecl) { |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6254 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6255 | assert(BuiltinVaListDecl->isImplicit()); |
| 6256 | } |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6257 | |
| 6258 | return BuiltinVaListDecl; |
| 6259 | } |
| 6260 | |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 6261 | QualType ASTContext::getVaListTagType() const { |
| 6262 | // Force the creation of VaListTagTy by building the __builtin_va_list |
| 6263 | // declaration. |
| 6264 | if (VaListTagTy.isNull()) |
| 6265 | (void) getBuiltinVaListDecl(); |
| 6266 | |
| 6267 | return VaListTagTy; |
| 6268 | } |
| 6269 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6270 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6271 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 6272 | "'NSConstantString' type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6273 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6274 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 6275 | } |
| 6276 | |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6277 | /// \brief Retrieve the template name that corresponds to a non-empty |
| 6278 | /// lookup. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6279 | TemplateName |
| 6280 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 6281 | UnresolvedSetIterator End) const { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6282 | unsigned size = End - Begin; |
| 6283 | assert(size > 1 && "set is not overloaded!"); |
| 6284 | |
| 6285 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 6286 | size * sizeof(FunctionTemplateDecl*)); |
| 6287 | OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size); |
| 6288 | |
| 6289 | NamedDecl **Storage = OT->getStorage(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 6290 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 6291 | NamedDecl *D = *I; |
| 6292 | assert(isa<FunctionTemplateDecl>(D) || |
| 6293 | (isa<UsingShadowDecl>(D) && |
| 6294 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 6295 | *Storage++ = D; |
| 6296 | } |
| 6297 | |
| 6298 | return TemplateName(OT); |
| 6299 | } |
| 6300 | |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6301 | /// \brief Retrieve the template name that represents a qualified |
| 6302 | /// template name such as \c std::vector. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6303 | TemplateName |
| 6304 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 6305 | bool TemplateKeyword, |
| 6306 | TemplateDecl *Template) const { |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 6307 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 6308 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6309 | // FIXME: Canonicalization? |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6310 | llvm::FoldingSetNodeID ID; |
| 6311 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 6312 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6313 | void *InsertPos = nullptr; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6314 | QualifiedTemplateName *QTN = |
| 6315 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6316 | if (!QTN) { |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6317 | QTN = new (*this, llvm::alignOf<QualifiedTemplateName>()) |
| 6318 | QualifiedTemplateName(NNS, TemplateKeyword, Template); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6319 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 6320 | } |
| 6321 | |
| 6322 | return TemplateName(QTN); |
| 6323 | } |
| 6324 | |
| 6325 | /// \brief Retrieve the template name that represents a dependent |
| 6326 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6327 | TemplateName |
| 6328 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 6329 | const IdentifierInfo *Name) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6330 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 6331 | "Nested name specifier must be dependent"); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6332 | |
| 6333 | llvm::FoldingSetNodeID ID; |
| 6334 | DependentTemplateName::Profile(ID, NNS, Name); |
| 6335 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6336 | void *InsertPos = nullptr; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6337 | DependentTemplateName *QTN = |
| 6338 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6339 | |
| 6340 | if (QTN) |
| 6341 | return TemplateName(QTN); |
| 6342 | |
| 6343 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 6344 | if (CanonNNS == NNS) { |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6345 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6346 | DependentTemplateName(NNS, Name); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6347 | } else { |
| 6348 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6349 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6350 | DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6351 | DependentTemplateName *CheckQTN = |
| 6352 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6353 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 6354 | (void)CheckQTN; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 6355 | } |
| 6356 | |
| 6357 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 6358 | return TemplateName(QTN); |
| 6359 | } |
| 6360 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6361 | /// \brief Retrieve the template name that represents a dependent |
| 6362 | /// template name such as \c MetaFun::template operator+. |
| 6363 | TemplateName |
| 6364 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6365 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6366 | assert((!NNS || NNS->isDependent()) && |
| 6367 | "Nested name specifier must be dependent"); |
| 6368 | |
| 6369 | llvm::FoldingSetNodeID ID; |
| 6370 | DependentTemplateName::Profile(ID, NNS, Operator); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6371 | |
| 6372 | void *InsertPos = nullptr; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6373 | DependentTemplateName *QTN |
| 6374 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6375 | |
| 6376 | if (QTN) |
| 6377 | return TemplateName(QTN); |
| 6378 | |
| 6379 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 6380 | if (CanonNNS == NNS) { |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6381 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6382 | DependentTemplateName(NNS, Operator); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6383 | } else { |
| 6384 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 6385 | QTN = new (*this, llvm::alignOf<DependentTemplateName>()) |
| 6386 | DependentTemplateName(NNS, Operator, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 6387 | |
| 6388 | DependentTemplateName *CheckQTN |
| 6389 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 6390 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 6391 | (void)CheckQTN; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 6392 | } |
| 6393 | |
| 6394 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 6395 | return TemplateName(QTN); |
| 6396 | } |
| 6397 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6398 | TemplateName |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6399 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 6400 | TemplateName replacement) const { |
| 6401 | llvm::FoldingSetNodeID ID; |
| 6402 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6403 | |
| 6404 | void *insertPos = nullptr; |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6405 | SubstTemplateTemplateParmStorage *subst |
| 6406 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 6407 | |
| 6408 | if (!subst) { |
| 6409 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 6410 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 6411 | } |
| 6412 | |
| 6413 | return TemplateName(subst); |
| 6414 | } |
| 6415 | |
| 6416 | TemplateName |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6417 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 6418 | const TemplateArgument &ArgPack) const { |
| 6419 | ASTContext &Self = const_cast<ASTContext &>(*this); |
| 6420 | llvm::FoldingSetNodeID ID; |
| 6421 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6422 | |
| 6423 | void *InsertPos = nullptr; |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6424 | SubstTemplateTemplateParmPackStorage *Subst |
| 6425 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 6426 | |
| 6427 | if (!Subst) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 6428 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 6429 | ArgPack.pack_size(), |
| 6430 | ArgPack.pack_begin()); |
| 6431 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 6432 | } |
| 6433 | |
| 6434 | return TemplateName(Subst); |
| 6435 | } |
| 6436 | |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6437 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | ab13857 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 6438 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 6439 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 6440 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6441 | switch (Type) { |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 6442 | case TargetInfo::NoInt: return CanQualType(); |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 6443 | case TargetInfo::SignedChar: return SignedCharTy; |
| 6444 | case TargetInfo::UnsignedChar: return UnsignedCharTy; |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6445 | case TargetInfo::SignedShort: return ShortTy; |
| 6446 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 6447 | case TargetInfo::SignedInt: return IntTy; |
| 6448 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 6449 | case TargetInfo::SignedLong: return LongTy; |
| 6450 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 6451 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 6452 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 6453 | } |
| 6454 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6455 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 6456 | } |
Ted Kremenek | 77c51b2 | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 6457 | |
| 6458 | //===----------------------------------------------------------------------===// |
| 6459 | // Type Predicates. |
| 6460 | //===----------------------------------------------------------------------===// |
| 6461 | |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6462 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 6463 | /// garbage collection attribute. |
| 6464 | /// |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6465 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6466 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6467 | return Qualifiers::GCNone; |
| 6468 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6469 | assert(getLangOpts().ObjC1); |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 6470 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 6471 | |
| 6472 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 6473 | // (or pointers to them) be treated as though they were declared |
| 6474 | // as __strong. |
| 6475 | if (GCAttrs == Qualifiers::GCNone) { |
| 6476 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 6477 | return Qualifiers::Strong; |
| 6478 | else if (Ty->isPointerType()) |
| 6479 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 6480 | } else { |
| 6481 | // It's not valid to set GC attributes on anything that isn't a |
| 6482 | // pointer. |
| 6483 | #ifndef NDEBUG |
| 6484 | QualType CT = Ty->getCanonicalTypeInternal(); |
| 6485 | while (const ArrayType *AT = dyn_cast<ArrayType>(CT)) |
| 6486 | CT = AT->getElementType(); |
| 6487 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 6488 | #endif |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6489 | } |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 6490 | return GCAttrs; |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 6491 | } |
| 6492 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6493 | //===----------------------------------------------------------------------===// |
| 6494 | // Type Compatibility Testing |
| 6495 | //===----------------------------------------------------------------------===// |
Chris Lattner | b338a6b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 6496 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6497 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6498 | /// compatible. |
| 6499 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 6500 | const VectorType *RHS) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 6501 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6502 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6503 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6504 | } |
| 6505 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6506 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 6507 | QualType SecondVec) { |
| 6508 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 6509 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 6510 | |
| 6511 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 6512 | return true; |
| 6513 | |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6514 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 6515 | // equivalent GCC vector types. |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6516 | const VectorType *First = FirstVec->getAs<VectorType>(); |
| 6517 | const VectorType *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6518 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6519 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 6520 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 6521 | First->getVectorKind() != VectorType::AltiVecBool && |
| 6522 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 6523 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6524 | return true; |
| 6525 | |
| 6526 | return false; |
| 6527 | } |
| 6528 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6529 | //===----------------------------------------------------------------------===// |
| 6530 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 6531 | //===----------------------------------------------------------------------===// |
| 6532 | |
| 6533 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 6534 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6535 | bool |
| 6536 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 6537 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 6538 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6539 | return true; |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 6540 | for (auto *PI : rProto->protocols()) |
| 6541 | if (ProtocolCompatibleWithProtocol(lProto, PI)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6542 | return true; |
| 6543 | return false; |
| 6544 | } |
| 6545 | |
Dmitri Gribenko | 4364fcf | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 6546 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and |
| 6547 | /// Class<pr1, ...>. |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6548 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 6549 | QualType rhs) { |
| 6550 | const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 6551 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
| 6552 | assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
| 6553 | |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6554 | for (auto *lhsProto : lhsQID->quals()) { |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6555 | bool match = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6556 | for (auto *rhsProto : rhsOPT->quals()) { |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6557 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 6558 | match = true; |
| 6559 | break; |
| 6560 | } |
| 6561 | } |
| 6562 | if (!match) |
| 6563 | return false; |
| 6564 | } |
| 6565 | return true; |
| 6566 | } |
| 6567 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6568 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 6569 | /// ObjCQualifiedIDType. |
| 6570 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 6571 | bool compare) { |
| 6572 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6573 | if (lhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6574 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 6575 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6576 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6577 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 6578 | return true; |
| 6579 | |
| 6580 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6581 | const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6582 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6583 | if (!rhsOPT) return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6584 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6585 | if (rhsOPT->qual_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6586 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6587 | // make sure we check the class hierarchy. |
| 6588 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6589 | for (auto *I : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6590 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6591 | // see if static class implements all of id's protocols, directly or |
| 6592 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6593 | if (!rhsID->ClassImplementsProtocol(I, true)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6594 | return false; |
| 6595 | } |
| 6596 | } |
| 6597 | // If there are no qualifiers and no interface, we have an 'id'. |
| 6598 | return true; |
| 6599 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6600 | // Both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6601 | for (auto *lhsProto : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6602 | bool match = false; |
| 6603 | |
| 6604 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6605 | // see if static class implements all of id's protocols, directly or |
| 6606 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6607 | for (auto *rhsProto : rhsOPT->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6608 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6609 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6610 | match = true; |
| 6611 | break; |
| 6612 | } |
| 6613 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6614 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6615 | // make sure we check the class hierarchy. |
| 6616 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6617 | for (auto *I : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6618 | // when comparing an id<P> on lhs with a static type on rhs, |
| 6619 | // see if static class implements all of id's protocols, directly or |
| 6620 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6621 | if (rhsID->ClassImplementsProtocol(I, true)) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6622 | match = true; |
| 6623 | break; |
| 6624 | } |
| 6625 | } |
| 6626 | } |
| 6627 | if (!match) |
| 6628 | return false; |
| 6629 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6630 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6631 | return true; |
| 6632 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6633 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6634 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 6635 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 6636 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6637 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6638 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6639 | // If both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6640 | for (auto *lhsProto : lhsOPT->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6641 | bool match = false; |
| 6642 | |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6643 | // 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] | 6644 | // see if static class implements all of id's protocols, directly or |
| 6645 | // through its super class and categories. |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6646 | // First, lhs protocols in the qualifier list must be found, direct |
| 6647 | // or indirect in rhs's qualifier list or it is a mismatch. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6648 | for (auto *rhsProto : rhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6649 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6650 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6651 | match = true; |
| 6652 | break; |
| 6653 | } |
| 6654 | } |
| 6655 | if (!match) |
| 6656 | return false; |
| 6657 | } |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6658 | |
| 6659 | // Static class's protocols, or its super class or category protocols |
| 6660 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 6661 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 6662 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 6663 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 6664 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 6665 | // no type qualifier and its class has no static protocol(s) |
| 6666 | // assume that it is mismatch. |
| 6667 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 6668 | return false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6669 | for (auto *lhsProto : LHSInheritedProtocols) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6670 | bool match = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6671 | for (auto *rhsProto : rhsQID->quals()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 6672 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 6673 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 6674 | match = true; |
| 6675 | break; |
| 6676 | } |
| 6677 | } |
| 6678 | if (!match) |
| 6679 | return false; |
| 6680 | } |
| 6681 | } |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6682 | return true; |
| 6683 | } |
| 6684 | return false; |
| 6685 | } |
| 6686 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6687 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6688 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 6689 | /// protocol qualifiers on the LHS or RHS. |
| 6690 | /// |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6691 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 6692 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6693 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 6694 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 6695 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 6696 | // 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] | 6697 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 6698 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6699 | return true; |
| 6700 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6701 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6702 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 6703 | QualType(RHSOPT,0), |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6704 | false); |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 6705 | |
| 6706 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) |
| 6707 | return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 6708 | QualType(RHSOPT,0)); |
| 6709 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6710 | // If we have 2 user-defined types, fall into that path. |
| 6711 | if (LHS->getInterface() && RHS->getInterface()) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6712 | return canAssignObjCInterfaces(LHS, RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6713 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 6714 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6715 | } |
| 6716 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6717 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 6718 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6719 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 6720 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 6721 | /// not OK. For the return type, the opposite is not OK. |
| 6722 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 6723 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6724 | const ObjCObjectPointerType *RHSOPT, |
| 6725 | bool BlockReturnType) { |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 6726 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6727 | return true; |
| 6728 | |
| 6729 | if (LHSOPT->isObjCBuiltinType()) { |
| 6730 | return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType(); |
| 6731 | } |
| 6732 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 6733 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6734 | return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 6735 | QualType(RHSOPT,0), |
| 6736 | false); |
| 6737 | |
| 6738 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 6739 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 6740 | if (LHS && RHS) { // We have 2 user-defined types. |
| 6741 | if (LHS != RHS) { |
| 6742 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6743 | return BlockReturnType; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6744 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6745 | return !BlockReturnType; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6746 | } |
| 6747 | else |
| 6748 | return true; |
| 6749 | } |
| 6750 | return false; |
| 6751 | } |
| 6752 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6753 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
| 6754 | /// of protocols inherited from two distinct objective-c pointer objects. |
| 6755 | /// It is used to build composite qualifier list of the composite type of |
| 6756 | /// the conditional expression involving two objective-c pointer objects. |
| 6757 | static |
| 6758 | void getIntersectionOfProtocols(ASTContext &Context, |
| 6759 | const ObjCObjectPointerType *LHSOPT, |
| 6760 | const ObjCObjectPointerType *RHSOPT, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6761 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6762 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6763 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 6764 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 6765 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 6766 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6767 | |
| 6768 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet; |
| 6769 | unsigned LHSNumProtocols = LHS->getNumProtocols(); |
| 6770 | if (LHSNumProtocols > 0) |
| 6771 | InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end()); |
| 6772 | else { |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6773 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6774 | Context.CollectInheritedProtocols(LHS->getInterface(), |
| 6775 | LHSInheritedProtocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6776 | InheritedProtocolSet.insert(LHSInheritedProtocols.begin(), |
| 6777 | LHSInheritedProtocols.end()); |
| 6778 | } |
| 6779 | |
| 6780 | unsigned RHSNumProtocols = RHS->getNumProtocols(); |
| 6781 | if (RHSNumProtocols > 0) { |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 6782 | ObjCProtocolDecl **RHSProtocols = |
| 6783 | const_cast<ObjCProtocolDecl **>(RHS->qual_begin()); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6784 | for (unsigned i = 0; i < RHSNumProtocols; ++i) |
| 6785 | if (InheritedProtocolSet.count(RHSProtocols[i])) |
| 6786 | IntersectionOfProtocols.push_back(RHSProtocols[i]); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 6787 | } else { |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 6788 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6789 | Context.CollectInheritedProtocols(RHS->getInterface(), |
| 6790 | RHSInheritedProtocols); |
Craig Topper | c6914d0 | 2014-08-25 04:15:02 +0000 | [diff] [blame] | 6791 | for (ObjCProtocolDecl *ProtDecl : RHSInheritedProtocols) |
| 6792 | if (InheritedProtocolSet.count(ProtDecl)) |
| 6793 | IntersectionOfProtocols.push_back(ProtDecl); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6794 | } |
| 6795 | } |
| 6796 | |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6797 | /// areCommonBaseCompatible - Returns common base class of the two classes if |
| 6798 | /// one found. Note that this is O'2 algorithm. But it will be called as the |
| 6799 | /// last type comparison in a ?-exp of ObjC pointer types before a |
| 6800 | /// warning is issued. So, its invokation is extremely rare. |
| 6801 | QualType ASTContext::areCommonBaseCompatible( |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6802 | const ObjCObjectPointerType *Lptr, |
| 6803 | const ObjCObjectPointerType *Rptr) { |
| 6804 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 6805 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 6806 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 6807 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | 0b144e1 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 6808 | if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl))) |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6809 | return QualType(); |
| 6810 | |
Fariborz Jahanian | b107143 | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 6811 | do { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6812 | LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6813 | if (canAssignObjCInterfaces(LHS, RHS)) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6814 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6815 | getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols); |
| 6816 | |
| 6817 | QualType Result = QualType(LHS, 0); |
| 6818 | if (!Protocols.empty()) |
| 6819 | Result = getObjCObjectType(Result, Protocols.data(), Protocols.size()); |
| 6820 | Result = getObjCObjectPointerType(Result); |
| 6821 | return Result; |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 6822 | } |
Fariborz Jahanian | b107143 | 2011-04-18 21:16:59 +0000 | [diff] [blame] | 6823 | } while ((LDecl = LDecl->getSuperClass())); |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 6824 | |
| 6825 | return QualType(); |
| 6826 | } |
| 6827 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6828 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 6829 | const ObjCObjectType *RHS) { |
| 6830 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 6831 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 6832 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6833 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 6834 | // the LHS. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6835 | if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface())) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6836 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6837 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6838 | // RHS must have a superset of the protocols in the LHS. If the LHS is not |
| 6839 | // protocol qualified at all, then we are good. |
Steve Naroff | c277ad1 | 2009-07-18 15:33:26 +0000 | [diff] [blame] | 6840 | if (LHS->getNumProtocols() == 0) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6841 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6842 | |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 6843 | // Okay, we know the LHS has protocol qualifiers. But RHS may or may not. |
| 6844 | // More detailed analysis is required. |
| 6845 | // OK, if LHS is same or a superclass of RHS *and* |
| 6846 | // this LHS, or as RHS's super class is assignment compatible with LHS. |
| 6847 | bool IsSuperClass = |
| 6848 | LHS->getInterface()->isSuperClassOf(RHS->getInterface()); |
| 6849 | if (IsSuperClass) { |
| 6850 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 6851 | // ; i.e., SuperClass may implement at least one of the protocols |
| 6852 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 6853 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 6854 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
| 6855 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
| 6856 | // Also, if RHS has explicit quelifiers, include them for comparing with LHS's |
| 6857 | // qualifiers. |
| 6858 | for (auto *RHSPI : RHS->quals()) |
| 6859 | SuperClassInheritedProtocols.insert(RHSPI->getCanonicalDecl()); |
| 6860 | // If there is no protocols associated with RHS, it is not a match. |
| 6861 | if (SuperClassInheritedProtocols.empty()) |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 6862 | return false; |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 6863 | |
| 6864 | for (const auto *LHSProto : LHS->quals()) { |
| 6865 | bool SuperImplementsProtocol = false; |
| 6866 | for (auto *SuperClassProto : SuperClassInheritedProtocols) |
| 6867 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 6868 | SuperImplementsProtocol = true; |
| 6869 | break; |
| 6870 | } |
| 6871 | if (!SuperImplementsProtocol) |
| 6872 | return false; |
| 6873 | } |
| 6874 | return true; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6875 | } |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 6876 | return false; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 6877 | } |
| 6878 | |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6879 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 6880 | // get the "pointed to" types |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6881 | const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 6882 | const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6883 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6884 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6885 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6886 | |
| 6887 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 6888 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 6889 | } |
| 6890 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6891 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 6892 | return canAssignObjCInterfaces( |
| 6893 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 6894 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 6895 | } |
| 6896 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6897 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 6898 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6899 | /// 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] | 6900 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6901 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 6902 | bool CompareUnqualified) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6903 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 6904 | return hasSameType(LHS, RHS); |
| 6905 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6906 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6907 | } |
| 6908 | |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6909 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | c87c879 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 6910 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 6911 | } |
| 6912 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6913 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 6914 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 6915 | } |
| 6916 | |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6917 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 6918 | /// of T is compatible with SubType, return the merged type, else return |
| 6919 | /// QualType() |
| 6920 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 6921 | bool OfBlockPointer, |
| 6922 | bool Unqualified) { |
| 6923 | if (const RecordType *UT = T->getAsUnionType()) { |
| 6924 | RecordDecl *UD = UT->getDecl(); |
| 6925 | if (UD->hasAttr<TransparentUnionAttr>()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6926 | for (const auto *I : UD->fields()) { |
| 6927 | QualType ET = I->getType().getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6928 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 6929 | if (!MT.isNull()) |
| 6930 | return MT; |
| 6931 | } |
| 6932 | } |
| 6933 | } |
| 6934 | |
| 6935 | return QualType(); |
| 6936 | } |
| 6937 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 6938 | /// mergeFunctionParameterTypes - merge two types which appear as function |
| 6939 | /// parameter types |
| 6940 | QualType ASTContext::mergeFunctionParameterTypes(QualType lhs, QualType rhs, |
| 6941 | bool OfBlockPointer, |
| 6942 | bool Unqualified) { |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 6943 | // GNU extension: two types are compatible if they appear as a function |
| 6944 | // argument, one of the types is a transparent union type and the other |
| 6945 | // type is compatible with a union member |
| 6946 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 6947 | Unqualified); |
| 6948 | if (!lmerge.isNull()) |
| 6949 | return lmerge; |
| 6950 | |
| 6951 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 6952 | Unqualified); |
| 6953 | if (!rmerge.isNull()) |
| 6954 | return rmerge; |
| 6955 | |
| 6956 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 6957 | } |
| 6958 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6959 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6960 | bool OfBlockPointer, |
| 6961 | bool Unqualified) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6962 | const FunctionType *lbase = lhs->getAs<FunctionType>(); |
| 6963 | const FunctionType *rbase = rhs->getAs<FunctionType>(); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 6964 | const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 6965 | const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6966 | bool allLTypes = true; |
| 6967 | bool allRTypes = true; |
| 6968 | |
| 6969 | // Check return type |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6970 | QualType retType; |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6971 | if (OfBlockPointer) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6972 | QualType RHS = rbase->getReturnType(); |
| 6973 | QualType LHS = lbase->getReturnType(); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6974 | bool UnqualifiedResult = Unqualified; |
| 6975 | if (!UnqualifiedResult) |
| 6976 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 6977 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 6978 | } |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 6979 | else |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6980 | retType = mergeTypes(lbase->getReturnType(), rbase->getReturnType(), false, |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6981 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 6982 | if (retType.isNull()) return QualType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6983 | |
| 6984 | if (Unqualified) |
| 6985 | retType = retType.getUnqualifiedType(); |
| 6986 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6987 | CanQualType LRetType = getCanonicalType(lbase->getReturnType()); |
| 6988 | CanQualType RRetType = getCanonicalType(rbase->getReturnType()); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6989 | if (Unqualified) { |
| 6990 | LRetType = LRetType.getUnqualifiedType(); |
| 6991 | RRetType = RRetType.getUnqualifiedType(); |
| 6992 | } |
| 6993 | |
| 6994 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6995 | allLTypes = false; |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 6996 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 6997 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 6998 | |
Daniel Dunbar | edd5bae | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 6999 | // FIXME: double check this |
| 7000 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 7001 | // rbase->getRegParmAttr() != 0 && |
| 7002 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 7003 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 7004 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7005 | |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 7006 | // Compatible functions must have compatible calling conventions |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 7007 | if (lbaseInfo.getCC() != rbaseInfo.getCC()) |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 7008 | return QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7009 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7010 | // Regparm is part of the calling convention. |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 7011 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
| 7012 | return QualType(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7013 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
| 7014 | return QualType(); |
| 7015 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7016 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
| 7017 | return QualType(); |
| 7018 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7019 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 7020 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7021 | |
Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 7022 | if (lbaseInfo.getNoReturn() != NoReturn) |
| 7023 | allLTypes = false; |
| 7024 | if (rbaseInfo.getNoReturn() != NoReturn) |
| 7025 | allRTypes = false; |
| 7026 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7027 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7028 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7029 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 7030 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 7031 | "C++ shouldn't be here"); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7032 | // Compatible functions must have the same number of parameters |
| 7033 | if (lproto->getNumParams() != rproto->getNumParams()) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7034 | return QualType(); |
| 7035 | |
| 7036 | // Variadic and non-variadic functions aren't compatible |
| 7037 | if (lproto->isVariadic() != rproto->isVariadic()) |
| 7038 | return QualType(); |
| 7039 | |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 7040 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
| 7041 | return QualType(); |
| 7042 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7043 | if (LangOpts.ObjCAutoRefCount && |
| 7044 | !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto)) |
| 7045 | return QualType(); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7046 | |
| 7047 | // Check parameter type compatibility |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7048 | SmallVector<QualType, 10> types; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7049 | for (unsigned i = 0, n = lproto->getNumParams(); i < n; i++) { |
| 7050 | QualType lParamType = lproto->getParamType(i).getUnqualifiedType(); |
| 7051 | QualType rParamType = rproto->getParamType(i).getUnqualifiedType(); |
| 7052 | QualType paramType = mergeFunctionParameterTypes( |
| 7053 | lParamType, rParamType, OfBlockPointer, Unqualified); |
| 7054 | if (paramType.isNull()) |
| 7055 | return QualType(); |
| 7056 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7057 | if (Unqualified) |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7058 | paramType = paramType.getUnqualifiedType(); |
| 7059 | |
| 7060 | types.push_back(paramType); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7061 | if (Unqualified) { |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7062 | lParamType = lParamType.getUnqualifiedType(); |
| 7063 | rParamType = rParamType.getUnqualifiedType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7064 | } |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7065 | |
| 7066 | if (getCanonicalType(paramType) != getCanonicalType(lParamType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7067 | allLTypes = false; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7068 | if (getCanonicalType(paramType) != getCanonicalType(rParamType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7069 | allRTypes = false; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7070 | } |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7071 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7072 | if (allLTypes) return lhs; |
| 7073 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7074 | |
| 7075 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 7076 | EPI.ExtInfo = einfo; |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 7077 | return getFunctionType(retType, types, EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7078 | } |
| 7079 | |
| 7080 | if (lproto) allRTypes = false; |
| 7081 | if (rproto) allLTypes = false; |
| 7082 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7083 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7084 | if (proto) { |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 7085 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7086 | if (proto->isVariadic()) return QualType(); |
| 7087 | // Check that the types are compatible with the types that |
| 7088 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 7089 | // The only types actually affected are promotable integer |
| 7090 | // types and floats, which would be passed as a different |
| 7091 | // type depending on whether the prototype is visible. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7092 | for (unsigned i = 0, n = proto->getNumParams(); i < n; ++i) { |
| 7093 | QualType paramTy = proto->getParamType(i); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7094 | |
Eli Friedman | 448ce40 | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 7095 | // 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] | 7096 | // to pass enum values. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7097 | if (const EnumType *Enum = paramTy->getAs<EnumType>()) { |
| 7098 | paramTy = Enum->getDecl()->getIntegerType(); |
| 7099 | if (paramTy.isNull()) |
Eli Friedman | 448ce40 | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 7100 | return QualType(); |
| 7101 | } |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7102 | |
| 7103 | if (paramTy->isPromotableIntegerType() || |
| 7104 | getCanonicalType(paramTy).getUnqualifiedType() == FloatTy) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7105 | return QualType(); |
| 7106 | } |
| 7107 | |
| 7108 | if (allLTypes) return lhs; |
| 7109 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7110 | |
| 7111 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 7112 | EPI.ExtInfo = einfo; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7113 | return getFunctionType(retType, proto->getParamTypes(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7114 | } |
| 7115 | |
| 7116 | if (allLTypes) return lhs; |
| 7117 | if (allRTypes) return rhs; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 7118 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7119 | } |
| 7120 | |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7121 | /// Given that we have an enum type and a non-enum type, try to merge them. |
| 7122 | static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET, |
| 7123 | QualType other, bool isBlockReturnType) { |
| 7124 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 7125 | // a signed integer type, or an unsigned integer type. |
| 7126 | // Compatibility is based on the underlying type, not the promotion |
| 7127 | // type. |
| 7128 | QualType underlyingType = ET->getDecl()->getIntegerType(); |
| 7129 | if (underlyingType.isNull()) return QualType(); |
| 7130 | if (Context.hasSameType(underlyingType, other)) |
| 7131 | return other; |
| 7132 | |
| 7133 | // In block return types, we're more permissive and accept any |
| 7134 | // integral type of the same size. |
| 7135 | if (isBlockReturnType && other->isIntegerType() && |
| 7136 | Context.getTypeSize(underlyingType) == Context.getTypeSize(other)) |
| 7137 | return other; |
| 7138 | |
| 7139 | return QualType(); |
| 7140 | } |
| 7141 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7142 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7143 | bool OfBlockPointer, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7144 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | db4e349 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 7145 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 7146 | // type is adjusted to "T" prior to any further analysis, the expression |
| 7147 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 7148 | // expression is an lvalue unless the reference is an rvalue reference and |
| 7149 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 7150 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 7151 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7152 | |
| 7153 | if (Unqualified) { |
| 7154 | LHS = LHS.getUnqualifiedType(); |
| 7155 | RHS = RHS.getUnqualifiedType(); |
| 7156 | } |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 7157 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7158 | QualType LHSCan = getCanonicalType(LHS), |
| 7159 | RHSCan = getCanonicalType(RHS); |
| 7160 | |
| 7161 | // If two types are identical, they are compatible. |
| 7162 | if (LHSCan == RHSCan) |
| 7163 | return LHS; |
| 7164 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7165 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 7166 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 7167 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7168 | if (LQuals != RQuals) { |
| 7169 | // If any of these qualifiers are different, we have a type |
| 7170 | // mismatch. |
| 7171 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7172 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
| 7173 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime()) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7174 | return QualType(); |
| 7175 | |
| 7176 | // Exactly one GC qualifier difference is allowed: __strong is |
| 7177 | // okay if the other type has no GC qualifier but is an Objective |
| 7178 | // C object pointer (i.e. implicitly strong by default). We fix |
| 7179 | // this by pretending that the unqualified type was actually |
| 7180 | // qualified __strong. |
| 7181 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 7182 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 7183 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 7184 | |
| 7185 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 7186 | return QualType(); |
| 7187 | |
| 7188 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 7189 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 7190 | } |
| 7191 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 7192 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 7193 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7194 | return QualType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 7195 | } |
| 7196 | |
| 7197 | // Okay, qualifiers are equal. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7198 | |
Eli Friedman | dcca633 | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 7199 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 7200 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7201 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7202 | // We want to consider the two function types to be the same for these |
| 7203 | // comparisons, just force one to the other. |
| 7204 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 7205 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 16f9096 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 7206 | |
| 7207 | // Same as above for arrays |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 7208 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 7209 | LHSClass = Type::ConstantArray; |
| 7210 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 7211 | RHSClass = Type::ConstantArray; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7212 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7213 | // ObjCInterfaces are just specialized ObjCObjects. |
| 7214 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 7215 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 7216 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 7217 | // Canonicalize ExtVector -> Vector. |
| 7218 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 7219 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7220 | |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 7221 | // If the canonical type classes don't match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7222 | if (LHSClass != RHSClass) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7223 | // Note that we only have special rules for turning block enum |
| 7224 | // returns into block int returns, not vice-versa. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7225 | if (const EnumType* ETy = LHS->getAs<EnumType>()) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7226 | return mergeEnumWithInteger(*this, ETy, RHS, false); |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 7227 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7228 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 7229 | return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType); |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 7230 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 7231 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 194904e | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 7232 | if (OfBlockPointer && !BlockReturnType) { |
| 7233 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 7234 | return LHS; |
| 7235 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 7236 | return RHS; |
| 7237 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 7238 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7239 | return QualType(); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7240 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7241 | |
Steve Naroff | c6edcbd | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 7242 | // The canonical type classes match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7243 | switch (LHSClass) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7244 | #define TYPE(Class, Base) |
| 7245 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 7246 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7247 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 7248 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 7249 | #include "clang/AST/TypeNodes.def" |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7250 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7251 | |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 7252 | case Type::Auto: |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 7253 | case Type::LValueReference: |
| 7254 | case Type::RValueReference: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7255 | case Type::MemberPointer: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7256 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7257 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7258 | case Type::ObjCInterface: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7259 | case Type::IncompleteArray: |
| 7260 | case Type::VariableArray: |
| 7261 | case Type::FunctionProto: |
| 7262 | case Type::ExtVector: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7263 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7264 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7265 | case Type::Pointer: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7266 | { |
| 7267 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7268 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 7269 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7270 | if (Unqualified) { |
| 7271 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 7272 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 7273 | } |
| 7274 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 7275 | Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7276 | if (ResultType.isNull()) return QualType(); |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 7277 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7278 | return LHS; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 7279 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7280 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7281 | return getPointerType(ResultType); |
| 7282 | } |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 7283 | case Type::BlockPointer: |
| 7284 | { |
| 7285 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7286 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 7287 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7288 | if (Unqualified) { |
| 7289 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 7290 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 7291 | } |
| 7292 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 7293 | Unqualified); |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 7294 | if (ResultType.isNull()) return QualType(); |
| 7295 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 7296 | return LHS; |
| 7297 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 7298 | return RHS; |
| 7299 | return getBlockPointerType(ResultType); |
| 7300 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 7301 | case Type::Atomic: |
| 7302 | { |
| 7303 | // Merge two pointer types, while trying to preserve typedef info |
| 7304 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 7305 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 7306 | if (Unqualified) { |
| 7307 | LHSValue = LHSValue.getUnqualifiedType(); |
| 7308 | RHSValue = RHSValue.getUnqualifiedType(); |
| 7309 | } |
| 7310 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 7311 | Unqualified); |
| 7312 | if (ResultType.isNull()) return QualType(); |
| 7313 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 7314 | return LHS; |
| 7315 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 7316 | return RHS; |
| 7317 | return getAtomicType(ResultType); |
| 7318 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7319 | case Type::ConstantArray: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7320 | { |
| 7321 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 7322 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 7323 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
| 7324 | return QualType(); |
| 7325 | |
| 7326 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 7327 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7328 | if (Unqualified) { |
| 7329 | LHSElem = LHSElem.getUnqualifiedType(); |
| 7330 | RHSElem = RHSElem.getUnqualifiedType(); |
| 7331 | } |
| 7332 | |
| 7333 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7334 | if (ResultType.isNull()) return QualType(); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7335 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 7336 | return LHS; |
| 7337 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 7338 | return RHS; |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 7339 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 7340 | ArrayType::ArraySizeModifier(), 0); |
| 7341 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 7342 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7343 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 7344 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7345 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 7346 | return LHS; |
| 7347 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 7348 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7349 | if (LVAT) { |
| 7350 | // FIXME: This isn't correct! But tricky to implement because |
| 7351 | // the array's size has to be the size of LHS, but the type |
| 7352 | // has to be different. |
| 7353 | return LHS; |
| 7354 | } |
| 7355 | if (RVAT) { |
| 7356 | // FIXME: This isn't correct! But tricky to implement because |
| 7357 | // the array's size has to be the size of RHS, but the type |
| 7358 | // has to be different. |
| 7359 | return RHS; |
| 7360 | } |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 7361 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 7362 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 7363 | return getIncompleteArrayType(ResultType, |
| 7364 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7365 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7366 | case Type::FunctionNoProto: |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 7367 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7368 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7369 | case Type::Enum: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7370 | return QualType(); |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 7371 | case Type::Builtin: |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 7372 | // Only exactly equal builtin types are compatible, which is tested above. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7373 | return QualType(); |
Daniel Dunbar | 804c044 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 7374 | case Type::Complex: |
| 7375 | // Distinct complex types are incompatible. |
| 7376 | return QualType(); |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 7377 | case Type::Vector: |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 7378 | // FIXME: The merged type should be an ExtVector! |
John McCall | 44c064b | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 7379 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 7380 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7381 | return LHS; |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7382 | return QualType(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7383 | case Type::ObjCObject: { |
| 7384 | // Check if the types are assignment compatible. |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 7385 | // FIXME: This should be type compatibility, e.g. whether |
| 7386 | // "LHS x; RHS x;" at global scope is legal. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7387 | const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>(); |
| 7388 | const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>(); |
| 7389 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 7a7814c | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 7390 | return LHS; |
| 7391 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7392 | return QualType(); |
Cedric Venet | 4fc88b7 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 7393 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7394 | case Type::ObjCObjectPointer: { |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7395 | if (OfBlockPointer) { |
| 7396 | if (canAssignObjCInterfacesInBlockPointer( |
| 7397 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7398 | RHS->getAs<ObjCObjectPointerType>(), |
| 7399 | BlockReturnType)) |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7400 | return LHS; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7401 | return QualType(); |
| 7402 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7403 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 7404 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7405 | return LHS; |
| 7406 | |
Steve Naroff | c68cfcf | 2008-12-10 22:14:21 +0000 | [diff] [blame] | 7407 | return QualType(); |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7408 | } |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7409 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 7410 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 7411 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 7412 | } |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 7413 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7414 | bool ASTContext::FunctionTypesMatchOnNSConsumedAttrs( |
| 7415 | const FunctionProtoType *FromFunctionType, |
| 7416 | const FunctionProtoType *ToFunctionType) { |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7417 | if (FromFunctionType->hasAnyConsumedParams() != |
| 7418 | ToFunctionType->hasAnyConsumedParams()) |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7419 | return false; |
| 7420 | FunctionProtoType::ExtProtoInfo FromEPI = |
| 7421 | FromFunctionType->getExtProtoInfo(); |
| 7422 | FunctionProtoType::ExtProtoInfo ToEPI = |
| 7423 | ToFunctionType->getExtProtoInfo(); |
Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 7424 | if (FromEPI.ConsumedParameters && ToEPI.ConsumedParameters) |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 7425 | for (unsigned i = 0, n = FromFunctionType->getNumParams(); i != n; ++i) { |
| 7426 | if (FromEPI.ConsumedParameters[i] != ToEPI.ConsumedParameters[i]) |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 7427 | return false; |
| 7428 | } |
| 7429 | return true; |
| 7430 | } |
| 7431 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7432 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 7433 | /// 'RHS' attributes and returns the merged version; including for function |
| 7434 | /// return types. |
| 7435 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 7436 | QualType LHSCan = getCanonicalType(LHS), |
| 7437 | RHSCan = getCanonicalType(RHS); |
| 7438 | // If two types are identical, they are compatible. |
| 7439 | if (LHSCan == RHSCan) |
| 7440 | return LHS; |
| 7441 | if (RHSCan->isFunctionType()) { |
| 7442 | if (!LHSCan->isFunctionType()) |
| 7443 | return QualType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7444 | QualType OldReturnType = |
| 7445 | cast<FunctionType>(RHSCan.getTypePtr())->getReturnType(); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7446 | QualType NewReturnType = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 7447 | cast<FunctionType>(LHSCan.getTypePtr())->getReturnType(); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7448 | QualType ResReturnType = |
| 7449 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 7450 | if (ResReturnType.isNull()) |
| 7451 | return QualType(); |
| 7452 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 7453 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 7454 | // In either case, use OldReturnType to build the new function type. |
| 7455 | const FunctionType *F = LHS->getAs<FunctionType>(); |
| 7456 | if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7457 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 7458 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Reid Kleckner | 896b32f | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 7459 | QualType ResultType = |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 7460 | getFunctionType(OldReturnType, FPT->getParamTypes(), EPI); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 7461 | return ResultType; |
| 7462 | } |
| 7463 | } |
| 7464 | return QualType(); |
| 7465 | } |
| 7466 | |
| 7467 | // If the qualifiers are different, the types can still be merged. |
| 7468 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 7469 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 7470 | if (LQuals != RQuals) { |
| 7471 | // If any of these qualifiers are different, we have a type mismatch. |
| 7472 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 7473 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
| 7474 | return QualType(); |
| 7475 | |
| 7476 | // Exactly one GC qualifier difference is allowed: __strong is |
| 7477 | // okay if the other type has no GC qualifier but is an Objective |
| 7478 | // C object pointer (i.e. implicitly strong by default). We fix |
| 7479 | // this by pretending that the unqualified type was actually |
| 7480 | // qualified __strong. |
| 7481 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 7482 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 7483 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 7484 | |
| 7485 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
| 7486 | return QualType(); |
| 7487 | |
| 7488 | if (GC_L == Qualifiers::Strong) |
| 7489 | return LHS; |
| 7490 | if (GC_R == Qualifiers::Strong) |
| 7491 | return RHS; |
| 7492 | return QualType(); |
| 7493 | } |
| 7494 | |
| 7495 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 7496 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 7497 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 7498 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 7499 | if (ResQT == LHSBaseQT) |
| 7500 | return LHS; |
| 7501 | if (ResQT == RHSBaseQT) |
| 7502 | return RHS; |
| 7503 | } |
| 7504 | return QualType(); |
| 7505 | } |
| 7506 | |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 7507 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7508 | // Integer Predicates |
| 7509 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3c91971 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 7510 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7511 | unsigned ASTContext::getIntWidth(QualType T) const { |
Richard Smith | e952106 | 2013-10-15 04:56:17 +0000 | [diff] [blame] | 7512 | if (const EnumType *ET = T->getAs<EnumType>()) |
Eli Friedman | ee275c8 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 7513 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7514 | if (T->isBooleanType()) |
| 7515 | return 1; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 7516 | // For builtin types, just use the standard type sizing method |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7517 | return (unsigned)getTypeSize(T); |
| 7518 | } |
| 7519 | |
Abramo Bagnara | 1364049 | 2012-09-09 10:21:24 +0000 | [diff] [blame] | 7520 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) const { |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7521 | assert(T->hasSignedIntegerRepresentation() && "Unexpected type"); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7522 | |
| 7523 | // Turn <4 x signed int> -> <4 x unsigned int> |
| 7524 | if (const VectorType *VTy = T->getAs<VectorType>()) |
| 7525 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 7526 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7527 | |
| 7528 | // For enums, we return the unsigned version of the base type. |
| 7529 | if (const EnumType *ETy = T->getAs<EnumType>()) |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7530 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 7531 | |
| 7532 | const BuiltinType *BTy = T->getAs<BuiltinType>(); |
| 7533 | assert(BTy && "Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7534 | switch (BTy->getKind()) { |
| 7535 | case BuiltinType::Char_S: |
| 7536 | case BuiltinType::SChar: |
| 7537 | return UnsignedCharTy; |
| 7538 | case BuiltinType::Short: |
| 7539 | return UnsignedShortTy; |
| 7540 | case BuiltinType::Int: |
| 7541 | return UnsignedIntTy; |
| 7542 | case BuiltinType::Long: |
| 7543 | return UnsignedLongTy; |
| 7544 | case BuiltinType::LongLong: |
| 7545 | return UnsignedLongLongTy; |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 7546 | case BuiltinType::Int128: |
| 7547 | return UnsignedInt128Ty; |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7548 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7549 | llvm_unreachable("Unexpected signed integer type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 7550 | } |
| 7551 | } |
| 7552 | |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 7553 | ASTMutationListener::~ASTMutationListener() { } |
| 7554 | |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 7555 | void ASTMutationListener::DeducedReturnType(const FunctionDecl *FD, |
| 7556 | QualType ReturnType) {} |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7557 | |
| 7558 | //===----------------------------------------------------------------------===// |
| 7559 | // Builtin Type Computation |
| 7560 | //===----------------------------------------------------------------------===// |
| 7561 | |
| 7562 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7563 | /// pointer over the consumed characters. This returns the resultant type. If |
| 7564 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 7565 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 7566 | /// a vector of "i*". |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7567 | /// |
| 7568 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 7569 | /// to be an Integer Constant Expression. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7570 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7571 | ASTContext::GetBuiltinTypeError &Error, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7572 | bool &RequiresICE, |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7573 | bool AllowTypeModifiers) { |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7574 | // Modifiers. |
| 7575 | int HowLong = 0; |
| 7576 | bool Signed = false, Unsigned = false; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7577 | RequiresICE = false; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7578 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7579 | // Read the prefixed modifiers first. |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7580 | bool Done = false; |
| 7581 | while (!Done) { |
| 7582 | switch (*Str++) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7583 | default: Done = true; --Str; break; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7584 | case 'I': |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7585 | RequiresICE = true; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7586 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7587 | case 'S': |
| 7588 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 7589 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 7590 | Signed = true; |
| 7591 | break; |
| 7592 | case 'U': |
| 7593 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
Sean Silva | 2a99514 | 2015-01-16 21:44:26 +0000 | [diff] [blame] | 7594 | assert(!Unsigned && "Can't use 'U' modifier multiple times!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7595 | Unsigned = true; |
| 7596 | break; |
| 7597 | case 'L': |
| 7598 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 7599 | ++HowLong; |
| 7600 | break; |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 7601 | case 'W': |
| 7602 | // This modifier represents int64 type. |
| 7603 | assert(HowLong == 0 && "Can't use both 'L' and 'W' modifiers!"); |
| 7604 | switch (Context.getTargetInfo().getInt64Type()) { |
| 7605 | default: |
| 7606 | llvm_unreachable("Unexpected integer type"); |
| 7607 | case TargetInfo::SignedLong: |
| 7608 | HowLong = 1; |
| 7609 | break; |
| 7610 | case TargetInfo::SignedLongLong: |
| 7611 | HowLong = 2; |
| 7612 | break; |
| 7613 | } |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7614 | } |
| 7615 | } |
| 7616 | |
| 7617 | QualType Type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7618 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7619 | // Read the base type. |
| 7620 | switch (*Str++) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7621 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7622 | case 'v': |
| 7623 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 7624 | "Bad modifiers used with 'v'!"); |
| 7625 | Type = Context.VoidTy; |
| 7626 | break; |
Jack Carter | 24bef98 | 2013-08-15 15:16:57 +0000 | [diff] [blame] | 7627 | case 'h': |
| 7628 | assert(HowLong == 0 && !Signed && !Unsigned && |
Sean Silva | 2a99514 | 2015-01-16 21:44:26 +0000 | [diff] [blame] | 7629 | "Bad modifiers used with 'h'!"); |
Jack Carter | 24bef98 | 2013-08-15 15:16:57 +0000 | [diff] [blame] | 7630 | Type = Context.HalfTy; |
| 7631 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7632 | case 'f': |
| 7633 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 7634 | "Bad modifiers used with 'f'!"); |
| 7635 | Type = Context.FloatTy; |
| 7636 | break; |
| 7637 | case 'd': |
| 7638 | assert(HowLong < 2 && !Signed && !Unsigned && |
| 7639 | "Bad modifiers used with 'd'!"); |
| 7640 | if (HowLong) |
| 7641 | Type = Context.LongDoubleTy; |
| 7642 | else |
| 7643 | Type = Context.DoubleTy; |
| 7644 | break; |
| 7645 | case 's': |
| 7646 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 7647 | if (Unsigned) |
| 7648 | Type = Context.UnsignedShortTy; |
| 7649 | else |
| 7650 | Type = Context.ShortTy; |
| 7651 | break; |
| 7652 | case 'i': |
| 7653 | if (HowLong == 3) |
| 7654 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 7655 | else if (HowLong == 2) |
| 7656 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 7657 | else if (HowLong == 1) |
| 7658 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 7659 | else |
| 7660 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 7661 | break; |
| 7662 | case 'c': |
| 7663 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 7664 | if (Signed) |
| 7665 | Type = Context.SignedCharTy; |
| 7666 | else if (Unsigned) |
| 7667 | Type = Context.UnsignedCharTy; |
| 7668 | else |
| 7669 | Type = Context.CharTy; |
| 7670 | break; |
| 7671 | case 'b': // boolean |
| 7672 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 7673 | Type = Context.BoolTy; |
| 7674 | break; |
| 7675 | case 'z': // size_t. |
| 7676 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 7677 | Type = Context.getSizeType(); |
| 7678 | break; |
| 7679 | case 'F': |
| 7680 | Type = Context.getCFConstantStringType(); |
| 7681 | break; |
Fariborz Jahanian | d11da7e | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 7682 | case 'G': |
| 7683 | Type = Context.getObjCIdType(); |
| 7684 | break; |
| 7685 | case 'H': |
| 7686 | Type = Context.getObjCSelType(); |
| 7687 | break; |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 7688 | case 'M': |
| 7689 | Type = Context.getObjCSuperType(); |
| 7690 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7691 | case 'a': |
| 7692 | Type = Context.getBuiltinVaListType(); |
| 7693 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 7694 | break; |
| 7695 | case 'A': |
| 7696 | // This is a "reference" to a va_list; however, what exactly |
| 7697 | // this means depends on how va_list is defined. There are two |
| 7698 | // different kinds of va_list: ones passed by value, and ones |
| 7699 | // passed by reference. An example of a by-value va_list is |
| 7700 | // x86, where va_list is a char*. An example of by-ref va_list |
| 7701 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 7702 | // we want this argument to be a char*&; for x86-64, we want |
| 7703 | // it to be a __va_list_tag*. |
| 7704 | Type = Context.getBuiltinVaListType(); |
| 7705 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7706 | if (Type->isArrayType()) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7707 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7708 | else |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7709 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7710 | break; |
| 7711 | case 'V': { |
| 7712 | char *End; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7713 | unsigned NumElements = strtoul(Str, &End, 10); |
| 7714 | assert(End != Str && "Missing vector size"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7715 | Str = End; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7716 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7717 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 7718 | RequiresICE, false); |
| 7719 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7720 | |
| 7721 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 7722 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 7723 | VectorType::GenericVector); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7724 | break; |
| 7725 | } |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 7726 | case 'E': { |
| 7727 | char *End; |
| 7728 | |
| 7729 | unsigned NumElements = strtoul(Str, &End, 10); |
| 7730 | assert(End != Str && "Missing vector size"); |
| 7731 | |
| 7732 | Str = End; |
| 7733 | |
| 7734 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 7735 | false); |
| 7736 | Type = Context.getExtVectorType(ElementType, NumElements); |
| 7737 | break; |
| 7738 | } |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 7739 | case 'X': { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7740 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
| 7741 | false); |
| 7742 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 7743 | Type = Context.getComplexType(ElementType); |
| 7744 | break; |
Fariborz Jahanian | 73952fc | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 7745 | } |
| 7746 | case 'Y' : { |
| 7747 | Type = Context.getPointerDiffType(); |
| 7748 | break; |
| 7749 | } |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 7750 | case 'P': |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 7751 | Type = Context.getFILEType(); |
| 7752 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7753 | Error = ASTContext::GE_Missing_stdio; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7754 | return QualType(); |
| 7755 | } |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7756 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 7757 | case 'J': |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7758 | if (Signed) |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 7759 | Type = Context.getsigjmp_bufType(); |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7760 | else |
| 7761 | Type = Context.getjmp_bufType(); |
| 7762 | |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7763 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 7764 | Error = ASTContext::GE_Missing_setjmp; |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 7765 | return QualType(); |
| 7766 | } |
| 7767 | break; |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 7768 | case 'K': |
| 7769 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 7770 | Type = Context.getucontext_tType(); |
| 7771 | |
| 7772 | if (Type.isNull()) { |
| 7773 | Error = ASTContext::GE_Missing_ucontext; |
| 7774 | return QualType(); |
| 7775 | } |
| 7776 | break; |
Eli Friedman | 4e91899e | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 7777 | case 'p': |
| 7778 | Type = Context.getProcessIDType(); |
| 7779 | break; |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 7780 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7781 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7782 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 7783 | Done = !AllowTypeModifiers; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7784 | while (!Done) { |
John McCall | b8b9466 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 7785 | switch (char c = *Str++) { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7786 | default: Done = true; --Str; break; |
| 7787 | case '*': |
| 7788 | case '&': { |
| 7789 | // Both pointers and references can have their pointee types |
| 7790 | // qualified with an address space. |
| 7791 | char *End; |
| 7792 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 7793 | if (End != Str && AddrSpace != 0) { |
| 7794 | Type = Context.getAddrSpaceQualType(Type, AddrSpace); |
| 7795 | Str = End; |
| 7796 | } |
| 7797 | if (c == '*') |
| 7798 | Type = Context.getPointerType(Type); |
| 7799 | else |
| 7800 | Type = Context.getLValueReferenceType(Type); |
| 7801 | break; |
| 7802 | } |
| 7803 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 7804 | case 'C': |
| 7805 | Type = Type.withConst(); |
| 7806 | break; |
| 7807 | case 'D': |
| 7808 | Type = Context.getVolatileType(Type); |
| 7809 | break; |
Ted Kremenek | f2a2f5f | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 7810 | case 'R': |
| 7811 | Type = Type.withRestrict(); |
| 7812 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7813 | } |
| 7814 | } |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7815 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7816 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 7817 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7818 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7819 | return Type; |
| 7820 | } |
| 7821 | |
| 7822 | /// GetBuiltinType - Return the type for the specified builtin. |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7823 | QualType ASTContext::GetBuiltinType(unsigned Id, |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7824 | GetBuiltinTypeError &Error, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7825 | unsigned *IntegerConstantArgs) const { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 7826 | const char *TypeStr = BuiltinInfo.GetTypeString(Id); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7827 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 7828 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7829 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7830 | bool RequiresICE = false; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7831 | Error = GE_None; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7832 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 7833 | RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7834 | if (Error != GE_None) |
| 7835 | return QualType(); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7836 | |
| 7837 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
| 7838 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7839 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7840 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7841 | if (Error != GE_None) |
| 7842 | return QualType(); |
| 7843 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 7844 | // If this argument is required to be an IntegerConstantExpression and the |
| 7845 | // caller cares, fill in the bitmask we return. |
| 7846 | if (RequiresICE && IntegerConstantArgs) |
| 7847 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 7848 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7849 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 7850 | if (Ty->isArrayType()) |
| 7851 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7852 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7853 | ArgTypes.push_back(Ty); |
| 7854 | } |
| 7855 | |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 7856 | if (Id == Builtin::BI__GetExceptionInfo) |
| 7857 | return QualType(); |
| 7858 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7859 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 7860 | "'.' should only occur at end of builtin type list!"); |
| 7861 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 7862 | FunctionType::ExtInfo EI(CC_C); |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 7863 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 7864 | |
| 7865 | bool Variadic = (TypeStr[0] == '.'); |
| 7866 | |
| 7867 | // We really shouldn't be making a no-proto type here, especially in C++. |
| 7868 | if (ArgTypes.empty() && Variadic) |
| 7869 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 7870 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7871 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 7872 | EPI.ExtInfo = EI; |
| 7873 | EPI.Variadic = Variadic; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 7874 | |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 7875 | return getFunctionType(ResType, ArgTypes, EPI); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 7876 | } |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 7877 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 7878 | static GVALinkage basicGVALinkageForFunction(const ASTContext &Context, |
| 7879 | const FunctionDecl *FD) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7880 | if (!FD->isExternallyVisible()) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7881 | return GVA_Internal; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7882 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7883 | GVALinkage External = GVA_StrongExternal; |
| 7884 | switch (FD->getTemplateSpecializationKind()) { |
| 7885 | case TSK_Undeclared: |
| 7886 | case TSK_ExplicitSpecialization: |
| 7887 | External = GVA_StrongExternal; |
| 7888 | break; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7889 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7890 | case TSK_ExplicitInstantiationDefinition: |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 7891 | return GVA_StrongODR; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7892 | |
David Majnemer | c3d0733 | 2014-05-15 06:25:57 +0000 | [diff] [blame] | 7893 | // C++11 [temp.explicit]p10: |
| 7894 | // [ Note: The intent is that an inline function that is the subject of |
| 7895 | // an explicit instantiation declaration will still be implicitly |
| 7896 | // instantiated when used so that the body can be considered for |
| 7897 | // inlining, but that no out-of-line copy of the inline function would be |
| 7898 | // generated in the translation unit. -- end note ] |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7899 | case TSK_ExplicitInstantiationDeclaration: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 7900 | return GVA_AvailableExternally; |
| 7901 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7902 | case TSK_ImplicitInstantiation: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 7903 | External = GVA_DiscardableODR; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7904 | break; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7905 | } |
| 7906 | |
| 7907 | if (!FD->isInlined()) |
| 7908 | return External; |
David Majnemer | 62f0ffd | 2013-08-01 17:26:42 +0000 | [diff] [blame] | 7909 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 7910 | if ((!Context.getLangOpts().CPlusPlus && !Context.getLangOpts().MSVCCompat && |
| 7911 | !FD->hasAttr<DLLExportAttr>()) || |
David Majnemer | 62f0ffd | 2013-08-01 17:26:42 +0000 | [diff] [blame] | 7912 | FD->hasAttr<GNUInlineAttr>()) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 7913 | // FIXME: This doesn't match gcc's behavior for dllexport inline functions. |
| 7914 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7915 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 7916 | // externally visible. |
| 7917 | if (FD->isInlineDefinitionExternallyVisible()) |
| 7918 | return External; |
| 7919 | |
| 7920 | // C99 inline semantics, where the symbol is not externally visible. |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 7921 | return GVA_AvailableExternally; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7922 | } |
| 7923 | |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 7924 | // Functions specified with extern and inline in -fms-compatibility mode |
| 7925 | // forcibly get emitted. While the body of the function cannot be later |
| 7926 | // replaced, the function definition cannot be discarded. |
David Majnemer | 7376870 | 2015-03-20 00:02:27 +0000 | [diff] [blame] | 7927 | if (FD->isMSExternInline()) |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 7928 | return GVA_StrongODR; |
| 7929 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 7930 | return GVA_DiscardableODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7931 | } |
| 7932 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 7933 | static GVALinkage adjustGVALinkageForDLLAttribute(GVALinkage L, const Decl *D) { |
| 7934 | // See http://msdn.microsoft.com/en-us/library/xa0d9ste.aspx |
| 7935 | // dllexport/dllimport on inline functions. |
| 7936 | if (D->hasAttr<DLLImportAttr>()) { |
| 7937 | if (L == GVA_DiscardableODR || L == GVA_StrongODR) |
| 7938 | return GVA_AvailableExternally; |
| 7939 | } else if (D->hasAttr<DLLExportAttr>()) { |
| 7940 | if (L == GVA_DiscardableODR) |
| 7941 | return GVA_StrongODR; |
| 7942 | } |
| 7943 | return L; |
| 7944 | } |
| 7945 | |
| 7946 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const { |
| 7947 | return adjustGVALinkageForDLLAttribute(basicGVALinkageForFunction(*this, FD), |
| 7948 | FD); |
| 7949 | } |
| 7950 | |
| 7951 | static GVALinkage basicGVALinkageForVariable(const ASTContext &Context, |
| 7952 | const VarDecl *VD) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7953 | if (!VD->isExternallyVisible()) |
| 7954 | return GVA_Internal; |
| 7955 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 7956 | if (VD->isStaticLocal()) { |
| 7957 | GVALinkage StaticLocalLinkage = GVA_DiscardableODR; |
| 7958 | const DeclContext *LexicalContext = VD->getParentFunctionOrMethod(); |
| 7959 | while (LexicalContext && !isa<FunctionDecl>(LexicalContext)) |
| 7960 | LexicalContext = LexicalContext->getLexicalParent(); |
| 7961 | |
Richard Smith | 9e2341d | 2015-03-23 03:25:59 +0000 | [diff] [blame] | 7962 | // Let the static local variable inherit its linkage from the nearest |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 7963 | // enclosing function. |
| 7964 | if (LexicalContext) |
| 7965 | StaticLocalLinkage = |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 7966 | Context.GetGVALinkageForFunction(cast<FunctionDecl>(LexicalContext)); |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 7967 | |
| 7968 | // GVA_StrongODR function linkage is stronger than what we need, |
| 7969 | // downgrade to GVA_DiscardableODR. |
| 7970 | // This allows us to discard the variable if we never end up needing it. |
| 7971 | return StaticLocalLinkage == GVA_StrongODR ? GVA_DiscardableODR |
| 7972 | : StaticLocalLinkage; |
| 7973 | } |
| 7974 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 7975 | // MSVC treats in-class initialized static data members as definitions. |
| 7976 | // By giving them non-strong linkage, out-of-line definitions won't |
| 7977 | // cause link errors. |
| 7978 | if (Context.isMSStaticDataMemberInlineDefinition(VD)) |
| 7979 | return GVA_DiscardableODR; |
| 7980 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 7981 | switch (VD->getTemplateSpecializationKind()) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7982 | case TSK_Undeclared: |
| 7983 | case TSK_ExplicitSpecialization: |
| 7984 | return GVA_StrongExternal; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7985 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7986 | case TSK_ExplicitInstantiationDefinition: |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 7987 | return GVA_StrongODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7988 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 7989 | case TSK_ExplicitInstantiationDeclaration: |
| 7990 | return GVA_AvailableExternally; |
| 7991 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 7992 | case TSK_ImplicitInstantiation: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 7993 | return GVA_DiscardableODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7994 | } |
Rafael Espindola | 27699c8 | 2013-05-13 14:05:53 +0000 | [diff] [blame] | 7995 | |
| 7996 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 7997 | } |
| 7998 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 7999 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
| 8000 | return adjustGVALinkageForDLLAttribute(basicGVALinkageForVariable(*this, VD), |
| 8001 | VD); |
| 8002 | } |
| 8003 | |
Argyrios Kyrtzidis | c904933 | 2010-07-29 20:08:05 +0000 | [diff] [blame] | 8004 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8005 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 8006 | if (!VD->isFileVarDecl()) |
| 8007 | return false; |
Renato Golin | 9258aa5 | 2014-05-21 10:40:27 +0000 | [diff] [blame] | 8008 | // Global named register variables (GNU extension) are never emitted. |
| 8009 | if (VD->getStorageClass() == SC_Register) |
| 8010 | return false; |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 8011 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 8012 | // We never need to emit an uninstantiated function template. |
| 8013 | if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) |
| 8014 | return false; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 8015 | } else if (isa<OMPThreadPrivateDecl>(D)) |
| 8016 | return true; |
| 8017 | else |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 8018 | return false; |
| 8019 | |
| 8020 | // If this is a member of a class template, we do not need to emit it. |
| 8021 | if (D->getDeclContext()->isDependentContext()) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8022 | return false; |
| 8023 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 8024 | // Weak references don't produce any output by themselves. |
| 8025 | if (D->hasAttr<WeakRefAttr>()) |
| 8026 | return false; |
| 8027 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8028 | // Aliases and used decls are required. |
| 8029 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 8030 | return true; |
| 8031 | |
| 8032 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 8033 | // Forward declarations aren't required. |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 8034 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 8035 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8036 | |
| 8037 | // Constructors and destructors are required. |
| 8038 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 8039 | return true; |
| 8040 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 8041 | // The key function for a class is required. This rule only comes |
| 8042 | // into play when inline functions can be key functions, though. |
| 8043 | if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
| 8044 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 8045 | const CXXRecordDecl *RD = MD->getParent(); |
| 8046 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 8047 | const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD); |
| 8048 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 8049 | return true; |
| 8050 | } |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8051 | } |
| 8052 | } |
| 8053 | |
| 8054 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 8055 | |
| 8056 | // static, static inline, always_inline, and extern inline functions can |
| 8057 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 8058 | // Implicit template instantiations can also be deferred in C++. |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 8059 | if (Linkage == GVA_Internal || Linkage == GVA_AvailableExternally || |
| 8060 | Linkage == GVA_DiscardableODR) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8061 | return false; |
| 8062 | return true; |
| 8063 | } |
Douglas Gregor | 87d8124 | 2011-09-10 00:22:34 +0000 | [diff] [blame] | 8064 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8065 | const VarDecl *VD = cast<VarDecl>(D); |
| 8066 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 8067 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 8068 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly && |
| 8069 | !isMSStaticDataMemberInlineDefinition(VD)) |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 8070 | return false; |
| 8071 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 8072 | // Variables that can be needed in other TUs are required. |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8073 | GVALinkage L = GetGVALinkageForVariable(VD); |
David Majnemer | c3d0733 | 2014-05-15 06:25:57 +0000 | [diff] [blame] | 8074 | if (L != GVA_Internal && L != GVA_AvailableExternally && |
| 8075 | L != GVA_DiscardableODR) |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 8076 | return true; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8077 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 8078 | // Variables that have destruction with side-effects are required. |
| 8079 | if (VD->getType().isDestructedType()) |
| 8080 | return true; |
| 8081 | |
| 8082 | // Variables that have initialization with side-effects are required. |
| 8083 | if (VD->getInit() && VD->getInit()->HasSideEffects(*this)) |
| 8084 | return true; |
| 8085 | |
| 8086 | return false; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 8087 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 8088 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 8089 | CallingConv ASTContext::getDefaultCallingConvention(bool IsVariadic, |
| 8090 | bool IsCXXMethod) const { |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 8091 | // Pass through to the C++ ABI object |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 8092 | if (IsCXXMethod) |
| 8093 | return ABI->getDefaultMethodCallConv(IsVariadic); |
Timur Iskhodzhanov | c5098ad | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 8094 | |
Alexander Kornienko | 21de0ae | 2015-01-20 11:20:41 +0000 | [diff] [blame] | 8095 | if (LangOpts.MRTD && !IsVariadic) return CC_X86StdCall; |
| 8096 | |
| 8097 | return Target->getDefaultCallingConv(TargetInfo::CCMT_Unknown); |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 8098 | } |
| 8099 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8100 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 8101 | // Pass through to the C++ ABI object |
| 8102 | return ABI->isNearlyEmpty(RD); |
| 8103 | } |
| 8104 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 8105 | VTableContextBase *ASTContext::getVTableContext() { |
| 8106 | if (!VTContext.get()) { |
| 8107 | if (Target->getCXXABI().isMicrosoft()) |
| 8108 | VTContext.reset(new MicrosoftVTableContext(*this)); |
| 8109 | else |
| 8110 | VTContext.reset(new ItaniumVTableContext(*this)); |
| 8111 | } |
| 8112 | return VTContext.get(); |
| 8113 | } |
| 8114 | |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 8115 | MangleContext *ASTContext::createMangleContext() { |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 8116 | switch (Target->getCXXABI().getKind()) { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 8117 | case TargetCXXABI::GenericAArch64: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 8118 | case TargetCXXABI::GenericItanium: |
| 8119 | case TargetCXXABI::GenericARM: |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 8120 | case TargetCXXABI::GenericMIPS: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 8121 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 8122 | case TargetCXXABI::iOS64: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 8123 | return ItaniumMangleContext::create(*this, getDiagnostics()); |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 8124 | case TargetCXXABI::Microsoft: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 8125 | return MicrosoftMangleContext::create(*this, getDiagnostics()); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 8126 | } |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8127 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 8128 | } |
| 8129 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 8130 | CXXABI::~CXXABI() {} |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 8131 | |
| 8132 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 8133 | return ASTRecordLayouts.getMemorySize() + |
| 8134 | llvm::capacity_in_bytes(ObjCLayouts) + |
| 8135 | llvm::capacity_in_bytes(KeyFunctions) + |
| 8136 | llvm::capacity_in_bytes(ObjCImpls) + |
| 8137 | llvm::capacity_in_bytes(BlockVarCopyInits) + |
| 8138 | llvm::capacity_in_bytes(DeclAttrs) + |
| 8139 | llvm::capacity_in_bytes(TemplateOrInstantiation) + |
| 8140 | llvm::capacity_in_bytes(InstantiatedFromUsingDecl) + |
| 8141 | llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) + |
| 8142 | llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) + |
| 8143 | llvm::capacity_in_bytes(OverriddenMethods) + |
| 8144 | llvm::capacity_in_bytes(Types) + |
| 8145 | llvm::capacity_in_bytes(VariableArrayTypes) + |
| 8146 | llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 8147 | } |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 8148 | |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 8149 | /// getIntTypeForBitwidth - |
| 8150 | /// sets integer QualTy according to specified details: |
| 8151 | /// bitwidth, signed/unsigned. |
| 8152 | /// Returns empty type if there is no appropriate target types. |
| 8153 | QualType ASTContext::getIntTypeForBitwidth(unsigned DestWidth, |
| 8154 | unsigned Signed) const { |
| 8155 | TargetInfo::IntType Ty = getTargetInfo().getIntTypeByWidth(DestWidth, Signed); |
| 8156 | CanQualType QualTy = getFromTargetType(Ty); |
| 8157 | if (!QualTy && DestWidth == 128) |
| 8158 | return Signed ? Int128Ty : UnsignedInt128Ty; |
| 8159 | return QualTy; |
| 8160 | } |
| 8161 | |
| 8162 | /// getRealTypeForBitwidth - |
| 8163 | /// sets floating point QualTy according to specified bitwidth. |
| 8164 | /// Returns empty type if there is no appropriate target types. |
| 8165 | QualType ASTContext::getRealTypeForBitwidth(unsigned DestWidth) const { |
| 8166 | TargetInfo::RealType Ty = getTargetInfo().getRealTypeByWidth(DestWidth); |
| 8167 | switch (Ty) { |
| 8168 | case TargetInfo::Float: |
| 8169 | return FloatTy; |
| 8170 | case TargetInfo::Double: |
| 8171 | return DoubleTy; |
| 8172 | case TargetInfo::LongDouble: |
| 8173 | return LongDoubleTy; |
| 8174 | case TargetInfo::NoFloat: |
| 8175 | return QualType(); |
| 8176 | } |
| 8177 | |
| 8178 | llvm_unreachable("Unhandled TargetInfo::RealType value"); |
| 8179 | } |
| 8180 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 8181 | void ASTContext::setManglingNumber(const NamedDecl *ND, unsigned Number) { |
| 8182 | if (Number > 1) |
| 8183 | MangleNumbers[ND] = Number; |
David Blaikie | 095deba | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 8184 | } |
| 8185 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 8186 | unsigned ASTContext::getManglingNumber(const NamedDecl *ND) const { |
| 8187 | llvm::DenseMap<const NamedDecl *, unsigned>::const_iterator I = |
| 8188 | MangleNumbers.find(ND); |
| 8189 | return I != MangleNumbers.end() ? I->second : 1; |
David Blaikie | 095deba | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 8190 | } |
| 8191 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 8192 | void ASTContext::setStaticLocalNumber(const VarDecl *VD, unsigned Number) { |
| 8193 | if (Number > 1) |
| 8194 | StaticLocalNumbers[VD] = Number; |
| 8195 | } |
| 8196 | |
| 8197 | unsigned ASTContext::getStaticLocalNumber(const VarDecl *VD) const { |
| 8198 | llvm::DenseMap<const VarDecl *, unsigned>::const_iterator I = |
| 8199 | StaticLocalNumbers.find(VD); |
| 8200 | return I != StaticLocalNumbers.end() ? I->second : 1; |
| 8201 | } |
| 8202 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 8203 | MangleNumberingContext & |
| 8204 | ASTContext::getManglingNumberContext(const DeclContext *DC) { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 8205 | assert(LangOpts.CPlusPlus); // We don't need mangling numbers for plain C. |
| 8206 | MangleNumberingContext *&MCtx = MangleNumberingContexts[DC]; |
| 8207 | if (!MCtx) |
| 8208 | MCtx = createMangleNumberingContext(); |
| 8209 | return *MCtx; |
| 8210 | } |
| 8211 | |
| 8212 | MangleNumberingContext *ASTContext::createMangleNumberingContext() const { |
| 8213 | return ABI->createMangleNumberingContext(); |
Douglas Gregor | 6379854 | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 8214 | } |
| 8215 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 8216 | const CXXConstructorDecl * |
| 8217 | ASTContext::getCopyConstructorForExceptionObject(CXXRecordDecl *RD) { |
| 8218 | return ABI->getCopyConstructorForExceptionObject( |
| 8219 | cast<CXXRecordDecl>(RD->getFirstDecl())); |
| 8220 | } |
| 8221 | |
| 8222 | void ASTContext::addCopyConstructorForExceptionObject(CXXRecordDecl *RD, |
| 8223 | CXXConstructorDecl *CD) { |
| 8224 | return ABI->addCopyConstructorForExceptionObject( |
| 8225 | cast<CXXRecordDecl>(RD->getFirstDecl()), |
| 8226 | cast<CXXConstructorDecl>(CD->getFirstDecl())); |
| 8227 | } |
| 8228 | |
David Majnemer | dfa6d20 | 2015-03-11 18:36:39 +0000 | [diff] [blame] | 8229 | void ASTContext::addDefaultArgExprForConstructor(const CXXConstructorDecl *CD, |
| 8230 | unsigned ParmIdx, Expr *DAE) { |
| 8231 | ABI->addDefaultArgExprForConstructor( |
| 8232 | cast<CXXConstructorDecl>(CD->getFirstDecl()), ParmIdx, DAE); |
| 8233 | } |
| 8234 | |
| 8235 | Expr *ASTContext::getDefaultArgExprForConstructor(const CXXConstructorDecl *CD, |
| 8236 | unsigned ParmIdx) { |
| 8237 | return ABI->getDefaultArgExprForConstructor( |
| 8238 | cast<CXXConstructorDecl>(CD->getFirstDecl()), ParmIdx); |
| 8239 | } |
| 8240 | |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 8241 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 8242 | ParamIndices[D] = index; |
| 8243 | } |
| 8244 | |
| 8245 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 8246 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 8247 | assert(I != ParamIndices.end() && |
| 8248 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 8249 | return I->second; |
| 8250 | } |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 8251 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 8252 | APValue * |
| 8253 | ASTContext::getMaterializedTemporaryValue(const MaterializeTemporaryExpr *E, |
| 8254 | bool MayCreate) { |
| 8255 | assert(E && E->getStorageDuration() == SD_Static && |
| 8256 | "don't need to cache the computed value for this temporary"); |
| 8257 | if (MayCreate) |
| 8258 | return &MaterializedTemporaryValues[E]; |
| 8259 | |
| 8260 | llvm::DenseMap<const MaterializeTemporaryExpr *, APValue>::iterator I = |
| 8261 | MaterializedTemporaryValues.find(E); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 8262 | return I == MaterializedTemporaryValues.end() ? nullptr : &I->second; |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 8263 | } |
| 8264 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 8265 | bool ASTContext::AtomicUsesUnsupportedLibcall(const AtomicExpr *E) const { |
| 8266 | const llvm::Triple &T = getTargetInfo().getTriple(); |
| 8267 | if (!T.isOSDarwin()) |
| 8268 | return false; |
| 8269 | |
Bob Wilson | 2c82c3d | 2013-11-02 23:27:49 +0000 | [diff] [blame] | 8270 | if (!(T.isiOS() && T.isOSVersionLT(7)) && |
| 8271 | !(T.isMacOSX() && T.isOSVersionLT(10, 9))) |
| 8272 | return false; |
| 8273 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 8274 | QualType AtomicTy = E->getPtr()->getType()->getPointeeType(); |
| 8275 | CharUnits sizeChars = getTypeSizeInChars(AtomicTy); |
| 8276 | uint64_t Size = sizeChars.getQuantity(); |
| 8277 | CharUnits alignChars = getTypeAlignInChars(AtomicTy); |
| 8278 | unsigned Align = alignChars.getQuantity(); |
| 8279 | unsigned MaxInlineWidthInBits = getTargetInfo().getMaxAtomicInlineWidth(); |
| 8280 | return (Size != Align || toBits(sizeChars) > MaxInlineWidthInBits); |
| 8281 | } |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8282 | |
| 8283 | namespace { |
| 8284 | |
| 8285 | /// \brief A \c RecursiveASTVisitor that builds a map from nodes to their |
| 8286 | /// parents as defined by the \c RecursiveASTVisitor. |
| 8287 | /// |
| 8288 | /// Note that the relationship described here is purely in terms of AST |
| 8289 | /// traversal - there are other relationships (for example declaration context) |
| 8290 | /// in the AST that are better modeled by special matchers. |
| 8291 | /// |
| 8292 | /// FIXME: Currently only builds up the map using \c Stmt and \c Decl nodes. |
| 8293 | class ParentMapASTVisitor : public RecursiveASTVisitor<ParentMapASTVisitor> { |
| 8294 | |
| 8295 | public: |
| 8296 | /// \brief Builds and returns the translation unit's parent map. |
| 8297 | /// |
| 8298 | /// The caller takes ownership of the returned \c ParentMap. |
| 8299 | static ASTContext::ParentMap *buildMap(TranslationUnitDecl &TU) { |
| 8300 | ParentMapASTVisitor Visitor(new ASTContext::ParentMap); |
| 8301 | Visitor.TraverseDecl(&TU); |
| 8302 | return Visitor.Parents; |
| 8303 | } |
| 8304 | |
| 8305 | private: |
| 8306 | typedef RecursiveASTVisitor<ParentMapASTVisitor> VisitorBase; |
| 8307 | |
| 8308 | ParentMapASTVisitor(ASTContext::ParentMap *Parents) : Parents(Parents) { |
| 8309 | } |
| 8310 | |
| 8311 | bool shouldVisitTemplateInstantiations() const { |
| 8312 | return true; |
| 8313 | } |
| 8314 | bool shouldVisitImplicitCode() const { |
| 8315 | return true; |
| 8316 | } |
| 8317 | // Disables data recursion. We intercept Traverse* methods in the RAV, which |
| 8318 | // are not triggered during data recursion. |
| 8319 | bool shouldUseDataRecursionFor(clang::Stmt *S) const { |
| 8320 | return false; |
| 8321 | } |
| 8322 | |
| 8323 | template <typename T> |
| 8324 | bool TraverseNode(T *Node, bool(VisitorBase:: *traverse) (T *)) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 8325 | if (!Node) |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8326 | return true; |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 8327 | if (ParentStack.size() > 0) { |
Samuel Benzaquen | 3ca0a7b | 2014-06-13 13:31:40 +0000 | [diff] [blame] | 8328 | // FIXME: Currently we add the same parent multiple times, but only |
| 8329 | // when no memoization data is available for the type. |
| 8330 | // For example when we visit all subexpressions of template |
| 8331 | // instantiations; this is suboptimal, but benign: the only way to |
| 8332 | // visit those is with hasAncestor / hasParent, and those do not create |
| 8333 | // new matches. |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8334 | // The plan is to enable DynTypedNode to be storable in a map or hash |
| 8335 | // map. The main problem there is to implement hash functions / |
| 8336 | // comparison operators for all types that DynTypedNode supports that |
| 8337 | // do not have pointer identity. |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 8338 | auto &NodeOrVector = (*Parents)[Node]; |
| 8339 | if (NodeOrVector.isNull()) { |
| 8340 | NodeOrVector = new ast_type_traits::DynTypedNode(ParentStack.back()); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 8341 | } else { |
Samuel Benzaquen | 3ca0a7b | 2014-06-13 13:31:40 +0000 | [diff] [blame] | 8342 | if (NodeOrVector.template is<ast_type_traits::DynTypedNode *>()) { |
| 8343 | auto *Node = |
| 8344 | NodeOrVector.template get<ast_type_traits::DynTypedNode *>(); |
| 8345 | auto *Vector = new ASTContext::ParentVector(1, *Node); |
| 8346 | NodeOrVector = Vector; |
| 8347 | delete Node; |
| 8348 | } |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 8349 | assert(NodeOrVector.template is<ASTContext::ParentVector *>()); |
Samuel Benzaquen | 3ca0a7b | 2014-06-13 13:31:40 +0000 | [diff] [blame] | 8350 | |
| 8351 | auto *Vector = |
| 8352 | NodeOrVector.template get<ASTContext::ParentVector *>(); |
| 8353 | // Skip duplicates for types that have memoization data. |
| 8354 | // We must check that the type has memoization data before calling |
| 8355 | // std::find() because DynTypedNode::operator== can't compare all |
| 8356 | // types. |
| 8357 | bool Found = ParentStack.back().getMemoizationData() && |
| 8358 | std::find(Vector->begin(), Vector->end(), |
| 8359 | ParentStack.back()) != Vector->end(); |
| 8360 | if (!Found) |
| 8361 | Vector->push_back(ParentStack.back()); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 8362 | } |
| 8363 | } |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8364 | ParentStack.push_back(ast_type_traits::DynTypedNode::create(*Node)); |
| 8365 | bool Result = (this ->* traverse) (Node); |
| 8366 | ParentStack.pop_back(); |
| 8367 | return Result; |
| 8368 | } |
| 8369 | |
| 8370 | bool TraverseDecl(Decl *DeclNode) { |
| 8371 | return TraverseNode(DeclNode, &VisitorBase::TraverseDecl); |
| 8372 | } |
| 8373 | |
| 8374 | bool TraverseStmt(Stmt *StmtNode) { |
| 8375 | return TraverseNode(StmtNode, &VisitorBase::TraverseStmt); |
| 8376 | } |
| 8377 | |
| 8378 | ASTContext::ParentMap *Parents; |
| 8379 | llvm::SmallVector<ast_type_traits::DynTypedNode, 16> ParentStack; |
| 8380 | |
| 8381 | friend class RecursiveASTVisitor<ParentMapASTVisitor>; |
| 8382 | }; |
| 8383 | |
| 8384 | } // end namespace |
| 8385 | |
Benjamin Kramer | 0793529 | 2014-10-04 17:01:26 +0000 | [diff] [blame] | 8386 | ArrayRef<ast_type_traits::DynTypedNode> |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8387 | ASTContext::getParents(const ast_type_traits::DynTypedNode &Node) { |
| 8388 | assert(Node.getMemoizationData() && |
| 8389 | "Invariant broken: only nodes that support memoization may be " |
| 8390 | "used in the parent map."); |
| 8391 | if (!AllParents) { |
| 8392 | // We always need to run over the whole translation unit, as |
| 8393 | // hasAncestor can escape any subtree. |
| 8394 | AllParents.reset( |
| 8395 | ParentMapASTVisitor::buildMap(*getTranslationUnitDecl())); |
| 8396 | } |
| 8397 | ParentMap::const_iterator I = AllParents->find(Node.getMemoizationData()); |
| 8398 | if (I == AllParents->end()) { |
Benjamin Kramer | 0793529 | 2014-10-04 17:01:26 +0000 | [diff] [blame] | 8399 | return None; |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8400 | } |
Benjamin Kramer | 0793529 | 2014-10-04 17:01:26 +0000 | [diff] [blame] | 8401 | if (auto *N = I->second.dyn_cast<ast_type_traits::DynTypedNode *>()) { |
| 8402 | return llvm::makeArrayRef(N, 1); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 8403 | } |
Benjamin Kramer | 0793529 | 2014-10-04 17:01:26 +0000 | [diff] [blame] | 8404 | return *I->second.get<ParentVector *>(); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 8405 | } |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 8406 | |
| 8407 | bool |
| 8408 | ASTContext::ObjCMethodsAreEqual(const ObjCMethodDecl *MethodDecl, |
| 8409 | const ObjCMethodDecl *MethodImpl) { |
| 8410 | // No point trying to match an unavailable/deprecated mothod. |
| 8411 | if (MethodDecl->hasAttr<UnavailableAttr>() |
| 8412 | || MethodDecl->hasAttr<DeprecatedAttr>()) |
| 8413 | return false; |
| 8414 | if (MethodDecl->getObjCDeclQualifier() != |
| 8415 | MethodImpl->getObjCDeclQualifier()) |
| 8416 | return false; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8417 | if (!hasSameType(MethodDecl->getReturnType(), MethodImpl->getReturnType())) |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 8418 | return false; |
| 8419 | |
| 8420 | if (MethodDecl->param_size() != MethodImpl->param_size()) |
| 8421 | return false; |
| 8422 | |
| 8423 | for (ObjCMethodDecl::param_const_iterator IM = MethodImpl->param_begin(), |
| 8424 | IF = MethodDecl->param_begin(), EM = MethodImpl->param_end(), |
| 8425 | EF = MethodDecl->param_end(); |
| 8426 | IM != EM && IF != EF; ++IM, ++IF) { |
| 8427 | const ParmVarDecl *DeclVar = (*IF); |
| 8428 | const ParmVarDecl *ImplVar = (*IM); |
| 8429 | if (ImplVar->getObjCDeclQualifier() != DeclVar->getObjCDeclQualifier()) |
| 8430 | return false; |
| 8431 | if (!hasSameType(DeclVar->getType(), ImplVar->getType())) |
| 8432 | return false; |
| 8433 | } |
| 8434 | return (MethodDecl->isVariadic() == MethodImpl->isVariadic()); |
| 8435 | |
| 8436 | } |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 8437 | |
| 8438 | // Explicitly instantiate this in case a Redeclarable<T> is used from a TU that |
| 8439 | // doesn't include ASTContext.h |
| 8440 | template |
| 8441 | clang::LazyGenerationalUpdatePtr< |
| 8442 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::ValueType |
| 8443 | clang::LazyGenerationalUpdatePtr< |
| 8444 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::makeValue( |
| 8445 | const clang::ASTContext &Ctx, Decl *Value); |