Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 1 | //===- ASTContext.cpp - Context to hold long-lived AST nodes --------------===// |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 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" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 16 | #include "clang/AST/APValue.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTMutationListener.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTTypeTraits.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/Attr.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 20 | #include "clang/AST/AttrIterator.h" |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 21 | #include "clang/AST/CharUnits.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 22 | #include "clang/AST/Comment.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 23 | #include "clang/AST/Decl.h" |
| 24 | #include "clang/AST/DeclBase.h" |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclCXX.h" |
Chandler Carruth | aa36b89 | 2015-12-30 03:40:23 +0000 | [diff] [blame] | 26 | #include "clang/AST/DeclContextInternals.h" |
Steve Naroff | 67391b8 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 27 | #include "clang/AST/DeclObjC.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 28 | #include "clang/AST/DeclOpenMP.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 29 | #include "clang/AST/DeclTemplate.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 30 | #include "clang/AST/DeclarationName.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 31 | #include "clang/AST/Expr.h" |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 32 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 33 | #include "clang/AST/ExternalASTSource.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 34 | #include "clang/AST/Mangle.h" |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 35 | #include "clang/AST/MangleNumberingContext.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 36 | #include "clang/AST/NestedNameSpecifier.h" |
| 37 | #include "clang/AST/RawCommentList.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 38 | #include "clang/AST/RecordLayout.h" |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 39 | #include "clang/AST/RecursiveASTVisitor.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 40 | #include "clang/AST/Stmt.h" |
| 41 | #include "clang/AST/TemplateBase.h" |
| 42 | #include "clang/AST/TemplateName.h" |
| 43 | #include "clang/AST/Type.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 44 | #include "clang/AST/TypeLoc.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 45 | #include "clang/AST/UnresolvedSet.h" |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 46 | #include "clang/AST/VTableBuilder.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 47 | #include "clang/Basic/AddressSpaces.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 48 | #include "clang/Basic/Builtins.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 49 | #include "clang/Basic/CommentOptions.h" |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 50 | #include "clang/Basic/ExceptionSpecificationType.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 51 | #include "clang/Basic/IdentifierTable.h" |
| 52 | #include "clang/Basic/LLVM.h" |
| 53 | #include "clang/Basic/LangOptions.h" |
| 54 | #include "clang/Basic/Linkage.h" |
| 55 | #include "clang/Basic/ObjCRuntime.h" |
| 56 | #include "clang/Basic/SanitizerBlacklist.h" |
| 57 | #include "clang/Basic/SourceLocation.h" |
Chris Lattner | d286851 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 58 | #include "clang/Basic/SourceManager.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 59 | #include "clang/Basic/Specifiers.h" |
| 60 | #include "clang/Basic/TargetCXXABI.h" |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 61 | #include "clang/Basic/TargetInfo.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 62 | #include "clang/Basic/XRayLists.h" |
| 63 | #include "llvm/ADT/APInt.h" |
| 64 | #include "llvm/ADT/APSInt.h" |
| 65 | #include "llvm/ADT/ArrayRef.h" |
| 66 | #include "llvm/ADT/DenseMap.h" |
| 67 | #include "llvm/ADT/DenseSet.h" |
| 68 | #include "llvm/ADT/FoldingSet.h" |
| 69 | #include "llvm/ADT/None.h" |
| 70 | #include "llvm/ADT/Optional.h" |
| 71 | #include "llvm/ADT/PointerUnion.h" |
| 72 | #include "llvm/ADT/STLExtras.h" |
| 73 | #include "llvm/ADT/SmallPtrSet.h" |
| 74 | #include "llvm/ADT/SmallVector.h" |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 75 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 76 | #include "llvm/ADT/StringRef.h" |
Robert Lytton | eaf6f36 | 2013-11-12 10:09:34 +0000 | [diff] [blame] | 77 | #include "llvm/ADT/Triple.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 78 | #include "llvm/Support/Capacity.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 79 | #include "llvm/Support/Casting.h" |
| 80 | #include "llvm/Support/Compiler.h" |
| 81 | #include "llvm/Support/ErrorHandling.h" |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 82 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 83 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 84 | #include <algorithm> |
| 85 | #include <cassert> |
| 86 | #include <cstddef> |
| 87 | #include <cstdint> |
| 88 | #include <cstdlib> |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 89 | #include <map> |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 90 | #include <memory> |
| 91 | #include <string> |
| 92 | #include <tuple> |
| 93 | #include <utility> |
Anders Carlsson | a4267a6 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 94 | |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 95 | using namespace clang; |
| 96 | |
Douglas Gregor | 9672f92 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 97 | unsigned ASTContext::NumImplicitDefaultConstructors; |
| 98 | unsigned ASTContext::NumImplicitDefaultConstructorsDeclared; |
Douglas Gregor | a6d6950 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 99 | unsigned ASTContext::NumImplicitCopyConstructors; |
| 100 | unsigned ASTContext::NumImplicitCopyConstructorsDeclared; |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 101 | unsigned ASTContext::NumImplicitMoveConstructors; |
| 102 | unsigned ASTContext::NumImplicitMoveConstructorsDeclared; |
Douglas Gregor | 330b9cf | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 103 | unsigned ASTContext::NumImplicitCopyAssignmentOperators; |
| 104 | unsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared; |
Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 105 | unsigned ASTContext::NumImplicitMoveAssignmentOperators; |
| 106 | unsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared; |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 107 | unsigned ASTContext::NumImplicitDestructors; |
| 108 | unsigned ASTContext::NumImplicitDestructorsDeclared; |
| 109 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 110 | enum FloatingRank { |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 111 | Float16Rank, HalfRank, FloatRank, DoubleRank, LongDoubleRank, Float128Rank |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
Dmitri Gribenko | f26054f | 2012-07-11 21:38:39 +0000 | [diff] [blame] | 114 | RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 115 | if (!CommentsLoaded && ExternalSource) { |
| 116 | ExternalSource->ReadComments(); |
Dmitri Gribenko | 9ee0e30 | 2014-03-27 15:40:39 +0000 | [diff] [blame] | 117 | |
| 118 | #ifndef NDEBUG |
| 119 | ArrayRef<RawComment *> RawComments = Comments.getComments(); |
| 120 | assert(std::is_sorted(RawComments.begin(), RawComments.end(), |
| 121 | BeforeThanCompare<RawComment>(SourceMgr))); |
| 122 | #endif |
| 123 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 124 | CommentsLoaded = true; |
| 125 | } |
| 126 | |
| 127 | assert(D); |
| 128 | |
Dmitri Gribenko | df17d64 | 2012-06-28 16:19:39 +0000 | [diff] [blame] | 129 | // User can not attach documentation to implicit declarations. |
| 130 | if (D->isImplicit()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 131 | return nullptr; |
Dmitri Gribenko | df17d64 | 2012-06-28 16:19:39 +0000 | [diff] [blame] | 132 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 133 | // User can not attach documentation to implicit instantiations. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 134 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 135 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 136 | return nullptr; |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 139 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 140 | if (VD->isStaticDataMember() && |
| 141 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 142 | return nullptr; |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 145 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(D)) { |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 146 | if (CRD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 147 | return nullptr; |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 150 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 151 | TemplateSpecializationKind TSK = CTSD->getSpecializationKind(); |
| 152 | if (TSK == TSK_ImplicitInstantiation || |
| 153 | TSK == TSK_Undeclared) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 154 | return nullptr; |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 157 | if (const auto *ED = dyn_cast<EnumDecl>(D)) { |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 158 | if (ED->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 159 | return nullptr; |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 160 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 161 | if (const auto *TD = dyn_cast<TagDecl>(D)) { |
Fariborz Jahanian | 799a403 | 2013-04-17 21:05:20 +0000 | [diff] [blame] | 162 | // When tag declaration (but not definition!) is part of the |
| 163 | // decl-specifier-seq of some other declaration, it doesn't get comment |
| 164 | if (TD->isEmbeddedInDeclarator() && !TD->isCompleteDefinition()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 165 | return nullptr; |
Fariborz Jahanian | 799a403 | 2013-04-17 21:05:20 +0000 | [diff] [blame] | 166 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 167 | // TODO: handle comments for function parameters properly. |
| 168 | if (isa<ParmVarDecl>(D)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 169 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 170 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 171 | // TODO: we could look up template parameter documentation in the template |
| 172 | // documentation. |
| 173 | if (isa<TemplateTypeParmDecl>(D) || |
| 174 | isa<NonTypeTemplateParmDecl>(D) || |
| 175 | isa<TemplateTemplateParmDecl>(D)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 176 | return nullptr; |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 177 | |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 178 | ArrayRef<RawComment *> RawComments = Comments.getComments(); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 179 | |
| 180 | // If there are no comments anywhere, we won't find anything. |
| 181 | if (RawComments.empty()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 182 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 183 | |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 184 | // Find declaration location. |
| 185 | // For Objective-C declarations we generally don't expect to have multiple |
| 186 | // declarators, thus use declaration starting location as the "declaration |
| 187 | // location". |
| 188 | // For all other declarations multiple declarators are used quite frequently, |
| 189 | // so we use the location of the identifier as the "declaration location". |
| 190 | SourceLocation DeclLoc; |
| 191 | if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) || |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 192 | isa<ObjCPropertyDecl>(D) || |
Dmitri Gribenko | 7f4b377 | 2012-08-02 20:49:51 +0000 | [diff] [blame] | 193 | isa<RedeclarableTemplateDecl>(D) || |
| 194 | isa<ClassTemplateSpecializationDecl>(D)) |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 195 | DeclLoc = D->getLocStart(); |
Fariborz Jahanian | b64e95f | 2013-07-24 22:58:51 +0000 | [diff] [blame] | 196 | else { |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 197 | DeclLoc = D->getLocation(); |
Dmitri Gribenko | ef099dc | 2014-03-27 16:40:51 +0000 | [diff] [blame] | 198 | if (DeclLoc.isMacroID()) { |
| 199 | if (isa<TypedefDecl>(D)) { |
| 200 | // If location of the typedef name is in a macro, it is because being |
| 201 | // declared via a macro. Try using declaration's starting location as |
| 202 | // the "declaration location". |
| 203 | DeclLoc = D->getLocStart(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 204 | } else if (const auto *TD = dyn_cast<TagDecl>(D)) { |
Dmitri Gribenko | ef099dc | 2014-03-27 16:40:51 +0000 | [diff] [blame] | 205 | // If location of the tag decl is inside a macro, but the spelling of |
| 206 | // the tag name comes from a macro argument, it looks like a special |
| 207 | // macro like NS_ENUM is being used to define the tag decl. In that |
| 208 | // case, adjust the source location to the expansion loc so that we can |
| 209 | // attach the comment to the tag decl. |
| 210 | if (SourceMgr.isMacroArgExpansion(DeclLoc) && |
| 211 | TD->isCompleteDefinition()) |
| 212 | DeclLoc = SourceMgr.getExpansionLoc(DeclLoc); |
| 213 | } |
| 214 | } |
Fariborz Jahanian | b64e95f | 2013-07-24 22:58:51 +0000 | [diff] [blame] | 215 | } |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 216 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 217 | // If the declaration doesn't map directly to a location in a file, we |
| 218 | // can't find the comment. |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 219 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 220 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 221 | |
| 222 | // Find the comment that occurs just after this declaration. |
Dmitri Gribenko | 82ea947 | 2012-07-17 22:01:09 +0000 | [diff] [blame] | 223 | ArrayRef<RawComment *>::iterator Comment; |
| 224 | { |
| 225 | // When searching for comments during parsing, the comment we are looking |
| 226 | // for is usually among the last two comments we parsed -- check them |
| 227 | // first. |
Dmitri Gribenko | a7d16ce | 2013-04-10 15:35:17 +0000 | [diff] [blame] | 228 | RawComment CommentAtDeclLoc( |
David L. Jones | 13d5a87 | 2018-03-02 00:07:45 +0000 | [diff] [blame] | 229 | SourceMgr, SourceRange(DeclLoc), LangOpts.CommentOpts, false); |
Dmitri Gribenko | 82ea947 | 2012-07-17 22:01:09 +0000 | [diff] [blame] | 230 | BeforeThanCompare<RawComment> Compare(SourceMgr); |
| 231 | ArrayRef<RawComment *>::iterator MaybeBeforeDecl = RawComments.end() - 1; |
| 232 | bool Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); |
| 233 | if (!Found && RawComments.size() >= 2) { |
| 234 | MaybeBeforeDecl--; |
| 235 | Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc); |
| 236 | } |
| 237 | |
| 238 | if (Found) { |
| 239 | Comment = MaybeBeforeDecl + 1; |
| 240 | assert(Comment == std::lower_bound(RawComments.begin(), RawComments.end(), |
| 241 | &CommentAtDeclLoc, Compare)); |
| 242 | } else { |
| 243 | // Slow path. |
| 244 | Comment = std::lower_bound(RawComments.begin(), RawComments.end(), |
| 245 | &CommentAtDeclLoc, Compare); |
| 246 | } |
| 247 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 248 | |
| 249 | // Decompose the location for the declaration and find the beginning of the |
| 250 | // file buffer. |
| 251 | std::pair<FileID, unsigned> DeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLoc); |
| 252 | |
| 253 | // First check whether we have a trailing comment. |
| 254 | if (Comment != RawComments.end() && |
David L. Jones | 13d5a87 | 2018-03-02 00:07:45 +0000 | [diff] [blame] | 255 | ((*Comment)->isDocumentation() || LangOpts.CommentOpts.ParseAllComments) |
| 256 | && (*Comment)->isTrailingComment() && |
Fariborz Jahanian | fad2854 | 2013-08-06 23:29:00 +0000 | [diff] [blame] | 257 | (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D) || |
Fariborz Jahanian | 3ab6222 | 2013-08-07 16:40:29 +0000 | [diff] [blame] | 258 | isa<ObjCMethodDecl>(D) || isa<ObjCPropertyDecl>(D))) { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 259 | std::pair<FileID, unsigned> CommentBeginDecomp |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 260 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getBegin()); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 261 | // Check that Doxygen trailing comment comes after the declaration, starts |
| 262 | // on the same line and in the same file as the declaration. |
| 263 | if (DeclLocDecomp.first == CommentBeginDecomp.first && |
| 264 | SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second) |
| 265 | == SourceMgr.getLineNumber(CommentBeginDecomp.first, |
| 266 | CommentBeginDecomp.second)) { |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 267 | return *Comment; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | // The comment just after the declaration was not a trailing comment. |
| 272 | // Let's look at the previous comment. |
| 273 | if (Comment == RawComments.begin()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 274 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 275 | --Comment; |
| 276 | |
| 277 | // Check that we actually have a non-member Doxygen comment. |
David L. Jones | 13d5a87 | 2018-03-02 00:07:45 +0000 | [diff] [blame] | 278 | if (!((*Comment)->isDocumentation() || |
| 279 | LangOpts.CommentOpts.ParseAllComments) || |
| 280 | (*Comment)->isTrailingComment()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 281 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 282 | |
| 283 | // Decompose the end of the comment. |
| 284 | std::pair<FileID, unsigned> CommentEndDecomp |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 285 | = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getEnd()); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 286 | |
| 287 | // If the comment and the declaration aren't in the same file, then they |
| 288 | // aren't related. |
| 289 | if (DeclLocDecomp.first != CommentEndDecomp.first) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 290 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 291 | |
| 292 | // Get the corresponding buffer. |
| 293 | bool Invalid = false; |
| 294 | const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first, |
| 295 | &Invalid).data(); |
| 296 | if (Invalid) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 297 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 298 | |
| 299 | // Extract text between the comment and declaration. |
| 300 | StringRef Text(Buffer + CommentEndDecomp.second, |
| 301 | DeclLocDecomp.second - CommentEndDecomp.second); |
| 302 | |
Dmitri Gribenko | 7e8729b | 2012-06-27 23:43:37 +0000 | [diff] [blame] | 303 | // There should be no other declarations or preprocessor directives between |
| 304 | // comment and declaration. |
Argyrios Kyrtzidis | b534d3a | 2013-07-26 18:38:12 +0000 | [diff] [blame] | 305 | if (Text.find_first_of(";{}#@") != StringRef::npos) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 306 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 307 | |
Dmitri Gribenko | 7dd29d4 | 2012-07-06 18:19:34 +0000 | [diff] [blame] | 308 | return *Comment; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 311 | /// If we have a 'templated' declaration for a template, adjust 'D' to |
| 312 | /// refer to the actual template. |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 313 | /// If we have an implicit instantiation, adjust 'D' to refer to template. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 314 | static const Decl *adjustDeclToTemplate(const Decl *D) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 315 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 316 | // Is this function declaration part of a function template? |
Douglas Gregor | 35ceb27 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 317 | if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate()) |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 318 | return FTD; |
| 319 | |
| 320 | // Nothing to do if function is not an implicit instantiation. |
| 321 | if (FD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
| 322 | return D; |
| 323 | |
| 324 | // Function is an implicit instantiation of a function template? |
| 325 | if (const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate()) |
| 326 | return FTD; |
| 327 | |
| 328 | // Function is instantiated from a member definition of a class template? |
| 329 | if (const FunctionDecl *MemberDecl = |
| 330 | FD->getInstantiatedFromMemberFunction()) |
| 331 | return MemberDecl; |
| 332 | |
| 333 | return D; |
Douglas Gregor | 35ceb27 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 334 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 335 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 336 | // Static data member is instantiated from a member definition of a class |
| 337 | // template? |
| 338 | if (VD->isStaticDataMember()) |
| 339 | if (const VarDecl *MemberDecl = VD->getInstantiatedFromStaticDataMember()) |
| 340 | return MemberDecl; |
| 341 | |
| 342 | return D; |
| 343 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 344 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(D)) { |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 345 | // Is this class declaration part of a class template? |
| 346 | if (const ClassTemplateDecl *CTD = CRD->getDescribedClassTemplate()) |
| 347 | return CTD; |
| 348 | |
| 349 | // Class is an implicit instantiation of a class template or partial |
| 350 | // specialization? |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 351 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(CRD)) { |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 352 | if (CTSD->getSpecializationKind() != TSK_ImplicitInstantiation) |
| 353 | return D; |
| 354 | llvm::PointerUnion<ClassTemplateDecl *, |
| 355 | ClassTemplatePartialSpecializationDecl *> |
| 356 | PU = CTSD->getSpecializedTemplateOrPartial(); |
| 357 | return PU.is<ClassTemplateDecl*>() ? |
| 358 | static_cast<const Decl*>(PU.get<ClassTemplateDecl *>()) : |
| 359 | static_cast<const Decl*>( |
| 360 | PU.get<ClassTemplatePartialSpecializationDecl *>()); |
| 361 | } |
| 362 | |
| 363 | // Class is instantiated from a member definition of a class template? |
| 364 | if (const MemberSpecializationInfo *Info = |
| 365 | CRD->getMemberSpecializationInfo()) |
| 366 | return Info->getInstantiatedFrom(); |
| 367 | |
| 368 | return D; |
| 369 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 370 | if (const auto *ED = dyn_cast<EnumDecl>(D)) { |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 371 | // Enum is instantiated from a member definition of a class template? |
| 372 | if (const EnumDecl *MemberDecl = ED->getInstantiatedFromMemberEnum()) |
| 373 | return MemberDecl; |
| 374 | |
| 375 | return D; |
| 376 | } |
| 377 | // FIXME: Adjust alias templates? |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 378 | return D; |
| 379 | } |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 380 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 381 | const RawComment *ASTContext::getRawCommentForAnyRedecl( |
| 382 | const Decl *D, |
| 383 | const Decl **OriginalDecl) const { |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 384 | D = adjustDeclToTemplate(D); |
Douglas Gregor | 35ceb27 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 385 | |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 386 | // Check whether we have cached a comment for this declaration already. |
| 387 | { |
| 388 | llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos = |
| 389 | RedeclComments.find(D); |
| 390 | if (Pos != RedeclComments.end()) { |
| 391 | const RawCommentAndCacheFlags &Raw = Pos->second; |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 392 | if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) { |
| 393 | if (OriginalDecl) |
| 394 | *OriginalDecl = Raw.getOriginalDecl(); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 395 | return Raw.getRaw(); |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 396 | } |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 397 | } |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 398 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 399 | |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 400 | // Search for comments attached to declarations in the redeclaration chain. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 401 | const RawComment *RC = nullptr; |
| 402 | const Decl *OriginalDeclForRC = nullptr; |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 403 | for (auto I : D->redecls()) { |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 404 | llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos = |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 405 | RedeclComments.find(I); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 406 | if (Pos != RedeclComments.end()) { |
| 407 | const RawCommentAndCacheFlags &Raw = Pos->second; |
| 408 | if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) { |
| 409 | RC = Raw.getRaw(); |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 410 | OriginalDeclForRC = Raw.getOriginalDecl(); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | } else { |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 414 | RC = getRawCommentForDeclNoCache(I); |
| 415 | OriginalDeclForRC = I; |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 416 | RawCommentAndCacheFlags Raw; |
| 417 | if (RC) { |
Will Wilson | f9de536 | 2015-10-27 17:01:10 +0000 | [diff] [blame] | 418 | // Call order swapped to work around ICE in VS2015 RTM (Release Win32) |
| 419 | // https://connect.microsoft.com/VisualStudio/feedback/details/1741530 |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 420 | Raw.setKind(RawCommentAndCacheFlags::FromDecl); |
Will Wilson | f9de536 | 2015-10-27 17:01:10 +0000 | [diff] [blame] | 421 | Raw.setRaw(RC); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 422 | } else |
| 423 | Raw.setKind(RawCommentAndCacheFlags::NoCommentInDecl); |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 424 | Raw.setOriginalDecl(I); |
| 425 | RedeclComments[I] = Raw; |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 426 | if (RC) |
| 427 | break; |
| 428 | } |
| 429 | } |
| 430 | |
Dmitri Gribenko | 5c8897d | 2012-06-28 16:25:36 +0000 | [diff] [blame] | 431 | // If we found a comment, it should be a documentation comment. |
David L. Jones | 13d5a87 | 2018-03-02 00:07:45 +0000 | [diff] [blame] | 432 | assert(!RC || RC->isDocumentation() || LangOpts.CommentOpts.ParseAllComments); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 433 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 434 | if (OriginalDecl) |
| 435 | *OriginalDecl = OriginalDeclForRC; |
| 436 | |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 437 | // Update cache for every declaration in the redeclaration chain. |
| 438 | RawCommentAndCacheFlags Raw; |
| 439 | Raw.setRaw(RC); |
| 440 | Raw.setKind(RawCommentAndCacheFlags::FromRedecl); |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 441 | Raw.setOriginalDecl(OriginalDeclForRC); |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 442 | |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 443 | for (auto I : D->redecls()) { |
| 444 | RawCommentAndCacheFlags &R = RedeclComments[I]; |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 445 | if (R.getKind() == RawCommentAndCacheFlags::NoCommentInDecl) |
| 446 | R = Raw; |
| 447 | } |
| 448 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 449 | return RC; |
| 450 | } |
| 451 | |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 452 | static void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod, |
| 453 | SmallVectorImpl<const NamedDecl *> &Redeclared) { |
| 454 | const DeclContext *DC = ObjCMethod->getDeclContext(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 455 | if (const auto *IMD = dyn_cast<ObjCImplDecl>(DC)) { |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 456 | const ObjCInterfaceDecl *ID = IMD->getClassInterface(); |
| 457 | if (!ID) |
| 458 | return; |
| 459 | // Add redeclared method here. |
Aaron Ballman | b4a5345 | 2014-03-13 21:57:01 +0000 | [diff] [blame] | 460 | for (const auto *Ext : ID->known_extensions()) { |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 461 | if (ObjCMethodDecl *RedeclaredMethod = |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 462 | Ext->getMethod(ObjCMethod->getSelector(), |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 463 | ObjCMethod->isInstanceMethod())) |
| 464 | Redeclared.push_back(RedeclaredMethod); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 469 | comments::FullComment *ASTContext::cloneFullComment(comments::FullComment *FC, |
| 470 | const Decl *D) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 471 | auto *ThisDeclInfo = new (*this) comments::DeclInfo; |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 472 | ThisDeclInfo->CommentDecl = D; |
| 473 | ThisDeclInfo->IsFilled = false; |
| 474 | ThisDeclInfo->fill(); |
| 475 | ThisDeclInfo->CommentDecl = FC->getDecl(); |
Argyrios Kyrtzidis | 7daabbd | 2014-04-27 22:53:03 +0000 | [diff] [blame] | 476 | if (!ThisDeclInfo->TemplateParameters) |
| 477 | ThisDeclInfo->TemplateParameters = FC->getDeclInfo()->TemplateParameters; |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 478 | comments::FullComment *CFC = |
| 479 | new (*this) comments::FullComment(FC->getBlocks(), |
| 480 | ThisDeclInfo); |
| 481 | return CFC; |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Richard Smith | b39b9d5 | 2013-05-21 05:24:00 +0000 | [diff] [blame] | 484 | comments::FullComment *ASTContext::getLocalCommentForDeclUncached(const Decl *D) const { |
| 485 | const RawComment *RC = getRawCommentForDeclNoCache(D); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 486 | return RC ? RC->parse(*this, nullptr, D) : nullptr; |
Richard Smith | b39b9d5 | 2013-05-21 05:24:00 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 489 | comments::FullComment *ASTContext::getCommentForDecl( |
| 490 | const Decl *D, |
| 491 | const Preprocessor *PP) const { |
Fariborz Jahanian | 096f7c1 | 2013-05-13 17:27:00 +0000 | [diff] [blame] | 492 | if (D->isInvalidDecl()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 493 | return nullptr; |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 494 | D = adjustDeclToTemplate(D); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 495 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 496 | const Decl *Canonical = D->getCanonicalDecl(); |
| 497 | llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos = |
| 498 | ParsedComments.find(Canonical); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 499 | |
| 500 | if (Pos != ParsedComments.end()) { |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 501 | if (Canonical != D) { |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 502 | comments::FullComment *FC = Pos->second; |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 503 | comments::FullComment *CFC = cloneFullComment(FC, D); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 504 | return CFC; |
| 505 | } |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 506 | return Pos->second; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 509 | const Decl *OriginalDecl; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 510 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 511 | const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 512 | if (!RC) { |
| 513 | if (isa<ObjCMethodDecl>(D) || isa<FunctionDecl>(D)) { |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 514 | SmallVector<const NamedDecl*, 8> Overridden; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 515 | const auto *OMD = dyn_cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 516 | if (OMD && OMD->isPropertyAccessor()) |
| 517 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) |
| 518 | if (comments::FullComment *FC = getCommentForDecl(PDecl, PP)) |
| 519 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 37494a1 | 2013-01-12 00:28:34 +0000 | [diff] [blame] | 520 | if (OMD) |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 521 | addRedeclaredMethods(OMD, Overridden); |
| 522 | getOverriddenMethods(dyn_cast<NamedDecl>(D), Overridden); |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 523 | for (unsigned i = 0, e = Overridden.size(); i < e; i++) |
| 524 | if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP)) |
| 525 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 526 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 527 | else if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 528 | // Attach any tag type's documentation to its typedef if latter |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 529 | // does not have one of its own. |
Fariborz Jahanian | 40abf34 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 530 | QualType QT = TD->getUnderlyingType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 531 | if (const auto *TT = QT->getAs<TagType>()) |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 532 | if (const Decl *TD = TT->getDecl()) |
| 533 | if (comments::FullComment *FC = getCommentForDecl(TD, PP)) |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 534 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 40abf34 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 535 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 536 | else if (const auto *IC = dyn_cast<ObjCInterfaceDecl>(D)) { |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 537 | while (IC->getSuperClass()) { |
| 538 | IC = IC->getSuperClass(); |
| 539 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
| 540 | return cloneFullComment(FC, D); |
| 541 | } |
| 542 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 543 | else if (const auto *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 544 | if (const ObjCInterfaceDecl *IC = CD->getClassInterface()) |
| 545 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
| 546 | return cloneFullComment(FC, D); |
| 547 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 548 | else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 549 | if (!(RD = RD->getDefinition())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 550 | return nullptr; |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 551 | // Check non-virtual bases. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 552 | for (const auto &I : RD->bases()) { |
| 553 | if (I.isVirtual() || (I.getAccessSpecifier() != AS_public)) |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 554 | continue; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 555 | QualType Ty = I.getType(); |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 556 | if (Ty.isNull()) |
| 557 | continue; |
| 558 | if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) { |
| 559 | if (!(NonVirtualBase= NonVirtualBase->getDefinition())) |
| 560 | continue; |
| 561 | |
| 562 | if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP)) |
| 563 | return cloneFullComment(FC, D); |
| 564 | } |
| 565 | } |
| 566 | // Check virtual bases. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 567 | for (const auto &I : RD->vbases()) { |
| 568 | if (I.getAccessSpecifier() != AS_public) |
Fariborz Jahanian | 5a2e4a2 | 2013-04-26 23:34:36 +0000 | [diff] [blame] | 569 | continue; |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 570 | QualType Ty = I.getType(); |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 571 | if (Ty.isNull()) |
| 572 | continue; |
| 573 | if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) { |
| 574 | if (!(VirtualBase= VirtualBase->getDefinition())) |
| 575 | continue; |
| 576 | if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP)) |
| 577 | return cloneFullComment(FC, D); |
| 578 | } |
| 579 | } |
| 580 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 581 | return nullptr; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Dmitri Gribenko | bfda9f7 | 2012-08-22 18:12:19 +0000 | [diff] [blame] | 584 | // If the RawComment was attached to other redeclaration of this Decl, we |
| 585 | // should parse the comment in context of that other Decl. This is important |
| 586 | // because comments can contain references to parameter names which can be |
| 587 | // different across redeclarations. |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 588 | if (D != OriginalDecl) |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 589 | return getCommentForDecl(OriginalDecl, PP); |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 590 | |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 591 | comments::FullComment *FC = RC->parse(*this, PP, D); |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 592 | ParsedComments[Canonical] = FC; |
| 593 | return FC; |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 596 | void |
| 597 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
| 598 | TemplateTemplateParmDecl *Parm) { |
| 599 | ID.AddInteger(Parm->getDepth()); |
| 600 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 601 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 602 | |
| 603 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 604 | ID.AddInteger(Params->size()); |
| 605 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 606 | PEnd = Params->end(); |
| 607 | P != PEnd; ++P) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 608 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 609 | ID.AddInteger(0); |
| 610 | ID.AddBoolean(TTP->isParameterPack()); |
| 611 | continue; |
| 612 | } |
| 613 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 614 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 615 | ID.AddInteger(1); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 616 | ID.AddBoolean(NTTP->isParameterPack()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 617 | ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 618 | if (NTTP->isExpandedParameterPack()) { |
| 619 | ID.AddBoolean(true); |
| 620 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 621 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 622 | QualType T = NTTP->getExpansionType(I); |
| 623 | ID.AddPointer(T.getCanonicalType().getAsOpaquePtr()); |
| 624 | } |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 625 | } else |
| 626 | ID.AddBoolean(false); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 627 | continue; |
| 628 | } |
| 629 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 630 | auto *TTP = cast<TemplateTemplateParmDecl>(*P); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 631 | ID.AddInteger(2); |
| 632 | Profile(ID, TTP); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | TemplateTemplateParmDecl * |
| 637 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 638 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 639 | // Check if we already have a canonical template template parameter. |
| 640 | llvm::FoldingSetNodeID ID; |
| 641 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 642 | void *InsertPos = nullptr; |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 643 | CanonicalTemplateTemplateParm *Canonical |
| 644 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 645 | if (Canonical) |
| 646 | return Canonical->getParam(); |
| 647 | |
| 648 | // Build a canonical template parameter list. |
| 649 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 650 | SmallVector<NamedDecl *, 4> CanonParams; |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 651 | CanonParams.reserve(Params->size()); |
| 652 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 653 | PEnd = Params->end(); |
| 654 | P != PEnd; ++P) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 655 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 656 | CanonParams.push_back( |
| 657 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 658 | SourceLocation(), |
| 659 | SourceLocation(), |
| 660 | TTP->getDepth(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 661 | TTP->getIndex(), nullptr, false, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 662 | TTP->isParameterPack())); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 663 | else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 664 | QualType T = getCanonicalType(NTTP->getType()); |
| 665 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 666 | NonTypeTemplateParmDecl *Param; |
| 667 | if (NTTP->isExpandedParameterPack()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 668 | SmallVector<QualType, 2> ExpandedTypes; |
| 669 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 670 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 671 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 672 | ExpandedTInfos.push_back( |
| 673 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 674 | } |
| 675 | |
| 676 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 677 | SourceLocation(), |
| 678 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 679 | NTTP->getDepth(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 680 | NTTP->getPosition(), nullptr, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 681 | T, |
| 682 | TInfo, |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 683 | ExpandedTypes, |
| 684 | ExpandedTInfos); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 685 | } else { |
| 686 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 687 | SourceLocation(), |
| 688 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 689 | NTTP->getDepth(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 690 | NTTP->getPosition(), nullptr, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 691 | T, |
| 692 | NTTP->isParameterPack(), |
| 693 | TInfo); |
| 694 | } |
| 695 | CanonParams.push_back(Param); |
| 696 | |
| 697 | } else |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 698 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 699 | cast<TemplateTemplateParmDecl>(*P))); |
| 700 | } |
| 701 | |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 702 | assert(!TTP->getRequiresClause() && |
| 703 | "Unexpected requires-clause on template template-parameter"); |
George Burgess IV | b7e4e48 | 2016-08-25 01:54:37 +0000 | [diff] [blame] | 704 | Expr *const CanonRequiresClause = nullptr; |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 705 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 706 | TemplateTemplateParmDecl *CanonTTP |
| 707 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
| 708 | SourceLocation(), TTP->getDepth(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 709 | TTP->getPosition(), |
| 710 | TTP->isParameterPack(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 711 | nullptr, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 712 | TemplateParameterList::Create(*this, SourceLocation(), |
| 713 | SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 714 | CanonParams, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 715 | SourceLocation(), |
| 716 | CanonRequiresClause)); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 717 | |
| 718 | // Get the new insert position for the node we care about. |
| 719 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 720 | assert(!Canonical && "Shouldn't be in the map!"); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 721 | (void)Canonical; |
| 722 | |
| 723 | // Create the canonical template template parameter entry. |
| 724 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 725 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 726 | return CanonTTP; |
| 727 | } |
| 728 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 729 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 730 | if (!LangOpts.CPlusPlus) return nullptr; |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 731 | |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 732 | switch (T.getCXXABI().getKind()) { |
Joerg Sonnenberger | daa13aa | 2014-05-13 11:20:16 +0000 | [diff] [blame] | 733 | case TargetCXXABI::GenericARM: // Same as Itanium at this level |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 734 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 735 | case TargetCXXABI::iOS64: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 736 | case TargetCXXABI::WatchOS: |
Joerg Sonnenberger | daa13aa | 2014-05-13 11:20:16 +0000 | [diff] [blame] | 737 | case TargetCXXABI::GenericAArch64: |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 738 | case TargetCXXABI::GenericMIPS: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 739 | case TargetCXXABI::GenericItanium: |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 740 | case TargetCXXABI::WebAssembly: |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 741 | return CreateItaniumCXXABI(*this); |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 742 | case TargetCXXABI::Microsoft: |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 743 | return CreateMicrosoftCXXABI(*this); |
| 744 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 745 | llvm_unreachable("Invalid CXXABI type!"); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 748 | static const LangASMap *getAddressSpaceMap(const TargetInfo &T, |
| 749 | const LangOptions &LOpts) { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 750 | if (LOpts.FakeAddressSpaceMap) { |
| 751 | // The fake address space map must have a distinct entry for each |
| 752 | // language-specific address space. |
| 753 | static const unsigned FakeAddrSpaceMap[] = { |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 754 | 0, // Default |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 755 | 1, // opencl_global |
Egor Churaev | 28f00aa | 2016-12-23 16:11:25 +0000 | [diff] [blame] | 756 | 3, // opencl_local |
| 757 | 2, // opencl_constant |
Yaxun Liu | b7318e0 | 2017-10-13 03:37:48 +0000 | [diff] [blame] | 758 | 0, // opencl_private |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 759 | 4, // opencl_generic |
| 760 | 5, // cuda_device |
| 761 | 6, // cuda_constant |
| 762 | 7 // cuda_shared |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 763 | }; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 764 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 765 | } else { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 766 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 770 | static bool isAddrSpaceMapManglingEnabled(const TargetInfo &TI, |
| 771 | const LangOptions &LangOpts) { |
| 772 | switch (LangOpts.getAddressSpaceMapMangling()) { |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 773 | case LangOptions::ASMM_Target: |
| 774 | return TI.useAddressSpaceMapMangling(); |
| 775 | case LangOptions::ASMM_On: |
| 776 | return true; |
| 777 | case LangOptions::ASMM_Off: |
| 778 | return false; |
| 779 | } |
NAKAMURA Takumi | 5c81ca4 | 2013-09-13 17:12:09 +0000 | [diff] [blame] | 780 | llvm_unreachable("getAddressSpaceMapMangling() doesn't cover anything."); |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Alexey Samsonov | 0b15e34 | 2014-10-15 22:17:27 +0000 | [diff] [blame] | 783 | ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM, |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 784 | IdentifierTable &idents, SelectorTable &sels, |
Alp Toker | 0804343 | 2014-05-03 03:46:04 +0000 | [diff] [blame] | 785 | Builtin::Context &builtins) |
Alexey Samsonov | 0b15e34 | 2014-10-15 22:17:27 +0000 | [diff] [blame] | 786 | : FunctionProtoTypes(this_()), TemplateSpecializationTypes(this_()), |
| 787 | DependentTemplateSpecializationTypes(this_()), |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 788 | SubstTemplateTemplateParmPacks(this_()), SourceMgr(SM), LangOpts(LOpts), |
Alexey Samsonov | a511cdd | 2015-02-04 17:40:08 +0000 | [diff] [blame] | 789 | SanitizerBL(new SanitizerBlacklist(LangOpts.SanitizerBlacklistFiles, SM)), |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 790 | XRayFilter(new XRayFunctionFilter(LangOpts.XRayAlwaysInstrumentFiles, |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame] | 791 | LangOpts.XRayNeverInstrumentFiles, |
| 792 | LangOpts.XRayAttrListFiles, SM)), |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 793 | PrintingPolicy(LOpts), Idents(idents), Selectors(sels), |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 794 | BuiltinInfo(builtins), DeclarationNames(*this), Comments(SM), |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 795 | CommentCommandTraits(BumpAlloc, LOpts.CommentOpts), |
| 796 | CompCategories(this_()), LastSDM(nullptr, 0) { |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 797 | TUDecl = TranslationUnitDecl::Create(*this); |
| 798 | } |
| 799 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 800 | ASTContext::~ASTContext() { |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 801 | ReleaseParentMapEntries(); |
| 802 | |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 803 | // Release the DenseMaps associated with DeclContext objects. |
| 804 | // FIXME: Is this the ideal solution? |
| 805 | ReleaseDeclContextMaps(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 806 | |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 807 | // Call all of the deallocation functions on all of their targets. |
Chandler Carruth | ff5a01a | 2015-12-30 03:00:23 +0000 | [diff] [blame] | 808 | for (auto &Pair : Deallocations) |
| 809 | (Pair.first)(Pair.second); |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 810 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 811 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 812 | // because they can contain DenseMaps. |
| 813 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 814 | const ASTRecordLayout*>::iterator |
| 815 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 816 | // Increment in loop to prevent using deallocated memory. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 817 | if (auto *R = const_cast<ASTRecordLayout *>((I++)->second)) |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 818 | R->Destroy(*this); |
| 819 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 820 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 821 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 822 | // Increment in loop to prevent using deallocated memory. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 823 | if (auto *R = const_cast<ASTRecordLayout *>((I++)->second)) |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 824 | R->Destroy(*this); |
| 825 | } |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 826 | |
| 827 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 828 | AEnd = DeclAttrs.end(); |
| 829 | A != AEnd; ++A) |
| 830 | A->second->~AttrVec(); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 831 | |
David Majnemer | e694f3e | 2015-08-14 14:43:50 +0000 | [diff] [blame] | 832 | for (std::pair<const MaterializeTemporaryExpr *, APValue *> &MTVPair : |
| 833 | MaterializedTemporaryValues) |
| 834 | MTVPair.second->~APValue(); |
| 835 | |
Richard Smith | 423f46f | 2016-07-20 21:38:26 +0000 | [diff] [blame] | 836 | for (const auto &Value : ModuleInitializers) |
| 837 | Value.second->~PerModuleInitializers(); |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 838 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 839 | |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 840 | void ASTContext::ReleaseParentMapEntries() { |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 841 | if (!PointerParents) return; |
| 842 | for (const auto &Entry : *PointerParents) { |
| 843 | if (Entry.second.is<ast_type_traits::DynTypedNode *>()) { |
| 844 | delete Entry.second.get<ast_type_traits::DynTypedNode *>(); |
| 845 | } else if (Entry.second.is<ParentVector *>()) { |
| 846 | delete Entry.second.get<ParentVector *>(); |
| 847 | } |
| 848 | } |
| 849 | for (const auto &Entry : *OtherParents) { |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 850 | if (Entry.second.is<ast_type_traits::DynTypedNode *>()) { |
| 851 | delete Entry.second.get<ast_type_traits::DynTypedNode *>(); |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 852 | } else if (Entry.second.is<ParentVector *>()) { |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 853 | delete Entry.second.get<ParentVector *>(); |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 858 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
Chandler Carruth | ff5a01a | 2015-12-30 03:00:23 +0000 | [diff] [blame] | 859 | Deallocations.push_back({Callback, Data}); |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 862 | void |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 863 | ASTContext::setExternalSource(IntrusiveRefCntPtr<ExternalASTSource> Source) { |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 864 | ExternalSource = std::move(Source); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 867 | void ASTContext::PrintStats() const { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 868 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 869 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 870 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 871 | unsigned counts[] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 872 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 873 | #define ABSTRACT_TYPE(Name, Parent) |
| 874 | #include "clang/AST/TypeNodes.def" |
| 875 | 0 // Extra |
| 876 | }; |
Douglas Gregor | b1fe2c9 | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 877 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 878 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 879 | Type *T = Types[i]; |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 880 | counts[(unsigned)T->getTypeClass()]++; |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 883 | unsigned Idx = 0; |
| 884 | unsigned TotalBytes = 0; |
| 885 | #define TYPE(Name, Parent) \ |
| 886 | if (counts[Idx]) \ |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 887 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
| 888 | << " types\n"; \ |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 889 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 890 | ++Idx; |
| 891 | #define ABSTRACT_TYPE(Name, Parent) |
| 892 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 894 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 895 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 896 | // Implicit special member functions. |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 897 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 898 | << NumImplicitDefaultConstructors |
| 899 | << " implicit default constructors created\n"; |
| 900 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 901 | << NumImplicitCopyConstructors |
| 902 | << " implicit copy constructors created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 903 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 904 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 905 | << NumImplicitMoveConstructors |
| 906 | << " implicit move constructors created\n"; |
| 907 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 908 | << NumImplicitCopyAssignmentOperators |
| 909 | << " implicit copy assignment operators created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 910 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 911 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 912 | << NumImplicitMoveAssignmentOperators |
| 913 | << " implicit move assignment operators created\n"; |
| 914 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 915 | << NumImplicitDestructors |
| 916 | << " implicit destructors created\n"; |
| 917 | |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 918 | if (ExternalSource) { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 919 | llvm::errs() << "\n"; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 920 | ExternalSource->PrintStats(); |
| 921 | } |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 922 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 923 | BumpAlloc.PrintStats(); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 926 | void ASTContext::mergeDefinitionIntoModule(NamedDecl *ND, Module *M, |
| 927 | bool NotifyListeners) { |
| 928 | if (NotifyListeners) |
| 929 | if (auto *Listener = getASTMutationListener()) |
| 930 | Listener->RedefinedHiddenDefinition(ND, M); |
| 931 | |
Benjamin Kramer | a72a70a | 2016-10-17 13:00:44 +0000 | [diff] [blame] | 932 | if (getLangOpts().ModulesLocalVisibility) |
| 933 | MergedDefModules[ND].push_back(M); |
| 934 | else |
Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 935 | ND->setVisibleDespiteOwningModule(); |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | void ASTContext::deduplicateMergedDefinitonsFor(NamedDecl *ND) { |
| 939 | auto It = MergedDefModules.find(ND); |
| 940 | if (It == MergedDefModules.end()) |
| 941 | return; |
| 942 | |
Benjamin Kramer | a72a70a | 2016-10-17 13:00:44 +0000 | [diff] [blame] | 943 | auto &Merged = It->second; |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 944 | llvm::DenseSet<Module*> Found; |
| 945 | for (Module *&M : Merged) |
| 946 | if (!Found.insert(M).second) |
| 947 | M = nullptr; |
| 948 | Merged.erase(std::remove(Merged.begin(), Merged.end(), nullptr), Merged.end()); |
| 949 | } |
| 950 | |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 951 | void ASTContext::PerModuleInitializers::resolve(ASTContext &Ctx) { |
| 952 | if (LazyInitializers.empty()) |
| 953 | return; |
| 954 | |
| 955 | auto *Source = Ctx.getExternalSource(); |
| 956 | assert(Source && "lazy initializers but no external source"); |
| 957 | |
| 958 | auto LazyInits = std::move(LazyInitializers); |
| 959 | LazyInitializers.clear(); |
| 960 | |
| 961 | for (auto ID : LazyInits) |
| 962 | Initializers.push_back(Source->GetExternalDecl(ID)); |
| 963 | |
| 964 | assert(LazyInitializers.empty() && |
| 965 | "GetExternalDecl for lazy module initializer added more inits"); |
| 966 | } |
| 967 | |
| 968 | void ASTContext::addModuleInitializer(Module *M, Decl *D) { |
| 969 | // One special case: if we add a module initializer that imports another |
| 970 | // module, and that module's only initializer is an ImportDecl, simplify. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 971 | if (const auto *ID = dyn_cast<ImportDecl>(D)) { |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 972 | auto It = ModuleInitializers.find(ID->getImportedModule()); |
| 973 | |
| 974 | // Maybe the ImportDecl does nothing at all. (Common case.) |
| 975 | if (It == ModuleInitializers.end()) |
| 976 | return; |
| 977 | |
| 978 | // Maybe the ImportDecl only imports another ImportDecl. |
| 979 | auto &Imported = *It->second; |
| 980 | if (Imported.Initializers.size() + Imported.LazyInitializers.size() == 1) { |
| 981 | Imported.resolve(*this); |
| 982 | auto *OnlyDecl = Imported.Initializers.front(); |
| 983 | if (isa<ImportDecl>(OnlyDecl)) |
| 984 | D = OnlyDecl; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | auto *&Inits = ModuleInitializers[M]; |
| 989 | if (!Inits) |
| 990 | Inits = new (*this) PerModuleInitializers; |
| 991 | Inits->Initializers.push_back(D); |
| 992 | } |
| 993 | |
| 994 | void ASTContext::addLazyModuleInitializers(Module *M, ArrayRef<uint32_t> IDs) { |
| 995 | auto *&Inits = ModuleInitializers[M]; |
| 996 | if (!Inits) |
| 997 | Inits = new (*this) PerModuleInitializers; |
| 998 | Inits->LazyInitializers.insert(Inits->LazyInitializers.end(), |
| 999 | IDs.begin(), IDs.end()); |
| 1000 | } |
| 1001 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1002 | ArrayRef<Decl *> ASTContext::getModuleInitializers(Module *M) { |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 1003 | auto It = ModuleInitializers.find(M); |
| 1004 | if (It == ModuleInitializers.end()) |
| 1005 | return None; |
| 1006 | |
| 1007 | auto *Inits = It->second; |
| 1008 | Inits->resolve(*this); |
| 1009 | return Inits->Initializers; |
| 1010 | } |
| 1011 | |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 1012 | ExternCContextDecl *ASTContext::getExternCContextDecl() const { |
| 1013 | if (!ExternCContext) |
| 1014 | ExternCContext = ExternCContextDecl::Create(*this, getTranslationUnitDecl()); |
| 1015 | |
| 1016 | return ExternCContext; |
| 1017 | } |
| 1018 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1019 | BuiltinTemplateDecl * |
| 1020 | ASTContext::buildBuiltinTemplateDecl(BuiltinTemplateKind BTK, |
| 1021 | const IdentifierInfo *II) const { |
| 1022 | auto *BuiltinTemplate = BuiltinTemplateDecl::Create(*this, TUDecl, II, BTK); |
| 1023 | BuiltinTemplate->setImplicit(); |
| 1024 | TUDecl->addDecl(BuiltinTemplate); |
| 1025 | |
| 1026 | return BuiltinTemplate; |
| 1027 | } |
| 1028 | |
| 1029 | BuiltinTemplateDecl * |
| 1030 | ASTContext::getMakeIntegerSeqDecl() const { |
| 1031 | if (!MakeIntegerSeqDecl) |
| 1032 | MakeIntegerSeqDecl = buildBuiltinTemplateDecl(BTK__make_integer_seq, |
| 1033 | getMakeIntegerSeqName()); |
| 1034 | return MakeIntegerSeqDecl; |
| 1035 | } |
| 1036 | |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 1037 | BuiltinTemplateDecl * |
| 1038 | ASTContext::getTypePackElementDecl() const { |
| 1039 | if (!TypePackElementDecl) |
| 1040 | TypePackElementDecl = buildBuiltinTemplateDecl(BTK__type_pack_element, |
| 1041 | getTypePackElementName()); |
| 1042 | return TypePackElementDecl; |
| 1043 | } |
| 1044 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 1045 | RecordDecl *ASTContext::buildImplicitRecord(StringRef Name, |
| 1046 | RecordDecl::TagKind TK) const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1047 | SourceLocation Loc; |
| 1048 | RecordDecl *NewDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 1049 | if (getLangOpts().CPlusPlus) |
| 1050 | NewDecl = CXXRecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, |
| 1051 | Loc, &Idents.get(Name)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1052 | else |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 1053 | NewDecl = RecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, Loc, |
| 1054 | &Idents.get(Name)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1055 | NewDecl->setImplicit(); |
David Majnemer | f863736 | 2015-01-15 08:41:25 +0000 | [diff] [blame] | 1056 | NewDecl->addAttr(TypeVisibilityAttr::CreateImplicit( |
| 1057 | const_cast<ASTContext &>(*this), TypeVisibilityAttr::Default)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1058 | return NewDecl; |
| 1059 | } |
| 1060 | |
| 1061 | TypedefDecl *ASTContext::buildImplicitTypedef(QualType T, |
| 1062 | StringRef Name) const { |
| 1063 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 1064 | TypedefDecl *NewDecl = TypedefDecl::Create( |
| 1065 | const_cast<ASTContext &>(*this), getTranslationUnitDecl(), |
| 1066 | SourceLocation(), SourceLocation(), &Idents.get(Name), TInfo); |
| 1067 | NewDecl->setImplicit(); |
| 1068 | return NewDecl; |
| 1069 | } |
| 1070 | |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 1071 | TypedefDecl *ASTContext::getInt128Decl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1072 | if (!Int128Decl) |
| 1073 | Int128Decl = buildImplicitTypedef(Int128Ty, "__int128_t"); |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 1074 | return Int128Decl; |
| 1075 | } |
| 1076 | |
| 1077 | TypedefDecl *ASTContext::getUInt128Decl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1078 | if (!UInt128Decl) |
| 1079 | UInt128Decl = buildImplicitTypedef(UnsignedInt128Ty, "__uint128_t"); |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 1080 | return UInt128Decl; |
| 1081 | } |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 1082 | |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 1083 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1084 | auto *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 1085 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1086 | Types.push_back(Ty); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 1089 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target, |
| 1090 | const TargetInfo *AuxTarget) { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1091 | assert((!this->Target || this->Target == &Target) && |
| 1092 | "Incorrect target reinitialization"); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1093 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1095 | this->Target = &Target; |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 1096 | this->AuxTarget = AuxTarget; |
| 1097 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1098 | ABI.reset(createCXXABI(Target)); |
| 1099 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 1100 | AddrSpaceMapMangling = isAddrSpaceMapManglingEnabled(Target, LangOpts); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1101 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1102 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1103 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1105 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1106 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1107 | // C99 6.2.5p3. |
Eli Friedman | 9ffd4a9 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 1108 | if (LangOpts.CharIsSigned) |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 1109 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 1110 | else |
| 1111 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1112 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1113 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 1114 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 1115 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 1116 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 1117 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1118 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1119 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1120 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 1121 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 1122 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 1123 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 1124 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1126 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1127 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 1128 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 1129 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1130 | |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1131 | // GNU extension, __float128 for IEEE quadruple precision |
| 1132 | InitBuiltinType(Float128Ty, BuiltinType::Float128); |
| 1133 | |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 1134 | // C11 extension ISO/IEC TS 18661-3 |
| 1135 | InitBuiltinType(Float16Ty, BuiltinType::Float16); |
| 1136 | |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1137 | // ISO/IEC JTC1 SC22 WG14 N1169 Extension |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1138 | InitBuiltinType(ShortAccumTy, BuiltinType::ShortAccum); |
| 1139 | InitBuiltinType(AccumTy, BuiltinType::Accum); |
| 1140 | InitBuiltinType(LongAccumTy, BuiltinType::LongAccum); |
| 1141 | InitBuiltinType(UnsignedShortAccumTy, BuiltinType::UShortAccum); |
| 1142 | InitBuiltinType(UnsignedAccumTy, BuiltinType::UAccum); |
| 1143 | InitBuiltinType(UnsignedLongAccumTy, BuiltinType::ULongAccum); |
| 1144 | InitBuiltinType(ShortFractTy, BuiltinType::ShortFract); |
| 1145 | InitBuiltinType(FractTy, BuiltinType::Fract); |
| 1146 | InitBuiltinType(LongFractTy, BuiltinType::LongFract); |
| 1147 | InitBuiltinType(UnsignedShortFractTy, BuiltinType::UShortFract); |
| 1148 | InitBuiltinType(UnsignedFractTy, BuiltinType::UFract); |
| 1149 | InitBuiltinType(UnsignedLongFractTy, BuiltinType::ULongFract); |
| 1150 | InitBuiltinType(SatShortAccumTy, BuiltinType::SatShortAccum); |
| 1151 | InitBuiltinType(SatAccumTy, BuiltinType::SatAccum); |
| 1152 | InitBuiltinType(SatLongAccumTy, BuiltinType::SatLongAccum); |
| 1153 | InitBuiltinType(SatUnsignedShortAccumTy, BuiltinType::SatUShortAccum); |
| 1154 | InitBuiltinType(SatUnsignedAccumTy, BuiltinType::SatUAccum); |
| 1155 | InitBuiltinType(SatUnsignedLongAccumTy, BuiltinType::SatULongAccum); |
| 1156 | InitBuiltinType(SatShortFractTy, BuiltinType::SatShortFract); |
| 1157 | InitBuiltinType(SatFractTy, BuiltinType::SatFract); |
| 1158 | InitBuiltinType(SatLongFractTy, BuiltinType::SatLongFract); |
| 1159 | InitBuiltinType(SatUnsignedShortFractTy, BuiltinType::SatUShortFract); |
| 1160 | InitBuiltinType(SatUnsignedFractTy, BuiltinType::SatUFract); |
| 1161 | InitBuiltinType(SatUnsignedLongFractTy, BuiltinType::SatULongFract); |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1162 | |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 1163 | // GNU extension, 128-bit integers. |
| 1164 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 1165 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 1166 | |
Hans Wennborg | 0d81e01 | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 1167 | // C++ 3.9.1p5 |
| 1168 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
| 1169 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 1170 | else // -fshort-wchar makes wchar_t be unsigned. |
| 1171 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 1172 | if (LangOpts.CPlusPlus && LangOpts.WChar) |
| 1173 | WideCharTy = WCharTy; |
| 1174 | else { |
| 1175 | // C99 (or C++ using -fno-wchar). |
| 1176 | WideCharTy = getFromTargetType(Target.getWCharType()); |
| 1177 | } |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1178 | |
James Molloy | 3636554 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 1179 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 1180 | |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 1181 | // C++20 (proposed) |
| 1182 | InitBuiltinType(Char8Ty, BuiltinType::Char8); |
| 1183 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1184 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 1185 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 1186 | else // C99 |
| 1187 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 1188 | |
| 1189 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 1190 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 1191 | else // C99 |
| 1192 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 1193 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1194 | // Placeholder type for type-dependent expressions whose type is |
| 1195 | // completely unknown. No code should ever check a type against |
| 1196 | // DependentTy and users should never see it; however, it is here to |
| 1197 | // help diagnose failures to properly check for type-dependent |
| 1198 | // expressions. |
| 1199 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1200 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 1201 | // Placeholder type for functions. |
| 1202 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 1203 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1204 | // Placeholder type for bound members. |
| 1205 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 1206 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1207 | // Placeholder type for pseudo-objects. |
| 1208 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 1209 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 1210 | // "any" type; useful for debugger-like clients. |
| 1211 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 1212 | |
John McCall | 8a6b59a | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 1213 | // Placeholder type for unbridged ARC casts. |
| 1214 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 1215 | |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 1216 | // Placeholder type for builtin functions. |
| 1217 | InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn); |
| 1218 | |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 1219 | // Placeholder type for OMP array sections. |
| 1220 | if (LangOpts.OpenMP) |
| 1221 | InitBuiltinType(OMPArraySectionTy, BuiltinType::OMPArraySection); |
| 1222 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1223 | // C99 6.2.5p11. |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1224 | FloatComplexTy = getComplexType(FloatTy); |
| 1225 | DoubleComplexTy = getComplexType(DoubleTy); |
| 1226 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1227 | Float128ComplexTy = getComplexType(Float128Ty); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1228 | |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1229 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1230 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 1231 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1232 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1233 | |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 1234 | if (LangOpts.OpenCL) { |
| 1235 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1236 | InitBuiltinType(SingletonId, BuiltinType::Id); |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 1237 | #include "clang/Basic/OpenCLImageTypes.def" |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1238 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1239 | InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1240 | InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1241 | InitBuiltinType(OCLClkEventTy, BuiltinType::OCLClkEvent); |
| 1242 | InitBuiltinType(OCLQueueTy, BuiltinType::OCLQueue); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1243 | InitBuiltinType(OCLReserveIDTy, BuiltinType::OCLReserveID); |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1244 | } |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1245 | |
| 1246 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 29898f4 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 1247 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 1248 | SignedCharTy : BoolTy); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1249 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1250 | ObjCConstantStringType = QualType(); |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1251 | |
| 1252 | ObjCSuperType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1253 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1254 | // void * type |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 1255 | if (LangOpts.OpenCLVersion >= 200) { |
| 1256 | auto Q = VoidTy.getQualifiers(); |
| 1257 | Q.setAddressSpace(LangAS::opencl_generic); |
| 1258 | VoidPtrTy = getPointerType(getCanonicalType( |
| 1259 | getQualifiedType(VoidTy.getUnqualifiedType(), Q))); |
| 1260 | } else { |
| 1261 | VoidPtrTy = getPointerType(VoidTy); |
| 1262 | } |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1263 | |
| 1264 | // nullptr type (C++0x 2.14.7) |
| 1265 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1266 | |
| 1267 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 1268 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 1269 | |
| 1270 | // Builtin type used to help define __builtin_va_list. |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 1271 | VaListTagDecl = nullptr; |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1274 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1275 | return SourceMgr.getDiagnostics(); |
| 1276 | } |
| 1277 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 1278 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 1279 | AttrVec *&Result = DeclAttrs[D]; |
| 1280 | if (!Result) { |
| 1281 | void *Mem = Allocate(sizeof(AttrVec)); |
| 1282 | Result = new (Mem) AttrVec; |
| 1283 | } |
| 1284 | |
| 1285 | return *Result; |
| 1286 | } |
| 1287 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1288 | /// Erase the attributes corresponding to the given declaration. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 1289 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 1290 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 1291 | if (Pos != DeclAttrs.end()) { |
| 1292 | Pos->second->~AttrVec(); |
| 1293 | DeclAttrs.erase(Pos); |
| 1294 | } |
| 1295 | } |
| 1296 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1297 | // FIXME: Remove ? |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1298 | MemberSpecializationInfo * |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1299 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1300 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1301 | return getTemplateOrSpecializationInfo(Var) |
| 1302 | .dyn_cast<MemberSpecializationInfo *>(); |
| 1303 | } |
| 1304 | |
| 1305 | ASTContext::TemplateOrSpecializationInfo |
| 1306 | ASTContext::getTemplateOrSpecializationInfo(const VarDecl *Var) { |
| 1307 | llvm::DenseMap<const VarDecl *, TemplateOrSpecializationInfo>::iterator Pos = |
| 1308 | TemplateOrInstantiation.find(Var); |
| 1309 | if (Pos == TemplateOrInstantiation.end()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1310 | return {}; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1312 | return Pos->second; |
| 1313 | } |
| 1314 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | void |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1316 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 1317 | TemplateSpecializationKind TSK, |
| 1318 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1319 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 1320 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1321 | setTemplateOrSpecializationInfo(Inst, new (*this) MemberSpecializationInfo( |
| 1322 | Tmpl, TSK, PointOfInstantiation)); |
| 1323 | } |
| 1324 | |
| 1325 | void |
| 1326 | ASTContext::setTemplateOrSpecializationInfo(VarDecl *Inst, |
| 1327 | TemplateOrSpecializationInfo TSI) { |
| 1328 | assert(!TemplateOrInstantiation[Inst] && |
| 1329 | "Already noted what the variable was instantiated from"); |
| 1330 | TemplateOrInstantiation[Inst] = TSI; |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1333 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 1334 | const FunctionDecl *FD){ |
| 1335 | assert(FD && "Specialization is 0"); |
| 1336 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1337 | = ClassScopeSpecializationPattern.find(FD); |
| 1338 | if (Pos == ClassScopeSpecializationPattern.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1339 | return nullptr; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1340 | |
| 1341 | return Pos->second; |
| 1342 | } |
| 1343 | |
| 1344 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 1345 | FunctionDecl *Pattern) { |
| 1346 | assert(FD && "Specialization is 0"); |
| 1347 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1348 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1351 | NamedDecl * |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1352 | ASTContext::getInstantiatedFromUsingDecl(NamedDecl *UUD) { |
| 1353 | auto Pos = InstantiatedFromUsingDecl.find(UUD); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1354 | if (Pos == InstantiatedFromUsingDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1355 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1356 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1357 | return Pos->second; |
| 1358 | } |
| 1359 | |
| 1360 | void |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1361 | ASTContext::setInstantiatedFromUsingDecl(NamedDecl *Inst, NamedDecl *Pattern) { |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1362 | assert((isa<UsingDecl>(Pattern) || |
| 1363 | isa<UnresolvedUsingValueDecl>(Pattern) || |
| 1364 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
| 1365 | "pattern decl is not a using decl"); |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1366 | assert((isa<UsingDecl>(Inst) || |
| 1367 | isa<UnresolvedUsingValueDecl>(Inst) || |
| 1368 | isa<UnresolvedUsingTypenameDecl>(Inst)) && |
| 1369 | "instantiation did not produce a using decl"); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1370 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 1371 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 1372 | } |
| 1373 | |
| 1374 | UsingShadowDecl * |
| 1375 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 1376 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 1377 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 1378 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1379 | return nullptr; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1380 | |
| 1381 | return Pos->second; |
| 1382 | } |
| 1383 | |
| 1384 | void |
| 1385 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 1386 | UsingShadowDecl *Pattern) { |
| 1387 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 1388 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1391 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 1392 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 1393 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 1394 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1395 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1396 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1397 | return Pos->second; |
| 1398 | } |
| 1399 | |
| 1400 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 1401 | FieldDecl *Tmpl) { |
| 1402 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 1403 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 1404 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 1405 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1406 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1407 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 1408 | } |
| 1409 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1410 | ASTContext::overridden_cxx_method_iterator |
| 1411 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 1412 | return overridden_methods(Method).begin(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | ASTContext::overridden_cxx_method_iterator |
| 1416 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 1417 | return overridden_methods(Method).end(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1420 | unsigned |
| 1421 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 1422 | auto Range = overridden_methods(Method); |
| 1423 | return Range.end() - Range.begin(); |
Clement Courbet | 8251ebf | 2016-06-10 11:54:43 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
Clement Courbet | 6ecaec8 | 2016-07-05 07:49:31 +0000 | [diff] [blame] | 1426 | ASTContext::overridden_method_range |
| 1427 | ASTContext::overridden_methods(const CXXMethodDecl *Method) const { |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 1428 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos = |
| 1429 | OverriddenMethods.find(Method->getCanonicalDecl()); |
| 1430 | if (Pos == OverriddenMethods.end()) |
| 1431 | return overridden_method_range(nullptr, nullptr); |
| 1432 | return overridden_method_range(Pos->second.begin(), Pos->second.end()); |
Clement Courbet | 6ecaec8 | 2016-07-05 07:49:31 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1435 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
| 1436 | const CXXMethodDecl *Overridden) { |
Argyrios Kyrtzidis | cc4ca0a | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1437 | assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl()); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1438 | OverriddenMethods[Method].push_back(Overridden); |
| 1439 | } |
| 1440 | |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 1441 | void ASTContext::getOverriddenMethods( |
| 1442 | const NamedDecl *D, |
| 1443 | SmallVectorImpl<const NamedDecl *> &Overridden) const { |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1444 | assert(D); |
| 1445 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1446 | if (const auto *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | c8e7008 | 2013-04-17 00:09:03 +0000 | [diff] [blame] | 1447 | Overridden.append(overridden_methods_begin(CXXMethod), |
| 1448 | overridden_methods_end(CXXMethod)); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1449 | return; |
| 1450 | } |
| 1451 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1452 | const auto *Method = dyn_cast<ObjCMethodDecl>(D); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1453 | if (!Method) |
| 1454 | return; |
| 1455 | |
Argyrios Kyrtzidis | 353f6a4 | 2012-10-09 18:19:01 +0000 | [diff] [blame] | 1456 | SmallVector<const ObjCMethodDecl *, 8> OverDecls; |
| 1457 | Method->getOverriddenMethods(OverDecls); |
Argyrios Kyrtzidis | a7a1081 | 2012-10-09 20:08:43 +0000 | [diff] [blame] | 1458 | Overridden.append(OverDecls.begin(), OverDecls.end()); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 1461 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 1462 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 1463 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 1464 | if (!FirstLocalImport) { |
| 1465 | FirstLocalImport = Import; |
| 1466 | LastLocalImport = Import; |
| 1467 | return; |
| 1468 | } |
| 1469 | |
| 1470 | LastLocalImport->NextLocalImport = Import; |
| 1471 | LastLocalImport = Import; |
| 1472 | } |
| 1473 | |
Chris Lattner | 53cfe80 | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1474 | //===----------------------------------------------------------------------===// |
| 1475 | // Type Sizing and Analysis |
| 1476 | //===----------------------------------------------------------------------===// |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1477 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1478 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 1479 | /// scalar floating point type. |
| 1480 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1481 | const auto *BT = T->getAs<BuiltinType>(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1482 | assert(BT && "Not a floating point type!"); |
| 1483 | switch (BT->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1484 | default: llvm_unreachable("Not a floating point type!"); |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 1485 | case BuiltinType::Float16: |
| 1486 | case BuiltinType::Half: |
| 1487 | return Target->getHalfFormat(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1488 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 1489 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 1490 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1491 | case BuiltinType::Float128: return Target->getFloat128Format(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1492 | } |
| 1493 | } |
| 1494 | |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1495 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1496 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1497 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1498 | bool UseAlignAttrOnly = false; |
| 1499 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 1500 | Align = AlignFromAttr; |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1501 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1502 | // __attribute__((aligned)) can increase or decrease alignment |
| 1503 | // *except* on a struct or struct member, where it only increases |
| 1504 | // alignment unless 'packed' is also specified. |
| 1505 | // |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1506 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1507 | // ignore that possibility; Sema should diagnose it. |
| 1508 | if (isa<FieldDecl>(D)) { |
| 1509 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 1510 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 1511 | } else { |
| 1512 | UseAlignAttrOnly = true; |
| 1513 | } |
| 1514 | } |
Fariborz Jahanian | 9f10718 | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 1515 | else if (isa<FieldDecl>(D)) |
| 1516 | UseAlignAttrOnly = |
| 1517 | D->hasAttr<PackedAttr>() || |
| 1518 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1519 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1520 | // If we're using the align attribute only, just ignore everything |
| 1521 | // else about the declaration and its type. |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1522 | if (UseAlignAttrOnly) { |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1523 | // do nothing |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1524 | } else if (const auto *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1525 | QualType T = VD->getType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1526 | if (const auto *RT = T->getAs<ReferenceType>()) { |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1527 | if (ForAlignof) |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1528 | T = RT->getPointeeType(); |
| 1529 | else |
| 1530 | T = getPointerType(RT->getPointeeType()); |
| 1531 | } |
Richard Smith | f6d7030 | 2014-06-10 23:34:28 +0000 | [diff] [blame] | 1532 | QualType BaseT = getBaseElementType(T); |
Akira Hatanaka | d62f2c8 | 2017-01-06 17:56:15 +0000 | [diff] [blame] | 1533 | if (T->isFunctionType()) |
| 1534 | Align = getTypeInfoImpl(T.getTypePtr()).Align; |
| 1535 | else if (!BaseT->isIncompleteType()) { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1536 | // Adjust alignments of declarations with array type by the |
| 1537 | // large-array alignment on the target. |
Rafael Espindola | 8857275 | 2013-08-07 18:08:19 +0000 | [diff] [blame] | 1538 | if (const ArrayType *arrayType = getAsArrayType(T)) { |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1539 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
| 1540 | if (!ForAlignof && MinWidth) { |
Rafael Espindola | 8857275 | 2013-08-07 18:08:19 +0000 | [diff] [blame] | 1541 | if (isa<VariableArrayType>(arrayType)) |
| 1542 | Align = std::max(Align, Target->getLargeArrayAlign()); |
| 1543 | else if (isa<ConstantArrayType>(arrayType) && |
| 1544 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
| 1545 | Align = std::max(Align, Target->getLargeArrayAlign()); |
| 1546 | } |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1547 | } |
Chad Rosier | 99ee782 | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 1548 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Roger Ferrer Ibanez | 3fa38a1 | 2017-03-08 14:00:44 +0000 | [diff] [blame] | 1549 | if (BaseT.getQualifiers().hasUnaligned()) |
| 1550 | Align = Target->getCharWidth(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1551 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Ulrich Weigand | b63f779 | 2015-04-21 17:26:18 +0000 | [diff] [blame] | 1552 | if (VD->hasGlobalStorage() && !ForAlignof) |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1553 | Align = std::max(Align, getTargetInfo().getMinGlobalAlign()); |
| 1554 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1555 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1556 | |
| 1557 | // Fields can be subject to extra alignment constraints, like if |
| 1558 | // the field is packed, the struct is packed, or the struct has a |
| 1559 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 1560 | // the actual alignment of the field within the struct, and then |
| 1561 | // (as we're expected to) constrain that by the alignment of the type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1562 | if (const auto *Field = dyn_cast<FieldDecl>(VD)) { |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1563 | const RecordDecl *Parent = Field->getParent(); |
| 1564 | // We can only produce a sensible answer if the record is valid. |
| 1565 | if (!Parent->isInvalidDecl()) { |
| 1566 | const ASTRecordLayout &Layout = getASTRecordLayout(Parent); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1567 | |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1568 | // Start with the record's overall alignment. |
| 1569 | unsigned FieldAlign = toBits(Layout.getAlignment()); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1570 | |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1571 | // Use the GCD of that and the offset within the record. |
| 1572 | uint64_t Offset = Layout.getFieldOffset(Field->getFieldIndex()); |
| 1573 | if (Offset > 0) { |
| 1574 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 1575 | // which means we get to do this crazy thing instead of Euclid's. |
| 1576 | uint64_t LowBitOfOffset = Offset & (~Offset + 1); |
| 1577 | if (LowBitOfOffset < FieldAlign) |
| 1578 | FieldAlign = static_cast<unsigned>(LowBitOfOffset); |
| 1579 | } |
| 1580 | |
| 1581 | Align = std::min(Align, FieldAlign); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1582 | } |
Charles Davis | 3fc5107 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 1583 | } |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1584 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1585 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1586 | return toCharUnitsFromBits(Align); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1587 | } |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1588 | |
John McCall | f124992 | 2012-08-21 04:10:00 +0000 | [diff] [blame] | 1589 | // getTypeInfoDataSizeInChars - Return the size of a type, in |
| 1590 | // chars. If the type is a record, its data size is returned. This is |
| 1591 | // the size of the memcpy that's performed when assigning this type |
| 1592 | // using a trivial copy/move assignment operator. |
| 1593 | std::pair<CharUnits, CharUnits> |
| 1594 | ASTContext::getTypeInfoDataSizeInChars(QualType T) const { |
| 1595 | std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T); |
| 1596 | |
| 1597 | // In C++, objects can sometimes be allocated into the tail padding |
| 1598 | // of a base-class subobject. We decide whether that's possible |
| 1599 | // during class layout, so here we can just trust the layout results. |
| 1600 | if (getLangOpts().CPlusPlus) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1601 | if (const auto *RT = T->getAs<RecordType>()) { |
John McCall | f124992 | 2012-08-21 04:10:00 +0000 | [diff] [blame] | 1602 | const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl()); |
| 1603 | sizeAndAlign.first = layout.getDataSize(); |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | return sizeAndAlign; |
| 1608 | } |
| 1609 | |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1610 | /// getConstantArrayInfoInChars - Performing the computation in CharUnits |
| 1611 | /// instead of in bits prevents overflowing the uint64_t for some large arrays. |
| 1612 | std::pair<CharUnits, CharUnits> |
| 1613 | static getConstantArrayInfoInChars(const ASTContext &Context, |
| 1614 | const ConstantArrayType *CAT) { |
| 1615 | std::pair<CharUnits, CharUnits> EltInfo = |
| 1616 | Context.getTypeInfoInChars(CAT->getElementType()); |
| 1617 | uint64_t Size = CAT->getSize().getZExtValue(); |
Richard Trieu | c750907 | 2013-05-14 23:41:50 +0000 | [diff] [blame] | 1618 | assert((Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity()) <= |
| 1619 | (uint64_t)(-1)/Size) && |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1620 | "Overflow in array type char size evaluation"); |
| 1621 | uint64_t Width = EltInfo.first.getQuantity() * Size; |
| 1622 | unsigned Align = EltInfo.second.getQuantity(); |
Warren Hunt | 5ae586a | 2013-11-01 23:59:41 +0000 | [diff] [blame] | 1623 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 1624 | Context.getTargetInfo().getPointerWidth(0) == 64) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1625 | Width = llvm::alignTo(Width, Align); |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1626 | return std::make_pair(CharUnits::fromQuantity(Width), |
| 1627 | CharUnits::fromQuantity(Align)); |
| 1628 | } |
| 1629 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1630 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1631 | ASTContext::getTypeInfoInChars(const Type *T) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1632 | if (const auto *CAT = dyn_cast<ConstantArrayType>(T)) |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1633 | return getConstantArrayInfoInChars(*this, CAT); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1634 | TypeInfo Info = getTypeInfo(T); |
| 1635 | return std::make_pair(toCharUnitsFromBits(Info.Width), |
| 1636 | toCharUnitsFromBits(Info.Align)); |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1640 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1641 | return getTypeInfoInChars(T.getTypePtr()); |
| 1642 | } |
| 1643 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1644 | bool ASTContext::isAlignmentRequired(const Type *T) const { |
| 1645 | return getTypeInfo(T).AlignIsRequired; |
| 1646 | } |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1647 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1648 | bool ASTContext::isAlignmentRequired(QualType T) const { |
| 1649 | return isAlignmentRequired(T.getTypePtr()); |
| 1650 | } |
| 1651 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1652 | unsigned ASTContext::getTypeAlignIfKnown(QualType T) const { |
| 1653 | // An alignment on a typedef overrides anything else. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1654 | if (const auto *TT = T->getAs<TypedefType>()) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1655 | if (unsigned Align = TT->getDecl()->getMaxAlignment()) |
| 1656 | return Align; |
| 1657 | |
| 1658 | // If we have an (array of) complete type, we're done. |
| 1659 | T = getBaseElementType(T); |
| 1660 | if (!T->isIncompleteType()) |
| 1661 | return getTypeAlign(T); |
| 1662 | |
| 1663 | // If we had an array type, its element type might be a typedef |
| 1664 | // type with an alignment attribute. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1665 | if (const auto *TT = T->getAs<TypedefType>()) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1666 | if (unsigned Align = TT->getDecl()->getMaxAlignment()) |
| 1667 | return Align; |
| 1668 | |
| 1669 | // Otherwise, see if the declaration of the type had an attribute. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1670 | if (const auto *TT = T->getAs<TagType>()) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1671 | return TT->getDecl()->getMaxAlignment(); |
| 1672 | |
| 1673 | return 0; |
| 1674 | } |
| 1675 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1676 | TypeInfo ASTContext::getTypeInfo(const Type *T) const { |
David Majnemer | f8d3864 | 2014-07-30 08:42:33 +0000 | [diff] [blame] | 1677 | TypeInfoMap::iterator I = MemoizedTypeInfo.find(T); |
| 1678 | if (I != MemoizedTypeInfo.end()) |
| 1679 | return I->second; |
| 1680 | |
| 1681 | // This call can invalidate MemoizedTypeInfo[T], so we need a second lookup. |
| 1682 | TypeInfo TI = getTypeInfoImpl(T); |
| 1683 | MemoizedTypeInfo[T] = TI; |
Rafael Espindola | eaa88c1 | 2014-07-30 04:40:23 +0000 | [diff] [blame] | 1684 | return TI; |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 1688 | /// method does not work on incomplete types. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1689 | /// |
| 1690 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 1691 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 1692 | /// should take a QualType, &c. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1693 | TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const { |
| 1694 | uint64_t Width = 0; |
| 1695 | unsigned Align = 8; |
| 1696 | bool AlignIsRequired = false; |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 1697 | unsigned AS = 0; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1698 | switch (T->getTypeClass()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1699 | #define TYPE(Class, Base) |
| 1700 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1701 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1702 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
David Blaikie | ab277d6 | 2013-07-13 21:08:03 +0000 | [diff] [blame] | 1703 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) \ |
| 1704 | case Type::Class: \ |
| 1705 | assert(!T->isDependentType() && "should not see dependent types here"); \ |
| 1706 | return getTypeInfo(cast<Class##Type>(T)->desugar().getTypePtr()); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1707 | #include "clang/AST/TypeNodes.def" |
John McCall | 15547bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 1708 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1709 | |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1710 | case Type::FunctionNoProto: |
| 1711 | case Type::FunctionProto: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1712 | // GCC extension: alignof(function) = 32 bits |
| 1713 | Width = 0; |
| 1714 | Align = 32; |
| 1715 | break; |
| 1716 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1717 | case Type::IncompleteArray: |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1718 | case Type::VariableArray: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1719 | Width = 0; |
| 1720 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 1721 | break; |
| 1722 | |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1723 | case Type::ConstantArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1724 | const auto *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1725 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1726 | TypeInfo EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | d541a4c | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1727 | uint64_t Size = CAT->getSize().getZExtValue(); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1728 | assert((Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) && |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1729 | "Overflow in array type bit size evaluation"); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1730 | Width = EltInfo.Width * Size; |
| 1731 | Align = EltInfo.Align; |
Warren Hunt | 5ae586a | 2013-11-01 23:59:41 +0000 | [diff] [blame] | 1732 | if (!getTargetInfo().getCXXABI().isMicrosoft() || |
| 1733 | getTargetInfo().getPointerWidth(0) == 64) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1734 | Width = llvm::alignTo(Width, Align); |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1735 | break; |
Christopher Lamb | c5fafa2 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 1736 | } |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1737 | case Type::ExtVector: |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1738 | case Type::Vector: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1739 | const auto *VT = cast<VectorType>(T); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1740 | TypeInfo EltInfo = getTypeInfo(VT->getElementType()); |
| 1741 | Width = EltInfo.Width * VT->getNumElements(); |
Eli Friedman | 3df5efe | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1742 | Align = Width; |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1743 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 1744 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | ef78c8e | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 1745 | if (Align & (Align-1)) { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1746 | Align = llvm::NextPowerOf2(Align); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1747 | Width = llvm::alignTo(Width, Align); |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1748 | } |
Chad Rosier | cc40ea7 | 2012-07-13 23:57:43 +0000 | [diff] [blame] | 1749 | // Adjust the alignment based on the target max. |
| 1750 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
| 1751 | if (TargetVectorAlign && TargetVectorAlign < Align) |
| 1752 | Align = TargetVectorAlign; |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1753 | break; |
| 1754 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1755 | |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1756 | case Type::Builtin: |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1757 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1758 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1759 | case BuiltinType::Void: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1760 | // GCC extension: alignof(void) = 8 bits. |
| 1761 | Width = 0; |
| 1762 | Align = 8; |
| 1763 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1764 | case BuiltinType::Bool: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1765 | Width = Target->getBoolWidth(); |
| 1766 | Align = Target->getBoolAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1767 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1768 | case BuiltinType::Char_S: |
| 1769 | case BuiltinType::Char_U: |
| 1770 | case BuiltinType::UChar: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1771 | case BuiltinType::SChar: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 1772 | case BuiltinType::Char8: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1773 | Width = Target->getCharWidth(); |
| 1774 | Align = Target->getCharAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1775 | break; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1776 | case BuiltinType::WChar_S: |
| 1777 | case BuiltinType::WChar_U: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1778 | Width = Target->getWCharWidth(); |
| 1779 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1780 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1781 | case BuiltinType::Char16: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1782 | Width = Target->getChar16Width(); |
| 1783 | Align = Target->getChar16Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1784 | break; |
| 1785 | case BuiltinType::Char32: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1786 | Width = Target->getChar32Width(); |
| 1787 | Align = Target->getChar32Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1788 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1789 | case BuiltinType::UShort: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1790 | case BuiltinType::Short: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1791 | Width = Target->getShortWidth(); |
| 1792 | Align = Target->getShortAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1793 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1794 | case BuiltinType::UInt: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1795 | case BuiltinType::Int: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1796 | Width = Target->getIntWidth(); |
| 1797 | Align = Target->getIntAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1798 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1799 | case BuiltinType::ULong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1800 | case BuiltinType::Long: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1801 | Width = Target->getLongWidth(); |
| 1802 | Align = Target->getLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1803 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1804 | case BuiltinType::ULongLong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1805 | case BuiltinType::LongLong: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1806 | Width = Target->getLongLongWidth(); |
| 1807 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1808 | break; |
Chris Lattner | 0a415ec | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 1809 | case BuiltinType::Int128: |
| 1810 | case BuiltinType::UInt128: |
| 1811 | Width = 128; |
| 1812 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 1813 | break; |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1814 | case BuiltinType::ShortAccum: |
| 1815 | case BuiltinType::UShortAccum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1816 | case BuiltinType::SatShortAccum: |
| 1817 | case BuiltinType::SatUShortAccum: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1818 | Width = Target->getShortAccumWidth(); |
| 1819 | Align = Target->getShortAccumAlign(); |
| 1820 | break; |
| 1821 | case BuiltinType::Accum: |
| 1822 | case BuiltinType::UAccum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1823 | case BuiltinType::SatAccum: |
| 1824 | case BuiltinType::SatUAccum: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1825 | Width = Target->getAccumWidth(); |
| 1826 | Align = Target->getAccumAlign(); |
| 1827 | break; |
| 1828 | case BuiltinType::LongAccum: |
| 1829 | case BuiltinType::ULongAccum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1830 | case BuiltinType::SatLongAccum: |
| 1831 | case BuiltinType::SatULongAccum: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1832 | Width = Target->getLongAccumWidth(); |
| 1833 | Align = Target->getLongAccumAlign(); |
| 1834 | break; |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1835 | case BuiltinType::ShortFract: |
| 1836 | case BuiltinType::UShortFract: |
| 1837 | case BuiltinType::SatShortFract: |
| 1838 | case BuiltinType::SatUShortFract: |
| 1839 | Width = Target->getShortFractWidth(); |
| 1840 | Align = Target->getShortFractAlign(); |
| 1841 | break; |
| 1842 | case BuiltinType::Fract: |
| 1843 | case BuiltinType::UFract: |
| 1844 | case BuiltinType::SatFract: |
| 1845 | case BuiltinType::SatUFract: |
| 1846 | Width = Target->getFractWidth(); |
| 1847 | Align = Target->getFractAlign(); |
| 1848 | break; |
| 1849 | case BuiltinType::LongFract: |
| 1850 | case BuiltinType::ULongFract: |
| 1851 | case BuiltinType::SatLongFract: |
| 1852 | case BuiltinType::SatULongFract: |
| 1853 | Width = Target->getLongFractWidth(); |
| 1854 | Align = Target->getLongFractAlign(); |
| 1855 | break; |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 1856 | case BuiltinType::Float16: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1857 | case BuiltinType::Half: |
| 1858 | Width = Target->getHalfWidth(); |
| 1859 | Align = Target->getHalfAlign(); |
| 1860 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1861 | case BuiltinType::Float: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1862 | Width = Target->getFloatWidth(); |
| 1863 | Align = Target->getFloatAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1864 | break; |
| 1865 | case BuiltinType::Double: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1866 | Width = Target->getDoubleWidth(); |
| 1867 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1868 | break; |
| 1869 | case BuiltinType::LongDouble: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1870 | Width = Target->getLongDoubleWidth(); |
| 1871 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1872 | break; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1873 | case BuiltinType::Float128: |
| 1874 | Width = Target->getFloat128Width(); |
| 1875 | Align = Target->getFloat128Align(); |
| 1876 | break; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1877 | case BuiltinType::NullPtr: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1878 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 1879 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | a81b0b7 | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 1880 | break; |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1881 | case BuiltinType::ObjCId: |
| 1882 | case BuiltinType::ObjCClass: |
| 1883 | case BuiltinType::ObjCSel: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1884 | Width = Target->getPointerWidth(0); |
| 1885 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1886 | break; |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 1887 | case BuiltinType::OCLSampler: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1888 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1889 | case BuiltinType::OCLClkEvent: |
| 1890 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1891 | case BuiltinType::OCLReserveID: |
Yaxun Liu | 99444cb | 2016-08-03 20:38:06 +0000 | [diff] [blame] | 1892 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1893 | case BuiltinType::Id: |
| 1894 | #include "clang/Basic/OpenCLImageTypes.def" |
Sven van Haastregt | 3bb7eaf | 2017-12-06 10:11:28 +0000 | [diff] [blame] | 1895 | AS = getTargetAddressSpace( |
| 1896 | Target->getOpenCLTypeAddrSpace(getOpenCLTypeKind(T))); |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 1897 | Width = Target->getPointerWidth(AS); |
| 1898 | Align = Target->getPointerAlign(AS); |
| 1899 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1900 | } |
Chris Lattner | 48f84b8 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 1901 | break; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1902 | case Type::ObjCObjectPointer: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1903 | Width = Target->getPointerWidth(0); |
| 1904 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1905 | break; |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 1906 | case Type::BlockPointer: |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 1907 | AS = getTargetAddressSpace(cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1908 | Width = Target->getPointerWidth(AS); |
| 1909 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1910 | break; |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1911 | case Type::LValueReference: |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 1912 | case Type::RValueReference: |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1913 | // alignof and sizeof should never enter this code path here, so we go |
| 1914 | // the pointer route. |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 1915 | AS = getTargetAddressSpace(cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1916 | Width = Target->getPointerWidth(AS); |
| 1917 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1918 | break; |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 1919 | case Type::Pointer: |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 1920 | AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1921 | Width = Target->getPointerWidth(AS); |
| 1922 | Align = Target->getPointerAlign(AS); |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1923 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1924 | case Type::MemberPointer: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1925 | const auto *MPT = cast<MemberPointerType>(T); |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 1926 | CXXABI::MemberPointerInfo MPI = ABI->getMemberPointerInfo(MPT); |
| 1927 | Width = MPI.Width; |
| 1928 | Align = MPI.Align; |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1929 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1930 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1931 | case Type::Complex: { |
| 1932 | // Complex types have the same alignment as their elements, but twice the |
| 1933 | // size. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1934 | TypeInfo EltInfo = getTypeInfo(cast<ComplexType>(T)->getElementType()); |
| 1935 | Width = EltInfo.Width * 2; |
| 1936 | Align = EltInfo.Align; |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1937 | break; |
| 1938 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1939 | case Type::ObjCObject: |
| 1940 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1941 | case Type::Adjusted: |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 1942 | case Type::Decayed: |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 1943 | return getTypeInfo(cast<AdjustedType>(T)->getAdjustedType().getTypePtr()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1944 | case Type::ObjCInterface: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1945 | const auto *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1946 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1947 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1948 | Align = toBits(Layout.getAlignment()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 1949 | break; |
| 1950 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1951 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1952 | case Type::Enum: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1953 | const auto *TT = cast<TagType>(T); |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 1954 | |
| 1955 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 7f97189 | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 1956 | Width = 8; |
| 1957 | Align = 8; |
Chris Lattner | 572100b | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 1958 | break; |
| 1959 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1960 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1961 | if (const auto *ET = dyn_cast<EnumType>(TT)) { |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 1962 | const EnumDecl *ED = ET->getDecl(); |
| 1963 | TypeInfo Info = |
| 1964 | getTypeInfo(ED->getIntegerType()->getUnqualifiedDesugaredType()); |
| 1965 | if (unsigned AttrAlign = ED->getMaxAlignment()) { |
| 1966 | Info.Align = AttrAlign; |
| 1967 | Info.AlignIsRequired = true; |
| 1968 | } |
| 1969 | return Info; |
| 1970 | } |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 1971 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1972 | const auto *RT = cast<RecordType>(TT); |
David Majnemer | 5821ff7 | 2015-02-03 08:49:29 +0000 | [diff] [blame] | 1973 | const RecordDecl *RD = RT->getDecl(); |
| 1974 | const ASTRecordLayout &Layout = getASTRecordLayout(RD); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 1975 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 1976 | Align = toBits(Layout.getAlignment()); |
David Majnemer | 5821ff7 | 2015-02-03 08:49:29 +0000 | [diff] [blame] | 1977 | AlignIsRequired = RD->hasAttr<AlignedAttr>(); |
Chris Lattner | 49a953a | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 1978 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1979 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 1980 | |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1981 | case Type::SubstTemplateTypeParm: |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1982 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 1983 | getReplacementType().getTypePtr()); |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 1984 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1985 | case Type::Auto: |
| 1986 | case Type::DeducedTemplateSpecialization: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1987 | const auto *A = cast<DeducedType>(T); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 1988 | assert(!A->getDeducedType().isNull() && |
| 1989 | "cannot request the size of an undeduced or dependent auto type"); |
Matt Beaumont-Gay | 7a24210e | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 1990 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 1993 | case Type::Paren: |
| 1994 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 1995 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 1996 | case Type::ObjCTypeParam: |
| 1997 | return getTypeInfo(cast<ObjCTypeParamType>(T)->desugar().getTypePtr()); |
| 1998 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1999 | case Type::Typedef: { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2000 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2001 | TypeInfo Info = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 2002 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 2003 | // alignment we have. This violates the GCC documentation (which says that |
| 2004 | // attribute(aligned) can only round up) but matches its implementation. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2005 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) { |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 2006 | Align = AttrAlign; |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2007 | AlignIsRequired = true; |
David Majnemer | 37bffb6 | 2014-08-04 05:11:01 +0000 | [diff] [blame] | 2008 | } else { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2009 | Align = Info.Align; |
David Majnemer | 37bffb6 | 2014-08-04 05:11:01 +0000 | [diff] [blame] | 2010 | AlignIsRequired = Info.AlignIsRequired; |
| 2011 | } |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2012 | Width = Info.Width; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2013 | break; |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 2014 | } |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 2015 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2016 | case Type::Elaborated: |
| 2017 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2019 | case Type::Attributed: |
| 2020 | return getTypeInfo( |
| 2021 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 2022 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2023 | case Type::Atomic: { |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 2024 | // Start with the base type information. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2025 | TypeInfo Info = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 2026 | Width = Info.Width; |
| 2027 | Align = Info.Align; |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 2028 | |
JF Bastien | 801fca2 | 2018-05-09 03:51:12 +0000 | [diff] [blame] | 2029 | if (!Width) { |
| 2030 | // An otherwise zero-sized type should still generate an |
| 2031 | // atomic operation. |
| 2032 | Width = Target->getCharWidth(); |
| 2033 | assert(Align); |
| 2034 | } else if (Width <= Target->getMaxAtomicPromoteWidth()) { |
| 2035 | // If the size of the type doesn't exceed the platform's max |
| 2036 | // atomic promotion width, make the size and alignment more |
| 2037 | // favorable to atomic operations: |
| 2038 | |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 2039 | // Round the size up to a power of 2. |
| 2040 | if (!llvm::isPowerOf2_64(Width)) |
| 2041 | Width = llvm::NextPowerOf2(Width); |
| 2042 | |
| 2043 | // Set the alignment equal to the size. |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 2044 | Align = static_cast<unsigned>(Width); |
| 2045 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2046 | } |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 2047 | break; |
| 2048 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2049 | case Type::Pipe: |
Anastasia Stulova | b339893 | 2017-06-05 11:27:03 +0000 | [diff] [blame] | 2050 | Width = Target->getPointerWidth(getTargetAddressSpace(LangAS::opencl_global)); |
| 2051 | Align = Target->getPointerAlign(getTargetAddressSpace(LangAS::opencl_global)); |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2052 | break; |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 2053 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2054 | |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 2055 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2056 | return TypeInfo(Width, Align, AlignIsRequired); |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2059 | unsigned ASTContext::getOpenMPDefaultSimdAlign(QualType T) const { |
| 2060 | unsigned SimdAlign = getTargetInfo().getSimdDefaultAlign(); |
| 2061 | // Target ppc64 with QPX: simd default alignment for pointer to double is 32. |
| 2062 | if ((getTargetInfo().getTriple().getArch() == llvm::Triple::ppc64 || |
| 2063 | getTargetInfo().getTriple().getArch() == llvm::Triple::ppc64le) && |
| 2064 | getTargetInfo().getABI() == "elfv1-qpx" && |
| 2065 | T->isSpecificBuiltinType(BuiltinType::Double)) |
| 2066 | SimdAlign = 256; |
| 2067 | return SimdAlign; |
| 2068 | } |
| 2069 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 2070 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 2071 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 2072 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 2073 | } |
| 2074 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 2075 | /// toBits - Convert a size in characters to a size in characters. |
| 2076 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 2077 | return CharSize.getQuantity() * getCharWidth(); |
| 2078 | } |
| 2079 | |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 2080 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 2081 | /// This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2082 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 2083 | return getTypeInfoInChars(T).first; |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 2084 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2085 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 2086 | return getTypeInfoInChars(T).first; |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
Ken Dyck | a6046ab | 2010-01-26 17:25:18 +0000 | [diff] [blame] | 2089 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 2090 | /// characters. This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2091 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 2092 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 2093 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2094 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 2095 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 2098 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 2099 | /// type for the current target in bits. This can be different than the ABI |
| 2100 | /// alignment in cases where it is beneficial for performance to overalign |
| 2101 | /// a data type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2102 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2103 | TypeInfo TI = getTypeInfo(T); |
| 2104 | unsigned ABIAlign = TI.Align; |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2105 | |
David Majnemer | e154456 | 2015-04-24 01:25:05 +0000 | [diff] [blame] | 2106 | T = T->getBaseElementTypeUnsafe(); |
| 2107 | |
| 2108 | // The preferred alignment of member pointers is that of a pointer. |
| 2109 | if (T->isMemberPointerType()) |
| 2110 | return getPreferredTypeAlign(getPointerDiffType().getTypePtr()); |
| 2111 | |
Andrey Turetskiy | db6655f | 2016-02-10 11:58:46 +0000 | [diff] [blame] | 2112 | if (!Target->allowsLargerPreferedTypeAlignment()) |
| 2113 | return ABIAlign; |
Robert Lytton | eaf6f36 | 2013-11-12 10:09:34 +0000 | [diff] [blame] | 2114 | |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2115 | // Double and long long should be naturally aligned if possible. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2116 | if (const auto *CT = T->getAs<ComplexType>()) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2117 | T = CT->getElementType().getTypePtr(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2118 | if (const auto *ET = T->getAs<EnumType>()) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 2119 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2120 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | b57321a | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 2121 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2122 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
David Majnemer | 8b6bd57 | 2014-02-24 23:34:17 +0000 | [diff] [blame] | 2123 | // Don't increase the alignment if an alignment attribute was specified on a |
| 2124 | // typedef declaration. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2125 | if (!TI.AlignIsRequired) |
David Majnemer | 8b6bd57 | 2014-02-24 23:34:17 +0000 | [diff] [blame] | 2126 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2127 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 2128 | return ABIAlign; |
| 2129 | } |
| 2130 | |
Ulrich Weigand | ca3cb7f | 2015-04-21 17:29:35 +0000 | [diff] [blame] | 2131 | /// getTargetDefaultAlignForAttributeAligned - Return the default alignment |
| 2132 | /// for __attribute__((aligned)) on this target, to be used if no alignment |
| 2133 | /// value is specified. |
Eugene Zelenko | d4304d2 | 2015-11-04 21:37:17 +0000 | [diff] [blame] | 2134 | unsigned ASTContext::getTargetDefaultAlignForAttributeAligned() const { |
Ulrich Weigand | ca3cb7f | 2015-04-21 17:29:35 +0000 | [diff] [blame] | 2135 | return getTargetInfo().getDefaultAlignForAttributeAligned(); |
| 2136 | } |
| 2137 | |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 2138 | /// getAlignOfGlobalVar - Return the alignment in bits that should be given |
| 2139 | /// to a global variable of the specified type. |
| 2140 | unsigned ASTContext::getAlignOfGlobalVar(QualType T) const { |
| 2141 | return std::max(getTypeAlign(T), getTargetInfo().getMinGlobalAlign()); |
| 2142 | } |
| 2143 | |
| 2144 | /// getAlignOfGlobalVarInChars - Return the alignment in characters that |
| 2145 | /// should be given to a global variable of the specified type. |
| 2146 | CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T) const { |
| 2147 | return toCharUnitsFromBits(getAlignOfGlobalVar(T)); |
| 2148 | } |
| 2149 | |
David Majnemer | 08ef2ba | 2015-06-23 20:34:18 +0000 | [diff] [blame] | 2150 | CharUnits ASTContext::getOffsetOfBaseWithVBPtr(const CXXRecordDecl *RD) const { |
| 2151 | CharUnits Offset = CharUnits::Zero(); |
| 2152 | const ASTRecordLayout *Layout = &getASTRecordLayout(RD); |
| 2153 | while (const CXXRecordDecl *Base = Layout->getBaseSharingVBPtr()) { |
| 2154 | Offset += Layout->getBaseClassOffset(Base); |
| 2155 | Layout = &getASTRecordLayout(Base); |
| 2156 | } |
| 2157 | return Offset; |
| 2158 | } |
| 2159 | |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2160 | /// DeepCollectObjCIvars - |
| 2161 | /// This routine first collects all declared, but not synthesized, ivars in |
| 2162 | /// super class and then collects all ivars, including those synthesized for |
| 2163 | /// current class. This routine is used for implementation of current class |
| 2164 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2165 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 2166 | bool leafClass, |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2167 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2168 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 2169 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 2170 | if (!leafClass) { |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 2171 | for (const auto *I : OI->ivars()) |
| 2172 | Ivars.push_back(I); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2173 | } else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2174 | auto *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2175 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 2176 | Iv= Iv->getNextIvar()) |
| 2177 | Ivars.push_back(Iv); |
| 2178 | } |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2181 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 2182 | /// those inherited by it. |
| 2183 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 2184 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2185 | if (const auto *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2186 | // We can use protocol_iterator here instead of |
| 2187 | // all_referenced_protocol_iterator since we are walking all categories. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2188 | for (auto *Proto : OI->all_referenced_protocols()) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 2189 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 2190 | } |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2191 | |
| 2192 | // Categories of this Interface. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 2193 | for (const auto *Cat : OI->visible_categories()) |
| 2194 | CollectInheritedProtocols(Cat, Protocols); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2195 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2196 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 2197 | while (SD) { |
| 2198 | CollectInheritedProtocols(SD, Protocols); |
| 2199 | SD = SD->getSuperClass(); |
| 2200 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2201 | } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 2202 | for (auto *Proto : OC->protocols()) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 2203 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2204 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2205 | } else if (const auto *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 2206 | // Insert the protocol. |
| 2207 | if (!Protocols.insert( |
| 2208 | const_cast<ObjCProtocolDecl *>(OP->getCanonicalDecl())).second) |
| 2209 | return; |
| 2210 | |
| 2211 | for (auto *Proto : OP->protocols()) |
| 2212 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2213 | } |
| 2214 | } |
| 2215 | |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2216 | static bool unionHasUniqueObjectRepresentations(const ASTContext &Context, |
| 2217 | const RecordDecl *RD) { |
| 2218 | assert(RD->isUnion() && "Must be union type"); |
| 2219 | CharUnits UnionSize = Context.getTypeSizeInChars(RD->getTypeForDecl()); |
| 2220 | |
| 2221 | for (const auto *Field : RD->fields()) { |
| 2222 | if (!Context.hasUniqueObjectRepresentations(Field->getType())) |
| 2223 | return false; |
| 2224 | CharUnits FieldSize = Context.getTypeSizeInChars(Field->getType()); |
| 2225 | if (FieldSize != UnionSize) |
| 2226 | return false; |
| 2227 | } |
Eric Fiselier | 12a9f34 | 2018-02-02 20:30:39 +0000 | [diff] [blame] | 2228 | return !RD->field_empty(); |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
Benjamin Kramer | 802e625 | 2017-12-24 12:46:22 +0000 | [diff] [blame] | 2231 | static bool isStructEmpty(QualType Ty) { |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2232 | const RecordDecl *RD = Ty->castAs<RecordType>()->getDecl(); |
| 2233 | |
| 2234 | if (!RD->field_empty()) |
| 2235 | return false; |
| 2236 | |
| 2237 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RD)) |
| 2238 | return ClassDecl->isEmpty(); |
| 2239 | |
| 2240 | return true; |
| 2241 | } |
| 2242 | |
| 2243 | static llvm::Optional<int64_t> |
| 2244 | structHasUniqueObjectRepresentations(const ASTContext &Context, |
| 2245 | const RecordDecl *RD) { |
| 2246 | assert(!RD->isUnion() && "Must be struct/class type"); |
| 2247 | const auto &Layout = Context.getASTRecordLayout(RD); |
| 2248 | |
| 2249 | int64_t CurOffsetInBits = 0; |
| 2250 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RD)) { |
| 2251 | if (ClassDecl->isDynamicClass()) |
| 2252 | return llvm::None; |
| 2253 | |
| 2254 | SmallVector<std::pair<QualType, int64_t>, 4> Bases; |
| 2255 | for (const auto Base : ClassDecl->bases()) { |
| 2256 | // Empty types can be inherited from, and non-empty types can potentially |
| 2257 | // have tail padding, so just make sure there isn't an error. |
| 2258 | if (!isStructEmpty(Base.getType())) { |
| 2259 | llvm::Optional<int64_t> Size = structHasUniqueObjectRepresentations( |
| 2260 | Context, Base.getType()->getAs<RecordType>()->getDecl()); |
| 2261 | if (!Size) |
| 2262 | return llvm::None; |
| 2263 | Bases.emplace_back(Base.getType(), Size.getValue()); |
| 2264 | } |
| 2265 | } |
| 2266 | |
Mandeep Singh Grang | c205d8c | 2018-03-27 16:50:00 +0000 | [diff] [blame] | 2267 | llvm::sort( |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2268 | Bases.begin(), Bases.end(), [&](const std::pair<QualType, int64_t> &L, |
| 2269 | const std::pair<QualType, int64_t> &R) { |
| 2270 | return Layout.getBaseClassOffset(L.first->getAsCXXRecordDecl()) < |
| 2271 | Layout.getBaseClassOffset(R.first->getAsCXXRecordDecl()); |
| 2272 | }); |
| 2273 | |
| 2274 | for (const auto Base : Bases) { |
| 2275 | int64_t BaseOffset = Context.toBits( |
| 2276 | Layout.getBaseClassOffset(Base.first->getAsCXXRecordDecl())); |
| 2277 | int64_t BaseSize = Base.second; |
| 2278 | if (BaseOffset != CurOffsetInBits) |
| 2279 | return llvm::None; |
| 2280 | CurOffsetInBits = BaseOffset + BaseSize; |
| 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | for (const auto *Field : RD->fields()) { |
| 2285 | if (!Field->getType()->isReferenceType() && |
| 2286 | !Context.hasUniqueObjectRepresentations(Field->getType())) |
| 2287 | return llvm::None; |
| 2288 | |
| 2289 | int64_t FieldSizeInBits = |
| 2290 | Context.toBits(Context.getTypeSizeInChars(Field->getType())); |
| 2291 | if (Field->isBitField()) { |
| 2292 | int64_t BitfieldSize = Field->getBitWidthValue(Context); |
| 2293 | |
| 2294 | if (BitfieldSize > FieldSizeInBits) |
| 2295 | return llvm::None; |
| 2296 | FieldSizeInBits = BitfieldSize; |
| 2297 | } |
| 2298 | |
| 2299 | int64_t FieldOffsetInBits = Context.getFieldOffset(Field); |
| 2300 | |
| 2301 | if (FieldOffsetInBits != CurOffsetInBits) |
| 2302 | return llvm::None; |
| 2303 | |
| 2304 | CurOffsetInBits = FieldSizeInBits + FieldOffsetInBits; |
| 2305 | } |
| 2306 | |
| 2307 | return CurOffsetInBits; |
| 2308 | } |
| 2309 | |
| 2310 | bool ASTContext::hasUniqueObjectRepresentations(QualType Ty) const { |
| 2311 | // C++17 [meta.unary.prop]: |
| 2312 | // The predicate condition for a template specialization |
| 2313 | // has_unique_object_representations<T> shall be |
| 2314 | // satisfied if and only if: |
| 2315 | // (9.1) - T is trivially copyable, and |
| 2316 | // (9.2) - any two objects of type T with the same value have the same |
| 2317 | // object representation, where two objects |
| 2318 | // of array or non-union class type are considered to have the same value |
| 2319 | // if their respective sequences of |
| 2320 | // direct subobjects have the same values, and two objects of union type |
| 2321 | // are considered to have the same |
| 2322 | // value if they have the same active member and the corresponding members |
| 2323 | // have the same value. |
| 2324 | // The set of scalar types for which this condition holds is |
| 2325 | // implementation-defined. [ Note: If a type has padding |
| 2326 | // bits, the condition does not hold; otherwise, the condition holds true |
| 2327 | // for unsigned integral types. -- end note ] |
| 2328 | assert(!Ty.isNull() && "Null QualType sent to unique object rep check"); |
| 2329 | |
| 2330 | // Arrays are unique only if their element type is unique. |
| 2331 | if (Ty->isArrayType()) |
| 2332 | return hasUniqueObjectRepresentations(getBaseElementType(Ty)); |
| 2333 | |
| 2334 | // (9.1) - T is trivially copyable... |
| 2335 | if (!Ty.isTriviallyCopyableType(*this)) |
| 2336 | return false; |
| 2337 | |
| 2338 | // All integrals and enums are unique. |
| 2339 | if (Ty->isIntegralOrEnumerationType()) |
| 2340 | return true; |
| 2341 | |
| 2342 | // All other pointers are unique. |
| 2343 | if (Ty->isPointerType()) |
| 2344 | return true; |
| 2345 | |
| 2346 | if (Ty->isMemberPointerType()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2347 | const auto *MPT = Ty->getAs<MemberPointerType>(); |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2348 | return !ABI->getMemberPointerInfo(MPT).HasPadding; |
| 2349 | } |
| 2350 | |
| 2351 | if (Ty->isRecordType()) { |
| 2352 | const RecordDecl *Record = Ty->getAs<RecordType>()->getDecl(); |
| 2353 | |
Erich Keane | bd2197c | 2017-12-12 16:02:06 +0000 | [diff] [blame] | 2354 | if (Record->isInvalidDecl()) |
| 2355 | return false; |
| 2356 | |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2357 | if (Record->isUnion()) |
| 2358 | return unionHasUniqueObjectRepresentations(*this, Record); |
| 2359 | |
| 2360 | Optional<int64_t> StructSize = |
| 2361 | structHasUniqueObjectRepresentations(*this, Record); |
| 2362 | |
| 2363 | return StructSize && |
| 2364 | StructSize.getValue() == static_cast<int64_t>(getTypeSize(Ty)); |
| 2365 | } |
| 2366 | |
| 2367 | // FIXME: More cases to handle here (list by rsmith): |
| 2368 | // vectors (careful about, eg, vector of 3 foo) |
| 2369 | // _Complex int and friends |
| 2370 | // _Atomic T |
| 2371 | // Obj-C block pointers |
| 2372 | // Obj-C object pointers |
| 2373 | // and perhaps OpenCL's various builtin types (pipe, sampler_t, event_t, |
| 2374 | // clk_event_t, queue_t, reserve_id_t) |
| 2375 | // There're also Obj-C class types and the Obj-C selector type, but I think it |
| 2376 | // makes sense for those to return false here. |
| 2377 | |
| 2378 | return false; |
| 2379 | } |
| 2380 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2381 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 2382 | unsigned count = 0; |
| 2383 | // Count ivars declared in class extension. |
Aaron Ballman | b4a5345 | 2014-03-13 21:57:01 +0000 | [diff] [blame] | 2384 | for (const auto *Ext : OI->known_extensions()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2385 | count += Ext->ivar_size(); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2386 | |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 2387 | // Count ivar defined in this class's implementation. This |
| 2388 | // includes synthesized ivars. |
| 2389 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 2390 | count += ImplDecl->ivar_size(); |
| 2391 | |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2392 | return count; |
| 2393 | } |
| 2394 | |
Argyrios Kyrtzidis | 2e809ce | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 2395 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 2396 | if (!E) |
| 2397 | return false; |
| 2398 | |
| 2399 | // nullptr_t is always treated as null. |
| 2400 | if (E->getType()->isNullPtrType()) return true; |
| 2401 | |
| 2402 | if (E->getType()->isAnyPointerType() && |
| 2403 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 2404 | Expr::NPC_ValueDependentIsNull)) |
| 2405 | return true; |
| 2406 | |
| 2407 | // Unfortunately, __null has type 'int'. |
| 2408 | if (isa<GNUNullExpr>(E)) return true; |
| 2409 | |
| 2410 | return false; |
| 2411 | } |
| 2412 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2413 | /// Get the implementation of ObjCInterfaceDecl, or nullptr if none |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2414 | /// exists. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2415 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 2416 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 2417 | I = ObjCImpls.find(D); |
| 2418 | if (I != ObjCImpls.end()) |
| 2419 | return cast<ObjCImplementationDecl>(I->second); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2420 | return nullptr; |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2421 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2422 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2423 | /// Get the implementation of ObjCCategoryDecl, or nullptr if none |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2424 | /// exists. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2425 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 2426 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 2427 | I = ObjCImpls.find(D); |
| 2428 | if (I != ObjCImpls.end()) |
| 2429 | return cast<ObjCCategoryImplDecl>(I->second); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2430 | return nullptr; |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2433 | /// Set the implementation of ObjCInterfaceDecl. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2434 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 2435 | ObjCImplementationDecl *ImplD) { |
| 2436 | assert(IFaceD && ImplD && "Passed null params"); |
| 2437 | ObjCImpls[IFaceD] = ImplD; |
| 2438 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2439 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2440 | /// Set the implementation of ObjCCategoryDecl. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2441 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 2442 | ObjCCategoryImplDecl *ImplD) { |
| 2443 | assert(CatD && ImplD && "Passed null params"); |
| 2444 | ObjCImpls[CatD] = ImplD; |
| 2445 | } |
| 2446 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 2447 | const ObjCMethodDecl * |
| 2448 | ASTContext::getObjCMethodRedeclaration(const ObjCMethodDecl *MD) const { |
| 2449 | return ObjCMethodRedecls.lookup(MD); |
| 2450 | } |
| 2451 | |
| 2452 | void ASTContext::setObjCMethodRedeclaration(const ObjCMethodDecl *MD, |
| 2453 | const ObjCMethodDecl *Redecl) { |
| 2454 | assert(!getObjCMethodRedeclaration(MD) && "MD already has a redeclaration"); |
| 2455 | ObjCMethodRedecls[MD] = Redecl; |
| 2456 | } |
| 2457 | |
Dmitri Gribenko | 37527c2 | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 2458 | const ObjCInterfaceDecl *ASTContext::getObjContainingInterface( |
| 2459 | const NamedDecl *ND) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2460 | if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2461 | return ID; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2462 | if (const auto *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2463 | return CD->getClassInterface(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2464 | if (const auto *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2465 | return IMD->getClassInterface(); |
| 2466 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2467 | return nullptr; |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2470 | /// Get the copy initialization expression of VarDecl, or nullptr if |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2471 | /// none exists. |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 2472 | Expr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) { |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2473 | assert(VD && "Passed null params"); |
| 2474 | assert(VD->hasAttr<BlocksAttr>() && |
| 2475 | "getBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 5019809 | 2010-12-02 17:02:11 +0000 | [diff] [blame] | 2476 | llvm::DenseMap<const VarDecl*, Expr*>::iterator |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2477 | I = BlockVarCopyInits.find(VD); |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 2478 | return (I != BlockVarCopyInits.end()) ? I->second : nullptr; |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2481 | /// Set the copy inialization expression of a block var decl. |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2482 | void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) { |
| 2483 | assert(VD && Init && "Passed null params"); |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2484 | assert(VD->hasAttr<BlocksAttr>() && |
| 2485 | "setBlockVarCopyInits - not __block var"); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2486 | BlockVarCopyInits[VD] = Init; |
| 2487 | } |
| 2488 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2489 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2490 | unsigned DataSize) const { |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 2491 | if (!DataSize) |
| 2492 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 2493 | else |
| 2494 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2495 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 2496 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2497 | auto *TInfo = |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2498 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 2499 | new (TInfo) TypeSourceInfo(T); |
| 2500 | return TInfo; |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2503 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2504 | SourceLocation L) const { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2505 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | 2d525f0 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2506 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | 3665e00 | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 2507 | return DI; |
| 2508 | } |
| 2509 | |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2510 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2511 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2512 | return getObjCLayout(D, nullptr); |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2516 | ASTContext::getASTObjCImplementationLayout( |
| 2517 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2518 | return getObjCLayout(D->getClassInterface(), D); |
| 2519 | } |
| 2520 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2521 | //===----------------------------------------------------------------------===// |
| 2522 | // Type creation/memoization methods |
| 2523 | //===----------------------------------------------------------------------===// |
| 2524 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2525 | QualType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2526 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 2527 | unsigned fastQuals = quals.getFastQualifiers(); |
| 2528 | quals.removeFastQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2529 | |
| 2530 | // Check if we've already instantiated this type. |
| 2531 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2532 | ExtQuals::Profile(ID, baseType, quals); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2533 | void *insertPos = nullptr; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2534 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 2535 | assert(eq->getQualifiers() == quals); |
| 2536 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2539 | // If the base type is not canonical, make the appropriate canonical type. |
| 2540 | QualType canon; |
| 2541 | if (!baseType->isCanonicalUnqualified()) { |
| 2542 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2543 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 2544 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2545 | |
| 2546 | // Re-find the insert position. |
| 2547 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 2548 | } |
| 2549 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2550 | auto *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2551 | ExtQualNodes.InsertNode(eq, insertPos); |
| 2552 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 2555 | QualType ASTContext::getAddrSpaceQualType(QualType T, |
| 2556 | LangAS AddressSpace) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2557 | QualType CanT = getCanonicalType(T); |
| 2558 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | 445fcab | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2559 | return T; |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2560 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2561 | // If we are composing extended qualifiers together, merge together |
| 2562 | // into one ExtQuals node. |
| 2563 | QualifierCollector Quals; |
| 2564 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2565 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2566 | // If this type already has an address space specified, it cannot get |
| 2567 | // another one. |
| 2568 | assert(!Quals.hasAddressSpace() && |
| 2569 | "Type cannot be in multiple addr spaces!"); |
| 2570 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2571 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2572 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | 025b5fb | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 2575 | QualType ASTContext::removeAddrSpaceQualType(QualType T) const { |
| 2576 | // If we are composing extended qualifiers together, merge together |
| 2577 | // into one ExtQuals node. |
| 2578 | QualifierCollector Quals; |
| 2579 | const Type *TypeNode = Quals.strip(T); |
| 2580 | |
| 2581 | // If the qualifier doesn't have an address space just return it. |
| 2582 | if (!Quals.hasAddressSpace()) |
| 2583 | return T; |
| 2584 | |
| 2585 | Quals.removeAddressSpace(); |
| 2586 | |
| 2587 | // Removal of the address space can mean there are no longer any |
| 2588 | // non-fast qualifiers, so creating an ExtQualType isn't possible (asserts) |
| 2589 | // or required. |
| 2590 | if (Quals.hasNonFastQualifiers()) |
| 2591 | return getExtQualType(TypeNode, Quals); |
| 2592 | else |
| 2593 | return QualType(TypeNode, Quals.getFastQualifiers()); |
| 2594 | } |
| 2595 | |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2596 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2597 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2598 | QualType CanT = getCanonicalType(T); |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2599 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2600 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2601 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2602 | if (const auto *ptr = T->getAs<PointerType>()) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2603 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 2604 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | b68215c | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 2605 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 2606 | return getPointerType(ResultType); |
| 2607 | } |
| 2608 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2609 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2610 | // If we are composing extended qualifiers together, merge together |
| 2611 | // into one ExtQuals node. |
| 2612 | QualifierCollector Quals; |
| 2613 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2614 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2615 | // If this type already has an ObjCGC specified, it cannot get |
| 2616 | // another one. |
| 2617 | assert(!Quals.hasObjCGCAttr() && |
| 2618 | "Type cannot have multiple ObjCGCs!"); |
| 2619 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2620 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2621 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2622 | } |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2623 | |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2624 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 2625 | FunctionType::ExtInfo Info) { |
| 2626 | if (T->getExtInfo() == Info) |
| 2627 | return T; |
| 2628 | |
| 2629 | QualType Result; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2630 | if (const auto *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2631 | Result = getFunctionNoProtoType(FNPT->getReturnType(), Info); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2632 | } else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2633 | const auto *FPT = cast<FunctionProtoType>(T); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2634 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 2635 | EPI.ExtInfo = Info; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2636 | Result = getFunctionType(FPT->getReturnType(), FPT->getParamTypes(), EPI); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | return cast<FunctionType>(Result.getTypePtr()); |
| 2640 | } |
| 2641 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2642 | void ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD, |
| 2643 | QualType ResultType) { |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2644 | FD = FD->getMostRecentDecl(); |
| 2645 | while (true) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2646 | const auto *FPT = FD->getType()->castAs<FunctionProtoType>(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2647 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2648 | FD->setType(getFunctionType(ResultType, FPT->getParamTypes(), EPI)); |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2649 | if (FunctionDecl *Next = FD->getPreviousDecl()) |
| 2650 | FD = Next; |
| 2651 | else |
| 2652 | break; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2653 | } |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2654 | if (ASTMutationListener *L = getASTMutationListener()) |
| 2655 | L->DeducedReturnType(FD, ResultType); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2658 | /// Get a function type and produce the equivalent function type with the |
| 2659 | /// specified exception specification. Type sugar that can be present on a |
| 2660 | /// declaration of a function with an exception specification is permitted |
| 2661 | /// and preserved. Other type sugar (for instance, typedefs) is not. |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2662 | QualType ASTContext::getFunctionTypeWithExceptionSpec( |
| 2663 | QualType Orig, const FunctionProtoType::ExceptionSpecInfo &ESI) { |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2664 | // Might have some parens. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2665 | if (const auto *PT = dyn_cast<ParenType>(Orig)) |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2666 | return getParenType( |
| 2667 | getFunctionTypeWithExceptionSpec(PT->getInnerType(), ESI)); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2668 | |
| 2669 | // Might have a calling-convention attribute. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2670 | if (const auto *AT = dyn_cast<AttributedType>(Orig)) |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2671 | return getAttributedType( |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2672 | AT->getAttrKind(), |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2673 | getFunctionTypeWithExceptionSpec(AT->getModifiedType(), ESI), |
| 2674 | getFunctionTypeWithExceptionSpec(AT->getEquivalentType(), ESI)); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2675 | |
| 2676 | // Anything else must be a function type. Rebuild it with the new exception |
| 2677 | // specification. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2678 | const auto *Proto = cast<FunctionProtoType>(Orig); |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2679 | return getFunctionType( |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2680 | Proto->getReturnType(), Proto->getParamTypes(), |
| 2681 | Proto->getExtProtoInfo().withExceptionSpec(ESI)); |
| 2682 | } |
| 2683 | |
Richard Smith | dfe85e2 | 2016-12-15 02:35:39 +0000 | [diff] [blame] | 2684 | bool ASTContext::hasSameFunctionTypeIgnoringExceptionSpec(QualType T, |
| 2685 | QualType U) { |
| 2686 | return hasSameType(T, U) || |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2687 | (getLangOpts().CPlusPlus17 && |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2688 | hasSameType(getFunctionTypeWithExceptionSpec(T, EST_None), |
| 2689 | getFunctionTypeWithExceptionSpec(U, EST_None))); |
Richard Smith | dfe85e2 | 2016-12-15 02:35:39 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2692 | void ASTContext::adjustExceptionSpec( |
| 2693 | FunctionDecl *FD, const FunctionProtoType::ExceptionSpecInfo &ESI, |
| 2694 | bool AsWritten) { |
| 2695 | // Update the type. |
| 2696 | QualType Updated = |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2697 | getFunctionTypeWithExceptionSpec(FD->getType(), ESI); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2698 | FD->setType(Updated); |
| 2699 | |
| 2700 | if (!AsWritten) |
| 2701 | return; |
| 2702 | |
| 2703 | // Update the type in the type source information too. |
| 2704 | if (TypeSourceInfo *TSInfo = FD->getTypeSourceInfo()) { |
| 2705 | // If the type and the type-as-written differ, we may need to update |
| 2706 | // the type-as-written too. |
| 2707 | if (TSInfo->getType() != FD->getType()) |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2708 | Updated = getFunctionTypeWithExceptionSpec(TSInfo->getType(), ESI); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2709 | |
| 2710 | // FIXME: When we get proper type location information for exceptions, |
| 2711 | // we'll also have to rebuild the TypeSourceInfo. For now, we just patch |
| 2712 | // up the TypeSourceInfo; |
| 2713 | assert(TypeLoc::getFullDataSizeForType(Updated) == |
| 2714 | TypeLoc::getFullDataSizeForType(TSInfo->getType()) && |
| 2715 | "TypeLoc size mismatch from updating exception specification"); |
| 2716 | TSInfo->overrideType(Updated); |
| 2717 | } |
| 2718 | } |
| 2719 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2720 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 2721 | /// number with the specified element type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2722 | QualType ASTContext::getComplexType(QualType T) const { |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2723 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2724 | // structure. |
| 2725 | llvm::FoldingSetNodeID ID; |
| 2726 | ComplexType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2727 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2728 | void *InsertPos = nullptr; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2729 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2730 | return QualType(CT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2731 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2732 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2733 | // so fill in the canonical type field. |
| 2734 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2735 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2736 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2737 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2738 | // Get the new insert position for the node we care about. |
| 2739 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2740 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2741 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2742 | auto *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2743 | Types.push_back(New); |
| 2744 | ComplexTypes.InsertNode(New, InsertPos); |
| 2745 | return QualType(New, 0); |
| 2746 | } |
| 2747 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 2748 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 2749 | /// the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2750 | QualType ASTContext::getPointerType(QualType T) const { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 2751 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2752 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2753 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2754 | PointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2756 | void *InsertPos = nullptr; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2757 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2758 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2759 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 2760 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2761 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2762 | QualType Canonical; |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2763 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2764 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2765 | |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2766 | // Get the new insert position for the node we care about. |
| 2767 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2768 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2769 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2770 | auto *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2771 | Types.push_back(New); |
| 2772 | PointerTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2773 | return QualType(New, 0); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2776 | QualType ASTContext::getAdjustedType(QualType Orig, QualType New) const { |
| 2777 | llvm::FoldingSetNodeID ID; |
| 2778 | AdjustedType::Profile(ID, Orig, New); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2779 | void *InsertPos = nullptr; |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2780 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2781 | if (AT) |
| 2782 | return QualType(AT, 0); |
| 2783 | |
| 2784 | QualType Canonical = getCanonicalType(New); |
| 2785 | |
| 2786 | // Get the new insert position for the node we care about. |
| 2787 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2788 | assert(!AT && "Shouldn't be in the map!"); |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2789 | |
| 2790 | AT = new (*this, TypeAlignment) |
| 2791 | AdjustedType(Type::Adjusted, Orig, New, Canonical); |
| 2792 | Types.push_back(AT); |
| 2793 | AdjustedTypes.InsertNode(AT, InsertPos); |
| 2794 | return QualType(AT, 0); |
| 2795 | } |
| 2796 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2797 | QualType ASTContext::getDecayedType(QualType T) const { |
| 2798 | assert((T->isArrayType() || T->isFunctionType()) && "T does not decay"); |
| 2799 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2800 | QualType Decayed; |
| 2801 | |
| 2802 | // C99 6.7.5.3p7: |
| 2803 | // A declaration of a parameter as "array of type" shall be |
| 2804 | // adjusted to "qualified pointer to type", where the type |
| 2805 | // qualifiers (if any) are those specified within the [ and ] of |
| 2806 | // the array type derivation. |
| 2807 | if (T->isArrayType()) |
| 2808 | Decayed = getArrayDecayedType(T); |
| 2809 | |
| 2810 | // C99 6.7.5.3p8: |
| 2811 | // A declaration of a parameter as "function returning type" |
| 2812 | // shall be adjusted to "pointer to function returning type", as |
| 2813 | // in 6.3.2.1. |
| 2814 | if (T->isFunctionType()) |
| 2815 | Decayed = getPointerType(T); |
| 2816 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2817 | llvm::FoldingSetNodeID ID; |
| 2818 | AdjustedType::Profile(ID, T, Decayed); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2819 | void *InsertPos = nullptr; |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2820 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2821 | if (AT) |
| 2822 | return QualType(AT, 0); |
| 2823 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2824 | QualType Canonical = getCanonicalType(Decayed); |
| 2825 | |
| 2826 | // Get the new insert position for the node we care about. |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2827 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2828 | assert(!AT && "Shouldn't be in the map!"); |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2829 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2830 | AT = new (*this, TypeAlignment) DecayedType(T, Decayed, Canonical); |
| 2831 | Types.push_back(AT); |
| 2832 | AdjustedTypes.InsertNode(AT, InsertPos); |
| 2833 | return QualType(AT, 0); |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2836 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2837 | /// a pointer to the specified block. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2838 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 0ac01283 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2839 | assert(T->isFunctionType() && "block of function types only"); |
| 2840 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2841 | // structure. |
| 2842 | llvm::FoldingSetNodeID ID; |
| 2843 | BlockPointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2844 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2845 | void *InsertPos = nullptr; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2846 | if (BlockPointerType *PT = |
| 2847 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2848 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2849 | |
| 2850 | // 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] | 2851 | // type either so fill in the canonical type field. |
| 2852 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2853 | if (!T.isCanonical()) { |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2854 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2856 | // Get the new insert position for the node we care about. |
| 2857 | BlockPointerType *NewIP = |
| 2858 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2859 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2860 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2861 | auto *New = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2862 | Types.push_back(New); |
| 2863 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 2864 | return QualType(New, 0); |
| 2865 | } |
| 2866 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2867 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 2868 | /// lvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2869 | QualType |
| 2870 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Douglas Gregor | 291e8ee | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 2871 | assert(getCanonicalType(T) != OverloadTy && |
| 2872 | "Unresolved overloaded function type"); |
| 2873 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2874 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2875 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2876 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2877 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2878 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2879 | void *InsertPos = nullptr; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2880 | if (LValueReferenceType *RT = |
| 2881 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2882 | return QualType(RT, 0); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2883 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2884 | const auto *InnerRef = T->getAs<ReferenceType>(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2885 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2886 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2887 | // either, so fill in the canonical type field. |
| 2888 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2889 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 2890 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2891 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2892 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2893 | // Get the new insert position for the node we care about. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2894 | LValueReferenceType *NewIP = |
| 2895 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2896 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2899 | auto *New = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 2900 | SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2901 | Types.push_back(New); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2902 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2903 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2904 | return QualType(New, 0); |
| 2905 | } |
| 2906 | |
| 2907 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 2908 | /// rvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2909 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2910 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2911 | // structure. |
| 2912 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2913 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2914 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2915 | void *InsertPos = nullptr; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2916 | if (RValueReferenceType *RT = |
| 2917 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2918 | return QualType(RT, 0); |
| 2919 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2920 | const auto *InnerRef = T->getAs<ReferenceType>(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2921 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2922 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2923 | // either, so fill in the canonical type field. |
| 2924 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2925 | if (InnerRef || !T.isCanonical()) { |
| 2926 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2927 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2928 | |
| 2929 | // Get the new insert position for the node we care about. |
| 2930 | RValueReferenceType *NewIP = |
| 2931 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2932 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2935 | auto *New = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2936 | Types.push_back(New); |
| 2937 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2938 | return QualType(New, 0); |
| 2939 | } |
| 2940 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2941 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 2942 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2943 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2944 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2945 | // structure. |
| 2946 | llvm::FoldingSetNodeID ID; |
| 2947 | MemberPointerType::Profile(ID, T, Cls); |
| 2948 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2949 | void *InsertPos = nullptr; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2950 | if (MemberPointerType *PT = |
| 2951 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2952 | return QualType(PT, 0); |
| 2953 | |
| 2954 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 2955 | // type either, so fill in the canonical type field. |
| 2956 | QualType Canonical; |
Douglas Gregor | 615ac67 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 2957 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2958 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 2959 | |
| 2960 | // Get the new insert position for the node we care about. |
| 2961 | MemberPointerType *NewIP = |
| 2962 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2963 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2964 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2965 | auto *New = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2966 | Types.push_back(New); |
| 2967 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 2968 | return QualType(New, 0); |
| 2969 | } |
| 2970 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2971 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 2972 | /// array of the specified element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2973 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2974 | const llvm::APInt &ArySizeIn, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 2975 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2976 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 2dfdb82 | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 2977 | assert((EltTy->isDependentType() || |
| 2978 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 2979 | "Constant array of VLAs is illegal!"); |
| 2980 | |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 2981 | // Convert the array size into a canonical width matching the pointer size for |
| 2982 | // the target. |
| 2983 | llvm::APInt ArySize(ArySizeIn); |
Konstantin Zhuravlyov | 9c1e310 | 2017-03-21 18:55:39 +0000 | [diff] [blame] | 2984 | ArySize = ArySize.zextOrTrunc(Target->getMaxPointerWidth()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2985 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2986 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 2987 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2988 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2989 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2990 | if (ConstantArrayType *ATP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 2991 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2992 | return QualType(ATP, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2993 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2994 | // If the element type isn't canonical or has qualifiers, this won't |
| 2995 | // be a canonical type either, so fill in the canonical type field. |
| 2996 | QualType Canon; |
| 2997 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 2998 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2999 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3000 | ASM, IndexTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3001 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3002 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 3003 | // Get the new insert position for the node we care about. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3004 | ConstantArrayType *NewIP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 3005 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3006 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 3007 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3008 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3009 | auto *New = new (*this,TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3010 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 3011 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 3012 | Types.push_back(New); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3013 | return QualType(New, 0); |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3016 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 3017 | /// variably-modified, into the corresponding type with all the known |
| 3018 | /// sizes replaced with [*]. |
| 3019 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 3020 | // Vastly most common case. |
| 3021 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3022 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3023 | QualType result; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3024 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3025 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3026 | const Type *ty = split.Ty; |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3027 | switch (ty->getTypeClass()) { |
| 3028 | #define TYPE(Class, Base) |
| 3029 | #define ABSTRACT_TYPE(Class, Base) |
| 3030 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 3031 | #include "clang/AST/TypeNodes.def" |
| 3032 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 3033 | |
| 3034 | // These types should never be variably-modified. |
| 3035 | case Type::Builtin: |
| 3036 | case Type::Complex: |
| 3037 | case Type::Vector: |
| 3038 | case Type::ExtVector: |
| 3039 | case Type::DependentSizedExtVector: |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3040 | case Type::DependentAddressSpace: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3041 | case Type::ObjCObject: |
| 3042 | case Type::ObjCInterface: |
| 3043 | case Type::ObjCObjectPointer: |
| 3044 | case Type::Record: |
| 3045 | case Type::Enum: |
| 3046 | case Type::UnresolvedUsing: |
| 3047 | case Type::TypeOfExpr: |
| 3048 | case Type::TypeOf: |
| 3049 | case Type::Decltype: |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3050 | case Type::UnaryTransform: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3051 | case Type::DependentName: |
| 3052 | case Type::InjectedClassName: |
| 3053 | case Type::TemplateSpecialization: |
| 3054 | case Type::DependentTemplateSpecialization: |
| 3055 | case Type::TemplateTypeParm: |
| 3056 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3057 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 3058 | case Type::DeducedTemplateSpecialization: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3059 | case Type::PackExpansion: |
| 3060 | llvm_unreachable("type should never be variably-modified"); |
| 3061 | |
| 3062 | // These types can be variably-modified but should never need to |
| 3063 | // further decay. |
| 3064 | case Type::FunctionNoProto: |
| 3065 | case Type::FunctionProto: |
| 3066 | case Type::BlockPointer: |
| 3067 | case Type::MemberPointer: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3068 | case Type::Pipe: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3069 | return type; |
| 3070 | |
| 3071 | // These types can be variably-modified. All these modifications |
| 3072 | // preserve structure except as noted by comments. |
| 3073 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 3074 | // optimizations available here. |
| 3075 | case Type::Pointer: |
| 3076 | result = getPointerType(getVariableArrayDecayedType( |
| 3077 | cast<PointerType>(ty)->getPointeeType())); |
| 3078 | break; |
| 3079 | |
| 3080 | case Type::LValueReference: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3081 | const auto *lv = cast<LValueReferenceType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3082 | result = getLValueReferenceType( |
| 3083 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 3084 | lv->isSpelledAsLValue()); |
| 3085 | break; |
| 3086 | } |
| 3087 | |
| 3088 | case Type::RValueReference: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3089 | const auto *lv = cast<RValueReferenceType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3090 | result = getRValueReferenceType( |
| 3091 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 3092 | break; |
| 3093 | } |
| 3094 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3095 | case Type::Atomic: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3096 | const auto *at = cast<AtomicType>(ty); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3097 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 3098 | break; |
| 3099 | } |
| 3100 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3101 | case Type::ConstantArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3102 | const auto *cat = cast<ConstantArrayType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3103 | result = getConstantArrayType( |
| 3104 | getVariableArrayDecayedType(cat->getElementType()), |
| 3105 | cat->getSize(), |
| 3106 | cat->getSizeModifier(), |
| 3107 | cat->getIndexTypeCVRQualifiers()); |
| 3108 | break; |
| 3109 | } |
| 3110 | |
| 3111 | case Type::DependentSizedArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3112 | const auto *dat = cast<DependentSizedArrayType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3113 | result = getDependentSizedArrayType( |
| 3114 | getVariableArrayDecayedType(dat->getElementType()), |
| 3115 | dat->getSizeExpr(), |
| 3116 | dat->getSizeModifier(), |
| 3117 | dat->getIndexTypeCVRQualifiers(), |
| 3118 | dat->getBracketsRange()); |
| 3119 | break; |
| 3120 | } |
| 3121 | |
| 3122 | // Turn incomplete types into [*] types. |
| 3123 | case Type::IncompleteArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3124 | const auto *iat = cast<IncompleteArrayType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3125 | result = getVariableArrayType( |
| 3126 | getVariableArrayDecayedType(iat->getElementType()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3127 | /*size*/ nullptr, |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3128 | ArrayType::Normal, |
| 3129 | iat->getIndexTypeCVRQualifiers(), |
| 3130 | SourceRange()); |
| 3131 | break; |
| 3132 | } |
| 3133 | |
| 3134 | // Turn VLA types into [*] types. |
| 3135 | case Type::VariableArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3136 | const auto *vat = cast<VariableArrayType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3137 | result = getVariableArrayType( |
| 3138 | getVariableArrayDecayedType(vat->getElementType()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3139 | /*size*/ nullptr, |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3140 | ArrayType::Star, |
| 3141 | vat->getIndexTypeCVRQualifiers(), |
| 3142 | vat->getBracketsRange()); |
| 3143 | break; |
| 3144 | } |
| 3145 | } |
| 3146 | |
| 3147 | // Apply the top-level qualifiers from the original. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3148 | return getQualifiedType(result, split.Quals); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3149 | } |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3150 | |
Steve Naroff | cadebd0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 3151 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 3152 | /// variable array of the specified element type. |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3153 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 3154 | Expr *NumElts, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 3155 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3156 | unsigned IndexTypeQuals, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3157 | SourceRange Brackets) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3158 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 3159 | // that have an expression provided for their size. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3160 | QualType Canon; |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 3161 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3162 | // Be sure to pull qualifiers off the element type. |
| 3163 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 3164 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3165 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3166 | IndexTypeQuals, Brackets); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3167 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3170 | auto *New = new (*this, TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3171 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3172 | |
| 3173 | VariableArrayTypes.push_back(New); |
| 3174 | Types.push_back(New); |
| 3175 | return QualType(New, 0); |
| 3176 | } |
| 3177 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3178 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 3179 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 3180 | /// type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3181 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 3182 | Expr *numElements, |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3183 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3184 | unsigned elementTypeQuals, |
| 3185 | SourceRange brackets) const { |
| 3186 | assert((!numElements || numElements->isTypeDependent() || |
| 3187 | numElements->isValueDependent()) && |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3188 | "Size must be type- or value-dependent!"); |
| 3189 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3190 | // Dependently-sized array types that do not have a specified number |
| 3191 | // of elements will have their sizes deduced from a dependent |
| 3192 | // initializer. We do no canonicalization here at all, which is okay |
| 3193 | // because they can't be used in most locations. |
| 3194 | if (!numElements) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3195 | auto *newType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3196 | = new (*this, TypeAlignment) |
| 3197 | DependentSizedArrayType(*this, elementType, QualType(), |
| 3198 | numElements, ASM, elementTypeQuals, |
| 3199 | brackets); |
| 3200 | Types.push_back(newType); |
| 3201 | return QualType(newType, 0); |
| 3202 | } |
| 3203 | |
| 3204 | // Otherwise, we actually build a new type every time, but we |
| 3205 | // also build a canonical type. |
| 3206 | |
| 3207 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 3208 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3209 | void *insertPos = nullptr; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3210 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3211 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3212 | QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3213 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 3214 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3215 | // Look for an existing type with these properties. |
| 3216 | DependentSizedArrayType *canonTy = |
| 3217 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 3218 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3219 | // If we don't have one, build one. |
| 3220 | if (!canonTy) { |
| 3221 | canonTy = new (*this, TypeAlignment) |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3222 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3223 | QualType(), numElements, ASM, elementTypeQuals, |
| 3224 | brackets); |
| 3225 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 3226 | Types.push_back(canonTy); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3229 | // Apply qualifiers from the element type to the array. |
| 3230 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3231 | canonElementType.Quals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3232 | |
David Majnemer | 16a7470 | 2015-07-24 05:54:19 +0000 | [diff] [blame] | 3233 | // If we didn't need extra canonicalization for the element type or the size |
| 3234 | // expression, then just use that as our result. |
| 3235 | if (QualType(canonElementType.Ty, 0) == elementType && |
| 3236 | canonTy->getSizeExpr() == numElements) |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3237 | return canon; |
| 3238 | |
| 3239 | // Otherwise, we need to build a type which follows the spelling |
| 3240 | // of the element type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3241 | auto *sugaredType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3242 | = new (*this, TypeAlignment) |
| 3243 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 3244 | ASM, elementTypeQuals, brackets); |
| 3245 | Types.push_back(sugaredType); |
| 3246 | return QualType(sugaredType, 0); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3249 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3250 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3251 | unsigned elementTypeQuals) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3252 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3253 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3254 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3255 | void *insertPos = nullptr; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3256 | if (IncompleteArrayType *iat = |
| 3257 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 3258 | return QualType(iat, 0); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3259 | |
| 3260 | // 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] | 3261 | // either, so fill in the canonical type field. We also have to pull |
| 3262 | // qualifiers off the element type. |
| 3263 | QualType canon; |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3264 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3265 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 3266 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3267 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3268 | ASM, elementTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3269 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3270 | |
| 3271 | // Get the new insert position for the node we care about. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3272 | IncompleteArrayType *existing = |
| 3273 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 3274 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 843ebedd | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 3275 | } |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3276 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3277 | auto *newType = new (*this, TypeAlignment) |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3278 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3279 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3280 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 3281 | Types.push_back(newType); |
| 3282 | return QualType(newType, 0); |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 3283 | } |
| 3284 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3285 | /// getVectorType - Return the unique reference to a vector type of |
| 3286 | /// 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] | 3287 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3288 | VectorType::VectorKind VecKind) const { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3289 | assert(vecType->isBuiltinType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3290 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3291 | // Check if we've already instantiated a vector of this type. |
| 3292 | llvm::FoldingSetNodeID ID; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 3293 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 3294 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3295 | void *InsertPos = nullptr; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3296 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3297 | return QualType(VTP, 0); |
| 3298 | |
| 3299 | // If the element type isn't canonical, this won't be a canonical type either, |
| 3300 | // so fill in the canonical type field. |
| 3301 | QualType Canonical; |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 3302 | if (!vecType.isCanonical()) { |
Bob Wilson | 7795480 | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 3303 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3304 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3305 | // Get the new insert position for the node we care about. |
| 3306 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3307 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3308 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3309 | auto *New = new (*this, TypeAlignment) |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 3310 | VectorType(vecType, NumElts, Canonical, VecKind); |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3311 | VectorTypes.InsertNode(New, InsertPos); |
| 3312 | Types.push_back(New); |
| 3313 | return QualType(New, 0); |
| 3314 | } |
| 3315 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3316 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3317 | /// 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] | 3318 | QualType |
| 3319 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 3320 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3322 | // Check if we've already instantiated a vector of this type. |
| 3323 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 3324 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 3325 | VectorType::GenericVector); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3326 | void *InsertPos = nullptr; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3327 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3328 | return QualType(VTP, 0); |
| 3329 | |
| 3330 | // If the element type isn't canonical, this won't be a canonical type either, |
| 3331 | // so fill in the canonical type field. |
| 3332 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3333 | if (!vecType.isCanonical()) { |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3334 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3335 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3336 | // Get the new insert position for the node we care about. |
| 3337 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3338 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3339 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3340 | auto *New = new (*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3341 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3342 | VectorTypes.InsertNode(New, InsertPos); |
| 3343 | Types.push_back(New); |
| 3344 | return QualType(New, 0); |
| 3345 | } |
| 3346 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3347 | QualType |
| 3348 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 3349 | Expr *SizeExpr, |
| 3350 | SourceLocation AttrLoc) const { |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3351 | llvm::FoldingSetNodeID ID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3352 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3353 | SizeExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3354 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3355 | void *InsertPos = nullptr; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3356 | DependentSizedExtVectorType *Canon |
| 3357 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3358 | DependentSizedExtVectorType *New; |
| 3359 | if (Canon) { |
| 3360 | // We already have a canonical version of this array type; use it as |
| 3361 | // the canonical type for a newly-built type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3362 | New = new (*this, TypeAlignment) |
| 3363 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 3364 | SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3365 | } else { |
| 3366 | QualType CanonVecTy = getCanonicalType(vecType); |
| 3367 | if (CanonVecTy == vecType) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3368 | New = new (*this, TypeAlignment) |
| 3369 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 3370 | AttrLoc); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3371 | |
| 3372 | DependentSizedExtVectorType *CanonCheck |
| 3373 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3374 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 3375 | (void)CanonCheck; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3376 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 3377 | } else { |
| 3378 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 3379 | SourceLocation()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3380 | New = new (*this, TypeAlignment) |
| 3381 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3382 | } |
| 3383 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3384 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 3385 | Types.push_back(New); |
| 3386 | return QualType(New, 0); |
| 3387 | } |
| 3388 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3389 | QualType ASTContext::getDependentAddressSpaceType(QualType PointeeType, |
| 3390 | Expr *AddrSpaceExpr, |
| 3391 | SourceLocation AttrLoc) const { |
| 3392 | assert(AddrSpaceExpr->isInstantiationDependent()); |
| 3393 | |
| 3394 | QualType canonPointeeType = getCanonicalType(PointeeType); |
| 3395 | |
| 3396 | void *insertPos = nullptr; |
| 3397 | llvm::FoldingSetNodeID ID; |
| 3398 | DependentAddressSpaceType::Profile(ID, *this, canonPointeeType, |
| 3399 | AddrSpaceExpr); |
| 3400 | |
| 3401 | DependentAddressSpaceType *canonTy = |
| 3402 | DependentAddressSpaceTypes.FindNodeOrInsertPos(ID, insertPos); |
| 3403 | |
| 3404 | if (!canonTy) { |
| 3405 | canonTy = new (*this, TypeAlignment) |
| 3406 | DependentAddressSpaceType(*this, canonPointeeType, |
| 3407 | QualType(), AddrSpaceExpr, AttrLoc); |
| 3408 | DependentAddressSpaceTypes.InsertNode(canonTy, insertPos); |
| 3409 | Types.push_back(canonTy); |
| 3410 | } |
| 3411 | |
| 3412 | if (canonPointeeType == PointeeType && |
| 3413 | canonTy->getAddrSpaceExpr() == AddrSpaceExpr) |
| 3414 | return QualType(canonTy, 0); |
| 3415 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3416 | auto *sugaredType |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3417 | = new (*this, TypeAlignment) |
| 3418 | DependentAddressSpaceType(*this, PointeeType, QualType(canonTy, 0), |
| 3419 | AddrSpaceExpr, AttrLoc); |
| 3420 | Types.push_back(sugaredType); |
| 3421 | return QualType(sugaredType, 0); |
| 3422 | } |
| 3423 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3424 | /// Determine whether \p T is canonical as the result type of a function. |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3425 | static bool isCanonicalResultType(QualType T) { |
| 3426 | return T.isCanonical() && |
| 3427 | (T.getObjCLifetime() == Qualifiers::OCL_None || |
| 3428 | T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone); |
| 3429 | } |
| 3430 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3431 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3432 | QualType |
| 3433 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 3434 | const FunctionType::ExtInfo &Info) const { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3435 | // Unique functions, to guarantee there is only one function of a particular |
| 3436 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 3437 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 3438 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3439 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3440 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3441 | if (FunctionNoProtoType *FT = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3442 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3443 | return QualType(FT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3444 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3445 | QualType Canonical; |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3446 | if (!isCanonicalResultType(ResultTy)) { |
| 3447 | Canonical = |
| 3448 | getFunctionNoProtoType(getCanonicalFunctionResultType(ResultTy), Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 3450 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3451 | FunctionNoProtoType *NewIP = |
| 3452 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3453 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 3454 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3455 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3456 | auto *New = new (*this, TypeAlignment) |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3457 | FunctionNoProtoType(ResultTy, Canonical, Info); |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 3458 | Types.push_back(New); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3459 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3460 | return QualType(New, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
Douglas Gregor | a602a15 | 2015-10-01 20:20:47 +0000 | [diff] [blame] | 3463 | CanQualType |
| 3464 | ASTContext::getCanonicalFunctionResultType(QualType ResultType) const { |
| 3465 | CanQualType CanResultType = getCanonicalType(ResultType); |
| 3466 | |
| 3467 | // Canonical result types do not have ARC lifetime qualifiers. |
| 3468 | if (CanResultType.getQualifiers().hasObjCLifetime()) { |
| 3469 | Qualifiers Qs = CanResultType.getQualifiers(); |
| 3470 | Qs.removeObjCLifetime(); |
| 3471 | return CanQualType::CreateUnsafe( |
| 3472 | getQualifiedType(CanResultType.getUnqualifiedType(), Qs)); |
| 3473 | } |
| 3474 | |
| 3475 | return CanResultType; |
| 3476 | } |
| 3477 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3478 | static bool isCanonicalExceptionSpecification( |
| 3479 | const FunctionProtoType::ExceptionSpecInfo &ESI, bool NoexceptInType) { |
| 3480 | if (ESI.Type == EST_None) |
| 3481 | return true; |
| 3482 | if (!NoexceptInType) |
| 3483 | return false; |
| 3484 | |
| 3485 | // C++17 onwards: exception specification is part of the type, as a simple |
| 3486 | // boolean "can this function type throw". |
| 3487 | if (ESI.Type == EST_BasicNoexcept) |
| 3488 | return true; |
| 3489 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3490 | // A noexcept(expr) specification is (possibly) canonical if expr is |
| 3491 | // value-dependent. |
| 3492 | if (ESI.Type == EST_DependentNoexcept) |
| 3493 | return true; |
| 3494 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3495 | // A dynamic exception specification is canonical if it only contains pack |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3496 | // expansions (so we can't tell whether it's non-throwing) and all its |
| 3497 | // contained types are canonical. |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3498 | if (ESI.Type == EST_Dynamic) { |
Richard Smith | fda59e5 | 2016-10-26 01:05:54 +0000 | [diff] [blame] | 3499 | bool AnyPackExpansions = false; |
| 3500 | for (QualType ET : ESI.Exceptions) { |
| 3501 | if (!ET.isCanonical()) |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3502 | return false; |
Richard Smith | fda59e5 | 2016-10-26 01:05:54 +0000 | [diff] [blame] | 3503 | if (ET->getAs<PackExpansionType>()) |
| 3504 | AnyPackExpansions = true; |
| 3505 | } |
| 3506 | return AnyPackExpansions; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3509 | return false; |
| 3510 | } |
| 3511 | |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3512 | QualType ASTContext::getFunctionTypeInternal( |
| 3513 | QualType ResultTy, ArrayRef<QualType> ArgArray, |
| 3514 | const FunctionProtoType::ExtProtoInfo &EPI, bool OnlyWantCanonical) const { |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3515 | size_t NumArgs = ArgArray.size(); |
| 3516 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3517 | // Unique functions, to guarantee there is only one function of a particular |
| 3518 | // structure. |
| 3519 | llvm::FoldingSetNodeID ID; |
| 3520 | FunctionProtoType::Profile(ID, ResultTy, ArgArray.begin(), NumArgs, EPI, |
| 3521 | *this, true); |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3522 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3523 | QualType Canonical; |
| 3524 | bool Unique = false; |
| 3525 | |
| 3526 | void *InsertPos = nullptr; |
| 3527 | if (FunctionProtoType *FPT = |
| 3528 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 3529 | QualType Existing = QualType(FPT, 0); |
| 3530 | |
| 3531 | // If we find a pre-existing equivalent FunctionProtoType, we can just reuse |
| 3532 | // it so long as our exception specification doesn't contain a dependent |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3533 | // noexcept expression, or we're just looking for a canonical type. |
| 3534 | // Otherwise, we're going to need to create a type |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3535 | // sugar node to hold the concrete expression. |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3536 | if (OnlyWantCanonical || !isComputedNoexcept(EPI.ExceptionSpec.Type) || |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3537 | EPI.ExceptionSpec.NoexceptExpr == FPT->getNoexceptExpr()) |
| 3538 | return Existing; |
| 3539 | |
| 3540 | // We need a new type sugar node for this one, to hold the new noexcept |
| 3541 | // expression. We do no canonicalization here, but that's OK since we don't |
| 3542 | // expect to see the same noexcept expression much more than once. |
| 3543 | Canonical = getCanonicalType(Existing); |
| 3544 | Unique = true; |
| 3545 | } |
| 3546 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3547 | bool NoexceptInType = getLangOpts().CPlusPlus17; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3548 | bool IsCanonicalExceptionSpec = |
| 3549 | isCanonicalExceptionSpecification(EPI.ExceptionSpec, NoexceptInType); |
| 3550 | |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3551 | // Determine whether the type being created is already canonical or not. |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3552 | bool isCanonical = !Unique && IsCanonicalExceptionSpec && |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3553 | isCanonicalResultType(ResultTy) && !EPI.HasTrailingReturn; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3554 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3555 | if (!ArgArray[i].isCanonicalAsParam()) |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3556 | isCanonical = false; |
| 3557 | |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3558 | if (OnlyWantCanonical) |
| 3559 | assert(isCanonical && |
| 3560 | "given non-canonical parameters constructing canonical type"); |
| 3561 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3562 | // If this type isn't canonical, get the canonical version of it if we don't |
| 3563 | // already have it. The exception spec is only partially part of the |
| 3564 | // canonical type, and only in C++17 onwards. |
| 3565 | if (!isCanonical && Canonical.isNull()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3566 | SmallVector<QualType, 16> CanonicalArgs; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3567 | CanonicalArgs.reserve(NumArgs); |
| 3568 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3569 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3570 | |
Benjamin Kramer | 3f515cd | 2016-10-26 12:51:45 +0000 | [diff] [blame] | 3571 | llvm::SmallVector<QualType, 8> ExceptionTypeStorage; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3572 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 3573 | CanonicalEPI.HasTrailingReturn = false; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3574 | |
| 3575 | if (IsCanonicalExceptionSpec) { |
| 3576 | // Exception spec is already OK. |
| 3577 | } else if (NoexceptInType) { |
| 3578 | switch (EPI.ExceptionSpec.Type) { |
| 3579 | case EST_Unparsed: case EST_Unevaluated: case EST_Uninstantiated: |
| 3580 | // We don't know yet. It shouldn't matter what we pick here; no-one |
| 3581 | // should ever look at this. |
| 3582 | LLVM_FALLTHROUGH; |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3583 | case EST_None: case EST_MSAny: case EST_NoexceptFalse: |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3584 | CanonicalEPI.ExceptionSpec.Type = EST_None; |
| 3585 | break; |
| 3586 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3587 | // A dynamic exception specification is almost always "not noexcept", |
| 3588 | // with the exception that a pack expansion might expand to no types. |
| 3589 | case EST_Dynamic: { |
| 3590 | bool AnyPacks = false; |
| 3591 | for (QualType ET : EPI.ExceptionSpec.Exceptions) { |
| 3592 | if (ET->getAs<PackExpansionType>()) |
| 3593 | AnyPacks = true; |
| 3594 | ExceptionTypeStorage.push_back(getCanonicalType(ET)); |
| 3595 | } |
| 3596 | if (!AnyPacks) |
| 3597 | CanonicalEPI.ExceptionSpec.Type = EST_None; |
| 3598 | else { |
| 3599 | CanonicalEPI.ExceptionSpec.Type = EST_Dynamic; |
| 3600 | CanonicalEPI.ExceptionSpec.Exceptions = ExceptionTypeStorage; |
| 3601 | } |
| 3602 | break; |
| 3603 | } |
| 3604 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3605 | case EST_DynamicNone: case EST_BasicNoexcept: case EST_NoexceptTrue: |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3606 | CanonicalEPI.ExceptionSpec.Type = EST_BasicNoexcept; |
| 3607 | break; |
| 3608 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3609 | case EST_DependentNoexcept: |
| 3610 | llvm_unreachable("dependent noexcept is already canonical"); |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3611 | } |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3612 | } else { |
| 3613 | CanonicalEPI.ExceptionSpec = FunctionProtoType::ExceptionSpecInfo(); |
| 3614 | } |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3615 | |
Douglas Gregor | a602a15 | 2015-10-01 20:20:47 +0000 | [diff] [blame] | 3616 | // Adjust the canonical function result type. |
| 3617 | CanQualType CanResultTy = getCanonicalFunctionResultType(ResultTy); |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3618 | Canonical = |
| 3619 | getFunctionTypeInternal(CanResultTy, CanonicalArgs, CanonicalEPI, true); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3620 | |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 3621 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3622 | FunctionProtoType *NewIP = |
| 3623 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3624 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3625 | } |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3626 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3627 | // FunctionProtoType objects are allocated with extra bytes after |
| 3628 | // them for three variable size arrays at the end: |
| 3629 | // - parameter types |
| 3630 | // - exception types |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3631 | // - extended parameter information |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3632 | // Instead of the exception types, there could be a noexcept |
Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3633 | // expression, or information used to resolve the exception |
| 3634 | // specification. |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3635 | size_t Size = |
| 3636 | sizeof(FunctionProtoType) + NumArgs * sizeof(QualType) + |
| 3637 | FunctionProtoType::getExceptionSpecSize( |
| 3638 | EPI.ExceptionSpec.Type, EPI.ExceptionSpec.Exceptions.size()); |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3639 | |
| 3640 | // Put the ExtParameterInfos last. If all were equal, it would make |
| 3641 | // more sense to put these before the exception specification, because |
| 3642 | // it's much easier to skip past them compared to the elaborate switch |
| 3643 | // required to skip the exception specification. However, all is not |
| 3644 | // equal; ExtParameterInfos are used to model very uncommon features, |
| 3645 | // and it's better not to burden the more common paths. |
| 3646 | if (EPI.ExtParameterInfos) { |
| 3647 | Size += NumArgs * sizeof(FunctionProtoType::ExtParameterInfo); |
| 3648 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3649 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3650 | auto *FTP = (FunctionProtoType *) Allocate(Size, TypeAlignment); |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 3651 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3652 | new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3653 | Types.push_back(FTP); |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3654 | if (!Unique) |
| 3655 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3656 | return QualType(FTP, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3657 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 3658 | |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3659 | QualType ASTContext::getPipeType(QualType T, bool ReadOnly) const { |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3660 | llvm::FoldingSetNodeID ID; |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3661 | PipeType::Profile(ID, T, ReadOnly); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3662 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 3663 | void *InsertPos = nullptr; |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3664 | if (PipeType *PT = PipeTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3665 | return QualType(PT, 0); |
| 3666 | |
| 3667 | // If the pipe element type isn't canonical, this won't be a canonical type |
| 3668 | // either, so fill in the canonical type field. |
| 3669 | QualType Canonical; |
| 3670 | if (!T.isCanonical()) { |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3671 | Canonical = getPipeType(getCanonicalType(T), ReadOnly); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3672 | |
| 3673 | // Get the new insert position for the node we care about. |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3674 | PipeType *NewIP = PipeTypes.FindNodeOrInsertPos(ID, InsertPos); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3675 | assert(!NewIP && "Shouldn't be in the map!"); |
| 3676 | (void)NewIP; |
| 3677 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3678 | auto *New = new (*this, TypeAlignment) PipeType(T, Canonical, ReadOnly); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3679 | Types.push_back(New); |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3680 | PipeTypes.InsertNode(New, InsertPos); |
Joey Gouly | 5788b78 | 2016-11-18 14:10:54 +0000 | [diff] [blame] | 3681 | return QualType(New, 0); |
| 3682 | } |
| 3683 | |
Anastasia Stulova | 59055b9 | 2018-05-09 13:23:26 +0000 | [diff] [blame] | 3684 | QualType ASTContext::adjustStringLiteralBaseType(QualType Ty) const { |
| 3685 | // OpenCL v1.1 s6.5.3: a string literal is in the constant address space. |
| 3686 | return LangOpts.OpenCL ? getAddrSpaceQualType(Ty, LangAS::opencl_constant) |
| 3687 | : Ty; |
| 3688 | } |
| 3689 | |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3690 | QualType ASTContext::getReadPipeType(QualType T) const { |
| 3691 | return getPipeType(T, true); |
| 3692 | } |
| 3693 | |
Joey Gouly | 5788b78 | 2016-11-18 14:10:54 +0000 | [diff] [blame] | 3694 | QualType ASTContext::getWritePipeType(QualType T) const { |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3695 | return getPipeType(T, false); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3698 | #ifndef NDEBUG |
| 3699 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 3700 | if (!isa<CXXRecordDecl>(D)) return false; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3701 | const auto *RD = cast<CXXRecordDecl>(D); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3702 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 3703 | return true; |
| 3704 | if (RD->getDescribedClassTemplate() && |
| 3705 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 3706 | return true; |
| 3707 | return false; |
| 3708 | } |
| 3709 | #endif |
| 3710 | |
| 3711 | /// getInjectedClassNameType - Return the unique reference to the |
| 3712 | /// injected class name type for the specified templated declaration. |
| 3713 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3714 | QualType TST) const { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3715 | assert(NeedsInjectedClassNameType(Decl)); |
| 3716 | if (Decl->TypeForDecl) { |
| 3717 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3718 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3719 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 3720 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3721 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 3722 | } else { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3723 | Type *newType = |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3724 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3725 | Decl->TypeForDecl = newType; |
| 3726 | Types.push_back(newType); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3727 | } |
| 3728 | return QualType(Decl->TypeForDecl, 0); |
| 3729 | } |
| 3730 | |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3731 | /// getTypeDeclType - Return the unique reference to the type for the |
| 3732 | /// specified type declaration. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3733 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 89656d2 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 3734 | assert(Decl && "Passed null for Decl param"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3735 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3736 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3737 | if (const auto *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3738 | return getTypedefType(Typedef); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3739 | |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3740 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 3741 | "Template type parameter types are always available."); |
| 3742 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3743 | if (const auto *Record = dyn_cast<RecordDecl>(Decl)) { |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 3744 | assert(Record->isFirstDecl() && "struct/union has previous declaration"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3745 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3746 | return getRecordType(Record); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3747 | } else if (const auto *Enum = dyn_cast<EnumDecl>(Decl)) { |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 3748 | assert(Enum->isFirstDecl() && "enum has previous declaration"); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3749 | return getEnumType(Enum); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3750 | } else if (const auto *Using = dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3751 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 3752 | Decl->TypeForDecl = newType; |
| 3753 | Types.push_back(newType); |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3754 | } else |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3755 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 3756 | |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 3757 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 3760 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3761 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3762 | QualType |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3763 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 3764 | QualType Canonical) const { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3765 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3766 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3767 | if (Canonical.isNull()) |
| 3768 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3769 | auto *newType = new (*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3770 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3771 | Decl->TypeForDecl = newType; |
| 3772 | Types.push_back(newType); |
| 3773 | return QualType(newType, 0); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 3774 | } |
| 3775 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3776 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3777 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 3778 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3779 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3780 | if (PrevDecl->TypeForDecl) |
| 3781 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 3782 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3783 | auto *newType = new (*this, TypeAlignment) RecordType(Decl); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3784 | Decl->TypeForDecl = newType; |
| 3785 | Types.push_back(newType); |
| 3786 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3787 | } |
| 3788 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3789 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3790 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 3791 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3792 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3793 | if (PrevDecl->TypeForDecl) |
| 3794 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
| 3795 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3796 | auto *newType = new (*this, TypeAlignment) EnumType(Decl); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3797 | Decl->TypeForDecl = newType; |
| 3798 | Types.push_back(newType); |
| 3799 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 3802 | QualType ASTContext::getAttributedType(AttributedType::Kind attrKind, |
| 3803 | QualType modifiedType, |
| 3804 | QualType equivalentType) { |
| 3805 | llvm::FoldingSetNodeID id; |
| 3806 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 3807 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3808 | void *insertPos = nullptr; |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 3809 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 3810 | if (type) return QualType(type, 0); |
| 3811 | |
| 3812 | QualType canon = getCanonicalType(equivalentType); |
| 3813 | type = new (*this, TypeAlignment) |
| 3814 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 3815 | |
| 3816 | Types.push_back(type); |
| 3817 | AttributedTypes.InsertNode(type, insertPos); |
| 3818 | |
| 3819 | return QualType(type, 0); |
| 3820 | } |
| 3821 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3822 | /// Retrieve a substitution-result type. |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3823 | QualType |
| 3824 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3825 | QualType Replacement) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3826 | assert(Replacement.isCanonical() |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3827 | && "replacement types must always be canonical"); |
| 3828 | |
| 3829 | llvm::FoldingSetNodeID ID; |
| 3830 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3831 | void *InsertPos = nullptr; |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3832 | SubstTemplateTypeParmType *SubstParm |
| 3833 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3834 | |
| 3835 | if (!SubstParm) { |
| 3836 | SubstParm = new (*this, TypeAlignment) |
| 3837 | SubstTemplateTypeParmType(Parm, Replacement); |
| 3838 | Types.push_back(SubstParm); |
| 3839 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 3840 | } |
| 3841 | |
| 3842 | return QualType(SubstParm, 0); |
| 3843 | } |
| 3844 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3845 | /// Retrieve a |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3846 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 3847 | const TemplateTypeParmType *Parm, |
| 3848 | const TemplateArgument &ArgPack) { |
| 3849 | #ifndef NDEBUG |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 3850 | for (const auto &P : ArgPack.pack_elements()) { |
| 3851 | assert(P.getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 3852 | assert(P.getAsType().isCanonical() && "Pack contains non-canonical type"); |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3853 | } |
| 3854 | #endif |
| 3855 | |
| 3856 | llvm::FoldingSetNodeID ID; |
| 3857 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3858 | void *InsertPos = nullptr; |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3859 | if (SubstTemplateTypeParmPackType *SubstParm |
| 3860 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3861 | return QualType(SubstParm, 0); |
| 3862 | |
| 3863 | QualType Canon; |
| 3864 | if (!Parm->isCanonicalUnqualified()) { |
| 3865 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 3866 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 3867 | ArgPack); |
| 3868 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3869 | } |
| 3870 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3871 | auto *SubstParm |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3872 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 3873 | ArgPack); |
| 3874 | Types.push_back(SubstParm); |
George Burgess IV | b5fe855 | 2017-06-12 17:44:30 +0000 | [diff] [blame] | 3875 | SubstTemplateTypeParmPackTypes.InsertNode(SubstParm, InsertPos); |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3876 | return QualType(SubstParm, 0); |
| 3877 | } |
| 3878 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3879 | /// Retrieve the template type parameter type for a template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3880 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3881 | /// name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3882 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3883 | bool ParameterPack, |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3884 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3885 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3886 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3887 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3888 | TemplateTypeParmType *TypeParm |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3889 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3890 | |
| 3891 | if (TypeParm) |
| 3892 | return QualType(TypeParm, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3893 | |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3894 | if (TTPDecl) { |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3895 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3896 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3897 | |
| 3898 | TemplateTypeParmType *TypeCheck |
| 3899 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3900 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 3901 | (void)TypeCheck; |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 3902 | } else |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3903 | TypeParm = new (*this, TypeAlignment) |
| 3904 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 3905 | |
| 3906 | Types.push_back(TypeParm); |
| 3907 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 3908 | |
| 3909 | return QualType(TypeParm, 0); |
| 3910 | } |
| 3911 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3912 | TypeSourceInfo * |
| 3913 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 3914 | SourceLocation NameLoc, |
| 3915 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3916 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3917 | assert(!Name.getAsDependentTemplateName() && |
| 3918 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3919 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3920 | |
| 3921 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3922 | TemplateSpecializationTypeLoc TL = |
| 3923 | DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>(); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 3924 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3925 | TL.setTemplateNameLoc(NameLoc); |
| 3926 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 3927 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 3928 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 3929 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 3930 | return DI; |
| 3931 | } |
| 3932 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3933 | QualType |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 3934 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3935 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3936 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3937 | assert(!Template.getAsDependentTemplateName() && |
| 3938 | "No dependent template names here!"); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3939 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3940 | SmallVector<TemplateArgument, 4> ArgVec; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3941 | ArgVec.reserve(Args.size()); |
| 3942 | for (const TemplateArgumentLoc &Arg : Args.arguments()) |
| 3943 | ArgVec.push_back(Arg.getArgument()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3944 | |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3945 | return getTemplateSpecializationType(Template, ArgVec, Underlying); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3946 | } |
| 3947 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3948 | #ifndef NDEBUG |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3949 | static bool hasAnyPackExpansions(ArrayRef<TemplateArgument> Args) { |
| 3950 | for (const TemplateArgument &Arg : Args) |
| 3951 | if (Arg.isPackExpansion()) |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3952 | return true; |
| 3953 | |
| 3954 | return true; |
| 3955 | } |
| 3956 | #endif |
| 3957 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 3958 | QualType |
| 3959 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3960 | ArrayRef<TemplateArgument> Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3961 | QualType Underlying) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3962 | assert(!Template.getAsDependentTemplateName() && |
| 3963 | "No dependent template names here!"); |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 3964 | // Look through qualified template names. |
| 3965 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 3966 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3967 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3968 | bool IsTypeAlias = |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3969 | Template.getAsTemplateDecl() && |
| 3970 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3971 | QualType CanonType; |
| 3972 | if (!Underlying.isNull()) |
| 3973 | CanonType = getCanonicalType(Underlying); |
| 3974 | else { |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3975 | // We can get here with an alias template when the specialization contains |
| 3976 | // a pack expansion that does not match up with a parameter pack. |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3977 | assert((!IsTypeAlias || hasAnyPackExpansions(Args)) && |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3978 | "Caller must compute aliased type"); |
| 3979 | IsTypeAlias = false; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3980 | CanonType = getCanonicalTemplateSpecializationType(Template, Args); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3981 | } |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 3982 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 3983 | // Allocate the (non-canonical) template specialization type, but don't |
| 3984 | // try to unique it: these types typically have location information that |
| 3985 | // we don't unique and don't want to lose. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 3986 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3987 | sizeof(TemplateArgument) * Args.size() + |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3988 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3989 | TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3990 | auto *Spec |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3991 | = new (Mem) TemplateSpecializationType(Template, Args, CanonType, |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 3992 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3993 | |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3994 | Types.push_back(Spec); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3995 | return QualType(Spec, 0); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3998 | QualType ASTContext::getCanonicalTemplateSpecializationType( |
| 3999 | TemplateName Template, ArrayRef<TemplateArgument> Args) const { |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 4000 | assert(!Template.getAsDependentTemplateName() && |
| 4001 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4002 | |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 4003 | // Look through qualified template names. |
| 4004 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 4005 | Template = TemplateName(QTN->getTemplateDecl()); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 4006 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4007 | // Build the canonical template specialization type. |
| 4008 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4009 | SmallVector<TemplateArgument, 4> CanonArgs; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4010 | unsigned NumArgs = Args.size(); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4011 | CanonArgs.reserve(NumArgs); |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4012 | for (const TemplateArgument &Arg : Args) |
| 4013 | CanonArgs.push_back(getCanonicalTemplateArgument(Arg)); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4014 | |
| 4015 | // Determine whether this canonical template specialization type already |
| 4016 | // exists. |
| 4017 | llvm::FoldingSetNodeID ID; |
| 4018 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4019 | CanonArgs, *this); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4020 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4021 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4022 | TemplateSpecializationType *Spec |
| 4023 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4024 | |
| 4025 | if (!Spec) { |
| 4026 | // Allocate a new canonical template specialization type. |
| 4027 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 4028 | sizeof(TemplateArgument) * NumArgs), |
| 4029 | TypeAlignment); |
| 4030 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4031 | CanonArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4032 | QualType(), QualType()); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4033 | Types.push_back(Spec); |
| 4034 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 4035 | } |
| 4036 | |
| 4037 | assert(Spec->isDependentType() && |
| 4038 | "Non-dependent template-id type must have a canonical type"); |
| 4039 | return QualType(Spec, 0); |
| 4040 | } |
| 4041 | |
Joel E. Denny | 7509a2f | 2018-05-14 19:36:45 +0000 | [diff] [blame] | 4042 | QualType ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 4043 | NestedNameSpecifier *NNS, |
| 4044 | QualType NamedType, |
| 4045 | TagDecl *OwnedTagDecl) const { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4046 | llvm::FoldingSetNodeID ID; |
Joel E. Denny | 7509a2f | 2018-05-14 19:36:45 +0000 | [diff] [blame] | 4047 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType, OwnedTagDecl); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4048 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4049 | void *InsertPos = nullptr; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4050 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4051 | if (T) |
| 4052 | return QualType(T, 0); |
| 4053 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4054 | QualType Canon = NamedType; |
| 4055 | if (!Canon.isCanonical()) { |
| 4056 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4057 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4058 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4059 | (void)CheckT; |
| 4060 | } |
| 4061 | |
Joel E. Denny | 7509a2f | 2018-05-14 19:36:45 +0000 | [diff] [blame] | 4062 | T = new (*this, TypeAlignment) |
| 4063 | ElaboratedType(Keyword, NNS, NamedType, Canon, OwnedTagDecl); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4064 | Types.push_back(T); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4065 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4066 | return QualType(T, 0); |
| 4067 | } |
| 4068 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4069 | QualType |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4070 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4071 | llvm::FoldingSetNodeID ID; |
| 4072 | ParenType::Profile(ID, InnerType); |
| 4073 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4074 | void *InsertPos = nullptr; |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4075 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4076 | if (T) |
| 4077 | return QualType(T, 0); |
| 4078 | |
| 4079 | QualType Canon = InnerType; |
| 4080 | if (!Canon.isCanonical()) { |
| 4081 | Canon = getCanonicalType(InnerType); |
| 4082 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4083 | assert(!CheckT && "Paren canonical type broken"); |
| 4084 | (void)CheckT; |
| 4085 | } |
| 4086 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 4087 | T = new (*this, TypeAlignment) ParenType(InnerType, Canon); |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4088 | Types.push_back(T); |
| 4089 | ParenTypes.InsertNode(T, InsertPos); |
| 4090 | return QualType(T, 0); |
| 4091 | } |
| 4092 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 4093 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 4094 | NestedNameSpecifier *NNS, |
| 4095 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4096 | QualType Canon) const { |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4097 | if (Canon.isNull()) { |
| 4098 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 4099 | if (CanonNNS != NNS) |
| 4100 | Canon = getDependentNameType(Keyword, CanonNNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4101 | } |
| 4102 | |
| 4103 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 4104 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4105 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4106 | void *InsertPos = nullptr; |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4107 | DependentNameType *T |
| 4108 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4109 | if (T) |
| 4110 | return QualType(T, 0); |
| 4111 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 4112 | T = new (*this, TypeAlignment) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4113 | Types.push_back(T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4114 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4115 | return QualType(T, 0); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4116 | } |
| 4117 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4118 | QualType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4119 | ASTContext::getDependentTemplateSpecializationType( |
| 4120 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 4121 | NestedNameSpecifier *NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4122 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4123 | const TemplateArgumentListInfo &Args) const { |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4124 | // TODO: avoid this copy |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4125 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4126 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 4127 | ArgCopy.push_back(Args[I].getArgument()); |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4128 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, ArgCopy); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4129 | } |
| 4130 | |
| 4131 | QualType |
| 4132 | ASTContext::getDependentTemplateSpecializationType( |
| 4133 | ElaboratedTypeKeyword Keyword, |
| 4134 | NestedNameSpecifier *NNS, |
| 4135 | const IdentifierInfo *Name, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4136 | ArrayRef<TemplateArgument> Args) const { |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 4137 | assert((!NNS || NNS->isDependent()) && |
| 4138 | "nested-name-specifier must be dependent"); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4139 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4140 | llvm::FoldingSetNodeID ID; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4141 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4142 | Name, Args); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4143 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4144 | void *InsertPos = nullptr; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4145 | DependentTemplateSpecializationType *T |
| 4146 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4147 | if (T) |
| 4148 | return QualType(T, 0); |
| 4149 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4150 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4151 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4152 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 4153 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 4154 | |
| 4155 | bool AnyNonCanonArgs = false; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4156 | unsigned NumArgs = Args.size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4157 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4158 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4159 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 4160 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 4161 | AnyNonCanonArgs = true; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4164 | QualType Canon; |
| 4165 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 4166 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4167 | Name, |
| 4168 | CanonArgs); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4169 | |
| 4170 | // Find the insert position again. |
| 4171 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4172 | } |
| 4173 | |
| 4174 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 4175 | sizeof(TemplateArgument) * NumArgs), |
| 4176 | TypeAlignment); |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 4177 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4178 | Name, Args, Canon); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4179 | Types.push_back(T); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4180 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4181 | return QualType(T, 0); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4182 | } |
| 4183 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 4184 | TemplateArgument ASTContext::getInjectedTemplateArg(NamedDecl *Param) { |
| 4185 | TemplateArgument Arg; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4186 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 4187 | QualType ArgType = getTypeDeclType(TTP); |
| 4188 | if (TTP->isParameterPack()) |
| 4189 | ArgType = getPackExpansionType(ArgType, None); |
| 4190 | |
| 4191 | Arg = TemplateArgument(ArgType); |
| 4192 | } else if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4193 | Expr *E = new (*this) DeclRefExpr( |
| 4194 | NTTP, /*enclosing*/false, |
| 4195 | NTTP->getType().getNonLValueExprType(*this), |
| 4196 | Expr::getValueKindForType(NTTP->getType()), NTTP->getLocation()); |
| 4197 | |
| 4198 | if (NTTP->isParameterPack()) |
| 4199 | E = new (*this) PackExpansionExpr(DependentTy, E, NTTP->getLocation(), |
| 4200 | None); |
| 4201 | Arg = TemplateArgument(E); |
| 4202 | } else { |
| 4203 | auto *TTP = cast<TemplateTemplateParmDecl>(Param); |
| 4204 | if (TTP->isParameterPack()) |
| 4205 | Arg = TemplateArgument(TemplateName(TTP), Optional<unsigned>()); |
| 4206 | else |
| 4207 | Arg = TemplateArgument(TemplateName(TTP)); |
| 4208 | } |
| 4209 | |
| 4210 | if (Param->isTemplateParameterPack()) |
| 4211 | Arg = TemplateArgument::CreatePackCopy(*this, Arg); |
| 4212 | |
| 4213 | return Arg; |
| 4214 | } |
| 4215 | |
Richard Smith | 43e14d2 | 2016-12-23 02:10:11 +0000 | [diff] [blame] | 4216 | void |
| 4217 | ASTContext::getInjectedTemplateArgs(const TemplateParameterList *Params, |
| 4218 | SmallVectorImpl<TemplateArgument> &Args) { |
| 4219 | Args.reserve(Args.size() + Params->size()); |
| 4220 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 4221 | for (NamedDecl *Param : *Params) |
| 4222 | Args.push_back(getInjectedTemplateArg(Param)); |
Richard Smith | 43e14d2 | 2016-12-23 02:10:11 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 4225 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4226 | Optional<unsigned> NumExpansions) { |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4227 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 4228 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4229 | |
| 4230 | assert(Pattern->containsUnexpandedParameterPack() && |
| 4231 | "Pack expansions must expand one or more parameter packs"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4232 | void *InsertPos = nullptr; |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4233 | PackExpansionType *T |
| 4234 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4235 | if (T) |
| 4236 | return QualType(T, 0); |
| 4237 | |
| 4238 | QualType Canon; |
| 4239 | if (!Pattern.isCanonical()) { |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 4240 | Canon = getCanonicalType(Pattern); |
| 4241 | // The canonical type might not contain an unexpanded parameter pack, if it |
| 4242 | // contains an alias template specialization which ignores one of its |
| 4243 | // parameters. |
| 4244 | if (Canon->containsUnexpandedParameterPack()) { |
Richard Smith | 8b4e1e2 | 2014-07-10 01:20:17 +0000 | [diff] [blame] | 4245 | Canon = getPackExpansionType(Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4246 | |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 4247 | // Find the insert position again, in case we inserted an element into |
| 4248 | // PackExpansionTypes and invalidated our insert position. |
| 4249 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4250 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4251 | } |
| 4252 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 4253 | T = new (*this, TypeAlignment) |
| 4254 | PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4255 | Types.push_back(T); |
| 4256 | PackExpansionTypes.InsertNode(T, InsertPos); |
Richard Smith | 8b4e1e2 | 2014-07-10 01:20:17 +0000 | [diff] [blame] | 4257 | return QualType(T, 0); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4260 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 4261 | /// alphabetically. |
Benjamin Kramer | 0eb262f | 2015-03-14 13:32:49 +0000 | [diff] [blame] | 4262 | static int CmpProtocolNames(ObjCProtocolDecl *const *LHS, |
| 4263 | ObjCProtocolDecl *const *RHS) { |
| 4264 | return DeclarationName::compare((*LHS)->getDeclName(), (*RHS)->getDeclName()); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4265 | } |
| 4266 | |
Craig Topper | 1f26d5b | 2016-01-03 19:43:23 +0000 | [diff] [blame] | 4267 | static bool areSortedAndUniqued(ArrayRef<ObjCProtocolDecl *> Protocols) { |
| 4268 | if (Protocols.empty()) return true; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4269 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 4270 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 4271 | return false; |
| 4272 | |
Craig Topper | 1f26d5b | 2016-01-03 19:43:23 +0000 | [diff] [blame] | 4273 | for (unsigned i = 1; i != Protocols.size(); ++i) |
Benjamin Kramer | 0eb262f | 2015-03-14 13:32:49 +0000 | [diff] [blame] | 4274 | if (CmpProtocolNames(&Protocols[i - 1], &Protocols[i]) >= 0 || |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 4275 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4276 | return false; |
| 4277 | return true; |
| 4278 | } |
| 4279 | |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4280 | static void |
| 4281 | SortAndUniqueProtocols(SmallVectorImpl<ObjCProtocolDecl *> &Protocols) { |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4282 | // Sort protocols, keyed by name. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4283 | llvm::array_pod_sort(Protocols.begin(), Protocols.end(), CmpProtocolNames); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4284 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 4285 | // Canonicalize. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4286 | for (ObjCProtocolDecl *&P : Protocols) |
| 4287 | P = P->getCanonicalDecl(); |
| 4288 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4289 | // Remove duplicates. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4290 | auto ProtocolsEnd = std::unique(Protocols.begin(), Protocols.end()); |
| 4291 | Protocols.erase(ProtocolsEnd, Protocols.end()); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4294 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 4295 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4296 | unsigned NumProtocols) const { |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 4297 | return getObjCObjectType(BaseType, {}, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4298 | llvm::makeArrayRef(Protocols, NumProtocols), |
| 4299 | /*isKindOf=*/false); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4300 | } |
| 4301 | |
| 4302 | QualType ASTContext::getObjCObjectType( |
| 4303 | QualType baseType, |
| 4304 | ArrayRef<QualType> typeArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4305 | ArrayRef<ObjCProtocolDecl *> protocols, |
| 4306 | bool isKindOf) const { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4307 | // If the base type is an interface and there aren't any protocols or |
| 4308 | // type arguments to add, then the interface type will do just fine. |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4309 | if (typeArgs.empty() && protocols.empty() && !isKindOf && |
| 4310 | isa<ObjCInterfaceType>(baseType)) |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4311 | return baseType; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4312 | |
| 4313 | // Look in the folding set for an existing type. |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4314 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4315 | ObjCObjectTypeImpl::Profile(ID, baseType, typeArgs, protocols, isKindOf); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4316 | void *InsertPos = nullptr; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4317 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4318 | return QualType(QT, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4319 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4320 | // Determine the type arguments to be used for canonicalization, |
| 4321 | // which may be explicitly specified here or written on the base |
| 4322 | // type. |
| 4323 | ArrayRef<QualType> effectiveTypeArgs = typeArgs; |
| 4324 | if (effectiveTypeArgs.empty()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4325 | if (const auto *baseObject = baseType->getAs<ObjCObjectType>()) |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4326 | effectiveTypeArgs = baseObject->getTypeArgs(); |
| 4327 | } |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4328 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4329 | // Build the canonical type, which has the canonical base type and a |
| 4330 | // sorted-and-uniqued list of protocols and the type arguments |
| 4331 | // canonicalized. |
| 4332 | QualType canonical; |
| 4333 | bool typeArgsAreCanonical = std::all_of(effectiveTypeArgs.begin(), |
| 4334 | effectiveTypeArgs.end(), |
| 4335 | [&](QualType type) { |
| 4336 | return type.isCanonical(); |
| 4337 | }); |
Craig Topper | 1f26d5b | 2016-01-03 19:43:23 +0000 | [diff] [blame] | 4338 | bool protocolsSorted = areSortedAndUniqued(protocols); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4339 | if (!typeArgsAreCanonical || !protocolsSorted || !baseType.isCanonical()) { |
| 4340 | // Determine the canonical type arguments. |
| 4341 | ArrayRef<QualType> canonTypeArgs; |
| 4342 | SmallVector<QualType, 4> canonTypeArgsVec; |
| 4343 | if (!typeArgsAreCanonical) { |
| 4344 | canonTypeArgsVec.reserve(effectiveTypeArgs.size()); |
| 4345 | for (auto typeArg : effectiveTypeArgs) |
| 4346 | canonTypeArgsVec.push_back(getCanonicalType(typeArg)); |
| 4347 | canonTypeArgs = canonTypeArgsVec; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4348 | } else { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4349 | canonTypeArgs = effectiveTypeArgs; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4352 | ArrayRef<ObjCProtocolDecl *> canonProtocols; |
| 4353 | SmallVector<ObjCProtocolDecl*, 8> canonProtocolsVec; |
| 4354 | if (!protocolsSorted) { |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4355 | canonProtocolsVec.append(protocols.begin(), protocols.end()); |
| 4356 | SortAndUniqueProtocols(canonProtocolsVec); |
| 4357 | canonProtocols = canonProtocolsVec; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4358 | } else { |
| 4359 | canonProtocols = protocols; |
| 4360 | } |
| 4361 | |
| 4362 | canonical = getObjCObjectType(getCanonicalType(baseType), canonTypeArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4363 | canonProtocols, isKindOf); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4364 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4365 | // Regenerate InsertPos. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4366 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4367 | } |
| 4368 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4369 | unsigned size = sizeof(ObjCObjectTypeImpl); |
| 4370 | size += typeArgs.size() * sizeof(QualType); |
| 4371 | size += protocols.size() * sizeof(ObjCProtocolDecl *); |
| 4372 | void *mem = Allocate(size, TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4373 | auto *T = |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4374 | new (mem) ObjCObjectTypeImpl(canonical, baseType, typeArgs, protocols, |
| 4375 | isKindOf); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4376 | |
| 4377 | Types.push_back(T); |
| 4378 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 4379 | return QualType(T, 0); |
| 4380 | } |
| 4381 | |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4382 | /// Apply Objective-C protocol qualifiers to the given type. |
| 4383 | /// If this is for the canonical type of a type parameter, we can apply |
| 4384 | /// protocol qualifiers on the ObjCObjectPointerType. |
| 4385 | QualType |
| 4386 | ASTContext::applyObjCProtocolQualifiers(QualType type, |
| 4387 | ArrayRef<ObjCProtocolDecl *> protocols, bool &hasError, |
| 4388 | bool allowOnPointerType) const { |
| 4389 | hasError = false; |
| 4390 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4391 | if (const auto *objT = dyn_cast<ObjCTypeParamType>(type.getTypePtr())) { |
Manman Ren | c5705ba | 2016-09-13 17:41:05 +0000 | [diff] [blame] | 4392 | return getObjCTypeParamType(objT->getDecl(), protocols); |
| 4393 | } |
| 4394 | |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4395 | // Apply protocol qualifiers to ObjCObjectPointerType. |
| 4396 | if (allowOnPointerType) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4397 | if (const auto *objPtr = |
| 4398 | dyn_cast<ObjCObjectPointerType>(type.getTypePtr())) { |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4399 | const ObjCObjectType *objT = objPtr->getObjectType(); |
| 4400 | // Merge protocol lists and construct ObjCObjectType. |
| 4401 | SmallVector<ObjCProtocolDecl*, 8> protocolsVec; |
| 4402 | protocolsVec.append(objT->qual_begin(), |
| 4403 | objT->qual_end()); |
| 4404 | protocolsVec.append(protocols.begin(), protocols.end()); |
| 4405 | ArrayRef<ObjCProtocolDecl *> protocols = protocolsVec; |
| 4406 | type = getObjCObjectType( |
| 4407 | objT->getBaseType(), |
| 4408 | objT->getTypeArgsAsWritten(), |
| 4409 | protocols, |
| 4410 | objT->isKindOfTypeAsWritten()); |
| 4411 | return getObjCObjectPointerType(type); |
| 4412 | } |
| 4413 | } |
| 4414 | |
| 4415 | // Apply protocol qualifiers to ObjCObjectType. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4416 | if (const auto *objT = dyn_cast<ObjCObjectType>(type.getTypePtr())){ |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4417 | // FIXME: Check for protocols to which the class type is already |
| 4418 | // known to conform. |
| 4419 | |
| 4420 | return getObjCObjectType(objT->getBaseType(), |
| 4421 | objT->getTypeArgsAsWritten(), |
| 4422 | protocols, |
| 4423 | objT->isKindOfTypeAsWritten()); |
| 4424 | } |
| 4425 | |
| 4426 | // If the canonical type is ObjCObjectType, ... |
| 4427 | if (type->isObjCObjectType()) { |
| 4428 | // Silently overwrite any existing protocol qualifiers. |
| 4429 | // TODO: determine whether that's the right thing to do. |
| 4430 | |
| 4431 | // FIXME: Check for protocols to which the class type is already |
| 4432 | // known to conform. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 4433 | return getObjCObjectType(type, {}, protocols, false); |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
| 4436 | // id<protocol-list> |
| 4437 | if (type->isObjCIdType()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4438 | const auto *objPtr = type->castAs<ObjCObjectPointerType>(); |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 4439 | type = getObjCObjectType(ObjCBuiltinIdTy, {}, protocols, |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4440 | objPtr->isKindOfType()); |
| 4441 | return getObjCObjectPointerType(type); |
| 4442 | } |
| 4443 | |
| 4444 | // Class<protocol-list> |
| 4445 | if (type->isObjCClassType()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4446 | const auto *objPtr = type->castAs<ObjCObjectPointerType>(); |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 4447 | type = getObjCObjectType(ObjCBuiltinClassTy, {}, protocols, |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4448 | objPtr->isKindOfType()); |
| 4449 | return getObjCObjectPointerType(type); |
| 4450 | } |
| 4451 | |
| 4452 | hasError = true; |
| 4453 | return type; |
| 4454 | } |
| 4455 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 4456 | QualType |
| 4457 | ASTContext::getObjCTypeParamType(const ObjCTypeParamDecl *Decl, |
| 4458 | ArrayRef<ObjCProtocolDecl *> protocols, |
| 4459 | QualType Canonical) const { |
| 4460 | // Look in the folding set for an existing type. |
| 4461 | llvm::FoldingSetNodeID ID; |
| 4462 | ObjCTypeParamType::Profile(ID, Decl, protocols); |
| 4463 | void *InsertPos = nullptr; |
| 4464 | if (ObjCTypeParamType *TypeParam = |
| 4465 | ObjCTypeParamTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4466 | return QualType(TypeParam, 0); |
| 4467 | |
| 4468 | if (Canonical.isNull()) { |
| 4469 | // We canonicalize to the underlying type. |
| 4470 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
| 4471 | if (!protocols.empty()) { |
| 4472 | // Apply the protocol qualifers. |
| 4473 | bool hasError; |
| 4474 | Canonical = applyObjCProtocolQualifiers(Canonical, protocols, hasError, |
| 4475 | true/*allowOnPointerType*/); |
| 4476 | assert(!hasError && "Error when apply protocol qualifier to bound type"); |
| 4477 | } |
| 4478 | } |
| 4479 | |
| 4480 | unsigned size = sizeof(ObjCTypeParamType); |
| 4481 | size += protocols.size() * sizeof(ObjCProtocolDecl *); |
| 4482 | void *mem = Allocate(size, TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4483 | auto *newType = new (mem) ObjCTypeParamType(Decl, Canonical, protocols); |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 4484 | |
| 4485 | Types.push_back(newType); |
| 4486 | ObjCTypeParamTypes.InsertNode(newType, InsertPos); |
| 4487 | return QualType(newType, 0); |
| 4488 | } |
| 4489 | |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4490 | /// ObjCObjectAdoptsQTypeProtocols - Checks that protocols in IC's |
| 4491 | /// protocol list adopt all protocols in QT's qualified-id protocol |
| 4492 | /// list. |
| 4493 | bool ASTContext::ObjCObjectAdoptsQTypeProtocols(QualType QT, |
| 4494 | ObjCInterfaceDecl *IC) { |
| 4495 | if (!QT->isObjCQualifiedIdType()) |
| 4496 | return false; |
| 4497 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4498 | if (const auto *OPT = QT->getAs<ObjCObjectPointerType>()) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4499 | // If both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4500 | for (auto *Proto : OPT->quals()) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4501 | if (!IC->ClassImplementsProtocol(Proto, false)) |
| 4502 | return false; |
| 4503 | } |
| 4504 | return true; |
| 4505 | } |
| 4506 | return false; |
| 4507 | } |
| 4508 | |
| 4509 | /// QIdProtocolsAdoptObjCObjectProtocols - Checks that protocols in |
| 4510 | /// QT's qualified-id protocol list adopt all protocols in IDecl's list |
| 4511 | /// of protocols. |
| 4512 | bool ASTContext::QIdProtocolsAdoptObjCObjectProtocols(QualType QT, |
| 4513 | ObjCInterfaceDecl *IDecl) { |
| 4514 | if (!QT->isObjCQualifiedIdType()) |
| 4515 | return false; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4516 | const auto *OPT = QT->getAs<ObjCObjectPointerType>(); |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4517 | if (!OPT) |
| 4518 | return false; |
| 4519 | if (!IDecl->hasDefinition()) |
| 4520 | return false; |
Fariborz Jahanian | 91fb0be | 2013-11-20 19:01:50 +0000 | [diff] [blame] | 4521 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocols; |
| 4522 | CollectInheritedProtocols(IDecl, InheritedProtocols); |
| 4523 | if (InheritedProtocols.empty()) |
| 4524 | return false; |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4525 | // Check that if every protocol in list of id<plist> conforms to a protocol |
Fariborz Jahanian | 48a01cb | 2014-04-04 23:53:45 +0000 | [diff] [blame] | 4526 | // of IDecl's, then bridge casting is ok. |
| 4527 | bool Conforms = false; |
| 4528 | for (auto *Proto : OPT->quals()) { |
| 4529 | Conforms = false; |
| 4530 | for (auto *PI : InheritedProtocols) { |
| 4531 | if (ProtocolCompatibleWithProtocol(Proto, PI)) { |
| 4532 | Conforms = true; |
| 4533 | break; |
| 4534 | } |
| 4535 | } |
| 4536 | if (!Conforms) |
| 4537 | break; |
| 4538 | } |
| 4539 | if (Conforms) |
| 4540 | return true; |
| 4541 | |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4542 | for (auto *PI : InheritedProtocols) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4543 | // If both the right and left sides have qualifiers. |
| 4544 | bool Adopts = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4545 | for (auto *Proto : OPT->quals()) { |
Fariborz Jahanian | 48a01cb | 2014-04-04 23:53:45 +0000 | [diff] [blame] | 4546 | // return 'true' if 'PI' is in the inheritance hierarchy of Proto |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4547 | if ((Adopts = ProtocolCompatibleWithProtocol(PI, Proto))) |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4548 | break; |
| 4549 | } |
| 4550 | if (!Adopts) |
Fariborz Jahanian | 91fb0be | 2013-11-20 19:01:50 +0000 | [diff] [blame] | 4551 | return false; |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4552 | } |
| 4553 | return true; |
| 4554 | } |
| 4555 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4556 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 4557 | /// the given object type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4558 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4559 | llvm::FoldingSetNodeID ID; |
| 4560 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 4561 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4562 | void *InsertPos = nullptr; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4563 | if (ObjCObjectPointerType *QT = |
| 4564 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4565 | return QualType(QT, 0); |
| 4566 | |
| 4567 | // Find the canonical object type. |
| 4568 | QualType Canonical; |
| 4569 | if (!ObjectT.isCanonical()) { |
| 4570 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 4571 | |
| 4572 | // Regenerate InsertPos. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4573 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4574 | } |
| 4575 | |
Douglas Gregor | f85bee6 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 4576 | // No match. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4577 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4578 | auto *QType = |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4579 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4580 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4581 | Types.push_back(QType); |
| 4582 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4583 | return QualType(QType, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4584 | } |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4585 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4586 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 4587 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 4588 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 4589 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4590 | if (Decl->TypeForDecl) |
| 4591 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4592 | |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 4593 | if (PrevDecl) { |
| 4594 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 4595 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 4596 | return QualType(PrevDecl->TypeForDecl, 0); |
| 4597 | } |
| 4598 | |
Douglas Gregor | 7671e53 | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 4599 | // Prefer the definition, if there is one. |
| 4600 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 4601 | Decl = Def; |
| 4602 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4603 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4604 | auto *T = new (Mem) ObjCInterfaceType(Decl); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4605 | Decl->TypeForDecl = T; |
| 4606 | Types.push_back(T); |
| 4607 | return QualType(T, 0); |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 4608 | } |
| 4609 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4610 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 4611 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4612 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4613 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4614 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4615 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4616 | TypeOfExprType *toe; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4617 | if (tofExpr->isTypeDependent()) { |
| 4618 | llvm::FoldingSetNodeID ID; |
| 4619 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4620 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4621 | void *InsertPos = nullptr; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4622 | DependentTypeOfExprType *Canon |
| 4623 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4624 | if (Canon) { |
| 4625 | // We already have a "canonical" version of an identical, dependent |
| 4626 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4627 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4628 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 4629 | } else { |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4630 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4631 | Canon |
| 4632 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4633 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 4634 | toe = Canon; |
| 4635 | } |
| 4636 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4637 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4638 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4639 | } |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4640 | Types.push_back(toe); |
| 4641 | return QualType(toe, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4644 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4645 | /// TypeOfType nodes. The only motivation to unique these nodes would be |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4646 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4647 | /// an issue. This doesn't affect the type checker, since it operates |
| 4648 | /// on canonical types (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4649 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4650 | QualType Canonical = getCanonicalType(tofType); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4651 | auto *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4652 | Types.push_back(tot); |
| 4653 | return QualType(tot, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4654 | } |
| 4655 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4656 | /// Unlike many "get<Type>" functions, we don't unique DecltypeType |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4657 | /// nodes. This would never be helpful, since each such type has its own |
| 4658 | /// expression, and would not give a significant memory saving, since there |
| 4659 | /// is an Expr tree under each such type. |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 4660 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4661 | DecltypeType *dt; |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4662 | |
| 4663 | // C++11 [temp.type]p2: |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4664 | // If an expression e involves a template parameter, decltype(e) denotes a |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4665 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 4666 | // type only if their expressions are equivalent (14.5.6.1). |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4667 | if (e->isInstantiationDependent()) { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4668 | llvm::FoldingSetNodeID ID; |
| 4669 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4670 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4671 | void *InsertPos = nullptr; |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4672 | DependentDecltypeType *Canon |
| 4673 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4674 | if (!Canon) { |
Yaron Keren | 633e14a | 2016-11-29 10:08:20 +0000 | [diff] [blame] | 4675 | // Build a new, canonical decltype(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4676 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4677 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4678 | } |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4679 | dt = new (*this, TypeAlignment) |
| 4680 | DecltypeType(e, UnderlyingType, QualType((DecltypeType *)Canon, 0)); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4681 | } else { |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4682 | dt = new (*this, TypeAlignment) |
| 4683 | DecltypeType(e, UnderlyingType, getCanonicalType(UnderlyingType)); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4684 | } |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 4685 | Types.push_back(dt); |
| 4686 | return QualType(dt, 0); |
| 4687 | } |
| 4688 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4689 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 4690 | /// savings are minimal and these are rare. |
| 4691 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 4692 | QualType UnderlyingType, |
| 4693 | UnaryTransformType::UTTKind Kind) |
| 4694 | const { |
Vassil Vassilev | bab6f96 | 2016-03-30 22:18:29 +0000 | [diff] [blame] | 4695 | UnaryTransformType *ut = nullptr; |
| 4696 | |
| 4697 | if (BaseType->isDependentType()) { |
| 4698 | // Look in the folding set for an existing type. |
| 4699 | llvm::FoldingSetNodeID ID; |
| 4700 | DependentUnaryTransformType::Profile(ID, getCanonicalType(BaseType), Kind); |
| 4701 | |
| 4702 | void *InsertPos = nullptr; |
| 4703 | DependentUnaryTransformType *Canon |
| 4704 | = DependentUnaryTransformTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4705 | |
| 4706 | if (!Canon) { |
| 4707 | // Build a new, canonical __underlying_type(type) type. |
| 4708 | Canon = new (*this, TypeAlignment) |
| 4709 | DependentUnaryTransformType(*this, getCanonicalType(BaseType), |
| 4710 | Kind); |
| 4711 | DependentUnaryTransformTypes.InsertNode(Canon, InsertPos); |
| 4712 | } |
| 4713 | ut = new (*this, TypeAlignment) UnaryTransformType (BaseType, |
| 4714 | QualType(), Kind, |
| 4715 | QualType(Canon, 0)); |
| 4716 | } else { |
| 4717 | QualType CanonType = getCanonicalType(UnderlyingType); |
| 4718 | ut = new (*this, TypeAlignment) UnaryTransformType (BaseType, |
| 4719 | UnderlyingType, Kind, |
| 4720 | CanonType); |
| 4721 | } |
| 4722 | Types.push_back(ut); |
| 4723 | return QualType(ut, 0); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4726 | /// getAutoType - Return the uniqued reference to the 'auto' type which has been |
| 4727 | /// deduced to the given type, or to the canonical undeduced 'auto' type, or the |
| 4728 | /// canonical deduced-but-dependent 'auto' type. |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4729 | QualType ASTContext::getAutoType(QualType DeducedType, AutoTypeKeyword Keyword, |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4730 | bool IsDependent) const { |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4731 | if (DeducedType.isNull() && Keyword == AutoTypeKeyword::Auto && !IsDependent) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4732 | return getAutoDeductType(); |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4733 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4734 | // Look in the folding set for an existing type. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4735 | void *InsertPos = nullptr; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4736 | llvm::FoldingSetNodeID ID; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4737 | AutoType::Profile(ID, DeducedType, Keyword, IsDependent); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4738 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4739 | return QualType(AT, 0); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4740 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4741 | auto *AT = new (*this, TypeAlignment) |
| 4742 | AutoType(DeducedType, Keyword, IsDependent); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4743 | Types.push_back(AT); |
| 4744 | if (InsertPos) |
| 4745 | AutoTypes.InsertNode(AT, InsertPos); |
| 4746 | return QualType(AT, 0); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4747 | } |
| 4748 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 4749 | /// Return the uniqued reference to the deduced template specialization type |
| 4750 | /// which has been deduced to the given type, or to the canonical undeduced |
| 4751 | /// such type, or the canonical deduced-but-dependent such type. |
| 4752 | QualType ASTContext::getDeducedTemplateSpecializationType( |
| 4753 | TemplateName Template, QualType DeducedType, bool IsDependent) const { |
| 4754 | // Look in the folding set for an existing type. |
| 4755 | void *InsertPos = nullptr; |
| 4756 | llvm::FoldingSetNodeID ID; |
| 4757 | DeducedTemplateSpecializationType::Profile(ID, Template, DeducedType, |
| 4758 | IsDependent); |
| 4759 | if (DeducedTemplateSpecializationType *DTST = |
| 4760 | DeducedTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4761 | return QualType(DTST, 0); |
| 4762 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4763 | auto *DTST = new (*this, TypeAlignment) |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 4764 | DeducedTemplateSpecializationType(Template, DeducedType, IsDependent); |
| 4765 | Types.push_back(DTST); |
| 4766 | if (InsertPos) |
| 4767 | DeducedTemplateSpecializationTypes.InsertNode(DTST, InsertPos); |
| 4768 | return QualType(DTST, 0); |
| 4769 | } |
| 4770 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4771 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 4772 | /// the given value type. |
| 4773 | QualType ASTContext::getAtomicType(QualType T) const { |
| 4774 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 4775 | // structure. |
| 4776 | llvm::FoldingSetNodeID ID; |
| 4777 | AtomicType::Profile(ID, T); |
| 4778 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4779 | void *InsertPos = nullptr; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4780 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4781 | return QualType(AT, 0); |
| 4782 | |
| 4783 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 4784 | // either, so fill in the canonical type field. |
| 4785 | QualType Canonical; |
| 4786 | if (!T.isCanonical()) { |
| 4787 | Canonical = getAtomicType(getCanonicalType(T)); |
| 4788 | |
| 4789 | // Get the new insert position for the node we care about. |
| 4790 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4791 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4792 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4793 | auto *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4794 | Types.push_back(New); |
| 4795 | AtomicTypes.InsertNode(New, InsertPos); |
| 4796 | return QualType(New, 0); |
| 4797 | } |
| 4798 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 4799 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 4800 | QualType ASTContext::getAutoDeductType() const { |
| 4801 | if (AutoDeductTy.isNull()) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4802 | AutoDeductTy = QualType( |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4803 | new (*this, TypeAlignment) AutoType(QualType(), AutoTypeKeyword::Auto, |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4804 | /*dependent*/false), |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4805 | 0); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 4806 | return AutoDeductTy; |
| 4807 | } |
| 4808 | |
| 4809 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 4810 | QualType ASTContext::getAutoRRefDeductType() const { |
| 4811 | if (AutoRRefDeductTy.isNull()) |
| 4812 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 4813 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 4814 | return AutoRRefDeductTy; |
| 4815 | } |
| 4816 | |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 4817 | /// getTagDeclType - Return the unique reference to the type for the |
| 4818 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4819 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 4820 | assert(Decl); |
Mike Stump | b93185d | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 4821 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 4822 | // away const? mutable? |
| 4823 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 4824 | } |
| 4825 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4826 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 4827 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 4828 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4829 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4830 | return getFromTargetType(Target->getSizeType()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 4831 | } |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 4832 | |
Alexander Shaposhnikov | 1e898d9 | 2017-07-14 17:30:14 +0000 | [diff] [blame] | 4833 | /// Return the unique signed counterpart of the integer type |
| 4834 | /// corresponding to size_t. |
| 4835 | CanQualType ASTContext::getSignedSizeType() const { |
| 4836 | return getFromTargetType(Target->getSignedSizeType()); |
| 4837 | } |
| 4838 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 4839 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 4840 | CanQualType ASTContext::getIntMaxType() const { |
| 4841 | return getFromTargetType(Target->getIntMaxType()); |
| 4842 | } |
| 4843 | |
| 4844 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 4845 | CanQualType ASTContext::getUIntMaxType() const { |
| 4846 | return getFromTargetType(Target->getUIntMaxType()); |
| 4847 | } |
| 4848 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4849 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 4850 | /// Used when in C++, as a GCC extension. |
| 4851 | QualType ASTContext::getSignedWCharType() const { |
| 4852 | // FIXME: derive from "Target" ? |
| 4853 | return WCharTy; |
| 4854 | } |
| 4855 | |
| 4856 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 4857 | /// Used when in C++, as a GCC extension. |
| 4858 | QualType ASTContext::getUnsignedWCharType() const { |
| 4859 | // FIXME: derive from "Target" ? |
| 4860 | return UnsignedIntTy; |
| 4861 | } |
| 4862 | |
Enea Zaffanella | f11ceb6 | 2013-01-26 17:08:37 +0000 | [diff] [blame] | 4863 | QualType ASTContext::getIntPtrType() const { |
| 4864 | return getFromTargetType(Target->getIntPtrType()); |
| 4865 | } |
| 4866 | |
| 4867 | QualType ASTContext::getUIntPtrType() const { |
| 4868 | return getCorrespondingUnsignedType(getIntPtrType()); |
| 4869 | } |
| 4870 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 4871 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 4872 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 4873 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4874 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 4875 | } |
| 4876 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4877 | /// Return the unique unsigned counterpart of "ptrdiff_t" |
Alexander Shaposhnikov | 195b25c | 2017-09-28 23:11:31 +0000 | [diff] [blame] | 4878 | /// integer type. The standard (C11 7.21.6.1p7) refers to this type |
| 4879 | /// in the definition of %tu format specifier. |
| 4880 | QualType ASTContext::getUnsignedPointerDiffType() const { |
| 4881 | return getFromTargetType(Target->getUnsignedPtrDiffType(0)); |
| 4882 | } |
| 4883 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4884 | /// Return the unique type for "pid_t" defined in |
Eli Friedman | 4e91899e | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 4885 | /// <sys/types.h>. We need this to compute the correct type for vfork(). |
| 4886 | QualType ASTContext::getProcessIDType() const { |
| 4887 | return getFromTargetType(Target->getProcessIDType()); |
| 4888 | } |
| 4889 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 4890 | //===----------------------------------------------------------------------===// |
| 4891 | // Type Operators |
| 4892 | //===----------------------------------------------------------------------===// |
| 4893 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4894 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4895 | // Push qualifiers into arrays, and then discard any remaining |
| 4896 | // qualifiers. |
| 4897 | T = getCanonicalType(T); |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4898 | T = getVariableArrayDecayedType(T); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4899 | const Type *Ty = T.getTypePtr(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4900 | QualType Result; |
| 4901 | if (isa<ArrayType>(Ty)) { |
| 4902 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 4903 | } else if (isa<FunctionType>(Ty)) { |
| 4904 | Result = getPointerType(QualType(Ty, 0)); |
| 4905 | } else { |
| 4906 | Result = QualType(Ty, 0); |
| 4907 | } |
| 4908 | |
| 4909 | return CanQualType::CreateUnsafe(Result); |
| 4910 | } |
| 4911 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4912 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 4913 | Qualifiers &quals) { |
| 4914 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 4915 | |
| 4916 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 4917 | // the unqualified desugared type and then drops it on the floor. |
| 4918 | // We then have to strip that sugar back off with |
| 4919 | // getUnqualifiedDesugaredType(), which is silly. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4920 | const auto *AT = |
| 4921 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4922 | |
| 4923 | // 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] | 4924 | if (!AT) { |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4925 | quals = splitType.Quals; |
| 4926 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4927 | } |
| 4928 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4929 | // Otherwise, recurse on the array's element type. |
| 4930 | QualType elementType = AT->getElementType(); |
| 4931 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 4932 | |
| 4933 | // If that didn't change the element type, AT has no qualifiers, so we |
| 4934 | // can just use the results in splitType. |
| 4935 | if (elementType == unqualElementType) { |
| 4936 | assert(quals.empty()); // from the recursive call |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4937 | quals = splitType.Quals; |
| 4938 | return QualType(splitType.Ty, 0); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4939 | } |
| 4940 | |
| 4941 | // Otherwise, add in the qualifiers from the outermost type, then |
| 4942 | // build the type back up. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 4943 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4944 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4945 | if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4946 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4947 | CAT->getSizeModifier(), 0); |
| 4948 | } |
| 4949 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4950 | if (const auto *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4951 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4954 | if (const auto *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4955 | return getVariableArrayType(unqualElementType, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 4956 | VAT->getSizeExpr(), |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 4957 | VAT->getSizeModifier(), |
| 4958 | VAT->getIndexTypeCVRQualifiers(), |
| 4959 | VAT->getBracketsRange()); |
| 4960 | } |
| 4961 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4962 | const auto *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 4963 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 4964 | DSAT->getSizeModifier(), 0, |
| 4965 | SourceRange()); |
| 4966 | } |
| 4967 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 4968 | /// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types that |
| 4969 | /// may be similar (C++ 4.4), replaces T1 and T2 with the type that |
| 4970 | /// they point to and return true. If T1 and T2 aren't pointer types |
| 4971 | /// or pointer-to-member types, or if they are not similar at this |
| 4972 | /// level, returns false and leaves T1 and T2 unchanged. Top-level |
| 4973 | /// qualifiers on T1 and T2 are ignored. This function will typically |
| 4974 | /// be called in a loop that successively "unwraps" pointer and |
| 4975 | /// pointer-to-member types to compare them at each level. |
| 4976 | bool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4977 | const auto *T1PtrType = T1->getAs<PointerType>(); |
| 4978 | const auto *T2PtrType = T2->getAs<PointerType>(); |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 4979 | if (T1PtrType && T2PtrType) { |
| 4980 | T1 = T1PtrType->getPointeeType(); |
| 4981 | T2 = T2PtrType->getPointeeType(); |
| 4982 | return true; |
| 4983 | } |
| 4984 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4985 | const auto *T1MPType = T1->getAs<MemberPointerType>(); |
| 4986 | const auto *T2MPType = T2->getAs<MemberPointerType>(); |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 4987 | if (T1MPType && T2MPType && |
| 4988 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
| 4989 | QualType(T2MPType->getClass(), 0))) { |
| 4990 | T1 = T1MPType->getPointeeType(); |
| 4991 | T2 = T2MPType->getPointeeType(); |
| 4992 | return true; |
| 4993 | } |
| 4994 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4995 | if (getLangOpts().ObjC1) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4996 | const auto *T1OPType = T1->getAs<ObjCObjectPointerType>(); |
| 4997 | const auto *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 4998 | if (T1OPType && T2OPType) { |
| 4999 | T1 = T1OPType->getPointeeType(); |
| 5000 | T2 = T2OPType->getPointeeType(); |
| 5001 | return true; |
| 5002 | } |
| 5003 | } |
| 5004 | |
| 5005 | // FIXME: Block pointers, too? |
| 5006 | |
| 5007 | return false; |
| 5008 | } |
| 5009 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5010 | DeclarationNameInfo |
| 5011 | ASTContext::getNameForTemplate(TemplateName Name, |
| 5012 | SourceLocation NameLoc) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5013 | switch (Name.getKind()) { |
| 5014 | case TemplateName::QualifiedTemplate: |
| 5015 | case TemplateName::Template: |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5016 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5017 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 5018 | NameLoc); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5019 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5020 | case TemplateName::OverloadedTemplate: { |
| 5021 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 5022 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 5023 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 5024 | } |
| 5025 | |
| 5026 | case TemplateName::DependentTemplate: { |
| 5027 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5028 | DeclarationName DName; |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 5029 | if (DTN->isIdentifier()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5030 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 5031 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 5032 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5033 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 5034 | // DNInfo work in progress: FIXME: source locations? |
| 5035 | DeclarationNameLoc DNLoc; |
| 5036 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 5037 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 5038 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 5039 | } |
| 5040 | } |
| 5041 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5042 | case TemplateName::SubstTemplateTemplateParm: { |
| 5043 | SubstTemplateTemplateParmStorage *subst |
| 5044 | = Name.getAsSubstTemplateTemplateParm(); |
| 5045 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 5046 | NameLoc); |
| 5047 | } |
| 5048 | |
| 5049 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 5050 | SubstTemplateTemplateParmPackStorage *subst |
| 5051 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 5052 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 5053 | NameLoc); |
| 5054 | } |
| 5055 | } |
| 5056 | |
| 5057 | llvm_unreachable("bad template name kind!"); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 5058 | } |
| 5059 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5060 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5061 | switch (Name.getKind()) { |
| 5062 | case TemplateName::QualifiedTemplate: |
| 5063 | case TemplateName::Template: { |
| 5064 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5065 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 5066 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
| 5067 | |
| 5068 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 5069 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 5070 | } |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 5071 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5072 | case TemplateName::OverloadedTemplate: |
| 5073 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5074 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5075 | case TemplateName::DependentTemplate: { |
| 5076 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 5077 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 5078 | return DTN->CanonicalTemplateName; |
| 5079 | } |
| 5080 | |
| 5081 | case TemplateName::SubstTemplateTemplateParm: { |
| 5082 | SubstTemplateTemplateParmStorage *subst |
| 5083 | = Name.getAsSubstTemplateTemplateParm(); |
| 5084 | return getCanonicalTemplateName(subst->getReplacement()); |
| 5085 | } |
| 5086 | |
| 5087 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 5088 | SubstTemplateTemplateParmPackStorage *subst |
| 5089 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 5090 | TemplateTemplateParmDecl *canonParameter |
| 5091 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 5092 | TemplateArgument canonArgPack |
| 5093 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 5094 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 5095 | } |
| 5096 | } |
| 5097 | |
| 5098 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 5099 | } |
| 5100 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 5101 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 5102 | X = getCanonicalTemplateName(X); |
| 5103 | Y = getCanonicalTemplateName(Y); |
| 5104 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 5105 | } |
| 5106 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5107 | TemplateArgument |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5108 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5109 | switch (Arg.getKind()) { |
| 5110 | case TemplateArgument::Null: |
| 5111 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5112 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5113 | case TemplateArgument::Expression: |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5114 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5115 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5116 | case TemplateArgument::Declaration: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5117 | auto *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl()); |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 5118 | return TemplateArgument(D, Arg.getParamTypeForDecl()); |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5119 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5120 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5121 | case TemplateArgument::NullPtr: |
| 5122 | return TemplateArgument(getCanonicalType(Arg.getNullPtrType()), |
| 5123 | /*isNullPtr*/true); |
| 5124 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5125 | case TemplateArgument::Template: |
| 5126 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5127 | |
| 5128 | case TemplateArgument::TemplateExpansion: |
| 5129 | return TemplateArgument(getCanonicalTemplateName( |
| 5130 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 5131 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5132 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5133 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5134 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5135 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5136 | case TemplateArgument::Type: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5137 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5138 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5139 | case TemplateArgument::Pack: { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 5140 | if (Arg.pack_size() == 0) |
| 5141 | return Arg; |
| 5142 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5143 | auto *CanonArgs = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5144 | unsigned Idx = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5145 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5146 | AEnd = Arg.pack_end(); |
| 5147 | A != AEnd; (void)++A, ++Idx) |
| 5148 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5149 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5150 | return TemplateArgument(llvm::makeArrayRef(CanonArgs, Arg.pack_size())); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5151 | } |
| 5152 | } |
| 5153 | |
| 5154 | // Silence GCC warning |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5155 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5156 | } |
| 5157 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5158 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5159 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5160 | if (!NNS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5161 | return nullptr; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5162 | |
| 5163 | switch (NNS->getKind()) { |
| 5164 | case NestedNameSpecifier::Identifier: |
| 5165 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5166 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5167 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 5168 | NNS->getAsIdentifier()); |
| 5169 | |
| 5170 | case NestedNameSpecifier::Namespace: |
| 5171 | // A namespace is canonical; build a nested-name-specifier with |
| 5172 | // this namespace and no prefix. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5173 | return NestedNameSpecifier::Create(*this, nullptr, |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 5174 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 5175 | |
| 5176 | case NestedNameSpecifier::NamespaceAlias: |
| 5177 | // A namespace is canonical; build a nested-name-specifier with |
| 5178 | // this namespace and no prefix. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5179 | return NestedNameSpecifier::Create(*this, nullptr, |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 5180 | NNS->getAsNamespaceAlias()->getNamespace() |
| 5181 | ->getOriginalNamespace()); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5182 | |
| 5183 | case NestedNameSpecifier::TypeSpec: |
| 5184 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 5185 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 5186 | |
| 5187 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 5188 | // break it apart into its prefix and identifier, then reconsititute those |
| 5189 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 5190 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 5191 | // types, e.g., |
| 5192 | // typedef typename T::type T1; |
| 5193 | // typedef typename T1::type T2; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5194 | if (const auto *DNT = T->getAs<DependentNameType>()) |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 5195 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 5196 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 5197 | |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 5198 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 5199 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 5200 | // first place? |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5201 | return NestedNameSpecifier::Create(*this, nullptr, false, |
| 5202 | const_cast<Type *>(T.getTypePtr())); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5203 | } |
| 5204 | |
| 5205 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 5206 | case NestedNameSpecifier::Super: |
| 5207 | // The global specifier and __super specifer are canonical and unique. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5208 | return NNS; |
| 5209 | } |
| 5210 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5211 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5212 | } |
| 5213 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5214 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5215 | // Handle the non-qualified case efficiently. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5216 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5217 | // Handle the common positive case fast. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5218 | if (const auto *AT = dyn_cast<ArrayType>(T)) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5219 | return AT; |
| 5220 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5221 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5222 | // Handle the common negative case fast. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5223 | if (!isa<ArrayType>(T.getCanonicalType())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5224 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5225 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5226 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5227 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 5228 | // 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] | 5229 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5230 | // If we get here, we either have type qualifiers on the type, or we have |
| 5231 | // 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] | 5232 | // we must propagate them down into the element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5233 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5234 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5235 | Qualifiers qs = split.Quals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5236 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5237 | // If we have a simple case, just return now. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5238 | const auto *ATy = dyn_cast<ArrayType>(split.Ty); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5239 | if (!ATy || qs.empty()) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5240 | return ATy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5241 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5242 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 5243 | // qualifiers into the array element type and return a new array type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5244 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5245 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5246 | if (const auto *CAT = dyn_cast<ConstantArrayType>(ATy)) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5247 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 5248 | CAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5249 | CAT->getIndexTypeCVRQualifiers())); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5250 | if (const auto *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5251 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 5252 | IAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5253 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5254 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5255 | if (const auto *DSAT = dyn_cast<DependentSizedArrayType>(ATy)) |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5256 | return cast<ArrayType>( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5257 | getDependentSizedArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5258 | DSAT->getSizeExpr(), |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5259 | DSAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5260 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5261 | DSAT->getBracketsRange())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5262 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5263 | const auto *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5264 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5265 | VAT->getSizeExpr(), |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5266 | VAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5267 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5268 | VAT->getBracketsRange())); |
Chris Lattner | ed0d079 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 5269 | } |
| 5270 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 5271 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 5272 | if (T->isArrayType() || T->isFunctionType()) |
| 5273 | return getDecayedType(T); |
| 5274 | return T; |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 5275 | } |
| 5276 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 5277 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 5278 | T = getVariableArrayDecayedType(T); |
| 5279 | T = getAdjustedParameterType(T); |
| 5280 | return T.getUnqualifiedType(); |
| 5281 | } |
| 5282 | |
David Majnemer | d09a51c | 2015-03-03 01:50:05 +0000 | [diff] [blame] | 5283 | QualType ASTContext::getExceptionObjectType(QualType T) const { |
| 5284 | // C++ [except.throw]p3: |
| 5285 | // A throw-expression initializes a temporary object, called the exception |
| 5286 | // object, the type of which is determined by removing any top-level |
| 5287 | // cv-qualifiers from the static type of the operand of throw and adjusting |
| 5288 | // the type from "array of T" or "function returning T" to "pointer to T" |
| 5289 | // or "pointer to function returning T", [...] |
| 5290 | T = getVariableArrayDecayedType(T); |
| 5291 | if (T->isArrayType() || T->isFunctionType()) |
| 5292 | T = getDecayedType(T); |
| 5293 | return T.getUnqualifiedType(); |
| 5294 | } |
| 5295 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 5296 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 5297 | /// specified array type to a pointer. This operation is non-trivial when |
| 5298 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 5299 | /// this returns a pointer to a properly qualified element of the array. |
| 5300 | /// |
| 5301 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5302 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5303 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 5304 | // typedefs in the element type of the array. This also handles propagation |
| 5305 | // of type qualifiers from the array type into the element type if present |
| 5306 | // (C99 6.7.3p8). |
| 5307 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 5308 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5309 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5310 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 5311 | |
| 5312 | // int x[restrict 4] -> int *restrict |
Jordan Rose | 303e2f1 | 2016-11-10 23:28:17 +0000 | [diff] [blame] | 5313 | QualType Result = getQualifiedType(PtrTy, |
| 5314 | PrettyArrayType->getIndexTypeQualifiers()); |
| 5315 | |
| 5316 | // int x[_Nullable] -> int * _Nullable |
| 5317 | if (auto Nullability = Ty->getNullability(*this)) { |
| 5318 | Result = const_cast<ASTContext *>(this)->getAttributedType( |
| 5319 | AttributedType::getNullabilityAttrKind(*Nullability), Result, Result); |
| 5320 | } |
| 5321 | return Result; |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 5322 | } |
| 5323 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5324 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 5325 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 5326 | } |
| 5327 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5328 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 5329 | Qualifiers qs; |
| 5330 | while (true) { |
| 5331 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5332 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5333 | if (!array) break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5334 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5335 | type = array->getElementType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5336 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5337 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5338 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5339 | return getQualifiedType(type, qs); |
Anders Carlsson | e0808df | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 5340 | } |
| 5341 | |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 5342 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5343 | uint64_t |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 5344 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 5345 | uint64_t ElementCount = 1; |
| 5346 | do { |
| 5347 | ElementCount *= CA->getSize().getZExtValue(); |
Richard Smith | 7808c6a | 2012-12-06 03:04:50 +0000 | [diff] [blame] | 5348 | CA = dyn_cast_or_null<ConstantArrayType>( |
| 5349 | CA->getElementType()->getAsArrayTypeUnsafe()); |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 5350 | } while (CA); |
| 5351 | return ElementCount; |
| 5352 | } |
| 5353 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 5354 | /// getFloatingRank - Return a relative rank for floating point types. |
| 5355 | /// 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] | 5356 | static FloatingRank getFloatingRank(QualType T) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5357 | if (const auto *CT = T->getAs<ComplexType>()) |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 5358 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5359 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5360 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 5361 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5362 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 5363 | case BuiltinType::Float16: return Float16Rank; |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 5364 | case BuiltinType::Half: return HalfRank; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 5365 | case BuiltinType::Float: return FloatRank; |
| 5366 | case BuiltinType::Double: return DoubleRank; |
| 5367 | case BuiltinType::LongDouble: return LongDoubleRank; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5368 | case BuiltinType::Float128: return Float128Rank; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5369 | } |
| 5370 | } |
| 5371 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5372 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 5373 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | fc6ffa2 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 5374 | /// 'typeDomain' is a real floating point or complex type. |
| 5375 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 5376 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 5377 | QualType Domain) const { |
| 5378 | FloatingRank EltRank = getFloatingRank(Size); |
| 5379 | if (Domain->isComplexType()) { |
| 5380 | switch (EltRank) { |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 5381 | case Float16Rank: |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 5382 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 5383 | case FloatRank: return FloatComplexTy; |
| 5384 | case DoubleRank: return DoubleComplexTy; |
| 5385 | case LongDoubleRank: return LongDoubleComplexTy; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5386 | case Float128Rank: return Float128ComplexTy; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 5387 | } |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 5388 | } |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 5389 | |
| 5390 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 5391 | switch (EltRank) { |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 5392 | case Float16Rank: return HalfTy; |
Joey Gouly | dd7f456 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 5393 | case HalfRank: return HalfTy; |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 5394 | case FloatRank: return FloatTy; |
| 5395 | case DoubleRank: return DoubleTy; |
| 5396 | case LongDoubleRank: return LongDoubleTy; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5397 | case Float128Rank: return Float128Ty; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 5398 | } |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 5399 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5400 | } |
| 5401 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5402 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 5403 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 5404 | /// '_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] | 5405 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5406 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5407 | FloatingRank LHSR = getFloatingRank(LHS); |
| 5408 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5409 | |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5410 | if (LHSR == RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 5411 | return 0; |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5412 | if (LHSR > RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 5413 | return 1; |
| 5414 | return -1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5415 | } |
| 5416 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5417 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 5418 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 5419 | /// or if it is not canonicalized. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5420 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 5421 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 5422 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5423 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5424 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5425 | case BuiltinType::Bool: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5426 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5427 | case BuiltinType::Char_S: |
| 5428 | case BuiltinType::Char_U: |
| 5429 | case BuiltinType::SChar: |
| 5430 | case BuiltinType::UChar: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5431 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5432 | case BuiltinType::Short: |
| 5433 | case BuiltinType::UShort: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5434 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5435 | case BuiltinType::Int: |
| 5436 | case BuiltinType::UInt: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5437 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5438 | case BuiltinType::Long: |
| 5439 | case BuiltinType::ULong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5440 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5441 | case BuiltinType::LongLong: |
| 5442 | case BuiltinType::ULongLong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5443 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 5444 | case BuiltinType::Int128: |
| 5445 | case BuiltinType::UInt128: |
| 5446 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5447 | } |
| 5448 | } |
| 5449 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5450 | /// Whether this is a promotable bitfield reference according |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5451 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 5452 | /// |
| 5453 | /// \returns the type this bit-field will promote to, or NULL if no |
| 5454 | /// promotion occurs. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5455 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | e05d3cb | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 5456 | if (E->isTypeDependent() || E->isValueDependent()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5457 | return {}; |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 5458 | |
| 5459 | // FIXME: We should not do this unless E->refersToBitField() is true. This |
| 5460 | // matters in C where getSourceBitField() will find bit-fields for various |
| 5461 | // cases where the source expression is not a bit-field designator. |
| 5462 | |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5463 | FieldDecl *Field = E->getSourceBitField(); // FIXME: conditional bit-fields? |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5464 | if (!Field) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5465 | return {}; |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5466 | |
| 5467 | QualType FT = Field->getType(); |
| 5468 | |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5469 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5470 | uint64_t IntSize = getTypeSize(IntTy); |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 5471 | // C++ [conv.prom]p5: |
| 5472 | // A prvalue for an integral bit-field can be converted to a prvalue of type |
| 5473 | // int if int can represent all the values of the bit-field; otherwise, it |
| 5474 | // can be converted to unsigned int if unsigned int can represent all the |
| 5475 | // values of the bit-field. If the bit-field is larger yet, no integral |
| 5476 | // promotion applies to it. |
| 5477 | // C11 6.3.1.1/2: |
| 5478 | // [For a bit-field of type _Bool, int, signed int, or unsigned int:] |
| 5479 | // If an int can represent all values of the original type (as restricted by |
| 5480 | // the width, for a bit-field), the value is converted to an int; otherwise, |
| 5481 | // it is converted to an unsigned int. |
| 5482 | // |
| 5483 | // FIXME: C does not permit promotion of a 'long : 3' bitfield to int. |
| 5484 | // We perform that promotion here to match GCC and C++. |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5485 | if (BitWidth < IntSize) |
| 5486 | return IntTy; |
| 5487 | |
| 5488 | if (BitWidth == IntSize) |
| 5489 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 5490 | |
| 5491 | // Types bigger than int are not subject to promotions, and therefore act |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 5492 | // like the base type. GCC has some weird bugs in this area that we |
| 5493 | // deliberately do not follow (GCC follows a pre-standard resolution to |
| 5494 | // C's DR315 which treats bit-width as being part of the type, and this leaks |
| 5495 | // into their semantics in some cases). |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5496 | return {}; |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5497 | } |
| 5498 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5499 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 5500 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 5501 | /// integer type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5502 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5503 | assert(!Promotable.isNull()); |
| 5504 | assert(Promotable->isPromotableIntegerType()); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5505 | if (const auto *ET = Promotable->getAs<EnumType>()) |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5506 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 5507 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5508 | if (const auto *BT = Promotable->getAs<BuiltinType>()) { |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 5509 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 5510 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 5511 | // types that can represent all the values of its underlying type: |
| 5512 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 5513 | // unsigned long long int [...] |
| 5514 | // FIXME: Is there some better way to compute this? |
| 5515 | if (BT->getKind() == BuiltinType::WChar_S || |
| 5516 | BT->getKind() == BuiltinType::WChar_U || |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 5517 | BT->getKind() == BuiltinType::Char8 || |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 5518 | BT->getKind() == BuiltinType::Char16 || |
| 5519 | BT->getKind() == BuiltinType::Char32) { |
| 5520 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 5521 | uint64_t FromSize = getTypeSize(BT); |
| 5522 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 5523 | LongLongTy, UnsignedLongLongTy }; |
| 5524 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 5525 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 5526 | if (FromSize < ToSize || |
| 5527 | (FromSize == ToSize && |
| 5528 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 5529 | return PromoteTypes[Idx]; |
| 5530 | } |
| 5531 | llvm_unreachable("char type should fit into long long"); |
| 5532 | } |
| 5533 | } |
| 5534 | |
| 5535 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5536 | if (Promotable->isSignedIntegerType()) |
| 5537 | return IntTy; |
Eli Friedman | 6745c3b | 2012-11-15 01:21:59 +0000 | [diff] [blame] | 5538 | uint64_t PromotableSize = getIntWidth(Promotable); |
| 5539 | uint64_t IntSize = getIntWidth(IntTy); |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5540 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 5541 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 5542 | } |
| 5543 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5544 | /// Recurses in pointer/array types until it finds an objc retainable |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 5545 | /// type and returns its ownership. |
| 5546 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 5547 | while (!T.isNull()) { |
| 5548 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 5549 | return T.getObjCLifetime(); |
| 5550 | if (T->isArrayType()) |
| 5551 | T = getBaseElementType(T); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5552 | else if (const auto *PT = T->getAs<PointerType>()) |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 5553 | T = PT->getPointeeType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5554 | else if (const auto *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 8e25253 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 5555 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 5556 | else |
| 5557 | break; |
| 5558 | } |
| 5559 | |
| 5560 | return Qualifiers::OCL_None; |
| 5561 | } |
| 5562 | |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5563 | static const Type *getIntegerTypeForEnum(const EnumType *ET) { |
| 5564 | // Incomplete enum types are not treated as integer types. |
| 5565 | // FIXME: In C++, enum types are never integer types. |
| 5566 | if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped()) |
| 5567 | return ET->getDecl()->getIntegerType().getTypePtr(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5568 | return nullptr; |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5569 | } |
| 5570 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5571 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5572 | /// 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] | 5573 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5574 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5575 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 5576 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5577 | |
| 5578 | // Unwrap enums to their underlying type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5579 | if (const auto *ET = dyn_cast<EnumType>(LHSC)) |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5580 | LHSC = getIntegerTypeForEnum(ET); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5581 | if (const auto *ET = dyn_cast<EnumType>(RHSC)) |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5582 | RHSC = getIntegerTypeForEnum(ET); |
| 5583 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5584 | if (LHSC == RHSC) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5585 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5586 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 5587 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5588 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5589 | unsigned LHSRank = getIntegerRank(LHSC); |
| 5590 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5591 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5592 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 5593 | if (LHSRank == RHSRank) return 0; |
| 5594 | return LHSRank > RHSRank ? 1 : -1; |
| 5595 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5596 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5597 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 5598 | if (LHSUnsigned) { |
| 5599 | // If the unsigned [LHS] type is larger, return it. |
| 5600 | if (LHSRank >= RHSRank) |
| 5601 | return 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5602 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5603 | // If the signed type can represent all values of the unsigned type, it |
| 5604 | // 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] | 5605 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5606 | return -1; |
| 5607 | } |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5608 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5609 | // If the unsigned [RHS] type is larger, return it. |
| 5610 | if (RHSRank >= LHSRank) |
| 5611 | return -1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5612 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5613 | // If the signed type can represent all values of the unsigned type, it |
| 5614 | // 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] | 5615 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5616 | return 1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5617 | } |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5618 | |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5619 | TypedefDecl *ASTContext::getCFConstantStringDecl() const { |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5620 | if (!CFConstantStringTypeDecl) { |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5621 | assert(!CFConstantStringTagDecl && |
| 5622 | "tag and typedef should be initialized together"); |
| 5623 | CFConstantStringTagDecl = buildImplicitRecord("__NSConstantString_tag"); |
| 5624 | CFConstantStringTagDecl->startDefinition(); |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 5625 | |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 5626 | QualType FieldTypes[4]; |
Ben Langmuir | 4791a80 | 2016-02-25 16:36:26 +0000 | [diff] [blame] | 5627 | const char *FieldNames[4]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5628 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5629 | // const int *isa; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5630 | FieldTypes[0] = getPointerType(IntTy.withConst()); |
Ben Langmuir | 4791a80 | 2016-02-25 16:36:26 +0000 | [diff] [blame] | 5631 | FieldNames[0] = "isa"; |
Anders Carlsson | 9c1011c | 2007-11-19 00:25:30 +0000 | [diff] [blame] | 5632 | // int flags; |
| 5633 | FieldTypes[1] = IntTy; |
Ben Langmuir | 4791a80 | 2016-02-25 16:36:26 +0000 | [diff] [blame] | 5634 | FieldNames[1] = "flags"; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5635 | // const char *str; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5636 | FieldTypes[2] = getPointerType(CharTy.withConst()); |
Ben Langmuir | 4791a80 | 2016-02-25 16:36:26 +0000 | [diff] [blame] | 5637 | FieldNames[2] = "str"; |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5638 | // long length; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5639 | FieldTypes[3] = LongTy; |
Ben Langmuir | 4791a80 | 2016-02-25 16:36:26 +0000 | [diff] [blame] | 5640 | FieldNames[3] = "length"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5641 | |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5642 | // Create fields |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5643 | for (unsigned i = 0; i < 4; ++i) { |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5644 | FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTagDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5645 | SourceLocation(), |
Ben Langmuir | 4791a80 | 2016-02-25 16:36:26 +0000 | [diff] [blame] | 5646 | SourceLocation(), |
| 5647 | &Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5648 | FieldTypes[i], /*TInfo=*/nullptr, |
| 5649 | /*BitWidth=*/nullptr, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 5650 | /*Mutable=*/false, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5651 | ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 5652 | Field->setAccess(AS_public); |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5653 | CFConstantStringTagDecl->addDecl(Field); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5654 | } |
| 5655 | |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5656 | CFConstantStringTagDecl->completeDefinition(); |
| 5657 | // This type is designed to be compatible with NSConstantString, but cannot |
| 5658 | // use the same name, since NSConstantString is an interface. |
| 5659 | auto tagType = getTagDeclType(CFConstantStringTagDecl); |
| 5660 | CFConstantStringTypeDecl = |
| 5661 | buildImplicitTypedef(tagType, "__NSConstantString"); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5662 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5663 | |
Quentin Colombet | 043406b | 2016-02-03 22:41:00 +0000 | [diff] [blame] | 5664 | return CFConstantStringTypeDecl; |
| 5665 | } |
| 5666 | |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5667 | RecordDecl *ASTContext::getCFConstantStringTagDecl() const { |
| 5668 | if (!CFConstantStringTagDecl) |
| 5669 | getCFConstantStringDecl(); // Build the tag and the typedef. |
| 5670 | return CFConstantStringTagDecl; |
| 5671 | } |
| 5672 | |
Quentin Colombet | 043406b | 2016-02-03 22:41:00 +0000 | [diff] [blame] | 5673 | // getCFConstantStringType - Return the type used for constant CFStrings. |
| 5674 | QualType ASTContext::getCFConstantStringType() const { |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5675 | return getTypedefType(getCFConstantStringDecl()); |
Gabor Greif | 412af03 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 5676 | } |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 5677 | |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 5678 | QualType ASTContext::getObjCSuperType() const { |
| 5679 | if (ObjCSuperType.isNull()) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5680 | RecordDecl *ObjCSuperTypeDecl = buildImplicitRecord("objc_super"); |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 5681 | TUDecl->addDecl(ObjCSuperTypeDecl); |
| 5682 | ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); |
| 5683 | } |
| 5684 | return ObjCSuperType; |
| 5685 | } |
| 5686 | |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 5687 | void ASTContext::setCFConstantStringType(QualType T) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5688 | const auto *TD = T->getAs<TypedefType>(); |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5689 | assert(TD && "Invalid CFConstantStringType"); |
| 5690 | CFConstantStringTypeDecl = cast<TypedefDecl>(TD->getDecl()); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5691 | const auto *TagType = |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5692 | CFConstantStringTypeDecl->getUnderlyingType()->getAs<RecordType>(); |
| 5693 | assert(TagType && "Invalid CFConstantStringType"); |
| 5694 | CFConstantStringTagDecl = TagType->getDecl(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 5695 | } |
| 5696 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5697 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5698 | if (BlockDescriptorType) |
| 5699 | return getTagDeclType(BlockDescriptorType); |
| 5700 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5701 | RecordDecl *RD; |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5702 | // FIXME: Needs the FlagAppleBlock bit. |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5703 | RD = buildImplicitRecord("__block_descriptor"); |
| 5704 | RD->startDefinition(); |
| 5705 | |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5706 | QualType FieldTypes[] = { |
| 5707 | UnsignedLongTy, |
| 5708 | UnsignedLongTy, |
| 5709 | }; |
| 5710 | |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 5711 | static const char *const FieldNames[] = { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5712 | "reserved", |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5713 | "Size" |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5714 | }; |
| 5715 | |
| 5716 | for (size_t i = 0; i < 2; ++i) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5717 | FieldDecl *Field = FieldDecl::Create( |
| 5718 | *this, RD, SourceLocation(), SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5719 | &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
| 5720 | /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 5721 | Field->setAccess(AS_public); |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5722 | RD->addDecl(Field); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5723 | } |
| 5724 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5725 | RD->completeDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5726 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5727 | BlockDescriptorType = RD; |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5728 | |
| 5729 | return getTagDeclType(BlockDescriptorType); |
| 5730 | } |
| 5731 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5732 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5733 | if (BlockDescriptorExtendedType) |
| 5734 | return getTagDeclType(BlockDescriptorExtendedType); |
| 5735 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5736 | RecordDecl *RD; |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5737 | // FIXME: Needs the FlagAppleBlock bit. |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5738 | RD = buildImplicitRecord("__block_descriptor_withcopydispose"); |
| 5739 | RD->startDefinition(); |
| 5740 | |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5741 | QualType FieldTypes[] = { |
| 5742 | UnsignedLongTy, |
| 5743 | UnsignedLongTy, |
| 5744 | getPointerType(VoidPtrTy), |
| 5745 | getPointerType(VoidPtrTy) |
| 5746 | }; |
| 5747 | |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 5748 | static const char *const FieldNames[] = { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5749 | "reserved", |
| 5750 | "Size", |
| 5751 | "CopyFuncPtr", |
| 5752 | "DestroyFuncPtr" |
| 5753 | }; |
| 5754 | |
| 5755 | for (size_t i = 0; i < 4; ++i) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5756 | FieldDecl *Field = FieldDecl::Create( |
| 5757 | *this, RD, SourceLocation(), SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5758 | &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
| 5759 | /*BitWidth=*/nullptr, |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5760 | /*Mutable=*/false, ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 5761 | Field->setAccess(AS_public); |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5762 | RD->addDecl(Field); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5765 | RD->completeDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5766 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5767 | BlockDescriptorExtendedType = RD; |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5768 | return getTagDeclType(BlockDescriptorExtendedType); |
| 5769 | } |
| 5770 | |
Sven van Haastregt | 3bb7eaf | 2017-12-06 10:11:28 +0000 | [diff] [blame] | 5771 | TargetInfo::OpenCLTypeKind ASTContext::getOpenCLTypeKind(const Type *T) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5772 | const auto *BT = dyn_cast<BuiltinType>(T); |
Sven van Haastregt | 3bb7eaf | 2017-12-06 10:11:28 +0000 | [diff] [blame] | 5773 | |
| 5774 | if (!BT) { |
| 5775 | if (isa<PipeType>(T)) |
| 5776 | return TargetInfo::OCLTK_Pipe; |
| 5777 | |
| 5778 | return TargetInfo::OCLTK_Default; |
| 5779 | } |
| 5780 | |
| 5781 | switch (BT->getKind()) { |
| 5782 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 5783 | case BuiltinType::Id: \ |
| 5784 | return TargetInfo::OCLTK_Image; |
| 5785 | #include "clang/Basic/OpenCLImageTypes.def" |
| 5786 | |
| 5787 | case BuiltinType::OCLClkEvent: |
| 5788 | return TargetInfo::OCLTK_ClkEvent; |
| 5789 | |
| 5790 | case BuiltinType::OCLEvent: |
| 5791 | return TargetInfo::OCLTK_Event; |
| 5792 | |
| 5793 | case BuiltinType::OCLQueue: |
| 5794 | return TargetInfo::OCLTK_Queue; |
| 5795 | |
| 5796 | case BuiltinType::OCLReserveID: |
| 5797 | return TargetInfo::OCLTK_ReserveID; |
| 5798 | |
| 5799 | case BuiltinType::OCLSampler: |
| 5800 | return TargetInfo::OCLTK_Sampler; |
| 5801 | |
| 5802 | default: |
| 5803 | return TargetInfo::OCLTK_Default; |
| 5804 | } |
| 5805 | } |
| 5806 | |
| 5807 | LangAS ASTContext::getOpenCLTypeAddrSpace(const Type *T) const { |
| 5808 | return Target->getOpenCLTypeAddrSpace(getOpenCLTypeKind(T)); |
| 5809 | } |
| 5810 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 5811 | /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty" |
| 5812 | /// requires copy/dispose. Note that this must match the logic |
| 5813 | /// in buildByrefHelpers. |
| 5814 | bool ASTContext::BlockRequiresCopying(QualType Ty, |
| 5815 | const VarDecl *D) { |
| 5816 | if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) { |
| 5817 | const Expr *copyExpr = getBlockVarCopyInits(D); |
| 5818 | if (!copyExpr && record->hasTrivialDestructor()) return false; |
| 5819 | |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 5820 | return true; |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 5821 | } |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 5822 | |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 5823 | // The block needs copy/destroy helpers if Ty is non-trivial to destructively |
| 5824 | // move or destroy. |
| 5825 | if (Ty.isNonTrivialToPrimitiveDestructiveMove() || Ty.isDestructedType()) |
| 5826 | return true; |
| 5827 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 5828 | if (!Ty->isObjCRetainableType()) return false; |
| 5829 | |
| 5830 | Qualifiers qs = Ty.getQualifiers(); |
| 5831 | |
| 5832 | // If we have lifetime, that dominates. |
| 5833 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 5834 | switch (lifetime) { |
| 5835 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
| 5836 | |
| 5837 | // These are just bits as far as the runtime is concerned. |
| 5838 | case Qualifiers::OCL_ExplicitNone: |
| 5839 | case Qualifiers::OCL_Autoreleasing: |
| 5840 | return false; |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 5841 | |
| 5842 | // These cases should have been taken care of when checking the type's |
| 5843 | // non-triviality. |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 5844 | case Qualifiers::OCL_Weak: |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 5845 | case Qualifiers::OCL_Strong: |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 5846 | llvm_unreachable("impossible"); |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 5847 | } |
| 5848 | llvm_unreachable("fell out of lifetime switch!"); |
| 5849 | } |
| 5850 | return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) || |
| 5851 | Ty->isObjCObjectPointerType()); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 5852 | } |
| 5853 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 5854 | bool ASTContext::getByrefLifetime(QualType Ty, |
| 5855 | Qualifiers::ObjCLifetime &LifeTime, |
| 5856 | bool &HasByrefExtendedLayout) const { |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 5857 | if (!getLangOpts().ObjC1 || |
| 5858 | getLangOpts().getGC() != LangOptions::NonGC) |
| 5859 | return false; |
| 5860 | |
| 5861 | HasByrefExtendedLayout = false; |
Fariborz Jahanian | f762b72 | 2012-12-11 19:58:01 +0000 | [diff] [blame] | 5862 | if (Ty->isRecordType()) { |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 5863 | HasByrefExtendedLayout = true; |
| 5864 | LifeTime = Qualifiers::OCL_None; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 5865 | } else if ((LifeTime = Ty.getObjCLifetime())) { |
| 5866 | // Honor the ARC qualifiers. |
| 5867 | } else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) { |
| 5868 | // The MRR rule. |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 5869 | LifeTime = Qualifiers::OCL_ExplicitNone; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 5870 | } else { |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 5871 | LifeTime = Qualifiers::OCL_None; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 5872 | } |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 5873 | return true; |
| 5874 | } |
| 5875 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 5876 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 5877 | if (!ObjCInstanceTypeDecl) |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 5878 | ObjCInstanceTypeDecl = |
| 5879 | buildImplicitTypedef(getObjCIdType(), "instancetype"); |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 5880 | return ObjCInstanceTypeDecl; |
| 5881 | } |
| 5882 | |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 5883 | // This returns true if a type has been typedefed to BOOL: |
| 5884 | // typedef <type> BOOL; |
Chris Lattner | e021899 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 5885 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5886 | if (const auto *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 9b1f279 | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 5887 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 5888 | return II->isStr("BOOL"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5889 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 5890 | return false; |
| 5891 | } |
| 5892 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 5893 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5894 | /// purpose. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5895 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5896 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 5897 | return CharUnits::Zero(); |
| 5898 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5899 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5900 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5901 | // 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] | 5902 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5903 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5904 | // Treat arrays as pointers, since that's how they're passed in. |
| 5905 | else if (type->isArrayType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5906 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5907 | return sz; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5908 | } |
| 5909 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 5910 | bool ASTContext::isMSStaticDataMemberInlineDefinition(const VarDecl *VD) const { |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 5911 | return getTargetInfo().getCXXABI().isMicrosoft() && |
| 5912 | VD->isStaticDataMember() && |
Hans Wennborg | b18da9b | 2018-02-20 12:43:02 +0000 | [diff] [blame] | 5913 | VD->getType()->isIntegralOrEnumerationType() && |
David Majnemer | fac5243 | 2015-10-19 23:22:49 +0000 | [diff] [blame] | 5914 | !VD->getFirstDecl()->isOutOfLine() && VD->getFirstDecl()->hasInit(); |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 5915 | } |
| 5916 | |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 5917 | ASTContext::InlineVariableDefinitionKind |
| 5918 | ASTContext::getInlineVariableDefinitionKind(const VarDecl *VD) const { |
| 5919 | if (!VD->isInline()) |
| 5920 | return InlineVariableDefinitionKind::None; |
| 5921 | |
| 5922 | // In almost all cases, it's a weak definition. |
| 5923 | auto *First = VD->getFirstDecl(); |
Richard Smith | 8910fe6 | 2017-10-23 03:58:34 +0000 | [diff] [blame] | 5924 | if (First->isInlineSpecified() || !First->isStaticDataMember()) |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 5925 | return InlineVariableDefinitionKind::Weak; |
| 5926 | |
| 5927 | // If there's a file-context declaration in this translation unit, it's a |
| 5928 | // non-discardable definition. |
| 5929 | for (auto *D : VD->redecls()) |
Richard Smith | 8910fe6 | 2017-10-23 03:58:34 +0000 | [diff] [blame] | 5930 | if (D->getLexicalDeclContext()->isFileContext() && |
| 5931 | !D->isInlineSpecified() && (D->isConstexpr() || First->isConstexpr())) |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 5932 | return InlineVariableDefinitionKind::Strong; |
| 5933 | |
| 5934 | // If we've not seen one yet, we don't know. |
| 5935 | return InlineVariableDefinitionKind::WeakUnknown; |
| 5936 | } |
| 5937 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5938 | static std::string charUnitsToString(const CharUnits &CU) { |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5939 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 5940 | } |
| 5941 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 5942 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5943 | /// declaration. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 5944 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 5945 | std::string S; |
| 5946 | |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5947 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 5948 | QualType BlockTy = |
| 5949 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 5950 | // Encode result type. |
Fariborz Jahanian | 0e3043b | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 5951 | if (getLangOpts().EncodeExtendedBlockSig) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5952 | getObjCEncodingForMethodParameter( |
| 5953 | Decl::OBJC_TQ_None, BlockTy->getAs<FunctionType>()->getReturnType(), S, |
| 5954 | true /*Extended*/); |
Fariborz Jahanian | 64223e6 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 5955 | else |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5956 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getReturnType(), S); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5957 | // Compute size of all parameters. |
| 5958 | // Start with computing size of a pointer in number of bytes. |
| 5959 | // FIXME: There might(should) be a better way of doing this computation! |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5960 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 5961 | CharUnits ParmOffset = PtrSize; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 5962 | for (auto PI : Decl->parameters()) { |
Aaron Ballman | b2b8b1d | 2014-03-07 16:09:59 +0000 | [diff] [blame] | 5963 | QualType PType = PI->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5964 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | d487941 | 2012-06-30 00:48:59 +0000 | [diff] [blame] | 5965 | if (sz.isZero()) |
| 5966 | continue; |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 5967 | assert(sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5968 | ParmOffset += sz; |
| 5969 | } |
| 5970 | // Size of the argument frame |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5971 | S += charUnitsToString(ParmOffset); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5972 | // Block pointer and offset. |
| 5973 | S += "@?0"; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5974 | |
| 5975 | // Argument types. |
| 5976 | ParmOffset = PtrSize; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 5977 | for (auto PVDecl : Decl->parameters()) { |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5978 | QualType PType = PVDecl->getOriginalType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5979 | if (const auto *AT = |
| 5980 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5981 | // Use array's original type only if it has known number of |
| 5982 | // elements. |
| 5983 | if (!isa<ConstantArrayType>(AT)) |
| 5984 | PType = PVDecl->getType(); |
| 5985 | } else if (PType->isFunctionType()) |
| 5986 | PType = PVDecl->getType(); |
Fariborz Jahanian | 0e3043b | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 5987 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 64223e6 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 5988 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType, |
| 5989 | S, true /*Extended*/); |
| 5990 | else |
| 5991 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 5992 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 5993 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5994 | } |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 5995 | |
| 5996 | return S; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 5997 | } |
| 5998 | |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 5999 | std::string |
| 6000 | ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl) const { |
| 6001 | std::string S; |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6002 | // Encode result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6003 | getObjCEncodingForType(Decl->getReturnType(), S); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6004 | CharUnits ParmOffset; |
| 6005 | // Compute size of all parameters. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 6006 | for (auto PI : Decl->parameters()) { |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 6007 | QualType PType = PI->getType(); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6008 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6009 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 6010 | continue; |
| 6011 | |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6012 | assert(sz.isPositive() && |
| 6013 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6014 | ParmOffset += sz; |
| 6015 | } |
| 6016 | S += charUnitsToString(ParmOffset); |
| 6017 | ParmOffset = CharUnits::Zero(); |
| 6018 | |
| 6019 | // Argument types. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 6020 | for (auto PVDecl : Decl->parameters()) { |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6021 | QualType PType = PVDecl->getOriginalType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6022 | if (const auto *AT = |
| 6023 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6024 | // Use array's original type only if it has known number of |
| 6025 | // elements. |
| 6026 | if (!isa<ConstantArrayType>(AT)) |
| 6027 | PType = PVDecl->getType(); |
| 6028 | } else if (PType->isFunctionType()) |
| 6029 | PType = PVDecl->getType(); |
| 6030 | getObjCEncodingForType(PType, S); |
| 6031 | S += charUnitsToString(ParmOffset); |
| 6032 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 6033 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6034 | |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6035 | return S; |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6036 | } |
| 6037 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6038 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
| 6039 | /// method parameter or return type. If Extended, include class names and |
| 6040 | /// block object types. |
| 6041 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 6042 | QualType T, std::string& S, |
| 6043 | bool Extended) const { |
| 6044 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 6045 | getObjCEncodingForTypeQualifier(QT, S); |
| 6046 | // Encode parameter type. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6047 | getObjCEncodingForTypeImpl(T, S, true, true, nullptr, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6048 | true /*OutermostType*/, |
| 6049 | false /*EncodingProperty*/, |
| 6050 | false /*StructField*/, |
| 6051 | Extended /*EncodeBlockParameters*/, |
| 6052 | Extended /*EncodeClassNames*/); |
| 6053 | } |
| 6054 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6055 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6056 | /// declaration. |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6057 | std::string ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
| 6058 | bool Extended) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6059 | // FIXME: This is not very efficient. |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6060 | // Encode return type. |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6061 | std::string S; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6062 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 6063 | Decl->getReturnType(), S, Extended); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6064 | // Compute size of all parameters. |
| 6065 | // Start with computing size of a pointer in number of bytes. |
| 6066 | // FIXME: There might(should) be a better way of doing this computation! |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6067 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6068 | // The first two arguments (self and _cmd) are pointers; account for |
| 6069 | // their size. |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6070 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 6071 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 6072 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 6073 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 6074 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6075 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 6076 | continue; |
| 6077 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 6078 | assert(sz.isPositive() && |
| 6079 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6080 | ParmOffset += sz; |
| 6081 | } |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6082 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6083 | S += "@0:"; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6084 | S += charUnitsToString(PtrSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6085 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6086 | // Argument types. |
| 6087 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 6088 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 6089 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 6090 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6091 | QualType PType = PVDecl->getOriginalType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6092 | if (const auto *AT = |
| 6093 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 6094 | // Use array's original type only if it has known number of |
| 6095 | // elements. |
Steve Naroff | 323827e | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 6096 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 6097 | PType = PVDecl->getType(); |
| 6098 | } else if (PType->isFunctionType()) |
| 6099 | PType = PVDecl->getType(); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6100 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
| 6101 | PType, S, Extended); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6102 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 6103 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6104 | } |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6105 | |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6106 | return S; |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6107 | } |
| 6108 | |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 6109 | ObjCPropertyImplDecl * |
| 6110 | ASTContext::getObjCPropertyImplDeclForPropertyDecl( |
| 6111 | const ObjCPropertyDecl *PD, |
| 6112 | const Decl *Container) const { |
| 6113 | if (!Container) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6114 | return nullptr; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6115 | if (const auto *CID = dyn_cast<ObjCCategoryImplDecl>(Container)) { |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 6116 | for (auto *PID : CID->property_impls()) |
| 6117 | if (PID->getPropertyDecl() == PD) |
| 6118 | return PID; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6119 | } else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6120 | const auto *OID = cast<ObjCImplementationDecl>(Container); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6121 | for (auto *PID : OID->property_impls()) |
| 6122 | if (PID->getPropertyDecl() == PD) |
| 6123 | return PID; |
| 6124 | } |
| 6125 | return nullptr; |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 6126 | } |
| 6127 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6128 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 6129 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6130 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 6131 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6132 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 6133 | /// attributes readonly and bycopy are encoded as single characters. The |
| 6134 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 6135 | /// encoded as single characters, followed by an identifier. Property types |
| 6136 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 6137 | /// these attributes are defined by the following enumeration: |
| 6138 | /// @code |
| 6139 | /// enum PropertyAttributes { |
| 6140 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 6141 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 6142 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 6143 | /// kPropertyDynamic = 'D', // property is dynamic |
| 6144 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 6145 | /// kPropertySetter = 'S', // followed by setter selector name |
| 6146 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 8cf6185 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 6147 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 6148 | /// kPropertyWeak = 'W' // 'weak' property |
| 6149 | /// kPropertyStrong = 'P' // property GC'able |
| 6150 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 6151 | /// }; |
| 6152 | /// @endcode |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6153 | std::string |
| 6154 | ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
| 6155 | const Decl *Container) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6156 | // Collect information from the property implementation decl(s). |
| 6157 | bool Dynamic = false; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6158 | ObjCPropertyImplDecl *SynthesizePID = nullptr; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6159 | |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 6160 | if (ObjCPropertyImplDecl *PropertyImpDecl = |
| 6161 | getObjCPropertyImplDeclForPropertyDecl(PD, Container)) { |
| 6162 | if (PropertyImpDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 6163 | Dynamic = true; |
| 6164 | else |
| 6165 | SynthesizePID = PropertyImpDecl; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6166 | } |
| 6167 | |
| 6168 | // FIXME: This is not very efficient. |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6169 | std::string S = "T"; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6170 | |
| 6171 | // Encode result type. |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 6172 | // GCC has some special rules regarding encoding of properties which |
| 6173 | // closely resembles encoding of ivars. |
Joe Groff | 98ac7d2 | 2014-07-07 22:25:15 +0000 | [diff] [blame] | 6174 | getObjCEncodingForPropertyType(PD->getType(), S); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6175 | |
| 6176 | if (PD->isReadOnly()) { |
| 6177 | S += ",R"; |
Nico Weber | 4e8626f | 2013-05-08 23:47:40 +0000 | [diff] [blame] | 6178 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) |
| 6179 | S += ",C"; |
| 6180 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) |
| 6181 | S += ",&"; |
Fariborz Jahanian | 33079ee | 2014-04-02 22:49:42 +0000 | [diff] [blame] | 6182 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) |
| 6183 | S += ",W"; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6184 | } else { |
| 6185 | switch (PD->getSetterKind()) { |
| 6186 | case ObjCPropertyDecl::Assign: break; |
| 6187 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6188 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 70a315c | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 6189 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6190 | } |
| 6191 | } |
| 6192 | |
| 6193 | // It really isn't clear at all what this means, since properties |
| 6194 | // are "dynamic by default". |
| 6195 | if (Dynamic) |
| 6196 | S += ",D"; |
| 6197 | |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 6198 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 6199 | S += ",N"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6200 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6201 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 6202 | S += ",G"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 6203 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6204 | } |
| 6205 | |
| 6206 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 6207 | S += ",S"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 6208 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6209 | } |
| 6210 | |
| 6211 | if (SynthesizePID) { |
| 6212 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 6213 | S += ",V"; |
Chris Lattner | 1cbaacc | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 6214 | S += OID->getNameAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6215 | } |
| 6216 | |
| 6217 | // FIXME: OBJCGC: weak & strong |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6218 | return S; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6219 | } |
| 6220 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6221 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6222 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 6223 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6224 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6225 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 6226 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6227 | if (const auto *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6228 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6229 | PointeeTy = UnsignedIntTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6230 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6231 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6232 | PointeeTy = IntTy; |
| 6233 | } |
| 6234 | } |
| 6235 | } |
| 6236 | |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 6237 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6238 | const FieldDecl *Field, |
| 6239 | QualType *NotEncodedT) const { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 6240 | // We follow the behavior of gcc, expanding structures which are |
| 6241 | // directly pointed to, and expanding embedded structures. Note that |
| 6242 | // these rules are sufficient to prevent recursive encoding of the |
| 6243 | // same type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6244 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6245 | true /* outermost type */, false, false, |
| 6246 | false, false, false, NotEncodedT); |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 6247 | } |
| 6248 | |
Joe Groff | 98ac7d2 | 2014-07-07 22:25:15 +0000 | [diff] [blame] | 6249 | void ASTContext::getObjCEncodingForPropertyType(QualType T, |
| 6250 | std::string& S) const { |
| 6251 | // Encode result type. |
| 6252 | // GCC has some special rules regarding encoding of properties which |
| 6253 | // closely resembles encoding of ivars. |
| 6254 | getObjCEncodingForTypeImpl(T, S, true, true, nullptr, |
| 6255 | true /* outermost type */, |
| 6256 | true /* encoding property */); |
| 6257 | } |
| 6258 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6259 | static char getObjCEncodingForPrimitiveKind(const ASTContext *C, |
| 6260 | BuiltinType::Kind kind) { |
| 6261 | switch (kind) { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6262 | case BuiltinType::Void: return 'v'; |
| 6263 | case BuiltinType::Bool: return 'B'; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 6264 | case BuiltinType::Char8: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6265 | case BuiltinType::Char_U: |
| 6266 | case BuiltinType::UChar: return 'C'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6267 | case BuiltinType::Char16: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6268 | case BuiltinType::UShort: return 'S'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6269 | case BuiltinType::Char32: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6270 | case BuiltinType::UInt: return 'I'; |
| 6271 | case BuiltinType::ULong: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6272 | return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6273 | case BuiltinType::UInt128: return 'T'; |
| 6274 | case BuiltinType::ULongLong: return 'Q'; |
| 6275 | case BuiltinType::Char_S: |
| 6276 | case BuiltinType::SChar: return 'c'; |
| 6277 | case BuiltinType::Short: return 's'; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 6278 | case BuiltinType::WChar_S: |
| 6279 | case BuiltinType::WChar_U: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6280 | case BuiltinType::Int: return 'i'; |
| 6281 | case BuiltinType::Long: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6282 | return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6283 | case BuiltinType::LongLong: return 'q'; |
| 6284 | case BuiltinType::Int128: return 't'; |
| 6285 | case BuiltinType::Float: return 'f'; |
| 6286 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 7cba5a7 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 6287 | case BuiltinType::LongDouble: return 'D'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6288 | case BuiltinType::NullPtr: return '*'; // like char* |
| 6289 | |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 6290 | case BuiltinType::Float16: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 6291 | case BuiltinType::Float128: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6292 | case BuiltinType::Half: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 6293 | case BuiltinType::ShortAccum: |
| 6294 | case BuiltinType::Accum: |
| 6295 | case BuiltinType::LongAccum: |
| 6296 | case BuiltinType::UShortAccum: |
| 6297 | case BuiltinType::UAccum: |
| 6298 | case BuiltinType::ULongAccum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 6299 | case BuiltinType::ShortFract: |
| 6300 | case BuiltinType::Fract: |
| 6301 | case BuiltinType::LongFract: |
| 6302 | case BuiltinType::UShortFract: |
| 6303 | case BuiltinType::UFract: |
| 6304 | case BuiltinType::ULongFract: |
| 6305 | case BuiltinType::SatShortAccum: |
| 6306 | case BuiltinType::SatAccum: |
| 6307 | case BuiltinType::SatLongAccum: |
| 6308 | case BuiltinType::SatUShortAccum: |
| 6309 | case BuiltinType::SatUAccum: |
| 6310 | case BuiltinType::SatULongAccum: |
| 6311 | case BuiltinType::SatShortFract: |
| 6312 | case BuiltinType::SatFract: |
| 6313 | case BuiltinType::SatLongFract: |
| 6314 | case BuiltinType::SatUShortFract: |
| 6315 | case BuiltinType::SatUFract: |
| 6316 | case BuiltinType::SatULongFract: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6317 | // FIXME: potentially need @encodes for these! |
| 6318 | return ' '; |
| 6319 | |
| 6320 | case BuiltinType::ObjCId: |
| 6321 | case BuiltinType::ObjCClass: |
| 6322 | case BuiltinType::ObjCSel: |
| 6323 | llvm_unreachable("@encoding ObjC primitive type"); |
| 6324 | |
| 6325 | // OpenCL and placeholder types don't need @encodings. |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 6326 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 6327 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 6328 | #include "clang/Basic/OpenCLImageTypes.def" |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6329 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 6330 | case BuiltinType::OCLClkEvent: |
| 6331 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 6332 | case BuiltinType::OCLReserveID: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6333 | case BuiltinType::OCLSampler: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6334 | case BuiltinType::Dependent: |
| 6335 | #define BUILTIN_TYPE(KIND, ID) |
| 6336 | #define PLACEHOLDER_TYPE(KIND, ID) \ |
| 6337 | case BuiltinType::KIND: |
| 6338 | #include "clang/AST/BuiltinTypes.def" |
| 6339 | llvm_unreachable("invalid builtin type for @encode"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6340 | } |
David Blaikie | 5a6a020 | 2013-01-09 17:48:41 +0000 | [diff] [blame] | 6341 | llvm_unreachable("invalid BuiltinType::Kind value"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6342 | } |
| 6343 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 6344 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 6345 | EnumDecl *Enum = ET->getDecl(); |
| 6346 | |
| 6347 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 6348 | if (!Enum->isFixed()) |
| 6349 | return 'i'; |
| 6350 | |
| 6351 | // The encoding of a fixed enum type matches its fixed underlying type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6352 | const auto *BT = Enum->getIntegerType()->castAs<BuiltinType>(); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6353 | return getObjCEncodingForPrimitiveKind(C, BT->getKind()); |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 6354 | } |
| 6355 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6356 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6357 | QualType T, const FieldDecl *FD) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6358 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 6359 | S += 'b'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6360 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 6361 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 6362 | // then the offset (in bits) of the first element, then the type of the |
| 6363 | // bitfield, then the size in bits. For example, in this structure: |
| 6364 | // |
| 6365 | // struct |
| 6366 | // { |
| 6367 | // int integer; |
| 6368 | // int flags:2; |
| 6369 | // }; |
| 6370 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 6371 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 6372 | // information is not especially sensible, but we're stuck with it for |
| 6373 | // compatibility with GCC, although providing it breaks anything that |
| 6374 | // actually uses runtime introspection and wants to work on both runtimes... |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 6375 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
Akira Hatanaka | 4b1c484 | 2017-06-27 04:34:04 +0000 | [diff] [blame] | 6376 | uint64_t Offset; |
| 6377 | |
| 6378 | if (const auto *IVD = dyn_cast<ObjCIvarDecl>(FD)) { |
| 6379 | Offset = Ctx->lookupFieldBitOffset(IVD->getContainingInterface(), nullptr, |
| 6380 | IVD); |
| 6381 | } else { |
| 6382 | const RecordDecl *RD = FD->getParent(); |
| 6383 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
| 6384 | Offset = RL.getFieldOffset(FD->getFieldIndex()); |
| 6385 | } |
| 6386 | |
| 6387 | S += llvm::utostr(Offset); |
| 6388 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6389 | if (const auto *ET = T->getAs<EnumType>()) |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 6390 | S += ObjCEncodingForEnumType(Ctx, ET); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6391 | else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6392 | const auto *BT = T->castAs<BuiltinType>(); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6393 | S += getObjCEncodingForPrimitiveKind(Ctx, BT->getKind()); |
| 6394 | } |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6395 | } |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6396 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 6397 | } |
| 6398 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6399 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 6400 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 6401 | bool ExpandPointedToStructures, |
| 6402 | bool ExpandStructures, |
Daniel Dunbar | c040ce4 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 6403 | const FieldDecl *FD, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 6404 | bool OutermostType, |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6405 | bool EncodingProperty, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6406 | bool StructField, |
| 6407 | bool EncodeBlockParameters, |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 6408 | bool EncodeClassNames, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6409 | bool EncodePointerToObjCTypedef, |
| 6410 | QualType *NotEncodedT) const { |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6411 | CanQualType CT = getCanonicalType(T); |
| 6412 | switch (CT->getTypeClass()) { |
| 6413 | case Type::Builtin: |
| 6414 | case Type::Enum: |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6415 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6416 | return EncodeBitField(this, S, T, FD); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6417 | if (const auto *BT = dyn_cast<BuiltinType>(CT)) |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6418 | S += getObjCEncodingForPrimitiveKind(this, BT->getKind()); |
| 6419 | else |
| 6420 | S += ObjCEncodingForEnumType(this, cast<EnumType>(CT)); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6421 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6422 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6423 | case Type::Complex: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6424 | const auto *CT = T->castAs<ComplexType>(); |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 6425 | S += 'j'; |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6426 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, nullptr); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6427 | return; |
| 6428 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6429 | |
| 6430 | case Type::Atomic: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6431 | const auto *AT = T->castAs<AtomicType>(); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6432 | S += 'A'; |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6433 | getObjCEncodingForTypeImpl(AT->getValueType(), S, false, false, nullptr); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6434 | return; |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 6435 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6436 | |
| 6437 | // encoding for pointer or reference types. |
| 6438 | case Type::Pointer: |
| 6439 | case Type::LValueReference: |
| 6440 | case Type::RValueReference: { |
| 6441 | QualType PointeeTy; |
| 6442 | if (isa<PointerType>(CT)) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6443 | const auto *PT = T->castAs<PointerType>(); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6444 | if (PT->isObjCSelType()) { |
| 6445 | S += ':'; |
| 6446 | return; |
| 6447 | } |
| 6448 | PointeeTy = PT->getPointeeType(); |
| 6449 | } else { |
| 6450 | PointeeTy = T->castAs<ReferenceType>()->getPointeeType(); |
| 6451 | } |
| 6452 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6453 | bool isReadOnly = false; |
| 6454 | // For historical/compatibility reasons, the read-only qualifier of the |
| 6455 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 6456 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6457 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 6458 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6459 | if (OutermostType && T.isConstQualified()) { |
| 6460 | isReadOnly = true; |
| 6461 | S += 'r'; |
| 6462 | } |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 6463 | } else if (OutermostType) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6464 | QualType P = PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6465 | while (P->getAs<PointerType>()) |
| 6466 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6467 | if (P.isConstQualified()) { |
| 6468 | isReadOnly = true; |
| 6469 | S += 'r'; |
| 6470 | } |
| 6471 | } |
| 6472 | if (isReadOnly) { |
| 6473 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 6474 | // combinations need to be rearranged. |
| 6475 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6476 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 6477 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6478 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6479 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6480 | if (PointeeTy->isCharType()) { |
| 6481 | // char pointer types should be encoded as '*' unless it is a |
| 6482 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 6483 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6484 | S += '*'; |
| 6485 | return; |
| 6486 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6487 | } else if (const auto *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 3de6b70 | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 6488 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 6489 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 6490 | S += '#'; |
| 6491 | return; |
| 6492 | } |
| 6493 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 6494 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 6495 | S += '@'; |
| 6496 | return; |
| 6497 | } |
| 6498 | // fall through... |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6499 | } |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6500 | S += '^'; |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6501 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 6502 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6503 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6504 | nullptr, false, false, false, false, false, false, |
| 6505 | NotEncodedT); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6506 | return; |
| 6507 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6508 | |
| 6509 | case Type::ConstantArray: |
| 6510 | case Type::IncompleteArray: |
| 6511 | case Type::VariableArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6512 | const auto *AT = cast<ArrayType>(CT); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6513 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6514 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6515 | // Incomplete arrays are encoded as a pointer to the array element. |
| 6516 | S += '^'; |
| 6517 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6518 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6519 | false, ExpandStructures, FD); |
| 6520 | } else { |
| 6521 | S += '['; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6522 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6523 | if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) |
Fariborz Jahanian | f0dc11a | 2013-06-04 16:04:37 +0000 | [diff] [blame] | 6524 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 6525 | else { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6526 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6527 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 6528 | "Unknown array type!"); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6529 | S += '0'; |
| 6530 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6531 | |
| 6532 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6533 | false, ExpandStructures, FD, |
| 6534 | false, false, false, false, false, false, |
| 6535 | NotEncodedT); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6536 | S += ']'; |
| 6537 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6538 | return; |
| 6539 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6540 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6541 | case Type::FunctionNoProto: |
| 6542 | case Type::FunctionProto: |
Anders Carlsson | df4cc61 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 6543 | S += '?'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6544 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6545 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6546 | case Type::Record: { |
| 6547 | RecordDecl *RDecl = cast<RecordType>(CT)->getDecl(); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 6548 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 6549 | // Anonymous structures print as '?' |
| 6550 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 6551 | S += II->getName(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6552 | if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 6553 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 6554 | llvm::raw_string_ostream OS(S); |
Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 6555 | printTemplateArgumentList(OS, TemplateArgs.asArray(), |
| 6556 | getPrintingPolicy()); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 6557 | } |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 6558 | } else { |
| 6559 | S += '?'; |
| 6560 | } |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 6561 | if (ExpandStructures) { |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 6562 | S += '='; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6563 | if (!RDecl->isUnion()) { |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6564 | getObjCEncodingForStructureImpl(RDecl, S, FD, true, NotEncodedT); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6565 | } else { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6566 | for (const auto *Field : RDecl->fields()) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6567 | if (FD) { |
| 6568 | S += '"'; |
| 6569 | S += Field->getNameAsString(); |
| 6570 | S += '"'; |
| 6571 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6572 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6573 | // Special case bit-fields. |
| 6574 | if (Field->isBitField()) { |
| 6575 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6576 | Field); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6577 | } else { |
| 6578 | QualType qt = Field->getType(); |
| 6579 | getLegacyIntegralTypeEncoding(qt); |
| 6580 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 6581 | FD, /*OutermostType*/false, |
| 6582 | /*EncodingProperty*/false, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6583 | /*StructField*/true, |
| 6584 | false, false, false, NotEncodedT); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6585 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 6586 | } |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 6587 | } |
Fariborz Jahanian | bc92fd7 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 6588 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 6589 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6590 | return; |
| 6591 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6592 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6593 | case Type::BlockPointer: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6594 | const auto *BT = T->castAs<BlockPointerType>(); |
Steve Naroff | 49140cb | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 6595 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6596 | if (EncodeBlockParameters) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6597 | const auto *FT = BT->getPointeeType()->castAs<FunctionType>(); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6598 | |
| 6599 | S += '<'; |
| 6600 | // Block return type |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6601 | getObjCEncodingForTypeImpl( |
| 6602 | FT->getReturnType(), S, ExpandPointedToStructures, ExpandStructures, |
| 6603 | FD, false /* OutermostType */, EncodingProperty, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6604 | false /* StructField */, EncodeBlockParameters, EncodeClassNames, false, |
| 6605 | NotEncodedT); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6606 | // Block self |
| 6607 | S += "@?"; |
| 6608 | // Block parameters |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6609 | if (const auto *FPT = dyn_cast<FunctionProtoType>(FT)) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 6610 | for (const auto &I : FPT->param_types()) |
| 6611 | getObjCEncodingForTypeImpl( |
| 6612 | I, S, ExpandPointedToStructures, ExpandStructures, FD, |
| 6613 | false /* OutermostType */, EncodingProperty, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6614 | false /* StructField */, EncodeBlockParameters, EncodeClassNames, |
| 6615 | false, NotEncodedT); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6616 | } |
| 6617 | S += '>'; |
| 6618 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6619 | return; |
| 6620 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6621 | |
Fariborz Jahanian | 33fa962 | 2014-01-28 20:41:15 +0000 | [diff] [blame] | 6622 | case Type::ObjCObject: { |
| 6623 | // hack to match legacy encoding of *id and *Class |
| 6624 | QualType Ty = getObjCObjectPointerType(CT); |
| 6625 | if (Ty->isObjCIdType()) { |
| 6626 | S += "{objc_object=}"; |
| 6627 | return; |
| 6628 | } |
| 6629 | else if (Ty->isObjCClassType()) { |
| 6630 | S += "{objc_class=}"; |
| 6631 | return; |
| 6632 | } |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 6633 | // TODO: Double check to make sure this intentionally falls through. |
Galina Kistanova | f87496d | 2017-06-03 06:31:42 +0000 | [diff] [blame] | 6634 | LLVM_FALLTHROUGH; |
Fariborz Jahanian | 33fa962 | 2014-01-28 20:41:15 +0000 | [diff] [blame] | 6635 | } |
| 6636 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6637 | case Type::ObjCInterface: { |
| 6638 | // Ignore protocol qualifiers when mangling at this level. |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 6639 | // @encode(class_name) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 6640 | ObjCInterfaceDecl *OI = T->castAs<ObjCObjectType>()->getInterface(); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 6641 | S += '{'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6642 | S += OI->getObjCRuntimeNameAsString(); |
Akira Hatanaka | fd0fb20 | 2016-08-17 19:42:22 +0000 | [diff] [blame] | 6643 | if (ExpandStructures) { |
| 6644 | S += '='; |
| 6645 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
| 6646 | DeepCollectObjCIvars(OI, true, Ivars); |
| 6647 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6648 | const FieldDecl *Field = Ivars[i]; |
Akira Hatanaka | fd0fb20 | 2016-08-17 19:42:22 +0000 | [diff] [blame] | 6649 | if (Field->isBitField()) |
| 6650 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
| 6651 | else |
| 6652 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD, |
| 6653 | false, false, false, false, false, |
| 6654 | EncodePointerToObjCTypedef, |
| 6655 | NotEncodedT); |
| 6656 | } |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 6657 | } |
| 6658 | S += '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6659 | return; |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 6660 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6661 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6662 | case Type::ObjCObjectPointer: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6663 | const auto *OPT = T->castAs<ObjCObjectPointerType>(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6664 | if (OPT->isObjCIdType()) { |
| 6665 | S += '@'; |
| 6666 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6667 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6668 | |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 6669 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 6670 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 6671 | // Since this is a binary compatibility issue, need to consult with runtime |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 6672 | // folks. Fortunately, this is a *very* obscure construct. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6673 | S += '#'; |
| 6674 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6675 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6676 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6677 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6678 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6679 | ExpandPointedToStructures, |
| 6680 | ExpandStructures, FD); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6681 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6682 | // Note that we do extended encoding of protocol qualifer list |
| 6683 | // Only when doing ivar or property encoding. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6684 | S += '"'; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6685 | for (const auto *I : OPT->quals()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6686 | S += '<'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6687 | S += I->getObjCRuntimeNameAsString(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6688 | S += '>'; |
| 6689 | } |
| 6690 | S += '"'; |
| 6691 | } |
| 6692 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6693 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6694 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6695 | QualType PointeeTy = OPT->getPointeeType(); |
| 6696 | if (!EncodingProperty && |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 6697 | isa<TypedefType>(PointeeTy.getTypePtr()) && |
| 6698 | !EncodePointerToObjCTypedef) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6699 | // Another historical/compatibility reason. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6700 | // We encode the underlying type which comes out as |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6701 | // {...}; |
| 6702 | S += '^'; |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 6703 | if (FD && OPT->getInterfaceDecl()) { |
Fariborz Jahanian | c682ef5 | 2013-07-12 16:41:56 +0000 | [diff] [blame] | 6704 | // Prevent recursive encoding of fields in some rare cases. |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 6705 | ObjCInterfaceDecl *OI = OPT->getInterfaceDecl(); |
| 6706 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
| 6707 | DeepCollectObjCIvars(OI, true, Ivars); |
| 6708 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6709 | if (Ivars[i] == FD) { |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 6710 | S += '{'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6711 | S += OI->getObjCRuntimeNameAsString(); |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 6712 | S += '}'; |
| 6713 | return; |
| 6714 | } |
| 6715 | } |
| 6716 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6717 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 6718 | false, ExpandPointedToStructures, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6719 | nullptr, |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 6720 | false, false, false, false, false, |
| 6721 | /*EncodePointerToObjCTypedef*/true); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6722 | return; |
| 6723 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6724 | |
| 6725 | S += '@'; |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6726 | if (OPT->getInterfaceDecl() && |
| 6727 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6728 | S += '"'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6729 | S += OPT->getInterfaceDecl()->getObjCRuntimeNameAsString(); |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6730 | for (const auto *I : OPT->quals()) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6731 | S += '<'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6732 | S += I->getObjCRuntimeNameAsString(); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6733 | S += '>'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6734 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6735 | S += '"'; |
| 6736 | } |
| 6737 | return; |
| 6738 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6739 | |
John McCall | a9e6e8d | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 6740 | // gcc just blithely ignores member pointers. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6741 | // FIXME: we shoul do better than that. 'M' is available. |
| 6742 | case Type::MemberPointer: |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6743 | // This matches gcc's encoding, even though technically it is insufficient. |
| 6744 | //FIXME. We should do a better job than gcc. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6745 | case Type::Vector: |
| 6746 | case Type::ExtVector: |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6747 | // Until we have a coherent encoding of these three types, issue warning. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 6748 | if (NotEncodedT) |
| 6749 | *NotEncodedT = T; |
| 6750 | return; |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6751 | |
| 6752 | // We could see an undeduced auto type here during error recovery. |
| 6753 | // Just ignore it. |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6754 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 6755 | case Type::DeducedTemplateSpecialization: |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 6756 | return; |
| 6757 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 6758 | case Type::Pipe: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6759 | #define ABSTRACT_TYPE(KIND, BASE) |
| 6760 | #define TYPE(KIND, BASE) |
| 6761 | #define DEPENDENT_TYPE(KIND, BASE) \ |
| 6762 | case Type::KIND: |
| 6763 | #define NON_CANONICAL_TYPE(KIND, BASE) \ |
| 6764 | case Type::KIND: |
| 6765 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \ |
| 6766 | case Type::KIND: |
| 6767 | #include "clang/AST/TypeNodes.def" |
| 6768 | llvm_unreachable("@encode for dependent type!"); |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 6769 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6770 | llvm_unreachable("bad type kind!"); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6771 | } |
| 6772 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6773 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 6774 | std::string &S, |
| 6775 | const FieldDecl *FD, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6776 | bool includeVBases, |
| 6777 | QualType *NotEncodedT) const { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6778 | assert(RDecl && "Expected non-null RecordDecl"); |
| 6779 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 6780 | if (!RDecl->getDefinition() || RDecl->getDefinition()->isInvalidDecl()) |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6781 | return; |
| 6782 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6783 | const auto *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6784 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 6785 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 6786 | |
| 6787 | if (CXXRec) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 6788 | for (const auto &BI : CXXRec->bases()) { |
| 6789 | if (!BI.isVirtual()) { |
| 6790 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 6791 | if (base->isEmpty()) |
| 6792 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 6793 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6794 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 6795 | std::make_pair(offs, base)); |
| 6796 | } |
| 6797 | } |
| 6798 | } |
| 6799 | |
| 6800 | unsigned i = 0; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6801 | for (auto *Field : RDecl->fields()) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6802 | uint64_t offs = layout.getFieldOffset(i); |
| 6803 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 6804 | std::make_pair(offs, Field)); |
| 6805 | ++i; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6806 | } |
| 6807 | |
| 6808 | if (CXXRec && includeVBases) { |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 6809 | for (const auto &BI : CXXRec->vbases()) { |
| 6810 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 6811 | if (base->isEmpty()) |
| 6812 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 6813 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); |
Eli Friedman | 1d24af8 | 2013-09-18 01:59:16 +0000 | [diff] [blame] | 6814 | if (offs >= uint64_t(toBits(layout.getNonVirtualSize())) && |
| 6815 | FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
Argyrios Kyrtzidis | 81c0b5c | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 6816 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 6817 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6818 | } |
| 6819 | } |
| 6820 | |
| 6821 | CharUnits size; |
| 6822 | if (CXXRec) { |
| 6823 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 6824 | } else { |
| 6825 | size = layout.getSize(); |
| 6826 | } |
| 6827 | |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6828 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6829 | uint64_t CurOffs = 0; |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6830 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6831 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 6832 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 6833 | |
Douglas Gregor | f5d6c74 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 6834 | if (CXXRec && CXXRec->isDynamicClass() && |
| 6835 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6836 | if (FD) { |
| 6837 | S += "\"_vptr$"; |
| 6838 | std::string recname = CXXRec->getNameAsString(); |
| 6839 | if (recname.empty()) recname = "?"; |
| 6840 | S += recname; |
| 6841 | S += '"'; |
| 6842 | } |
| 6843 | S += "^^?"; |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6844 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6845 | CurOffs += getTypeSize(VoidPtrTy); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6846 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6847 | } |
| 6848 | |
| 6849 | if (!RDecl->hasFlexibleArrayMember()) { |
| 6850 | // Mark the end of the structure. |
| 6851 | uint64_t offs = toBits(size); |
| 6852 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6853 | std::make_pair(offs, nullptr)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6854 | } |
| 6855 | |
| 6856 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6857 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6858 | assert(CurOffs <= CurLayObj->first); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6859 | if (CurOffs < CurLayObj->first) { |
| 6860 | uint64_t padding = CurLayObj->first - CurOffs; |
| 6861 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 6862 | // packing/alignment of members is different that normal, in which case |
| 6863 | // the encoding will be out-of-sync with the real layout. |
| 6864 | // If the runtime switches to just consider the size of types without |
| 6865 | // taking into account alignment, we could make padding explicit in the |
| 6866 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 6867 | // longer then though. |
| 6868 | CurOffs += padding; |
| 6869 | } |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6870 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6871 | |
| 6872 | NamedDecl *dcl = CurLayObj->second; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6873 | if (!dcl) |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6874 | break; // reached end of structure. |
| 6875 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6876 | if (auto *base = dyn_cast<CXXRecordDecl>(dcl)) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6877 | // We expand the bases without their virtual bases since those are going |
| 6878 | // in the initial structure. Note that this differs from gcc which |
| 6879 | // expands virtual bases each time one is encountered in the hierarchy, |
| 6880 | // making the encoding type bigger than it really is. |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6881 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false, |
| 6882 | NotEncodedT); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 6883 | assert(!base->isEmpty()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6884 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 6885 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6886 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6887 | } else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6888 | const auto *field = cast<FieldDecl>(dcl); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6889 | if (FD) { |
| 6890 | S += '"'; |
| 6891 | S += field->getNameAsString(); |
| 6892 | S += '"'; |
| 6893 | } |
| 6894 | |
| 6895 | if (field->isBitField()) { |
| 6896 | EncodeBitField(this, S, field->getType(), field); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6897 | #ifndef NDEBUG |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6898 | CurOffs += field->getBitWidthValue(*this); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6899 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6900 | } else { |
| 6901 | QualType qt = field->getType(); |
| 6902 | getLegacyIntegralTypeEncoding(qt); |
| 6903 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 6904 | /*OutermostType*/false, |
| 6905 | /*EncodingProperty*/false, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6906 | /*StructField*/true, |
| 6907 | false, false, false, NotEncodedT); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6908 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6909 | CurOffs += getTypeSize(field->getType()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 6910 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6911 | } |
| 6912 | } |
| 6913 | } |
| 6914 | } |
| 6915 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6916 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 6917 | std::string& S) const { |
| 6918 | if (QT & Decl::OBJC_TQ_In) |
| 6919 | S += 'n'; |
| 6920 | if (QT & Decl::OBJC_TQ_Inout) |
| 6921 | S += 'N'; |
| 6922 | if (QT & Decl::OBJC_TQ_Out) |
| 6923 | S += 'o'; |
| 6924 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 6925 | S += 'O'; |
| 6926 | if (QT & Decl::OBJC_TQ_Byref) |
| 6927 | S += 'R'; |
| 6928 | if (QT & Decl::OBJC_TQ_Oneway) |
| 6929 | S += 'V'; |
| 6930 | } |
| 6931 | |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 6932 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 6933 | if (!ObjCIdDecl) { |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 6934 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, {}, {}); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 6935 | T = getObjCObjectPointerType(T); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6936 | ObjCIdDecl = buildImplicitTypedef(T, "id"); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 6937 | } |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 6938 | return ObjCIdDecl; |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 6939 | } |
| 6940 | |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 6941 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 6942 | if (!ObjCSelDecl) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6943 | QualType T = getPointerType(ObjCBuiltinSelTy); |
| 6944 | ObjCSelDecl = buildImplicitTypedef(T, "SEL"); |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 6945 | } |
| 6946 | return ObjCSelDecl; |
Fariborz Jahanian | 4bef462 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 6947 | } |
| 6948 | |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 6949 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 6950 | if (!ObjCClassDecl) { |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 6951 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, {}, {}); |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 6952 | T = getObjCObjectPointerType(T); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6953 | ObjCClassDecl = buildImplicitTypedef(T, "Class"); |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 6954 | } |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 6955 | return ObjCClassDecl; |
Anders Carlsson | f56a7ae | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 6956 | } |
| 6957 | |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 6958 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 6959 | if (!ObjCProtocolClassDecl) { |
| 6960 | ObjCProtocolClassDecl |
| 6961 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
| 6962 | SourceLocation(), |
| 6963 | &Idents.get("Protocol"), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 6964 | /*typeParamList=*/nullptr, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6965 | /*PrevDecl=*/nullptr, |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 6966 | SourceLocation(), true); |
| 6967 | } |
| 6968 | |
| 6969 | return ObjCProtocolClassDecl; |
| 6970 | } |
| 6971 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6972 | //===----------------------------------------------------------------------===// |
| 6973 | // __builtin_va_list Construction Functions |
| 6974 | //===----------------------------------------------------------------------===// |
| 6975 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 6976 | static TypedefDecl *CreateCharPtrNamedVaListDecl(const ASTContext *Context, |
| 6977 | StringRef Name) { |
| 6978 | // typedef char* __builtin[_ms]_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6979 | QualType T = Context->getPointerType(Context->CharTy); |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 6980 | return Context->buildImplicitTypedef(T, Name); |
| 6981 | } |
| 6982 | |
| 6983 | static TypedefDecl *CreateMSVaListDecl(const ASTContext *Context) { |
| 6984 | return CreateCharPtrNamedVaListDecl(Context, "__builtin_ms_va_list"); |
| 6985 | } |
| 6986 | |
| 6987 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 6988 | return CreateCharPtrNamedVaListDecl(Context, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6989 | } |
| 6990 | |
| 6991 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 6992 | // typedef void* __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6993 | QualType T = Context->getPointerType(Context->VoidTy); |
| 6994 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 6995 | } |
| 6996 | |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 6997 | static TypedefDecl * |
| 6998 | CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6999 | // struct __va_list |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7000 | RecordDecl *VaListTagDecl = Context->buildImplicitRecord("__va_list"); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7001 | if (Context->getLangOpts().CPlusPlus) { |
| 7002 | // namespace std { struct __va_list { |
| 7003 | NamespaceDecl *NS; |
| 7004 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 7005 | Context->getTranslationUnitDecl(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7006 | /*Inline*/ false, SourceLocation(), |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7007 | SourceLocation(), &Context->Idents.get("std"), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7008 | /*PrevDecl*/ nullptr); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7009 | NS->setImplicit(); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7010 | VaListTagDecl->setDeclContext(NS); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7011 | } |
| 7012 | |
| 7013 | VaListTagDecl->startDefinition(); |
| 7014 | |
| 7015 | const size_t NumFields = 5; |
| 7016 | QualType FieldTypes[NumFields]; |
| 7017 | const char *FieldNames[NumFields]; |
| 7018 | |
| 7019 | // void *__stack; |
| 7020 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); |
| 7021 | FieldNames[0] = "__stack"; |
| 7022 | |
| 7023 | // void *__gr_top; |
| 7024 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); |
| 7025 | FieldNames[1] = "__gr_top"; |
| 7026 | |
| 7027 | // void *__vr_top; |
| 7028 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 7029 | FieldNames[2] = "__vr_top"; |
| 7030 | |
| 7031 | // int __gr_offs; |
| 7032 | FieldTypes[3] = Context->IntTy; |
| 7033 | FieldNames[3] = "__gr_offs"; |
| 7034 | |
| 7035 | // int __vr_offs; |
| 7036 | FieldTypes[4] = Context->IntTy; |
| 7037 | FieldNames[4] = "__vr_offs"; |
| 7038 | |
| 7039 | // Create fields |
| 7040 | for (unsigned i = 0; i < NumFields; ++i) { |
| 7041 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 7042 | VaListTagDecl, |
| 7043 | SourceLocation(), |
| 7044 | SourceLocation(), |
| 7045 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7046 | FieldTypes[i], /*TInfo=*/nullptr, |
| 7047 | /*BitWidth=*/nullptr, |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7048 | /*Mutable=*/false, |
| 7049 | ICIS_NoInit); |
| 7050 | Field->setAccess(AS_public); |
| 7051 | VaListTagDecl->addDecl(Field); |
| 7052 | } |
| 7053 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7054 | Context->VaListTagDecl = VaListTagDecl; |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7055 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7056 | |
| 7057 | // } __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7058 | return Context->buildImplicitTypedef(VaListTagType, "__builtin_va_list"); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7059 | } |
| 7060 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7061 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
| 7062 | // typedef struct __va_list_tag { |
| 7063 | RecordDecl *VaListTagDecl; |
| 7064 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7065 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7066 | VaListTagDecl->startDefinition(); |
| 7067 | |
| 7068 | const size_t NumFields = 5; |
| 7069 | QualType FieldTypes[NumFields]; |
| 7070 | const char *FieldNames[NumFields]; |
| 7071 | |
| 7072 | // unsigned char gpr; |
| 7073 | FieldTypes[0] = Context->UnsignedCharTy; |
| 7074 | FieldNames[0] = "gpr"; |
| 7075 | |
| 7076 | // unsigned char fpr; |
| 7077 | FieldTypes[1] = Context->UnsignedCharTy; |
| 7078 | FieldNames[1] = "fpr"; |
| 7079 | |
| 7080 | // unsigned short reserved; |
| 7081 | FieldTypes[2] = Context->UnsignedShortTy; |
| 7082 | FieldNames[2] = "reserved"; |
| 7083 | |
| 7084 | // void* overflow_arg_area; |
| 7085 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 7086 | FieldNames[3] = "overflow_arg_area"; |
| 7087 | |
| 7088 | // void* reg_save_area; |
| 7089 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
| 7090 | FieldNames[4] = "reg_save_area"; |
| 7091 | |
| 7092 | // Create fields |
| 7093 | for (unsigned i = 0; i < NumFields; ++i) { |
| 7094 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
| 7095 | SourceLocation(), |
| 7096 | SourceLocation(), |
| 7097 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7098 | FieldTypes[i], /*TInfo=*/nullptr, |
| 7099 | /*BitWidth=*/nullptr, |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7100 | /*Mutable=*/false, |
| 7101 | ICIS_NoInit); |
| 7102 | Field->setAccess(AS_public); |
| 7103 | VaListTagDecl->addDecl(Field); |
| 7104 | } |
| 7105 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7106 | Context->VaListTagDecl = VaListTagDecl; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7107 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 7108 | |
| 7109 | // } __va_list_tag; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7110 | TypedefDecl *VaListTagTypedefDecl = |
| 7111 | Context->buildImplicitTypedef(VaListTagType, "__va_list_tag"); |
| 7112 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7113 | QualType VaListTagTypedefType = |
| 7114 | Context->getTypedefType(VaListTagTypedefDecl); |
| 7115 | |
| 7116 | // typedef __va_list_tag __builtin_va_list[1]; |
| 7117 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 7118 | QualType VaListTagArrayType |
| 7119 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 7120 | Size, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7121 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7122 | } |
| 7123 | |
| 7124 | static TypedefDecl * |
| 7125 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7126 | // struct __va_list_tag { |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7127 | RecordDecl *VaListTagDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7128 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7129 | VaListTagDecl->startDefinition(); |
| 7130 | |
| 7131 | const size_t NumFields = 4; |
| 7132 | QualType FieldTypes[NumFields]; |
| 7133 | const char *FieldNames[NumFields]; |
| 7134 | |
| 7135 | // unsigned gp_offset; |
| 7136 | FieldTypes[0] = Context->UnsignedIntTy; |
| 7137 | FieldNames[0] = "gp_offset"; |
| 7138 | |
| 7139 | // unsigned fp_offset; |
| 7140 | FieldTypes[1] = Context->UnsignedIntTy; |
| 7141 | FieldNames[1] = "fp_offset"; |
| 7142 | |
| 7143 | // void* overflow_arg_area; |
| 7144 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 7145 | FieldNames[2] = "overflow_arg_area"; |
| 7146 | |
| 7147 | // void* reg_save_area; |
| 7148 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 7149 | FieldNames[3] = "reg_save_area"; |
| 7150 | |
| 7151 | // Create fields |
| 7152 | for (unsigned i = 0; i < NumFields; ++i) { |
| 7153 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 7154 | VaListTagDecl, |
| 7155 | SourceLocation(), |
| 7156 | SourceLocation(), |
| 7157 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7158 | FieldTypes[i], /*TInfo=*/nullptr, |
| 7159 | /*BitWidth=*/nullptr, |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7160 | /*Mutable=*/false, |
| 7161 | ICIS_NoInit); |
| 7162 | Field->setAccess(AS_public); |
| 7163 | VaListTagDecl->addDecl(Field); |
| 7164 | } |
| 7165 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7166 | Context->VaListTagDecl = VaListTagDecl; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7167 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 7168 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7169 | // }; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7170 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7171 | // typedef struct __va_list_tag __builtin_va_list[1]; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7172 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7173 | QualType VaListTagArrayType = |
| 7174 | Context->getConstantArrayType(VaListTagType, Size, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7175 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7176 | } |
| 7177 | |
| 7178 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
| 7179 | // typedef int __builtin_va_list[4]; |
| 7180 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); |
Yaron Keren | e0bcdd4 | 2016-10-08 06:45:10 +0000 | [diff] [blame] | 7181 | QualType IntArrayType = |
| 7182 | Context->getConstantArrayType(Context->IntTy, Size, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7183 | return Context->buildImplicitTypedef(IntArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7184 | } |
| 7185 | |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7186 | static TypedefDecl * |
| 7187 | CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7188 | // struct __va_list |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7189 | RecordDecl *VaListDecl = Context->buildImplicitRecord("__va_list"); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7190 | if (Context->getLangOpts().CPlusPlus) { |
| 7191 | // namespace std { struct __va_list { |
| 7192 | NamespaceDecl *NS; |
| 7193 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 7194 | Context->getTranslationUnitDecl(), |
| 7195 | /*Inline*/false, SourceLocation(), |
| 7196 | SourceLocation(), &Context->Idents.get("std"), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7197 | /*PrevDecl*/ nullptr); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7198 | NS->setImplicit(); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7199 | VaListDecl->setDeclContext(NS); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7200 | } |
| 7201 | |
| 7202 | VaListDecl->startDefinition(); |
| 7203 | |
| 7204 | // void * __ap; |
| 7205 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 7206 | VaListDecl, |
| 7207 | SourceLocation(), |
| 7208 | SourceLocation(), |
| 7209 | &Context->Idents.get("__ap"), |
| 7210 | Context->getPointerType(Context->VoidTy), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7211 | /*TInfo=*/nullptr, |
| 7212 | /*BitWidth=*/nullptr, |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7213 | /*Mutable=*/false, |
| 7214 | ICIS_NoInit); |
| 7215 | Field->setAccess(AS_public); |
| 7216 | VaListDecl->addDecl(Field); |
| 7217 | |
| 7218 | // }; |
| 7219 | VaListDecl->completeDefinition(); |
Oleg Ranevskyy | b88d247 | 2016-03-30 21:30:30 +0000 | [diff] [blame] | 7220 | Context->VaListTagDecl = VaListDecl; |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7221 | |
| 7222 | // typedef struct __va_list __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7223 | QualType T = Context->getRecordType(VaListDecl); |
| 7224 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7225 | } |
| 7226 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7227 | static TypedefDecl * |
| 7228 | CreateSystemZBuiltinVaListDecl(const ASTContext *Context) { |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7229 | // struct __va_list_tag { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7230 | RecordDecl *VaListTagDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7231 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7232 | VaListTagDecl->startDefinition(); |
| 7233 | |
| 7234 | const size_t NumFields = 4; |
| 7235 | QualType FieldTypes[NumFields]; |
| 7236 | const char *FieldNames[NumFields]; |
| 7237 | |
| 7238 | // long __gpr; |
| 7239 | FieldTypes[0] = Context->LongTy; |
| 7240 | FieldNames[0] = "__gpr"; |
| 7241 | |
| 7242 | // long __fpr; |
| 7243 | FieldTypes[1] = Context->LongTy; |
| 7244 | FieldNames[1] = "__fpr"; |
| 7245 | |
| 7246 | // void *__overflow_arg_area; |
| 7247 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 7248 | FieldNames[2] = "__overflow_arg_area"; |
| 7249 | |
| 7250 | // void *__reg_save_area; |
| 7251 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 7252 | FieldNames[3] = "__reg_save_area"; |
| 7253 | |
| 7254 | // Create fields |
| 7255 | for (unsigned i = 0; i < NumFields; ++i) { |
| 7256 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 7257 | VaListTagDecl, |
| 7258 | SourceLocation(), |
| 7259 | SourceLocation(), |
| 7260 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7261 | FieldTypes[i], /*TInfo=*/nullptr, |
| 7262 | /*BitWidth=*/nullptr, |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7263 | /*Mutable=*/false, |
| 7264 | ICIS_NoInit); |
| 7265 | Field->setAccess(AS_public); |
| 7266 | VaListTagDecl->addDecl(Field); |
| 7267 | } |
| 7268 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7269 | Context->VaListTagDecl = VaListTagDecl; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7270 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7271 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7272 | // }; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7273 | |
| 7274 | // typedef __va_list_tag __builtin_va_list[1]; |
| 7275 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7276 | QualType VaListTagArrayType = |
| 7277 | Context->getConstantArrayType(VaListTagType, Size, ArrayType::Normal, 0); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7278 | |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7279 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7280 | } |
| 7281 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7282 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
| 7283 | TargetInfo::BuiltinVaListKind Kind) { |
| 7284 | switch (Kind) { |
| 7285 | case TargetInfo::CharPtrBuiltinVaList: |
| 7286 | return CreateCharPtrBuiltinVaListDecl(Context); |
| 7287 | case TargetInfo::VoidPtrBuiltinVaList: |
| 7288 | return CreateVoidPtrBuiltinVaListDecl(Context); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7289 | case TargetInfo::AArch64ABIBuiltinVaList: |
| 7290 | return CreateAArch64ABIBuiltinVaListDecl(Context); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7291 | case TargetInfo::PowerABIBuiltinVaList: |
| 7292 | return CreatePowerABIBuiltinVaListDecl(Context); |
| 7293 | case TargetInfo::X86_64ABIBuiltinVaList: |
| 7294 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
| 7295 | case TargetInfo::PNaClABIBuiltinVaList: |
| 7296 | return CreatePNaClABIBuiltinVaListDecl(Context); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7297 | case TargetInfo::AAPCSABIBuiltinVaList: |
| 7298 | return CreateAAPCSABIBuiltinVaListDecl(Context); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7299 | case TargetInfo::SystemZBuiltinVaList: |
| 7300 | return CreateSystemZBuiltinVaListDecl(Context); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7301 | } |
| 7302 | |
| 7303 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
| 7304 | } |
| 7305 | |
| 7306 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7307 | if (!BuiltinVaListDecl) { |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7308 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7309 | assert(BuiltinVaListDecl->isImplicit()); |
| 7310 | } |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7311 | |
| 7312 | return BuiltinVaListDecl; |
| 7313 | } |
| 7314 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7315 | Decl *ASTContext::getVaListTagDecl() const { |
| 7316 | // Force the creation of VaListTagDecl by building the __builtin_va_list |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 7317 | // declaration. |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7318 | if (!VaListTagDecl) |
| 7319 | (void)getBuiltinVaListDecl(); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 7320 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7321 | return VaListTagDecl; |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 7322 | } |
| 7323 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 7324 | TypedefDecl *ASTContext::getBuiltinMSVaListDecl() const { |
| 7325 | if (!BuiltinMSVaListDecl) |
| 7326 | BuiltinMSVaListDecl = CreateMSVaListDecl(this); |
| 7327 | |
| 7328 | return BuiltinMSVaListDecl; |
| 7329 | } |
| 7330 | |
Erich Keane | 41af971 | 2018-04-16 21:30:08 +0000 | [diff] [blame] | 7331 | bool ASTContext::canBuiltinBeRedeclared(const FunctionDecl *FD) const { |
| 7332 | return BuiltinInfo.canBeRedeclared(FD->getBuiltinID()); |
| 7333 | } |
| 7334 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 7335 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7336 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 7337 | "'NSConstantString' type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7338 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 7339 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 7340 | } |
| 7341 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7342 | /// Retrieve the template name that corresponds to a non-empty |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7343 | /// lookup. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7344 | TemplateName |
| 7345 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 7346 | UnresolvedSetIterator End) const { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7347 | unsigned size = End - Begin; |
| 7348 | assert(size > 1 && "set is not overloaded!"); |
| 7349 | |
| 7350 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 7351 | size * sizeof(FunctionTemplateDecl*)); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7352 | auto *OT = new (memory) OverloadedTemplateStorage(size); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7353 | |
| 7354 | NamedDecl **Storage = OT->getStorage(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 7355 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7356 | NamedDecl *D = *I; |
| 7357 | assert(isa<FunctionTemplateDecl>(D) || |
Richard Smith | ef53a3e | 2018-06-06 16:36:56 +0000 | [diff] [blame] | 7358 | isa<UnresolvedUsingValueDecl>(D) || |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7359 | (isa<UsingShadowDecl>(D) && |
| 7360 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 7361 | *Storage++ = D; |
| 7362 | } |
| 7363 | |
| 7364 | return TemplateName(OT); |
| 7365 | } |
| 7366 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7367 | /// Retrieve the template name that represents a qualified |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7368 | /// template name such as \c std::vector. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7369 | TemplateName |
| 7370 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 7371 | bool TemplateKeyword, |
| 7372 | TemplateDecl *Template) const { |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 7373 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
| 7374 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7375 | // FIXME: Canonicalization? |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7376 | llvm::FoldingSetNodeID ID; |
| 7377 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 7378 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7379 | void *InsertPos = nullptr; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7380 | QualifiedTemplateName *QTN = |
| 7381 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7382 | if (!QTN) { |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7383 | QTN = new (*this, alignof(QualifiedTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7384 | QualifiedTemplateName(NNS, TemplateKeyword, Template); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7385 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 7386 | } |
| 7387 | |
| 7388 | return TemplateName(QTN); |
| 7389 | } |
| 7390 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7391 | /// Retrieve the template name that represents a dependent |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7392 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7393 | TemplateName |
| 7394 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 7395 | const IdentifierInfo *Name) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7396 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 7397 | "Nested name specifier must be dependent"); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7398 | |
| 7399 | llvm::FoldingSetNodeID ID; |
| 7400 | DependentTemplateName::Profile(ID, NNS, Name); |
| 7401 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7402 | void *InsertPos = nullptr; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7403 | DependentTemplateName *QTN = |
| 7404 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7405 | |
| 7406 | if (QTN) |
| 7407 | return TemplateName(QTN); |
| 7408 | |
| 7409 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 7410 | if (CanonNNS == NNS) { |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7411 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7412 | DependentTemplateName(NNS, Name); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7413 | } else { |
| 7414 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7415 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7416 | DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7417 | DependentTemplateName *CheckQTN = |
| 7418 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7419 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 7420 | (void)CheckQTN; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7421 | } |
| 7422 | |
| 7423 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 7424 | return TemplateName(QTN); |
| 7425 | } |
| 7426 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7427 | /// Retrieve the template name that represents a dependent |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7428 | /// template name such as \c MetaFun::template operator+. |
| 7429 | TemplateName |
| 7430 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7431 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7432 | assert((!NNS || NNS->isDependent()) && |
| 7433 | "Nested name specifier must be dependent"); |
| 7434 | |
| 7435 | llvm::FoldingSetNodeID ID; |
| 7436 | DependentTemplateName::Profile(ID, NNS, Operator); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7437 | |
| 7438 | void *InsertPos = nullptr; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7439 | DependentTemplateName *QTN |
| 7440 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7441 | |
| 7442 | if (QTN) |
| 7443 | return TemplateName(QTN); |
| 7444 | |
| 7445 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 7446 | if (CanonNNS == NNS) { |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7447 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7448 | DependentTemplateName(NNS, Operator); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7449 | } else { |
| 7450 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7451 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7452 | DependentTemplateName(NNS, Operator, Canon); |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7453 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7454 | DependentTemplateName *CheckQTN |
| 7455 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7456 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 7457 | (void)CheckQTN; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7458 | } |
| 7459 | |
| 7460 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 7461 | return TemplateName(QTN); |
| 7462 | } |
| 7463 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7464 | TemplateName |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 7465 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 7466 | TemplateName replacement) const { |
| 7467 | llvm::FoldingSetNodeID ID; |
| 7468 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7469 | |
| 7470 | void *insertPos = nullptr; |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 7471 | SubstTemplateTemplateParmStorage *subst |
| 7472 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
| 7473 | |
| 7474 | if (!subst) { |
| 7475 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 7476 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 7477 | } |
| 7478 | |
| 7479 | return TemplateName(subst); |
| 7480 | } |
| 7481 | |
| 7482 | TemplateName |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7483 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 7484 | const TemplateArgument &ArgPack) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7485 | auto &Self = const_cast<ASTContext &>(*this); |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7486 | llvm::FoldingSetNodeID ID; |
| 7487 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7488 | |
| 7489 | void *InsertPos = nullptr; |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7490 | SubstTemplateTemplateParmPackStorage *Subst |
| 7491 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
| 7492 | |
| 7493 | if (!Subst) { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 7494 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7495 | ArgPack.pack_size(), |
| 7496 | ArgPack.pack_begin()); |
| 7497 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 7498 | } |
| 7499 | |
| 7500 | return TemplateName(Subst); |
| 7501 | } |
| 7502 | |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7503 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | ab13857 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 7504 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 7505 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 7506 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7507 | switch (Type) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7508 | case TargetInfo::NoInt: return {}; |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 7509 | case TargetInfo::SignedChar: return SignedCharTy; |
| 7510 | case TargetInfo::UnsignedChar: return UnsignedCharTy; |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7511 | case TargetInfo::SignedShort: return ShortTy; |
| 7512 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 7513 | case TargetInfo::SignedInt: return IntTy; |
| 7514 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 7515 | case TargetInfo::SignedLong: return LongTy; |
| 7516 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 7517 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 7518 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 7519 | } |
| 7520 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7521 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7522 | } |
Ted Kremenek | 77c51b2 | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 7523 | |
| 7524 | //===----------------------------------------------------------------------===// |
| 7525 | // Type Predicates. |
| 7526 | //===----------------------------------------------------------------------===// |
| 7527 | |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 7528 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 7529 | /// garbage collection attribute. |
| 7530 | /// |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7531 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7532 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7533 | return Qualifiers::GCNone; |
| 7534 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7535 | assert(getLangOpts().ObjC1); |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7536 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 7537 | |
| 7538 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 7539 | // (or pointers to them) be treated as though they were declared |
| 7540 | // as __strong. |
| 7541 | if (GCAttrs == Qualifiers::GCNone) { |
| 7542 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 7543 | return Qualifiers::Strong; |
| 7544 | else if (Ty->isPointerType()) |
| 7545 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 7546 | } else { |
| 7547 | // It's not valid to set GC attributes on anything that isn't a |
| 7548 | // pointer. |
| 7549 | #ifndef NDEBUG |
| 7550 | QualType CT = Ty->getCanonicalTypeInternal(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7551 | while (const auto *AT = dyn_cast<ArrayType>(CT)) |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7552 | CT = AT->getElementType(); |
| 7553 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 7554 | #endif |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 7555 | } |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 7556 | return GCAttrs; |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 7557 | } |
| 7558 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7559 | //===----------------------------------------------------------------------===// |
| 7560 | // Type Compatibility Testing |
| 7561 | //===----------------------------------------------------------------------===// |
Chris Lattner | b338a6b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 7562 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7563 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7564 | /// compatible. |
| 7565 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 7566 | const VectorType *RHS) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 7567 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7568 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7569 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7570 | } |
| 7571 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7572 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 7573 | QualType SecondVec) { |
| 7574 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 7575 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 7576 | |
| 7577 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 7578 | return true; |
| 7579 | |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 7580 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 7581 | // equivalent GCC vector types. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7582 | const auto *First = FirstVec->getAs<VectorType>(); |
| 7583 | const auto *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 7584 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7585 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 7586 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 7587 | First->getVectorKind() != VectorType::AltiVecBool && |
| 7588 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 7589 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7590 | return true; |
| 7591 | |
| 7592 | return false; |
| 7593 | } |
| 7594 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7595 | //===----------------------------------------------------------------------===// |
| 7596 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 7597 | //===----------------------------------------------------------------------===// |
| 7598 | |
| 7599 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 7600 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7601 | bool |
| 7602 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 7603 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 7604 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7605 | return true; |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 7606 | for (auto *PI : rProto->protocols()) |
| 7607 | if (ProtocolCompatibleWithProtocol(lProto, PI)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7608 | return true; |
| 7609 | return false; |
| 7610 | } |
| 7611 | |
Dmitri Gribenko | 4364fcf | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 7612 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and |
| 7613 | /// Class<pr1, ...>. |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 7614 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
| 7615 | QualType rhs) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7616 | const auto *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 7617 | const auto *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 7618 | assert((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 7619 | |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7620 | for (auto *lhsProto : lhsQID->quals()) { |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 7621 | bool match = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7622 | for (auto *rhsProto : rhsOPT->quals()) { |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 7623 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 7624 | match = true; |
| 7625 | break; |
| 7626 | } |
| 7627 | } |
| 7628 | if (!match) |
| 7629 | return false; |
| 7630 | } |
| 7631 | return true; |
| 7632 | } |
| 7633 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7634 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 7635 | /// ObjCQualifiedIDType. |
| 7636 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 7637 | bool compare) { |
| 7638 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7639 | if (lhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7640 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 7641 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7642 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7643 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 7644 | return true; |
| 7645 | |
| 7646 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7647 | const auto *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7648 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7649 | if (!rhsOPT) return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7650 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7651 | if (rhsOPT->qual_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7652 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7653 | // make sure we check the class hierarchy. |
| 7654 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7655 | for (auto *I : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7656 | // when comparing an id<P> on lhs with a static type on rhs, |
| 7657 | // see if static class implements all of id's protocols, directly or |
| 7658 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7659 | if (!rhsID->ClassImplementsProtocol(I, true)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7660 | return false; |
| 7661 | } |
| 7662 | } |
| 7663 | // If there are no qualifiers and no interface, we have an 'id'. |
| 7664 | return true; |
| 7665 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7666 | // Both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7667 | for (auto *lhsProto : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7668 | bool match = false; |
| 7669 | |
| 7670 | // when comparing an id<P> on lhs with a static type on rhs, |
| 7671 | // see if static class implements all of id's protocols, directly or |
| 7672 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7673 | for (auto *rhsProto : rhsOPT->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7674 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 7675 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 7676 | match = true; |
| 7677 | break; |
| 7678 | } |
| 7679 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7680 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7681 | // make sure we check the class hierarchy. |
| 7682 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7683 | for (auto *I : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7684 | // when comparing an id<P> on lhs with a static type on rhs, |
| 7685 | // see if static class implements all of id's protocols, directly or |
| 7686 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7687 | if (rhsID->ClassImplementsProtocol(I, true)) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7688 | match = true; |
| 7689 | break; |
| 7690 | } |
| 7691 | } |
| 7692 | } |
| 7693 | if (!match) |
| 7694 | return false; |
| 7695 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7696 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7697 | return true; |
| 7698 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7699 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7700 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 7701 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 7702 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7703 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7704 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7705 | // If both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7706 | for (auto *lhsProto : lhsOPT->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7707 | bool match = false; |
| 7708 | |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7709 | // 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] | 7710 | // see if static class implements all of id's protocols, directly or |
| 7711 | // through its super class and categories. |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7712 | // First, lhs protocols in the qualifier list must be found, direct |
| 7713 | // or indirect in rhs's qualifier list or it is a mismatch. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7714 | for (auto *rhsProto : rhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7715 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 7716 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 7717 | match = true; |
| 7718 | break; |
| 7719 | } |
| 7720 | } |
| 7721 | if (!match) |
| 7722 | return false; |
| 7723 | } |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7724 | |
| 7725 | // Static class's protocols, or its super class or category protocols |
| 7726 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 7727 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 7728 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 7729 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 7730 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 7731 | // no type qualifier and its class has no static protocol(s) |
| 7732 | // assume that it is mismatch. |
| 7733 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 7734 | return false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7735 | for (auto *lhsProto : LHSInheritedProtocols) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7736 | bool match = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7737 | for (auto *rhsProto : rhsQID->quals()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7738 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 7739 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 7740 | match = true; |
| 7741 | break; |
| 7742 | } |
| 7743 | } |
| 7744 | if (!match) |
| 7745 | return false; |
| 7746 | } |
| 7747 | } |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7748 | return true; |
| 7749 | } |
| 7750 | return false; |
| 7751 | } |
| 7752 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 7753 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7754 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 7755 | /// protocol qualifiers on the LHS or RHS. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7756 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 7757 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7758 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 7759 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 7760 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 7761 | // 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] | 7762 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 7763 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7764 | return true; |
| 7765 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7766 | // Function object that propagates a successful result or handles |
| 7767 | // __kindof types. |
| 7768 | auto finish = [&](bool succeeded) -> bool { |
| 7769 | if (succeeded) |
| 7770 | return true; |
| 7771 | |
| 7772 | if (!RHS->isKindOfType()) |
| 7773 | return false; |
| 7774 | |
| 7775 | // Strip off __kindof and protocol qualifiers, then check whether |
| 7776 | // we can assign the other way. |
| 7777 | return canAssignObjCInterfaces(RHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 7778 | LHSOPT->stripObjCKindOfTypeAndQuals(*this)); |
| 7779 | }; |
| 7780 | |
| 7781 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) { |
| 7782 | return finish(ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 7783 | QualType(RHSOPT,0), |
| 7784 | false)); |
| 7785 | } |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 7786 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7787 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) { |
| 7788 | return finish(ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 7789 | QualType(RHSOPT,0))); |
| 7790 | } |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 7791 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7792 | // If we have 2 user-defined types, fall into that path. |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7793 | if (LHS->getInterface() && RHS->getInterface()) { |
| 7794 | return finish(canAssignObjCInterfaces(LHS, RHS)); |
| 7795 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7796 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7797 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7798 | } |
| 7799 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7800 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 7801 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7802 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 7803 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 7804 | /// not OK. For the return type, the opposite is not OK. |
| 7805 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 7806 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 7807 | const ObjCObjectPointerType *RHSOPT, |
| 7808 | bool BlockReturnType) { |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7809 | |
| 7810 | // Function object that propagates a successful result or handles |
| 7811 | // __kindof types. |
| 7812 | auto finish = [&](bool succeeded) -> bool { |
| 7813 | if (succeeded) |
| 7814 | return true; |
| 7815 | |
| 7816 | const ObjCObjectPointerType *Expected = BlockReturnType ? RHSOPT : LHSOPT; |
| 7817 | if (!Expected->isKindOfType()) |
| 7818 | return false; |
| 7819 | |
| 7820 | // Strip off __kindof and protocol qualifiers, then check whether |
| 7821 | // we can assign the other way. |
| 7822 | return canAssignObjCInterfacesInBlockPointer( |
| 7823 | RHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 7824 | LHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 7825 | BlockReturnType); |
| 7826 | }; |
| 7827 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 7828 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7829 | return true; |
| 7830 | |
| 7831 | if (LHSOPT->isObjCBuiltinType()) { |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7832 | return finish(RHSOPT->isObjCBuiltinType() || |
| 7833 | RHSOPT->isObjCQualifiedIdType()); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7834 | } |
| 7835 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 7836 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7837 | return finish(ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 7838 | QualType(RHSOPT,0), |
| 7839 | false)); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7840 | |
| 7841 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 7842 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 7843 | if (LHS && RHS) { // We have 2 user-defined types. |
| 7844 | if (LHS != RHS) { |
| 7845 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7846 | return finish(BlockReturnType); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7847 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7848 | return finish(!BlockReturnType); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 7849 | } |
| 7850 | else |
| 7851 | return true; |
| 7852 | } |
| 7853 | return false; |
| 7854 | } |
| 7855 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7856 | /// Comparison routine for Objective-C protocols to be used with |
| 7857 | /// llvm::array_pod_sort. |
| 7858 | static int compareObjCProtocolsByName(ObjCProtocolDecl * const *lhs, |
| 7859 | ObjCProtocolDecl * const *rhs) { |
| 7860 | return (*lhs)->getName().compare((*rhs)->getName()); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7861 | } |
| 7862 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7863 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7864 | /// of protocols inherited from two distinct objective-c pointer objects with |
| 7865 | /// the given common base. |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7866 | /// It is used to build composite qualifier list of the composite type of |
| 7867 | /// the conditional expression involving two objective-c pointer objects. |
| 7868 | static |
| 7869 | void getIntersectionOfProtocols(ASTContext &Context, |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7870 | const ObjCInterfaceDecl *CommonBase, |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7871 | const ObjCObjectPointerType *LHSOPT, |
| 7872 | const ObjCObjectPointerType *RHSOPT, |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7873 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionSet) { |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7874 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7875 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 7876 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 7877 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 7878 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7879 | |
| 7880 | // Add all of the protocols for the LHS. |
| 7881 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSProtocolSet; |
| 7882 | |
| 7883 | // Start with the protocol qualifiers. |
| 7884 | for (auto proto : LHS->quals()) { |
| 7885 | Context.CollectInheritedProtocols(proto, LHSProtocolSet); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7886 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7887 | |
| 7888 | // Also add the protocols associated with the LHS interface. |
| 7889 | Context.CollectInheritedProtocols(LHS->getInterface(), LHSProtocolSet); |
| 7890 | |
| 7891 | // Add all of the protocls for the RHS. |
| 7892 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSProtocolSet; |
| 7893 | |
| 7894 | // Start with the protocol qualifiers. |
| 7895 | for (auto proto : RHS->quals()) { |
| 7896 | Context.CollectInheritedProtocols(proto, RHSProtocolSet); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7897 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7898 | |
| 7899 | // Also add the protocols associated with the RHS interface. |
| 7900 | Context.CollectInheritedProtocols(RHS->getInterface(), RHSProtocolSet); |
| 7901 | |
| 7902 | // Compute the intersection of the collected protocol sets. |
| 7903 | for (auto proto : LHSProtocolSet) { |
| 7904 | if (RHSProtocolSet.count(proto)) |
| 7905 | IntersectionSet.push_back(proto); |
| 7906 | } |
| 7907 | |
| 7908 | // Compute the set of protocols that is implied by either the common type or |
| 7909 | // the protocols within the intersection. |
| 7910 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ImpliedProtocols; |
| 7911 | Context.CollectInheritedProtocols(CommonBase, ImpliedProtocols); |
| 7912 | |
| 7913 | // Remove any implied protocols from the list of inherited protocols. |
| 7914 | if (!ImpliedProtocols.empty()) { |
| 7915 | IntersectionSet.erase( |
| 7916 | std::remove_if(IntersectionSet.begin(), |
| 7917 | IntersectionSet.end(), |
| 7918 | [&](ObjCProtocolDecl *proto) -> bool { |
| 7919 | return ImpliedProtocols.count(proto) > 0; |
| 7920 | }), |
| 7921 | IntersectionSet.end()); |
| 7922 | } |
| 7923 | |
| 7924 | // Sort the remaining protocols by name. |
| 7925 | llvm::array_pod_sort(IntersectionSet.begin(), IntersectionSet.end(), |
| 7926 | compareObjCProtocolsByName); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 7927 | } |
| 7928 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7929 | /// Determine whether the first type is a subtype of the second. |
| 7930 | static bool canAssignObjCObjectTypes(ASTContext &ctx, QualType lhs, |
| 7931 | QualType rhs) { |
| 7932 | // Common case: two object pointers. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7933 | const auto *lhsOPT = lhs->getAs<ObjCObjectPointerType>(); |
| 7934 | const auto *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7935 | if (lhsOPT && rhsOPT) |
| 7936 | return ctx.canAssignObjCInterfaces(lhsOPT, rhsOPT); |
| 7937 | |
| 7938 | // Two block pointers. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7939 | const auto *lhsBlock = lhs->getAs<BlockPointerType>(); |
| 7940 | const auto *rhsBlock = rhs->getAs<BlockPointerType>(); |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7941 | if (lhsBlock && rhsBlock) |
| 7942 | return ctx.typesAreBlockPointerCompatible(lhs, rhs); |
| 7943 | |
| 7944 | // If either is an unqualified 'id' and the other is a block, it's |
| 7945 | // acceptable. |
| 7946 | if ((lhsOPT && lhsOPT->isObjCIdType() && rhsBlock) || |
| 7947 | (rhsOPT && rhsOPT->isObjCIdType() && lhsBlock)) |
| 7948 | return true; |
| 7949 | |
| 7950 | return false; |
| 7951 | } |
| 7952 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7953 | // Check that the given Objective-C type argument lists are equivalent. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7954 | static bool sameObjCTypeArgs(ASTContext &ctx, |
| 7955 | const ObjCInterfaceDecl *iface, |
| 7956 | ArrayRef<QualType> lhsArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7957 | ArrayRef<QualType> rhsArgs, |
| 7958 | bool stripKindOf) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7959 | if (lhsArgs.size() != rhsArgs.size()) |
| 7960 | return false; |
| 7961 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7962 | ObjCTypeParamList *typeParams = iface->getTypeParamList(); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7963 | for (unsigned i = 0, n = lhsArgs.size(); i != n; ++i) { |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7964 | if (ctx.hasSameType(lhsArgs[i], rhsArgs[i])) |
| 7965 | continue; |
| 7966 | |
| 7967 | switch (typeParams->begin()[i]->getVariance()) { |
| 7968 | case ObjCTypeParamVariance::Invariant: |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7969 | if (!stripKindOf || |
| 7970 | !ctx.hasSameType(lhsArgs[i].stripObjCKindOfType(ctx), |
| 7971 | rhsArgs[i].stripObjCKindOfType(ctx))) { |
| 7972 | return false; |
| 7973 | } |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 7974 | break; |
| 7975 | |
| 7976 | case ObjCTypeParamVariance::Covariant: |
| 7977 | if (!canAssignObjCObjectTypes(ctx, lhsArgs[i], rhsArgs[i])) |
| 7978 | return false; |
| 7979 | break; |
| 7980 | |
| 7981 | case ObjCTypeParamVariance::Contravariant: |
| 7982 | if (!canAssignObjCObjectTypes(ctx, rhsArgs[i], lhsArgs[i])) |
| 7983 | return false; |
| 7984 | break; |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 7985 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7986 | } |
| 7987 | |
| 7988 | return true; |
| 7989 | } |
| 7990 | |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 7991 | QualType ASTContext::areCommonBaseCompatible( |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7992 | const ObjCObjectPointerType *Lptr, |
| 7993 | const ObjCObjectPointerType *Rptr) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7994 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 7995 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 7996 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 7997 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7998 | |
| 7999 | if (!LDecl || !RDecl) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8000 | return {}; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8001 | |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 8002 | // When either LHS or RHS is a kindof type, we should return a kindof type. |
| 8003 | // For example, for common base of kindof(ASub1) and kindof(ASub2), we return |
| 8004 | // kindof(A). |
| 8005 | bool anyKindOf = LHS->isKindOfType() || RHS->isKindOfType(); |
| 8006 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8007 | // Follow the left-hand side up the class hierarchy until we either hit a |
| 8008 | // root or find the RHS. Record the ancestors in case we don't find it. |
| 8009 | llvm::SmallDenseMap<const ObjCInterfaceDecl *, const ObjCObjectType *, 4> |
| 8010 | LHSAncestors; |
| 8011 | while (true) { |
| 8012 | // Record this ancestor. We'll need this if the common type isn't in the |
| 8013 | // path from the LHS to the root. |
| 8014 | LHSAncestors[LHS->getInterface()->getCanonicalDecl()] = LHS; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8015 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8016 | if (declaresSameEntity(LHS->getInterface(), RDecl)) { |
| 8017 | // Get the type arguments. |
| 8018 | ArrayRef<QualType> LHSTypeArgs = LHS->getTypeArgsAsWritten(); |
| 8019 | bool anyChanges = false; |
| 8020 | if (LHS->isSpecialized() && RHS->isSpecialized()) { |
| 8021 | // Both have type arguments, compare them. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8022 | if (!sameObjCTypeArgs(*this, LHS->getInterface(), |
| 8023 | LHS->getTypeArgs(), RHS->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8024 | /*stripKindOf=*/true)) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8025 | return {}; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8026 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { |
| 8027 | // If only one has type arguments, the result will not have type |
| 8028 | // arguments. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 8029 | LHSTypeArgs = {}; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8030 | anyChanges = true; |
| 8031 | } |
| 8032 | |
| 8033 | // Compute the intersection of protocols. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8034 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8035 | getIntersectionOfProtocols(*this, LHS->getInterface(), Lptr, Rptr, |
| 8036 | Protocols); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8037 | if (!Protocols.empty()) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8038 | anyChanges = true; |
| 8039 | |
| 8040 | // If anything in the LHS will have changed, build a new result type. |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 8041 | // If we need to return a kindof type but LHS is not a kindof type, we |
| 8042 | // build a new result type. |
| 8043 | if (anyChanges || LHS->isKindOfType() != anyKindOf) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8044 | QualType Result = getObjCInterfaceType(LHS->getInterface()); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8045 | Result = getObjCObjectType(Result, LHSTypeArgs, Protocols, |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 8046 | anyKindOf || LHS->isKindOfType()); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8047 | return getObjCObjectPointerType(Result); |
| 8048 | } |
| 8049 | |
| 8050 | return getObjCObjectPointerType(QualType(LHS, 0)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8051 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8052 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8053 | // Find the superclass. |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8054 | QualType LHSSuperType = LHS->getSuperClassType(); |
| 8055 | if (LHSSuperType.isNull()) |
| 8056 | break; |
| 8057 | |
| 8058 | LHS = LHSSuperType->castAs<ObjCObjectType>(); |
| 8059 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8060 | |
| 8061 | // We didn't find anything by following the LHS to its root; now check |
| 8062 | // the RHS against the cached set of ancestors. |
| 8063 | while (true) { |
| 8064 | auto KnownLHS = LHSAncestors.find(RHS->getInterface()->getCanonicalDecl()); |
| 8065 | if (KnownLHS != LHSAncestors.end()) { |
| 8066 | LHS = KnownLHS->second; |
| 8067 | |
| 8068 | // Get the type arguments. |
| 8069 | ArrayRef<QualType> RHSTypeArgs = RHS->getTypeArgsAsWritten(); |
| 8070 | bool anyChanges = false; |
| 8071 | if (LHS->isSpecialized() && RHS->isSpecialized()) { |
| 8072 | // Both have type arguments, compare them. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8073 | if (!sameObjCTypeArgs(*this, LHS->getInterface(), |
| 8074 | LHS->getTypeArgs(), RHS->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8075 | /*stripKindOf=*/true)) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8076 | return {}; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8077 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { |
| 8078 | // If only one has type arguments, the result will not have type |
| 8079 | // arguments. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 8080 | RHSTypeArgs = {}; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8081 | anyChanges = true; |
| 8082 | } |
| 8083 | |
| 8084 | // Compute the intersection of protocols. |
| 8085 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 8086 | getIntersectionOfProtocols(*this, RHS->getInterface(), Lptr, Rptr, |
| 8087 | Protocols); |
| 8088 | if (!Protocols.empty()) |
| 8089 | anyChanges = true; |
| 8090 | |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 8091 | // If we need to return a kindof type but RHS is not a kindof type, we |
| 8092 | // build a new result type. |
| 8093 | if (anyChanges || RHS->isKindOfType() != anyKindOf) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8094 | QualType Result = getObjCInterfaceType(RHS->getInterface()); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8095 | Result = getObjCObjectType(Result, RHSTypeArgs, Protocols, |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 8096 | anyKindOf || RHS->isKindOfType()); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8097 | return getObjCObjectPointerType(Result); |
| 8098 | } |
| 8099 | |
| 8100 | return getObjCObjectPointerType(QualType(RHS, 0)); |
| 8101 | } |
| 8102 | |
| 8103 | // Find the superclass of the RHS. |
| 8104 | QualType RHSSuperType = RHS->getSuperClassType(); |
| 8105 | if (RHSSuperType.isNull()) |
| 8106 | break; |
| 8107 | |
| 8108 | RHS = RHSSuperType->castAs<ObjCObjectType>(); |
| 8109 | } |
| 8110 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8111 | return {}; |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 8112 | } |
| 8113 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8114 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 8115 | const ObjCObjectType *RHS) { |
| 8116 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 8117 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 8118 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 8119 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 8120 | // the LHS. |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8121 | ObjCInterfaceDecl *LHSInterface = LHS->getInterface(); |
| 8122 | bool IsSuperClass = LHSInterface->isSuperClassOf(RHS->getInterface()); |
| 8123 | if (!IsSuperClass) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 8124 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8125 | |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8126 | // If the LHS has protocol qualifiers, determine whether all of them are |
| 8127 | // satisfied by the RHS (i.e., the RHS has a superset of the protocols in the |
| 8128 | // LHS). |
| 8129 | if (LHS->getNumProtocols() > 0) { |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 8130 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 8131 | // ; i.e., SuperClass may implement at least one of the protocols |
| 8132 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 8133 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 8134 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
| 8135 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
| 8136 | // Also, if RHS has explicit quelifiers, include them for comparing with LHS's |
| 8137 | // qualifiers. |
| 8138 | for (auto *RHSPI : RHS->quals()) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8139 | CollectInheritedProtocols(RHSPI, SuperClassInheritedProtocols); |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 8140 | // If there is no protocols associated with RHS, it is not a match. |
| 8141 | if (SuperClassInheritedProtocols.empty()) |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 8142 | return false; |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 8143 | |
| 8144 | for (const auto *LHSProto : LHS->quals()) { |
| 8145 | bool SuperImplementsProtocol = false; |
| 8146 | for (auto *SuperClassProto : SuperClassInheritedProtocols) |
| 8147 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 8148 | SuperImplementsProtocol = true; |
| 8149 | break; |
| 8150 | } |
| 8151 | if (!SuperImplementsProtocol) |
| 8152 | return false; |
| 8153 | } |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 8154 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8155 | |
| 8156 | // If the LHS is specialized, we may need to check type arguments. |
| 8157 | if (LHS->isSpecialized()) { |
| 8158 | // Follow the superclass chain until we've matched the LHS class in the |
| 8159 | // hierarchy. This substitutes type arguments through. |
| 8160 | const ObjCObjectType *RHSSuper = RHS; |
| 8161 | while (!declaresSameEntity(RHSSuper->getInterface(), LHSInterface)) |
| 8162 | RHSSuper = RHSSuper->getSuperClassType()->castAs<ObjCObjectType>(); |
| 8163 | |
| 8164 | // If the RHS is specializd, compare type arguments. |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8165 | if (RHSSuper->isSpecialized() && |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8166 | !sameObjCTypeArgs(*this, LHS->getInterface(), |
| 8167 | LHS->getTypeArgs(), RHSSuper->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8168 | /*stripKindOf=*/true)) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8169 | return false; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8170 | } |
| 8171 | } |
| 8172 | |
| 8173 | return true; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 8174 | } |
| 8175 | |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 8176 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 8177 | // get the "pointed to" types |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8178 | const auto *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 8179 | const auto *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8180 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8181 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 8182 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8183 | |
| 8184 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 8185 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 8186 | } |
| 8187 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 8188 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 8189 | return canAssignObjCInterfaces( |
| 8190 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 8191 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 8192 | } |
| 8193 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8194 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 8195 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8196 | /// 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] | 8197 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8198 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 8199 | bool CompareUnqualified) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8200 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 8201 | return hasSameType(LHS, RHS); |
Joey Gouly | 5788b78 | 2016-11-18 14:10:54 +0000 | [diff] [blame] | 8202 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8203 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8204 | } |
| 8205 | |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 8206 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | c87c879 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 8207 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 8208 | } |
| 8209 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8210 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 8211 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 8212 | } |
| 8213 | |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 8214 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 8215 | /// of T is compatible with SubType, return the merged type, else return |
| 8216 | /// QualType() |
| 8217 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 8218 | bool OfBlockPointer, |
| 8219 | bool Unqualified) { |
| 8220 | if (const RecordType *UT = T->getAsUnionType()) { |
| 8221 | RecordDecl *UD = UT->getDecl(); |
| 8222 | if (UD->hasAttr<TransparentUnionAttr>()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8223 | for (const auto *I : UD->fields()) { |
| 8224 | QualType ET = I->getType().getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 8225 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 8226 | if (!MT.isNull()) |
| 8227 | return MT; |
| 8228 | } |
| 8229 | } |
| 8230 | } |
| 8231 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8232 | return {}; |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 8233 | } |
| 8234 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8235 | /// mergeFunctionParameterTypes - merge two types which appear as function |
| 8236 | /// parameter types |
| 8237 | QualType ASTContext::mergeFunctionParameterTypes(QualType lhs, QualType rhs, |
| 8238 | bool OfBlockPointer, |
| 8239 | bool Unqualified) { |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 8240 | // GNU extension: two types are compatible if they appear as a function |
| 8241 | // argument, one of the types is a transparent union type and the other |
| 8242 | // type is compatible with a union member |
| 8243 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 8244 | Unqualified); |
| 8245 | if (!lmerge.isNull()) |
| 8246 | return lmerge; |
| 8247 | |
| 8248 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 8249 | Unqualified); |
| 8250 | if (!rmerge.isNull()) |
| 8251 | return rmerge; |
| 8252 | |
| 8253 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 8254 | } |
| 8255 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8256 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8257 | bool OfBlockPointer, |
| 8258 | bool Unqualified) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8259 | const auto *lbase = lhs->getAs<FunctionType>(); |
| 8260 | const auto *rbase = rhs->getAs<FunctionType>(); |
| 8261 | const auto *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 8262 | const auto *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8263 | bool allLTypes = true; |
| 8264 | bool allRTypes = true; |
| 8265 | |
| 8266 | // Check return type |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8267 | QualType retType; |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 8268 | if (OfBlockPointer) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8269 | QualType RHS = rbase->getReturnType(); |
| 8270 | QualType LHS = lbase->getReturnType(); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 8271 | bool UnqualifiedResult = Unqualified; |
| 8272 | if (!UnqualifiedResult) |
| 8273 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 8274 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 8275 | } |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8276 | else |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8277 | retType = mergeTypes(lbase->getReturnType(), rbase->getReturnType(), false, |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8278 | Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8279 | if (retType.isNull()) |
| 8280 | return {}; |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8281 | |
| 8282 | if (Unqualified) |
| 8283 | retType = retType.getUnqualifiedType(); |
| 8284 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8285 | CanQualType LRetType = getCanonicalType(lbase->getReturnType()); |
| 8286 | CanQualType RRetType = getCanonicalType(rbase->getReturnType()); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8287 | if (Unqualified) { |
| 8288 | LRetType = LRetType.getUnqualifiedType(); |
| 8289 | RRetType = RRetType.getUnqualifiedType(); |
| 8290 | } |
| 8291 | |
| 8292 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8293 | allLTypes = false; |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8294 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8295 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8296 | |
Daniel Dunbar | edd5bae | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 8297 | // FIXME: double check this |
| 8298 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 8299 | // rbase->getRegParmAttr() != 0 && |
| 8300 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 8301 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 8302 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8303 | |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 8304 | // Compatible functions must have compatible calling conventions |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 8305 | if (lbaseInfo.getCC() != rbaseInfo.getCC()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8306 | return {}; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8307 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8308 | // Regparm is part of the calling convention. |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 8309 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8310 | return {}; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8311 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8312 | return {}; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8313 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8314 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8315 | return {}; |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 8316 | if (lbaseInfo.getNoCallerSavedRegs() != rbaseInfo.getNoCallerSavedRegs()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8317 | return {}; |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 8318 | if (lbaseInfo.getNoCfCheck() != rbaseInfo.getNoCfCheck()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8319 | return {}; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8320 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8321 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 8322 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8323 | |
Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 8324 | if (lbaseInfo.getNoReturn() != NoReturn) |
| 8325 | allLTypes = false; |
| 8326 | if (rbaseInfo.getNoReturn() != NoReturn) |
| 8327 | allRTypes = false; |
| 8328 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8329 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8330 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8331 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 8332 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 8333 | "C++ shouldn't be here"); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8334 | // Compatible functions must have the same number of parameters |
| 8335 | if (lproto->getNumParams() != rproto->getNumParams()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8336 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8337 | |
| 8338 | // Variadic and non-variadic functions aren't compatible |
| 8339 | if (lproto->isVariadic() != rproto->isVariadic()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8340 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8341 | |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 8342 | if (lproto->getTypeQuals() != rproto->getTypeQuals()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8343 | return {}; |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 8344 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8345 | SmallVector<FunctionProtoType::ExtParameterInfo, 4> newParamInfos; |
| 8346 | bool canUseLeft, canUseRight; |
| 8347 | if (!mergeExtParameterInfo(lproto, rproto, canUseLeft, canUseRight, |
| 8348 | newParamInfos)) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8349 | return {}; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8350 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8351 | if (!canUseLeft) |
| 8352 | allLTypes = false; |
| 8353 | if (!canUseRight) |
| 8354 | allRTypes = false; |
| 8355 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8356 | // Check parameter type compatibility |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8357 | SmallVector<QualType, 10> types; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8358 | for (unsigned i = 0, n = lproto->getNumParams(); i < n; i++) { |
| 8359 | QualType lParamType = lproto->getParamType(i).getUnqualifiedType(); |
| 8360 | QualType rParamType = rproto->getParamType(i).getUnqualifiedType(); |
| 8361 | QualType paramType = mergeFunctionParameterTypes( |
| 8362 | lParamType, rParamType, OfBlockPointer, Unqualified); |
| 8363 | if (paramType.isNull()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8364 | return {}; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8365 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8366 | if (Unqualified) |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8367 | paramType = paramType.getUnqualifiedType(); |
| 8368 | |
| 8369 | types.push_back(paramType); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8370 | if (Unqualified) { |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8371 | lParamType = lParamType.getUnqualifiedType(); |
| 8372 | rParamType = rParamType.getUnqualifiedType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8373 | } |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8374 | |
| 8375 | if (getCanonicalType(paramType) != getCanonicalType(lParamType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8376 | allLTypes = false; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8377 | if (getCanonicalType(paramType) != getCanonicalType(rParamType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8378 | allRTypes = false; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8379 | } |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 8380 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8381 | if (allLTypes) return lhs; |
| 8382 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8383 | |
| 8384 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 8385 | EPI.ExtInfo = einfo; |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8386 | EPI.ExtParameterInfos = |
| 8387 | newParamInfos.empty() ? nullptr : newParamInfos.data(); |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 8388 | return getFunctionType(retType, types, EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8389 | } |
| 8390 | |
| 8391 | if (lproto) allRTypes = false; |
| 8392 | if (rproto) allLTypes = false; |
| 8393 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8394 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8395 | if (proto) { |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 8396 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8397 | if (proto->isVariadic()) |
| 8398 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8399 | // Check that the types are compatible with the types that |
| 8400 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 8401 | // The only types actually affected are promotable integer |
| 8402 | // types and floats, which would be passed as a different |
| 8403 | // type depending on whether the prototype is visible. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8404 | for (unsigned i = 0, n = proto->getNumParams(); i < n; ++i) { |
| 8405 | QualType paramTy = proto->getParamType(i); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8406 | |
Eli Friedman | 448ce40 | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 8407 | // 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] | 8408 | // to pass enum values. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8409 | if (const auto *Enum = paramTy->getAs<EnumType>()) { |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8410 | paramTy = Enum->getDecl()->getIntegerType(); |
| 8411 | if (paramTy.isNull()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8412 | return {}; |
Eli Friedman | 448ce40 | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 8413 | } |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8414 | |
| 8415 | if (paramTy->isPromotableIntegerType() || |
| 8416 | getCanonicalType(paramTy).getUnqualifiedType() == FloatTy) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8417 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8418 | } |
| 8419 | |
| 8420 | if (allLTypes) return lhs; |
| 8421 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8422 | |
| 8423 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 8424 | EPI.ExtInfo = einfo; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8425 | return getFunctionType(retType, proto->getParamTypes(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8426 | } |
| 8427 | |
| 8428 | if (allLTypes) return lhs; |
| 8429 | if (allRTypes) return rhs; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8430 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8431 | } |
| 8432 | |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8433 | /// Given that we have an enum type and a non-enum type, try to merge them. |
| 8434 | static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET, |
| 8435 | QualType other, bool isBlockReturnType) { |
| 8436 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 8437 | // a signed integer type, or an unsigned integer type. |
| 8438 | // Compatibility is based on the underlying type, not the promotion |
| 8439 | // type. |
| 8440 | QualType underlyingType = ET->getDecl()->getIntegerType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8441 | if (underlyingType.isNull()) |
| 8442 | return {}; |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8443 | if (Context.hasSameType(underlyingType, other)) |
| 8444 | return other; |
| 8445 | |
| 8446 | // In block return types, we're more permissive and accept any |
| 8447 | // integral type of the same size. |
| 8448 | if (isBlockReturnType && other->isIntegerType() && |
| 8449 | Context.getTypeSize(underlyingType) == Context.getTypeSize(other)) |
| 8450 | return other; |
| 8451 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8452 | return {}; |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8453 | } |
| 8454 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8455 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8456 | bool OfBlockPointer, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 8457 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | db4e349 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 8458 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 8459 | // type is adjusted to "T" prior to any further analysis, the expression |
| 8460 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 8461 | // expression is an lvalue unless the reference is an rvalue reference and |
| 8462 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 8463 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 8464 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8465 | |
| 8466 | if (Unqualified) { |
| 8467 | LHS = LHS.getUnqualifiedType(); |
| 8468 | RHS = RHS.getUnqualifiedType(); |
| 8469 | } |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 8470 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8471 | QualType LHSCan = getCanonicalType(LHS), |
| 8472 | RHSCan = getCanonicalType(RHS); |
| 8473 | |
| 8474 | // If two types are identical, they are compatible. |
| 8475 | if (LHSCan == RHSCan) |
| 8476 | return LHS; |
| 8477 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8478 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 8479 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 8480 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8481 | if (LQuals != RQuals) { |
| 8482 | // If any of these qualifiers are different, we have a type |
| 8483 | // mismatch. |
| 8484 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8485 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
Roger Ferrer Ibanez | d93add3 | 2017-02-24 08:41:09 +0000 | [diff] [blame] | 8486 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime() || |
| 8487 | LQuals.hasUnaligned() != RQuals.hasUnaligned()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8488 | return {}; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8489 | |
| 8490 | // Exactly one GC qualifier difference is allowed: __strong is |
| 8491 | // okay if the other type has no GC qualifier but is an Objective |
| 8492 | // C object pointer (i.e. implicitly strong by default). We fix |
| 8493 | // this by pretending that the unqualified type was actually |
| 8494 | // qualified __strong. |
| 8495 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 8496 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 8497 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 8498 | |
| 8499 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8500 | return {}; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8501 | |
| 8502 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 8503 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 8504 | } |
| 8505 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 8506 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 8507 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8508 | return {}; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8509 | } |
| 8510 | |
| 8511 | // Okay, qualifiers are equal. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8512 | |
Eli Friedman | dcca633 | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 8513 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 8514 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8515 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8516 | // We want to consider the two function types to be the same for these |
| 8517 | // comparisons, just force one to the other. |
| 8518 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 8519 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 16f9096 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 8520 | |
| 8521 | // Same as above for arrays |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 8522 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 8523 | LHSClass = Type::ConstantArray; |
| 8524 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 8525 | RHSClass = Type::ConstantArray; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8526 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8527 | // ObjCInterfaces are just specialized ObjCObjects. |
| 8528 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 8529 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 8530 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 8531 | // Canonicalize ExtVector -> Vector. |
| 8532 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 8533 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8534 | |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 8535 | // If the canonical type classes don't match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8536 | if (LHSClass != RHSClass) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8537 | // Note that we only have special rules for turning block enum |
| 8538 | // returns into block int returns, not vice-versa. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8539 | if (const auto *ETy = LHS->getAs<EnumType>()) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8540 | return mergeEnumWithInteger(*this, ETy, RHS, false); |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 8541 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 8542 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8543 | return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType); |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 8544 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 8545 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 194904e | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 8546 | if (OfBlockPointer && !BlockReturnType) { |
| 8547 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 8548 | return LHS; |
| 8549 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 8550 | return RHS; |
| 8551 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 8552 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8553 | return {}; |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 8554 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8555 | |
Steve Naroff | c6edcbd | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 8556 | // The canonical type classes match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8557 | switch (LHSClass) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8558 | #define TYPE(Class, Base) |
| 8559 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 8560 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8561 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 8562 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 8563 | #include "clang/AST/TypeNodes.def" |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8564 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8565 | |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 8566 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 8567 | case Type::DeducedTemplateSpecialization: |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 8568 | case Type::LValueReference: |
| 8569 | case Type::RValueReference: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8570 | case Type::MemberPointer: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8571 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8572 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8573 | case Type::ObjCInterface: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8574 | case Type::IncompleteArray: |
| 8575 | case Type::VariableArray: |
| 8576 | case Type::FunctionProto: |
| 8577 | case Type::ExtVector: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8578 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8579 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8580 | case Type::Pointer: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8581 | { |
| 8582 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 8583 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 8584 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8585 | if (Unqualified) { |
| 8586 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 8587 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 8588 | } |
| 8589 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
| 8590 | Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8591 | if (ResultType.isNull()) |
| 8592 | return {}; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 8593 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8594 | return LHS; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 8595 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8596 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8597 | return getPointerType(ResultType); |
| 8598 | } |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 8599 | case Type::BlockPointer: |
| 8600 | { |
| 8601 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 8602 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 8603 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8604 | if (Unqualified) { |
| 8605 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 8606 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 8607 | } |
Anastasia Stulova | 81a25e35 | 2017-03-10 15:23:07 +0000 | [diff] [blame] | 8608 | if (getLangOpts().OpenCL) { |
| 8609 | Qualifiers LHSPteeQual = LHSPointee.getQualifiers(); |
| 8610 | Qualifiers RHSPteeQual = RHSPointee.getQualifiers(); |
| 8611 | // Blocks can't be an expression in a ternary operator (OpenCL v2.0 |
| 8612 | // 6.12.5) thus the following check is asymmetric. |
| 8613 | if (!LHSPteeQual.isAddressSpaceSupersetOf(RHSPteeQual)) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8614 | return {}; |
Anastasia Stulova | 81a25e35 | 2017-03-10 15:23:07 +0000 | [diff] [blame] | 8615 | LHSPteeQual.removeAddressSpace(); |
| 8616 | RHSPteeQual.removeAddressSpace(); |
| 8617 | LHSPointee = |
| 8618 | QualType(LHSPointee.getTypePtr(), LHSPteeQual.getAsOpaqueValue()); |
| 8619 | RHSPointee = |
| 8620 | QualType(RHSPointee.getTypePtr(), RHSPteeQual.getAsOpaqueValue()); |
| 8621 | } |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8622 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 8623 | Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8624 | if (ResultType.isNull()) |
| 8625 | return {}; |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 8626 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 8627 | return LHS; |
| 8628 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 8629 | return RHS; |
| 8630 | return getBlockPointerType(ResultType); |
| 8631 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 8632 | case Type::Atomic: |
| 8633 | { |
| 8634 | // Merge two pointer types, while trying to preserve typedef info |
| 8635 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 8636 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 8637 | if (Unqualified) { |
| 8638 | LHSValue = LHSValue.getUnqualifiedType(); |
| 8639 | RHSValue = RHSValue.getUnqualifiedType(); |
| 8640 | } |
| 8641 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
| 8642 | Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8643 | if (ResultType.isNull()) |
| 8644 | return {}; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 8645 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 8646 | return LHS; |
| 8647 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 8648 | return RHS; |
| 8649 | return getAtomicType(ResultType); |
| 8650 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8651 | case Type::ConstantArray: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8652 | { |
| 8653 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 8654 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 8655 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8656 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8657 | |
| 8658 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 8659 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8660 | if (Unqualified) { |
| 8661 | LHSElem = LHSElem.getUnqualifiedType(); |
| 8662 | RHSElem = RHSElem.getUnqualifiedType(); |
| 8663 | } |
| 8664 | |
| 8665 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8666 | if (ResultType.isNull()) |
| 8667 | return {}; |
Jeremy Morse | 8129c5c | 2018-06-05 09:18:26 +0000 | [diff] [blame] | 8668 | |
| 8669 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 8670 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
| 8671 | |
| 8672 | // If either side is a variable array, and both are complete, check whether |
| 8673 | // the current dimension is definite. |
| 8674 | if (LVAT || RVAT) { |
| 8675 | auto SizeFetch = [this](const VariableArrayType* VAT, |
| 8676 | const ConstantArrayType* CAT) |
| 8677 | -> std::pair<bool,llvm::APInt> { |
| 8678 | if (VAT) { |
| 8679 | llvm::APSInt TheInt; |
| 8680 | Expr *E = VAT->getSizeExpr(); |
| 8681 | if (E && E->isIntegerConstantExpr(TheInt, *this)) |
| 8682 | return std::make_pair(true, TheInt); |
| 8683 | else |
| 8684 | return std::make_pair(false, TheInt); |
| 8685 | } else if (CAT) { |
| 8686 | return std::make_pair(true, CAT->getSize()); |
| 8687 | } else { |
| 8688 | return std::make_pair(false, llvm::APInt()); |
| 8689 | } |
| 8690 | }; |
| 8691 | |
| 8692 | bool HaveLSize, HaveRSize; |
| 8693 | llvm::APInt LSize, RSize; |
| 8694 | std::tie(HaveLSize, LSize) = SizeFetch(LVAT, LCAT); |
| 8695 | std::tie(HaveRSize, RSize) = SizeFetch(RVAT, RCAT); |
| 8696 | if (HaveLSize && HaveRSize && !llvm::APInt::isSameValue(LSize, RSize)) |
| 8697 | return {}; // Definite, but unequal, array dimension |
| 8698 | } |
| 8699 | |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8700 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 8701 | return LHS; |
| 8702 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 8703 | return RHS; |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 8704 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 8705 | ArrayType::ArraySizeModifier(), 0); |
| 8706 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 8707 | ArrayType::ArraySizeModifier(), 0); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8708 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 8709 | return LHS; |
| 8710 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 8711 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8712 | if (LVAT) { |
| 8713 | // FIXME: This isn't correct! But tricky to implement because |
| 8714 | // the array's size has to be the size of LHS, but the type |
| 8715 | // has to be different. |
| 8716 | return LHS; |
| 8717 | } |
| 8718 | if (RVAT) { |
| 8719 | // FIXME: This isn't correct! But tricky to implement because |
| 8720 | // the array's size has to be the size of RHS, but the type |
| 8721 | // has to be different. |
| 8722 | return RHS; |
| 8723 | } |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 8724 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 8725 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 8726 | return getIncompleteArrayType(ResultType, |
| 8727 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8728 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8729 | case Type::FunctionNoProto: |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8730 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8731 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8732 | case Type::Enum: |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8733 | return {}; |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8734 | case Type::Builtin: |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 8735 | // Only exactly equal builtin types are compatible, which is tested above. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8736 | return {}; |
Daniel Dunbar | 804c044 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 8737 | case Type::Complex: |
| 8738 | // Distinct complex types are incompatible. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8739 | return {}; |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 8740 | case Type::Vector: |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 8741 | // FIXME: The merged type should be an ExtVector! |
John McCall | 44c064b | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 8742 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 8743 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8744 | return LHS; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8745 | return {}; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8746 | case Type::ObjCObject: { |
| 8747 | // Check if the types are assignment compatible. |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 8748 | // FIXME: This should be type compatibility, e.g. whether |
| 8749 | // "LHS x; RHS x;" at global scope is legal. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8750 | const auto *LHSIface = LHS->getAs<ObjCObjectType>(); |
| 8751 | const auto *RHSIface = RHS->getAs<ObjCObjectType>(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8752 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 7a7814c | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 8753 | return LHS; |
| 8754 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8755 | return {}; |
Cedric Venet | 4fc88b7 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 8756 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 8757 | case Type::ObjCObjectPointer: |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8758 | if (OfBlockPointer) { |
| 8759 | if (canAssignObjCInterfacesInBlockPointer( |
| 8760 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 8761 | RHS->getAs<ObjCObjectPointerType>(), |
| 8762 | BlockReturnType)) |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 8763 | return LHS; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8764 | return {}; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8765 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 8766 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 8767 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8768 | return LHS; |
| 8769 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8770 | return {}; |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 8771 | case Type::Pipe: |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 8772 | assert(LHS != RHS && |
| 8773 | "Equivalent pipe types should have already been handled!"); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8774 | return {}; |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 8775 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8776 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 8777 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 8778 | } |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 8779 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8780 | bool ASTContext::mergeExtParameterInfo( |
| 8781 | const FunctionProtoType *FirstFnType, const FunctionProtoType *SecondFnType, |
| 8782 | bool &CanUseFirst, bool &CanUseSecond, |
| 8783 | SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &NewParamInfos) { |
| 8784 | assert(NewParamInfos.empty() && "param info list not empty"); |
| 8785 | CanUseFirst = CanUseSecond = true; |
| 8786 | bool FirstHasInfo = FirstFnType->hasExtParameterInfos(); |
| 8787 | bool SecondHasInfo = SecondFnType->hasExtParameterInfos(); |
| 8788 | |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 8789 | // Fast path: if the first type doesn't have ext parameter infos, |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8790 | // we match if and only if the second type also doesn't have them. |
| 8791 | if (!FirstHasInfo && !SecondHasInfo) |
| 8792 | return true; |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 8793 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8794 | bool NeedParamInfo = false; |
| 8795 | size_t E = FirstHasInfo ? FirstFnType->getExtParameterInfos().size() |
| 8796 | : SecondFnType->getExtParameterInfos().size(); |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 8797 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8798 | for (size_t I = 0; I < E; ++I) { |
| 8799 | FunctionProtoType::ExtParameterInfo FirstParam, SecondParam; |
| 8800 | if (FirstHasInfo) |
| 8801 | FirstParam = FirstFnType->getExtParameterInfo(I); |
| 8802 | if (SecondHasInfo) |
| 8803 | SecondParam = SecondFnType->getExtParameterInfo(I); |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 8804 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8805 | // Cannot merge unless everything except the noescape flag matches. |
| 8806 | if (FirstParam.withIsNoEscape(false) != SecondParam.withIsNoEscape(false)) |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 8807 | return false; |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8808 | |
| 8809 | bool FirstNoEscape = FirstParam.isNoEscape(); |
| 8810 | bool SecondNoEscape = SecondParam.isNoEscape(); |
| 8811 | bool IsNoEscape = FirstNoEscape && SecondNoEscape; |
| 8812 | NewParamInfos.push_back(FirstParam.withIsNoEscape(IsNoEscape)); |
| 8813 | if (NewParamInfos.back().getOpaqueValue()) |
| 8814 | NeedParamInfo = true; |
| 8815 | if (FirstNoEscape != IsNoEscape) |
| 8816 | CanUseFirst = false; |
| 8817 | if (SecondNoEscape != IsNoEscape) |
| 8818 | CanUseSecond = false; |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 8819 | } |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8820 | |
| 8821 | if (!NeedParamInfo) |
| 8822 | NewParamInfos.clear(); |
| 8823 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 8824 | return true; |
| 8825 | } |
| 8826 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 8827 | void ASTContext::ResetObjCLayout(const ObjCContainerDecl *CD) { |
| 8828 | ObjCLayouts[CD] = nullptr; |
| 8829 | } |
| 8830 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 8831 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 8832 | /// 'RHS' attributes and returns the merged version; including for function |
| 8833 | /// return types. |
| 8834 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 8835 | QualType LHSCan = getCanonicalType(LHS), |
| 8836 | RHSCan = getCanonicalType(RHS); |
| 8837 | // If two types are identical, they are compatible. |
| 8838 | if (LHSCan == RHSCan) |
| 8839 | return LHS; |
| 8840 | if (RHSCan->isFunctionType()) { |
| 8841 | if (!LHSCan->isFunctionType()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8842 | return {}; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8843 | QualType OldReturnType = |
| 8844 | cast<FunctionType>(RHSCan.getTypePtr())->getReturnType(); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 8845 | QualType NewReturnType = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8846 | cast<FunctionType>(LHSCan.getTypePtr())->getReturnType(); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 8847 | QualType ResReturnType = |
| 8848 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 8849 | if (ResReturnType.isNull()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8850 | return {}; |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 8851 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 8852 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 8853 | // In either case, use OldReturnType to build the new function type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8854 | const auto *F = LHS->getAs<FunctionType>(); |
| 8855 | if (const auto *FPT = cast<FunctionProtoType>(F)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8856 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8857 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Reid Kleckner | 896b32f | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 8858 | QualType ResultType = |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8859 | getFunctionType(OldReturnType, FPT->getParamTypes(), EPI); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 8860 | return ResultType; |
| 8861 | } |
| 8862 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8863 | return {}; |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 8864 | } |
| 8865 | |
| 8866 | // If the qualifiers are different, the types can still be merged. |
| 8867 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 8868 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 8869 | if (LQuals != RQuals) { |
| 8870 | // If any of these qualifiers are different, we have a type mismatch. |
| 8871 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 8872 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8873 | return {}; |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 8874 | |
| 8875 | // Exactly one GC qualifier difference is allowed: __strong is |
| 8876 | // okay if the other type has no GC qualifier but is an Objective |
| 8877 | // C object pointer (i.e. implicitly strong by default). We fix |
| 8878 | // this by pretending that the unqualified type was actually |
| 8879 | // qualified __strong. |
| 8880 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 8881 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 8882 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 8883 | |
| 8884 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8885 | return {}; |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 8886 | |
| 8887 | if (GC_L == Qualifiers::Strong) |
| 8888 | return LHS; |
| 8889 | if (GC_R == Qualifiers::Strong) |
| 8890 | return RHS; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8891 | return {}; |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 8892 | } |
| 8893 | |
| 8894 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 8895 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 8896 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 8897 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 8898 | if (ResQT == LHSBaseQT) |
| 8899 | return LHS; |
| 8900 | if (ResQT == RHSBaseQT) |
| 8901 | return RHS; |
| 8902 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8903 | return {}; |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 8904 | } |
| 8905 | |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 8906 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 8907 | // Integer Predicates |
| 8908 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3c91971 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 8909 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8910 | unsigned ASTContext::getIntWidth(QualType T) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8911 | if (const auto *ET = T->getAs<EnumType>()) |
Eli Friedman | ee275c8 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 8912 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 8913 | if (T->isBooleanType()) |
| 8914 | return 1; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 8915 | // For builtin types, just use the standard type sizing method |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 8916 | return (unsigned)getTypeSize(T); |
| 8917 | } |
| 8918 | |
Abramo Bagnara | 1364049 | 2012-09-09 10:21:24 +0000 | [diff] [blame] | 8919 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) const { |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 8920 | assert((T->hasSignedIntegerRepresentation() || T->isSignedFixedPointType()) && |
| 8921 | "Unexpected type"); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 8922 | |
| 8923 | // Turn <4 x signed int> -> <4 x unsigned int> |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8924 | if (const auto *VTy = T->getAs<VectorType>()) |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 8925 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 8926 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 8927 | |
| 8928 | // For enums, we return the unsigned version of the base type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8929 | if (const auto *ETy = T->getAs<EnumType>()) |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 8930 | T = ETy->getDecl()->getIntegerType(); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 8931 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8932 | const auto *BTy = T->getAs<BuiltinType>(); |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 8933 | assert(BTy && "Unexpected signed integer or fixed point type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 8934 | switch (BTy->getKind()) { |
| 8935 | case BuiltinType::Char_S: |
| 8936 | case BuiltinType::SChar: |
| 8937 | return UnsignedCharTy; |
| 8938 | case BuiltinType::Short: |
| 8939 | return UnsignedShortTy; |
| 8940 | case BuiltinType::Int: |
| 8941 | return UnsignedIntTy; |
| 8942 | case BuiltinType::Long: |
| 8943 | return UnsignedLongTy; |
| 8944 | case BuiltinType::LongLong: |
| 8945 | return UnsignedLongLongTy; |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 8946 | case BuiltinType::Int128: |
| 8947 | return UnsignedInt128Ty; |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 8948 | |
| 8949 | case BuiltinType::ShortAccum: |
| 8950 | return UnsignedShortAccumTy; |
| 8951 | case BuiltinType::Accum: |
| 8952 | return UnsignedAccumTy; |
| 8953 | case BuiltinType::LongAccum: |
| 8954 | return UnsignedLongAccumTy; |
| 8955 | case BuiltinType::SatShortAccum: |
| 8956 | return SatUnsignedShortAccumTy; |
| 8957 | case BuiltinType::SatAccum: |
| 8958 | return SatUnsignedAccumTy; |
| 8959 | case BuiltinType::SatLongAccum: |
| 8960 | return SatUnsignedLongAccumTy; |
| 8961 | case BuiltinType::ShortFract: |
| 8962 | return UnsignedShortFractTy; |
| 8963 | case BuiltinType::Fract: |
| 8964 | return UnsignedFractTy; |
| 8965 | case BuiltinType::LongFract: |
| 8966 | return UnsignedLongFractTy; |
| 8967 | case BuiltinType::SatShortFract: |
| 8968 | return SatUnsignedShortFractTy; |
| 8969 | case BuiltinType::SatFract: |
| 8970 | return SatUnsignedFractTy; |
| 8971 | case BuiltinType::SatLongFract: |
| 8972 | return SatUnsignedLongFractTy; |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 8973 | default: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 8974 | llvm_unreachable("Unexpected signed integer or fixed point type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 8975 | } |
| 8976 | } |
| 8977 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 8978 | ASTMutationListener::~ASTMutationListener() = default; |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 8979 | |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 8980 | void ASTMutationListener::DeducedReturnType(const FunctionDecl *FD, |
| 8981 | QualType ReturnType) {} |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8982 | |
| 8983 | //===----------------------------------------------------------------------===// |
| 8984 | // Builtin Type Computation |
| 8985 | //===----------------------------------------------------------------------===// |
| 8986 | |
| 8987 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 8988 | /// pointer over the consumed characters. This returns the resultant type. If |
| 8989 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 8990 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 8991 | /// a vector of "i*". |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 8992 | /// |
| 8993 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 8994 | /// to be an Integer Constant Expression. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 8995 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8996 | ASTContext::GetBuiltinTypeError &Error, |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 8997 | bool &RequiresICE, |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 8998 | bool AllowTypeModifiers) { |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 8999 | // Modifiers. |
| 9000 | int HowLong = 0; |
| 9001 | bool Signed = false, Unsigned = false; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9002 | RequiresICE = false; |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9003 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9004 | // Read the prefixed modifiers first. |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9005 | bool Done = false; |
| 9006 | #ifndef NDEBUG |
| 9007 | bool IsSpecialLong = false; |
| 9008 | #endif |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9009 | while (!Done) { |
| 9010 | switch (*Str++) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9011 | default: Done = true; --Str; break; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 9012 | case 'I': |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9013 | RequiresICE = true; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 9014 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9015 | case 'S': |
| 9016 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 9017 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 9018 | Signed = true; |
| 9019 | break; |
| 9020 | case 'U': |
| 9021 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
Sean Silva | 2a99514 | 2015-01-16 21:44:26 +0000 | [diff] [blame] | 9022 | assert(!Unsigned && "Can't use 'U' modifier multiple times!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9023 | Unsigned = true; |
| 9024 | break; |
| 9025 | case 'L': |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9026 | assert(!IsSpecialLong && "Can't use 'L' with 'W' or 'N' modifiers"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9027 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 9028 | ++HowLong; |
| 9029 | break; |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9030 | case 'N': |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9031 | // 'N' behaves like 'L' for all non LP64 targets and 'int' otherwise. |
| 9032 | assert(!IsSpecialLong && "Can't use two 'N' or 'W' modifiers!"); |
| 9033 | assert(HowLong == 0 && "Can't use both 'L' and 'N' modifiers!"); |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9034 | #ifndef NDEBUG |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9035 | IsSpecialLong = true; |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9036 | #endif |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9037 | if (Context.getTargetInfo().getLongWidth() == 32) |
| 9038 | ++HowLong; |
| 9039 | break; |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 9040 | case 'W': |
| 9041 | // This modifier represents int64 type. |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9042 | assert(!IsSpecialLong && "Can't use two 'N' or 'W' modifiers!"); |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 9043 | assert(HowLong == 0 && "Can't use both 'L' and 'W' modifiers!"); |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9044 | #ifndef NDEBUG |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9045 | IsSpecialLong = true; |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9046 | #endif |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 9047 | switch (Context.getTargetInfo().getInt64Type()) { |
| 9048 | default: |
| 9049 | llvm_unreachable("Unexpected integer type"); |
| 9050 | case TargetInfo::SignedLong: |
| 9051 | HowLong = 1; |
| 9052 | break; |
| 9053 | case TargetInfo::SignedLongLong: |
| 9054 | HowLong = 2; |
| 9055 | break; |
| 9056 | } |
Duncan P. N. Exon Smith | eae8caa | 2017-06-14 21:26:31 +0000 | [diff] [blame] | 9057 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9058 | } |
| 9059 | } |
| 9060 | |
| 9061 | QualType Type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9062 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9063 | // Read the base type. |
| 9064 | switch (*Str++) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 9065 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9066 | case 'v': |
| 9067 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 9068 | "Bad modifiers used with 'v'!"); |
| 9069 | Type = Context.VoidTy; |
| 9070 | break; |
Jack Carter | 24bef98 | 2013-08-15 15:16:57 +0000 | [diff] [blame] | 9071 | case 'h': |
| 9072 | assert(HowLong == 0 && !Signed && !Unsigned && |
Sean Silva | 2a99514 | 2015-01-16 21:44:26 +0000 | [diff] [blame] | 9073 | "Bad modifiers used with 'h'!"); |
Jack Carter | 24bef98 | 2013-08-15 15:16:57 +0000 | [diff] [blame] | 9074 | Type = Context.HalfTy; |
| 9075 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9076 | case 'f': |
| 9077 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 9078 | "Bad modifiers used with 'f'!"); |
| 9079 | Type = Context.FloatTy; |
| 9080 | break; |
| 9081 | case 'd': |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9082 | assert(HowLong < 3 && !Signed && !Unsigned && |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9083 | "Bad modifiers used with 'd'!"); |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9084 | if (HowLong == 1) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9085 | Type = Context.LongDoubleTy; |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9086 | else if (HowLong == 2) |
| 9087 | Type = Context.Float128Ty; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9088 | else |
| 9089 | Type = Context.DoubleTy; |
| 9090 | break; |
| 9091 | case 's': |
| 9092 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 9093 | if (Unsigned) |
| 9094 | Type = Context.UnsignedShortTy; |
| 9095 | else |
| 9096 | Type = Context.ShortTy; |
| 9097 | break; |
| 9098 | case 'i': |
| 9099 | if (HowLong == 3) |
| 9100 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 9101 | else if (HowLong == 2) |
| 9102 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 9103 | else if (HowLong == 1) |
| 9104 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 9105 | else |
| 9106 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 9107 | break; |
| 9108 | case 'c': |
| 9109 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 9110 | if (Signed) |
| 9111 | Type = Context.SignedCharTy; |
| 9112 | else if (Unsigned) |
| 9113 | Type = Context.UnsignedCharTy; |
| 9114 | else |
| 9115 | Type = Context.CharTy; |
| 9116 | break; |
| 9117 | case 'b': // boolean |
| 9118 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 9119 | Type = Context.BoolTy; |
| 9120 | break; |
| 9121 | case 'z': // size_t. |
| 9122 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 9123 | Type = Context.getSizeType(); |
| 9124 | break; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 9125 | case 'w': // wchar_t. |
| 9126 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'w'!"); |
| 9127 | Type = Context.getWideCharType(); |
| 9128 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9129 | case 'F': |
| 9130 | Type = Context.getCFConstantStringType(); |
| 9131 | break; |
Fariborz Jahanian | d11da7e | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 9132 | case 'G': |
| 9133 | Type = Context.getObjCIdType(); |
| 9134 | break; |
| 9135 | case 'H': |
| 9136 | Type = Context.getObjCSelType(); |
| 9137 | break; |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 9138 | case 'M': |
| 9139 | Type = Context.getObjCSuperType(); |
| 9140 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9141 | case 'a': |
| 9142 | Type = Context.getBuiltinVaListType(); |
| 9143 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 9144 | break; |
| 9145 | case 'A': |
| 9146 | // This is a "reference" to a va_list; however, what exactly |
| 9147 | // this means depends on how va_list is defined. There are two |
| 9148 | // different kinds of va_list: ones passed by value, and ones |
| 9149 | // passed by reference. An example of a by-value va_list is |
| 9150 | // x86, where va_list is a char*. An example of by-ref va_list |
| 9151 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 9152 | // we want this argument to be a char*&; for x86-64, we want |
| 9153 | // it to be a __va_list_tag*. |
| 9154 | Type = Context.getBuiltinVaListType(); |
| 9155 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9156 | if (Type->isArrayType()) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9157 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9158 | else |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9159 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9160 | break; |
| 9161 | case 'V': { |
| 9162 | char *End; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9163 | unsigned NumElements = strtoul(Str, &End, 10); |
| 9164 | assert(End != Str && "Missing vector size"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9165 | Str = End; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9166 | |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9167 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
| 9168 | RequiresICE, false); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9169 | assert(!RequiresICE && "Can't require vector ICE"); |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9170 | |
| 9171 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 9172 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 9173 | VectorType::GenericVector); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9174 | break; |
| 9175 | } |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9176 | case 'E': { |
| 9177 | char *End; |
| 9178 | |
| 9179 | unsigned NumElements = strtoul(Str, &End, 10); |
| 9180 | assert(End != Str && "Missing vector size"); |
| 9181 | |
| 9182 | Str = End; |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9183 | |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9184 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9185 | false); |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9186 | Type = Context.getExtVectorType(ElementType, NumElements); |
| 9187 | break; |
| 9188 | } |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 9189 | case 'X': { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9190 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9191 | false); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9192 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 9193 | Type = Context.getComplexType(ElementType); |
| 9194 | break; |
Fariborz Jahanian | 73952fc | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 9195 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9196 | case 'Y': |
Fariborz Jahanian | 73952fc | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 9197 | Type = Context.getPointerDiffType(); |
| 9198 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 9199 | case 'P': |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 9200 | Type = Context.getFILEType(); |
| 9201 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 9202 | Error = ASTContext::GE_Missing_stdio; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9203 | return {}; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9204 | } |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 9205 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 9206 | case 'J': |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 9207 | if (Signed) |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 9208 | Type = Context.getsigjmp_bufType(); |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 9209 | else |
| 9210 | Type = Context.getjmp_bufType(); |
| 9211 | |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 9212 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 9213 | Error = ASTContext::GE_Missing_setjmp; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9214 | return {}; |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 9215 | } |
| 9216 | break; |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 9217 | case 'K': |
| 9218 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 9219 | Type = Context.getucontext_tType(); |
| 9220 | |
| 9221 | if (Type.isNull()) { |
| 9222 | Error = ASTContext::GE_Missing_ucontext; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9223 | return {}; |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 9224 | } |
| 9225 | break; |
Eli Friedman | 4e91899e | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 9226 | case 'p': |
| 9227 | Type = Context.getProcessIDType(); |
| 9228 | break; |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 9229 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9230 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9231 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 9232 | Done = !AllowTypeModifiers; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9233 | while (!Done) { |
John McCall | b8b9466 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 9234 | switch (char c = *Str++) { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9235 | default: Done = true; --Str; break; |
| 9236 | case '*': |
| 9237 | case '&': { |
| 9238 | // Both pointers and references can have their pointee types |
| 9239 | // qualified with an address space. |
| 9240 | char *End; |
| 9241 | unsigned AddrSpace = strtoul(Str, &End, 10); |
| 9242 | if (End != Str && AddrSpace != 0) { |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 9243 | Type = Context.getAddrSpaceQualType(Type, |
| 9244 | getLangASFromTargetAS(AddrSpace)); |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9245 | Str = End; |
| 9246 | } |
| 9247 | if (c == '*') |
| 9248 | Type = Context.getPointerType(Type); |
| 9249 | else |
| 9250 | Type = Context.getLValueReferenceType(Type); |
| 9251 | break; |
| 9252 | } |
| 9253 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 9254 | case 'C': |
| 9255 | Type = Type.withConst(); |
| 9256 | break; |
| 9257 | case 'D': |
| 9258 | Type = Context.getVolatileType(Type); |
| 9259 | break; |
Ted Kremenek | f2a2f5f | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 9260 | case 'R': |
| 9261 | Type = Type.withRestrict(); |
| 9262 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9263 | } |
| 9264 | } |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 9265 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9266 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 9267 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9268 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9269 | return Type; |
| 9270 | } |
| 9271 | |
| 9272 | /// GetBuiltinType - Return the type for the specified builtin. |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9273 | QualType ASTContext::GetBuiltinType(unsigned Id, |
| 9274 | GetBuiltinTypeError &Error, |
| 9275 | unsigned *IntegerConstantArgs) const { |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 9276 | const char *TypeStr = BuiltinInfo.getTypeString(Id); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9277 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9278 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9279 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9280 | bool RequiresICE = false; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9281 | Error = GE_None; |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9282 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 9283 | RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9284 | if (Error != GE_None) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9285 | return {}; |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9286 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9287 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9288 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9289 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9290 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9291 | if (Error != GE_None) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9292 | return {}; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9293 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9294 | // If this argument is required to be an IntegerConstantExpression and the |
| 9295 | // caller cares, fill in the bitmask we return. |
| 9296 | if (RequiresICE && IntegerConstantArgs) |
| 9297 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
| 9298 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9299 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 9300 | if (Ty->isArrayType()) |
| 9301 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9302 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9303 | ArgTypes.push_back(Ty); |
| 9304 | } |
| 9305 | |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 9306 | if (Id == Builtin::BI__GetExceptionInfo) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9307 | return {}; |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 9308 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9309 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 9310 | "'.' should only occur at end of builtin type list!"); |
| 9311 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 9312 | FunctionType::ExtInfo EI(CC_C); |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 9313 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 9314 | |
| 9315 | bool Variadic = (TypeStr[0] == '.'); |
| 9316 | |
Richard Smith | 836de6b | 2016-12-19 23:59:34 +0000 | [diff] [blame] | 9317 | // We really shouldn't be making a no-proto type here. |
| 9318 | if (ArgTypes.empty() && Variadic && !getLangOpts().CPlusPlus) |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 9319 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 9320 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9321 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 9322 | EPI.ExtInfo = EI; |
| 9323 | EPI.Variadic = Variadic; |
Richard Smith | 391fb86 | 2016-10-18 07:13:55 +0000 | [diff] [blame] | 9324 | if (getLangOpts().CPlusPlus && BuiltinInfo.isNoThrow(Id)) |
| 9325 | EPI.ExceptionSpec.Type = |
| 9326 | getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9327 | |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 9328 | return getFunctionType(ResType, ArgTypes, EPI); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9329 | } |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 9330 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9331 | static GVALinkage basicGVALinkageForFunction(const ASTContext &Context, |
| 9332 | const FunctionDecl *FD) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9333 | if (!FD->isExternallyVisible()) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9334 | return GVA_Internal; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9335 | |
Richard Smith | e2467b7 | 2017-11-16 23:54:56 +0000 | [diff] [blame] | 9336 | // Non-user-provided functions get emitted as weak definitions with every |
| 9337 | // use, no matter whether they've been explicitly instantiated etc. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9338 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
Richard Smith | e2467b7 | 2017-11-16 23:54:56 +0000 | [diff] [blame] | 9339 | if (!MD->isUserProvided()) |
| 9340 | return GVA_DiscardableODR; |
| 9341 | |
Yaron Keren | 4cd211b | 2017-02-22 14:32:39 +0000 | [diff] [blame] | 9342 | GVALinkage External; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9343 | switch (FD->getTemplateSpecializationKind()) { |
| 9344 | case TSK_Undeclared: |
| 9345 | case TSK_ExplicitSpecialization: |
| 9346 | External = GVA_StrongExternal; |
| 9347 | break; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9348 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9349 | case TSK_ExplicitInstantiationDefinition: |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 9350 | return GVA_StrongODR; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9351 | |
David Majnemer | c3d0733 | 2014-05-15 06:25:57 +0000 | [diff] [blame] | 9352 | // C++11 [temp.explicit]p10: |
| 9353 | // [ Note: The intent is that an inline function that is the subject of |
| 9354 | // an explicit instantiation declaration will still be implicitly |
| 9355 | // instantiated when used so that the body can be considered for |
| 9356 | // inlining, but that no out-of-line copy of the inline function would be |
| 9357 | // generated in the translation unit. -- end note ] |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9358 | case TSK_ExplicitInstantiationDeclaration: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9359 | return GVA_AvailableExternally; |
| 9360 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9361 | case TSK_ImplicitInstantiation: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9362 | External = GVA_DiscardableODR; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9363 | break; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9364 | } |
| 9365 | |
| 9366 | if (!FD->isInlined()) |
| 9367 | return External; |
David Majnemer | 62f0ffd | 2013-08-01 17:26:42 +0000 | [diff] [blame] | 9368 | |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 9369 | if ((!Context.getLangOpts().CPlusPlus && |
| 9370 | !Context.getTargetInfo().getCXXABI().isMicrosoft() && |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9371 | !FD->hasAttr<DLLExportAttr>()) || |
David Majnemer | 62f0ffd | 2013-08-01 17:26:42 +0000 | [diff] [blame] | 9372 | FD->hasAttr<GNUInlineAttr>()) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9373 | // FIXME: This doesn't match gcc's behavior for dllexport inline functions. |
| 9374 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9375 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 9376 | // externally visible. |
| 9377 | if (FD->isInlineDefinitionExternallyVisible()) |
| 9378 | return External; |
| 9379 | |
| 9380 | // C99 inline semantics, where the symbol is not externally visible. |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9381 | return GVA_AvailableExternally; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9382 | } |
| 9383 | |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 9384 | // Functions specified with extern and inline in -fms-compatibility mode |
| 9385 | // forcibly get emitted. While the body of the function cannot be later |
| 9386 | // replaced, the function definition cannot be discarded. |
David Majnemer | 7376870 | 2015-03-20 00:02:27 +0000 | [diff] [blame] | 9387 | if (FD->isMSExternInline()) |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 9388 | return GVA_StrongODR; |
| 9389 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9390 | return GVA_DiscardableODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9391 | } |
| 9392 | |
Artem Belevich | ca2b951 | 2016-05-02 20:30:03 +0000 | [diff] [blame] | 9393 | static GVALinkage adjustGVALinkageForAttributes(const ASTContext &Context, |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9394 | const Decl *D, GVALinkage L) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9395 | // See http://msdn.microsoft.com/en-us/library/xa0d9ste.aspx |
| 9396 | // dllexport/dllimport on inline functions. |
| 9397 | if (D->hasAttr<DLLImportAttr>()) { |
| 9398 | if (L == GVA_DiscardableODR || L == GVA_StrongODR) |
| 9399 | return GVA_AvailableExternally; |
Artem Belevich | ca2b951 | 2016-05-02 20:30:03 +0000 | [diff] [blame] | 9400 | } else if (D->hasAttr<DLLExportAttr>()) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9401 | if (L == GVA_DiscardableODR) |
| 9402 | return GVA_StrongODR; |
Artem Belevich | ca2b951 | 2016-05-02 20:30:03 +0000 | [diff] [blame] | 9403 | } else if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice && |
| 9404 | D->hasAttr<CUDAGlobalAttr>()) { |
| 9405 | // Device-side functions with __global__ attribute must always be |
| 9406 | // visible externally so they can be launched from host. |
| 9407 | if (L == GVA_DiscardableODR || L == GVA_Internal) |
| 9408 | return GVA_StrongODR; |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9409 | } |
| 9410 | return L; |
| 9411 | } |
| 9412 | |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9413 | /// Adjust the GVALinkage for a declaration based on what an external AST source |
| 9414 | /// knows about whether there can be other definitions of this declaration. |
| 9415 | static GVALinkage |
| 9416 | adjustGVALinkageForExternalDefinitionKind(const ASTContext &Ctx, const Decl *D, |
| 9417 | GVALinkage L) { |
| 9418 | ExternalASTSource *Source = Ctx.getExternalSource(); |
| 9419 | if (!Source) |
| 9420 | return L; |
| 9421 | |
| 9422 | switch (Source->hasExternalDefinitions(D)) { |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9423 | case ExternalASTSource::EK_Never: |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9424 | // Other translation units rely on us to provide the definition. |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9425 | if (L == GVA_DiscardableODR) |
| 9426 | return GVA_StrongODR; |
| 9427 | break; |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9428 | |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9429 | case ExternalASTSource::EK_Always: |
| 9430 | return GVA_AvailableExternally; |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9431 | |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9432 | case ExternalASTSource::EK_ReplyHazy: |
| 9433 | break; |
| 9434 | } |
| 9435 | return L; |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9436 | } |
| 9437 | |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9438 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const { |
| 9439 | return adjustGVALinkageForExternalDefinitionKind(*this, FD, |
| 9440 | adjustGVALinkageForAttributes(*this, FD, |
| 9441 | basicGVALinkageForFunction(*this, FD))); |
| 9442 | } |
| 9443 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9444 | static GVALinkage basicGVALinkageForVariable(const ASTContext &Context, |
| 9445 | const VarDecl *VD) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9446 | if (!VD->isExternallyVisible()) |
| 9447 | return GVA_Internal; |
| 9448 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9449 | if (VD->isStaticLocal()) { |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9450 | const DeclContext *LexicalContext = VD->getParentFunctionOrMethod(); |
| 9451 | while (LexicalContext && !isa<FunctionDecl>(LexicalContext)) |
| 9452 | LexicalContext = LexicalContext->getLexicalParent(); |
| 9453 | |
David Blaikie | eb21001 | 2017-01-27 23:11:10 +0000 | [diff] [blame] | 9454 | // ObjC Blocks can create local variables that don't have a FunctionDecl |
| 9455 | // LexicalContext. |
| 9456 | if (!LexicalContext) |
| 9457 | return GVA_DiscardableODR; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9458 | |
David Blaikie | eb21001 | 2017-01-27 23:11:10 +0000 | [diff] [blame] | 9459 | // Otherwise, let the static local variable inherit its linkage from the |
| 9460 | // nearest enclosing function. |
| 9461 | auto StaticLocalLinkage = |
| 9462 | Context.GetGVALinkageForFunction(cast<FunctionDecl>(LexicalContext)); |
| 9463 | |
| 9464 | // Itanium ABI 5.2.2: "Each COMDAT group [for a static local variable] must |
| 9465 | // be emitted in any object with references to the symbol for the object it |
| 9466 | // contains, whether inline or out-of-line." |
| 9467 | // Similar behavior is observed with MSVC. An alternative ABI could use |
| 9468 | // StrongODR/AvailableExternally to match the function, but none are |
| 9469 | // known/supported currently. |
| 9470 | if (StaticLocalLinkage == GVA_StrongODR || |
| 9471 | StaticLocalLinkage == GVA_AvailableExternally) |
| 9472 | return GVA_DiscardableODR; |
| 9473 | return StaticLocalLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9474 | } |
| 9475 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 9476 | // MSVC treats in-class initialized static data members as definitions. |
| 9477 | // By giving them non-strong linkage, out-of-line definitions won't |
| 9478 | // cause link errors. |
| 9479 | if (Context.isMSStaticDataMemberInlineDefinition(VD)) |
| 9480 | return GVA_DiscardableODR; |
| 9481 | |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 9482 | // Most non-template variables have strong linkage; inline variables are |
| 9483 | // linkonce_odr or (occasionally, for compatibility) weak_odr. |
| 9484 | GVALinkage StrongLinkage; |
| 9485 | switch (Context.getInlineVariableDefinitionKind(VD)) { |
| 9486 | case ASTContext::InlineVariableDefinitionKind::None: |
| 9487 | StrongLinkage = GVA_StrongExternal; |
| 9488 | break; |
| 9489 | case ASTContext::InlineVariableDefinitionKind::Weak: |
| 9490 | case ASTContext::InlineVariableDefinitionKind::WeakUnknown: |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 9491 | StrongLinkage = GVA_DiscardableODR; |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 9492 | break; |
| 9493 | case ASTContext::InlineVariableDefinitionKind::Strong: |
| 9494 | StrongLinkage = GVA_StrongODR; |
| 9495 | break; |
| 9496 | } |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 9497 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 9498 | switch (VD->getTemplateSpecializationKind()) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9499 | case TSK_Undeclared: |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 9500 | return StrongLinkage; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9501 | |
David Majnemer | 6d1780c | 2015-07-17 23:36:49 +0000 | [diff] [blame] | 9502 | case TSK_ExplicitSpecialization: |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 9503 | return Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 9504 | VD->isStaticDataMember() |
David Majnemer | 6d1780c | 2015-07-17 23:36:49 +0000 | [diff] [blame] | 9505 | ? GVA_StrongODR |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 9506 | : StrongLinkage; |
David Majnemer | 6d1780c | 2015-07-17 23:36:49 +0000 | [diff] [blame] | 9507 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9508 | case TSK_ExplicitInstantiationDefinition: |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 9509 | return GVA_StrongODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9510 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9511 | case TSK_ExplicitInstantiationDeclaration: |
| 9512 | return GVA_AvailableExternally; |
| 9513 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9514 | case TSK_ImplicitInstantiation: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9515 | return GVA_DiscardableODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9516 | } |
Rafael Espindola | 27699c8 | 2013-05-13 14:05:53 +0000 | [diff] [blame] | 9517 | |
| 9518 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9519 | } |
| 9520 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9521 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9522 | return adjustGVALinkageForExternalDefinitionKind(*this, VD, |
| 9523 | adjustGVALinkageForAttributes(*this, VD, |
| 9524 | basicGVALinkageForVariable(*this, VD))); |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9525 | } |
| 9526 | |
David Blaikie | e6b7c28 | 2017-04-11 20:46:34 +0000 | [diff] [blame] | 9527 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9528 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9529 | if (!VD->isFileVarDecl()) |
| 9530 | return false; |
Renato Golin | 9258aa5 | 2014-05-21 10:40:27 +0000 | [diff] [blame] | 9531 | // Global named register variables (GNU extension) are never emitted. |
| 9532 | if (VD->getStorageClass() == SC_Register) |
| 9533 | return false; |
Richard Smith | 7747ce2 | 2015-08-19 20:49:38 +0000 | [diff] [blame] | 9534 | if (VD->getDescribedVarTemplate() || |
| 9535 | isa<VarTemplatePartialSpecializationDecl>(VD)) |
| 9536 | return false; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9537 | } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 9538 | // We never need to emit an uninstantiated function template. |
| 9539 | if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) |
| 9540 | return false; |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 9541 | } else if (isa<PragmaCommentDecl>(D)) |
| 9542 | return true; |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9543 | else if (isa<OMPThreadPrivateDecl>(D)) |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 9544 | return true; |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 9545 | else if (isa<PragmaDetectMismatchDecl>(D)) |
| 9546 | return true; |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 9547 | else if (isa<OMPThreadPrivateDecl>(D)) |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 9548 | return !D->getDeclContext()->isDependentContext(); |
| 9549 | else if (isa<OMPDeclareReductionDecl>(D)) |
| 9550 | return !D->getDeclContext()->isDependentContext(); |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 9551 | else if (isa<ImportDecl>(D)) |
| 9552 | return true; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 9553 | else |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 9554 | return false; |
| 9555 | |
Hans Wennborg | 08c5a7b | 2018-06-25 13:23:49 +0000 | [diff] [blame^] | 9556 | if (D->isFromASTFile() && !LangOpts.BuildingPCHWithObjectFile) { |
| 9557 | assert(getExternalSource() && "It's from an AST file; must have a source."); |
| 9558 | // On Windows, PCH files are built together with an object file. If this |
| 9559 | // declaration comes from such a PCH and DeclMustBeEmitted would return |
| 9560 | // true, it would have returned true and the decl would have been emitted |
| 9561 | // into that object file, so it doesn't need to be emitted here. |
| 9562 | // Note that decls are still emitted if they're referenced, as usual; |
| 9563 | // DeclMustBeEmitted is used to decide whether a decl must be emitted even |
| 9564 | // if it's not referenced. |
| 9565 | // |
| 9566 | // Explicit template instantiation definitions are tricky. If there was an |
| 9567 | // explicit template instantiation decl in the PCH before, it will look like |
| 9568 | // the definition comes from there, even if that was just the declaration. |
| 9569 | // (Explicit instantiation defs of variable templates always get emitted.) |
| 9570 | bool IsExpInstDef = |
| 9571 | isa<FunctionDecl>(D) && |
| 9572 | cast<FunctionDecl>(D)->getTemplateSpecializationKind() == |
| 9573 | TSK_ExplicitInstantiationDefinition; |
| 9574 | |
| 9575 | if (getExternalSource()->DeclIsFromPCHWithObjectFile(D) && !IsExpInstDef) |
| 9576 | return false; |
| 9577 | } |
| 9578 | |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 9579 | // If this is a member of a class template, we do not need to emit it. |
| 9580 | if (D->getDeclContext()->isDependentContext()) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9581 | return false; |
| 9582 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 9583 | // Weak references don't produce any output by themselves. |
| 9584 | if (D->hasAttr<WeakRefAttr>()) |
| 9585 | return false; |
| 9586 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9587 | // Aliases and used decls are required. |
| 9588 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 9589 | return true; |
| 9590 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9591 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9592 | // Forward declarations aren't required. |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 9593 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 9594 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9595 | |
| 9596 | // Constructors and destructors are required. |
| 9597 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 9598 | return true; |
Hans Wennborg | 08c5a7b | 2018-06-25 13:23:49 +0000 | [diff] [blame^] | 9599 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 9600 | // The key function for a class is required. This rule only comes |
| 9601 | // into play when inline functions can be key functions, though. |
| 9602 | if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9603 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 9604 | const CXXRecordDecl *RD = MD->getParent(); |
| 9605 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 9606 | const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD); |
| 9607 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 9608 | return true; |
| 9609 | } |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9610 | } |
| 9611 | } |
| 9612 | |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9613 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 9614 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9615 | // static, static inline, always_inline, and extern inline functions can |
| 9616 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 9617 | // Implicit template instantiations can also be deferred in C++. |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9618 | return !isDiscardableGVALinkage(Linkage); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9619 | } |
Hans Wennborg | 08c5a7b | 2018-06-25 13:23:49 +0000 | [diff] [blame^] | 9620 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9621 | const auto *VD = cast<VarDecl>(D); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9622 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 9623 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 9624 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly && |
| 9625 | !isMSStaticDataMemberInlineDefinition(VD)) |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 9626 | return false; |
| 9627 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9628 | // Variables that can be needed in other TUs are required. |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9629 | auto Linkage = GetGVALinkageForVariable(VD); |
| 9630 | if (!isDiscardableGVALinkage(Linkage)) |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9631 | return true; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9632 | |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9633 | // We never need to emit a variable that is available in another TU. |
| 9634 | if (Linkage == GVA_AvailableExternally) |
| 9635 | return false; |
| 9636 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9637 | // Variables that have destruction with side-effects are required. |
| 9638 | if (VD->getType().isDestructedType()) |
| 9639 | return true; |
| 9640 | |
| 9641 | // Variables that have initialization with side-effects are required. |
Richard Smith | 7747ce2 | 2015-08-19 20:49:38 +0000 | [diff] [blame] | 9642 | if (VD->getInit() && VD->getInit()->HasSideEffects(*this) && |
Richard Smith | 187ffb4 | 2017-01-20 01:19:46 +0000 | [diff] [blame] | 9643 | // We can get a value-dependent initializer during error recovery. |
| 9644 | (VD->getInit()->isValueDependent() || !VD->evaluateValue())) |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9645 | return true; |
| 9646 | |
Richard Smith | da38363 | 2016-08-15 01:33:41 +0000 | [diff] [blame] | 9647 | // Likewise, variables with tuple-like bindings are required if their |
| 9648 | // bindings have side-effects. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9649 | if (const auto *DD = dyn_cast<DecompositionDecl>(VD)) |
| 9650 | for (const auto *BD : DD->bindings()) |
| 9651 | if (const auto *BindingVD = BD->getHoldingVar()) |
Richard Smith | da38363 | 2016-08-15 01:33:41 +0000 | [diff] [blame] | 9652 | if (DeclMustBeEmitted(BindingVD)) |
| 9653 | return true; |
| 9654 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9655 | // If the decl is marked as `declare target`, it should be emitted. |
Alexey Bataev | 92327c5 | 2018-03-26 16:40:55 +0000 | [diff] [blame] | 9656 | for (const auto *Decl : D->redecls()) { |
| 9657 | if (!Decl->hasAttrs()) |
| 9658 | continue; |
| 9659 | if (const auto *Attr = Decl->getAttr<OMPDeclareTargetDeclAttr>()) |
| 9660 | if (Attr->getMapType() != OMPDeclareTargetDeclAttr::MT_Link) |
| 9661 | return true; |
| 9662 | } |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9663 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9664 | return false; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9665 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 9666 | |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 9667 | void ASTContext::forEachMultiversionedFunctionVersion( |
| 9668 | const FunctionDecl *FD, |
| 9669 | llvm::function_ref<void(const FunctionDecl *)> Pred) const { |
| 9670 | assert(FD->isMultiVersion() && "Only valid for multiversioned functions"); |
| 9671 | llvm::SmallDenseSet<const FunctionDecl*, 4> SeenDecls; |
| 9672 | FD = FD->getCanonicalDecl(); |
| 9673 | for (auto *CurDecl : |
| 9674 | FD->getDeclContext()->getRedeclContext()->lookup(FD->getDeclName())) { |
| 9675 | FunctionDecl *CurFD = CurDecl->getAsFunction()->getCanonicalDecl(); |
| 9676 | if (CurFD && hasSameType(CurFD->getType(), FD->getType()) && |
| 9677 | std::end(SeenDecls) == llvm::find(SeenDecls, CurFD)) { |
| 9678 | SeenDecls.insert(CurFD); |
| 9679 | Pred(CurFD); |
| 9680 | } |
| 9681 | } |
| 9682 | } |
| 9683 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 9684 | CallingConv ASTContext::getDefaultCallingConvention(bool IsVariadic, |
| 9685 | bool IsCXXMethod) const { |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 9686 | // Pass through to the C++ ABI object |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 9687 | if (IsCXXMethod) |
| 9688 | return ABI->getDefaultMethodCallConv(IsVariadic); |
Timur Iskhodzhanov | c5098ad | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 9689 | |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 9690 | switch (LangOpts.getDefaultCallingConv()) { |
| 9691 | case LangOptions::DCC_None: |
| 9692 | break; |
| 9693 | case LangOptions::DCC_CDecl: |
| 9694 | return CC_C; |
| 9695 | case LangOptions::DCC_FastCall: |
Erich Keane | 5759fa7 | 2017-10-24 23:12:01 +0000 | [diff] [blame] | 9696 | if (getTargetInfo().hasFeature("sse2") && !IsVariadic) |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 9697 | return CC_X86FastCall; |
| 9698 | break; |
| 9699 | case LangOptions::DCC_StdCall: |
| 9700 | if (!IsVariadic) |
| 9701 | return CC_X86StdCall; |
| 9702 | break; |
| 9703 | case LangOptions::DCC_VectorCall: |
| 9704 | // __vectorcall cannot be applied to variadic functions. |
| 9705 | if (!IsVariadic) |
| 9706 | return CC_X86VectorCall; |
| 9707 | break; |
Erich Keane | a957ffb | 2017-11-02 21:08:00 +0000 | [diff] [blame] | 9708 | case LangOptions::DCC_RegCall: |
| 9709 | // __regcall cannot be applied to variadic functions. |
| 9710 | if (!IsVariadic) |
| 9711 | return CC_X86RegCall; |
| 9712 | break; |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 9713 | } |
Alexander Kornienko | 21de0ae | 2015-01-20 11:20:41 +0000 | [diff] [blame] | 9714 | return Target->getDefaultCallingConv(TargetInfo::CCMT_Unknown); |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 9715 | } |
| 9716 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 9717 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 9718 | // Pass through to the C++ ABI object |
| 9719 | return ABI->isNearlyEmpty(RD); |
| 9720 | } |
| 9721 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 9722 | VTableContextBase *ASTContext::getVTableContext() { |
| 9723 | if (!VTContext.get()) { |
| 9724 | if (Target->getCXXABI().isMicrosoft()) |
| 9725 | VTContext.reset(new MicrosoftVTableContext(*this)); |
| 9726 | else |
| 9727 | VTContext.reset(new ItaniumVTableContext(*this)); |
| 9728 | } |
| 9729 | return VTContext.get(); |
| 9730 | } |
| 9731 | |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 9732 | MangleContext *ASTContext::createMangleContext() { |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 9733 | switch (Target->getCXXABI().getKind()) { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 9734 | case TargetCXXABI::GenericAArch64: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 9735 | case TargetCXXABI::GenericItanium: |
| 9736 | case TargetCXXABI::GenericARM: |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 9737 | case TargetCXXABI::GenericMIPS: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 9738 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9739 | case TargetCXXABI::iOS64: |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 9740 | case TargetCXXABI::WebAssembly: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 9741 | case TargetCXXABI::WatchOS: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 9742 | return ItaniumMangleContext::create(*this, getDiagnostics()); |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 9743 | case TargetCXXABI::Microsoft: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 9744 | return MicrosoftMangleContext::create(*this, getDiagnostics()); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 9745 | } |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 9746 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 9747 | } |
| 9748 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9749 | CXXABI::~CXXABI() = default; |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 9750 | |
| 9751 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 9752 | return ASTRecordLayouts.getMemorySize() + |
| 9753 | llvm::capacity_in_bytes(ObjCLayouts) + |
| 9754 | llvm::capacity_in_bytes(KeyFunctions) + |
| 9755 | llvm::capacity_in_bytes(ObjCImpls) + |
| 9756 | llvm::capacity_in_bytes(BlockVarCopyInits) + |
| 9757 | llvm::capacity_in_bytes(DeclAttrs) + |
| 9758 | llvm::capacity_in_bytes(TemplateOrInstantiation) + |
| 9759 | llvm::capacity_in_bytes(InstantiatedFromUsingDecl) + |
| 9760 | llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) + |
| 9761 | llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) + |
| 9762 | llvm::capacity_in_bytes(OverriddenMethods) + |
| 9763 | llvm::capacity_in_bytes(Types) + |
| 9764 | llvm::capacity_in_bytes(VariableArrayTypes) + |
| 9765 | llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 9766 | } |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 9767 | |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 9768 | /// getIntTypeForBitwidth - |
| 9769 | /// sets integer QualTy according to specified details: |
| 9770 | /// bitwidth, signed/unsigned. |
| 9771 | /// Returns empty type if there is no appropriate target types. |
| 9772 | QualType ASTContext::getIntTypeForBitwidth(unsigned DestWidth, |
| 9773 | unsigned Signed) const { |
| 9774 | TargetInfo::IntType Ty = getTargetInfo().getIntTypeByWidth(DestWidth, Signed); |
| 9775 | CanQualType QualTy = getFromTargetType(Ty); |
| 9776 | if (!QualTy && DestWidth == 128) |
| 9777 | return Signed ? Int128Ty : UnsignedInt128Ty; |
| 9778 | return QualTy; |
| 9779 | } |
| 9780 | |
| 9781 | /// getRealTypeForBitwidth - |
| 9782 | /// sets floating point QualTy according to specified bitwidth. |
| 9783 | /// Returns empty type if there is no appropriate target types. |
| 9784 | QualType ASTContext::getRealTypeForBitwidth(unsigned DestWidth) const { |
| 9785 | TargetInfo::RealType Ty = getTargetInfo().getRealTypeByWidth(DestWidth); |
| 9786 | switch (Ty) { |
| 9787 | case TargetInfo::Float: |
| 9788 | return FloatTy; |
| 9789 | case TargetInfo::Double: |
| 9790 | return DoubleTy; |
| 9791 | case TargetInfo::LongDouble: |
| 9792 | return LongDoubleTy; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 9793 | case TargetInfo::Float128: |
| 9794 | return Float128Ty; |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 9795 | case TargetInfo::NoFloat: |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9796 | return {}; |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 9797 | } |
| 9798 | |
| 9799 | llvm_unreachable("Unhandled TargetInfo::RealType value"); |
| 9800 | } |
| 9801 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 9802 | void ASTContext::setManglingNumber(const NamedDecl *ND, unsigned Number) { |
| 9803 | if (Number > 1) |
| 9804 | MangleNumbers[ND] = Number; |
David Blaikie | 095deba | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 9805 | } |
| 9806 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 9807 | unsigned ASTContext::getManglingNumber(const NamedDecl *ND) const { |
Richard Smith | e9b02d6 | 2016-03-21 22:33:02 +0000 | [diff] [blame] | 9808 | auto I = MangleNumbers.find(ND); |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 9809 | return I != MangleNumbers.end() ? I->second : 1; |
David Blaikie | 095deba | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 9810 | } |
| 9811 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 9812 | void ASTContext::setStaticLocalNumber(const VarDecl *VD, unsigned Number) { |
| 9813 | if (Number > 1) |
| 9814 | StaticLocalNumbers[VD] = Number; |
| 9815 | } |
| 9816 | |
| 9817 | unsigned ASTContext::getStaticLocalNumber(const VarDecl *VD) const { |
Richard Smith | e9b02d6 | 2016-03-21 22:33:02 +0000 | [diff] [blame] | 9818 | auto I = StaticLocalNumbers.find(VD); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 9819 | return I != StaticLocalNumbers.end() ? I->second : 1; |
| 9820 | } |
| 9821 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 9822 | MangleNumberingContext & |
| 9823 | ASTContext::getManglingNumberContext(const DeclContext *DC) { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 9824 | assert(LangOpts.CPlusPlus); // We don't need mangling numbers for plain C. |
Justin Lebar | 20ebffc | 2016-10-10 16:26:19 +0000 | [diff] [blame] | 9825 | std::unique_ptr<MangleNumberingContext> &MCtx = MangleNumberingContexts[DC]; |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 9826 | if (!MCtx) |
| 9827 | MCtx = createMangleNumberingContext(); |
| 9828 | return *MCtx; |
| 9829 | } |
| 9830 | |
Justin Lebar | 20ebffc | 2016-10-10 16:26:19 +0000 | [diff] [blame] | 9831 | std::unique_ptr<MangleNumberingContext> |
| 9832 | ASTContext::createMangleNumberingContext() const { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 9833 | return ABI->createMangleNumberingContext(); |
Douglas Gregor | 6379854 | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 9834 | } |
| 9835 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 9836 | const CXXConstructorDecl * |
| 9837 | ASTContext::getCopyConstructorForExceptionObject(CXXRecordDecl *RD) { |
| 9838 | return ABI->getCopyConstructorForExceptionObject( |
| 9839 | cast<CXXRecordDecl>(RD->getFirstDecl())); |
| 9840 | } |
| 9841 | |
| 9842 | void ASTContext::addCopyConstructorForExceptionObject(CXXRecordDecl *RD, |
| 9843 | CXXConstructorDecl *CD) { |
| 9844 | return ABI->addCopyConstructorForExceptionObject( |
| 9845 | cast<CXXRecordDecl>(RD->getFirstDecl()), |
| 9846 | cast<CXXConstructorDecl>(CD->getFirstDecl())); |
| 9847 | } |
| 9848 | |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 9849 | void ASTContext::addTypedefNameForUnnamedTagDecl(TagDecl *TD, |
| 9850 | TypedefNameDecl *DD) { |
| 9851 | return ABI->addTypedefNameForUnnamedTagDecl(TD, DD); |
| 9852 | } |
| 9853 | |
| 9854 | TypedefNameDecl * |
| 9855 | ASTContext::getTypedefNameForUnnamedTagDecl(const TagDecl *TD) { |
| 9856 | return ABI->getTypedefNameForUnnamedTagDecl(TD); |
| 9857 | } |
| 9858 | |
| 9859 | void ASTContext::addDeclaratorForUnnamedTagDecl(TagDecl *TD, |
| 9860 | DeclaratorDecl *DD) { |
| 9861 | return ABI->addDeclaratorForUnnamedTagDecl(TD, DD); |
| 9862 | } |
| 9863 | |
| 9864 | DeclaratorDecl *ASTContext::getDeclaratorForUnnamedTagDecl(const TagDecl *TD) { |
| 9865 | return ABI->getDeclaratorForUnnamedTagDecl(TD); |
| 9866 | } |
| 9867 | |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 9868 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 9869 | ParamIndices[D] = index; |
| 9870 | } |
| 9871 | |
| 9872 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 9873 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
| 9874 | assert(I != ParamIndices.end() && |
| 9875 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 9876 | return I->second; |
| 9877 | } |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 9878 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 9879 | APValue * |
| 9880 | ASTContext::getMaterializedTemporaryValue(const MaterializeTemporaryExpr *E, |
| 9881 | bool MayCreate) { |
| 9882 | assert(E && E->getStorageDuration() == SD_Static && |
| 9883 | "don't need to cache the computed value for this temporary"); |
David Majnemer | 2dcef9e | 2015-08-13 23:50:15 +0000 | [diff] [blame] | 9884 | if (MayCreate) { |
| 9885 | APValue *&MTVI = MaterializedTemporaryValues[E]; |
| 9886 | if (!MTVI) |
| 9887 | MTVI = new (*this) APValue; |
| 9888 | return MTVI; |
| 9889 | } |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 9890 | |
David Majnemer | 2dcef9e | 2015-08-13 23:50:15 +0000 | [diff] [blame] | 9891 | return MaterializedTemporaryValues.lookup(E); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 9892 | } |
| 9893 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 9894 | bool ASTContext::AtomicUsesUnsupportedLibcall(const AtomicExpr *E) const { |
| 9895 | const llvm::Triple &T = getTargetInfo().getTriple(); |
| 9896 | if (!T.isOSDarwin()) |
| 9897 | return false; |
| 9898 | |
Bob Wilson | 2c82c3d | 2013-11-02 23:27:49 +0000 | [diff] [blame] | 9899 | if (!(T.isiOS() && T.isOSVersionLT(7)) && |
| 9900 | !(T.isMacOSX() && T.isOSVersionLT(10, 9))) |
| 9901 | return false; |
| 9902 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 9903 | QualType AtomicTy = E->getPtr()->getType()->getPointeeType(); |
| 9904 | CharUnits sizeChars = getTypeSizeInChars(AtomicTy); |
| 9905 | uint64_t Size = sizeChars.getQuantity(); |
| 9906 | CharUnits alignChars = getTypeAlignInChars(AtomicTy); |
| 9907 | unsigned Align = alignChars.getQuantity(); |
| 9908 | unsigned MaxInlineWidthInBits = getTargetInfo().getMaxAtomicInlineWidth(); |
| 9909 | return (Size != Align || toBits(sizeChars) > MaxInlineWidthInBits); |
| 9910 | } |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9911 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9912 | static ast_type_traits::DynTypedNode getSingleDynTypedNodeFromParentMap( |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9913 | ASTContext::ParentMapPointers::mapped_type U) { |
Benjamin Kramer | fbfa7a1 | 2015-10-22 11:26:35 +0000 | [diff] [blame] | 9914 | if (const auto *D = U.dyn_cast<const Decl *>()) |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 9915 | return ast_type_traits::DynTypedNode::create(*D); |
Benjamin Kramer | fbfa7a1 | 2015-10-22 11:26:35 +0000 | [diff] [blame] | 9916 | if (const auto *S = U.dyn_cast<const Stmt *>()) |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 9917 | return ast_type_traits::DynTypedNode::create(*S); |
Benjamin Kramer | fbfa7a1 | 2015-10-22 11:26:35 +0000 | [diff] [blame] | 9918 | return *U.get<ast_type_traits::DynTypedNode *>(); |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 9919 | } |
| 9920 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9921 | namespace { |
| 9922 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9923 | /// Template specializations to abstract away from pointers and TypeLocs. |
| 9924 | /// @{ |
| 9925 | template <typename T> |
| 9926 | ast_type_traits::DynTypedNode createDynTypedNode(const T &Node) { |
| 9927 | return ast_type_traits::DynTypedNode::create(*Node); |
| 9928 | } |
| 9929 | template <> |
| 9930 | ast_type_traits::DynTypedNode createDynTypedNode(const TypeLoc &Node) { |
| 9931 | return ast_type_traits::DynTypedNode::create(Node); |
| 9932 | } |
| 9933 | template <> |
| 9934 | ast_type_traits::DynTypedNode |
| 9935 | createDynTypedNode(const NestedNameSpecifierLoc &Node) { |
| 9936 | return ast_type_traits::DynTypedNode::create(Node); |
| 9937 | } |
| 9938 | /// @} |
| 9939 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9940 | /// A \c RecursiveASTVisitor that builds a map from nodes to their |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9941 | /// parents as defined by the \c RecursiveASTVisitor. |
| 9942 | /// |
| 9943 | /// Note that the relationship described here is purely in terms of AST |
| 9944 | /// traversal - there are other relationships (for example declaration context) |
| 9945 | /// in the AST that are better modeled by special matchers. |
| 9946 | /// |
Benjamin Kramer | e8c51fd | 2015-10-21 10:07:26 +0000 | [diff] [blame] | 9947 | /// FIXME: Currently only builds up the map using \c Stmt and \c Decl nodes. |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9948 | class ParentMapASTVisitor : public RecursiveASTVisitor<ParentMapASTVisitor> { |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9949 | public: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 9950 | /// Builds and returns the translation unit's parent map. |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9951 | /// |
| 9952 | /// The caller takes ownership of the returned \c ParentMap. |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9953 | static std::pair<ASTContext::ParentMapPointers *, |
| 9954 | ASTContext::ParentMapOtherNodes *> |
| 9955 | buildMap(TranslationUnitDecl &TU) { |
| 9956 | ParentMapASTVisitor Visitor(new ASTContext::ParentMapPointers, |
| 9957 | new ASTContext::ParentMapOtherNodes); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9958 | Visitor.TraverseDecl(&TU); |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9959 | return std::make_pair(Visitor.Parents, Visitor.OtherParents); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9960 | } |
| 9961 | |
| 9962 | private: |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9963 | friend class RecursiveASTVisitor<ParentMapASTVisitor>; |
| 9964 | |
| 9965 | using VisitorBase = RecursiveASTVisitor<ParentMapASTVisitor>; |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9966 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9967 | ParentMapASTVisitor(ASTContext::ParentMapPointers *Parents, |
| 9968 | ASTContext::ParentMapOtherNodes *OtherParents) |
| 9969 | : Parents(Parents), OtherParents(OtherParents) {} |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9970 | |
| 9971 | bool shouldVisitTemplateInstantiations() const { |
| 9972 | return true; |
| 9973 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9974 | |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9975 | bool shouldVisitImplicitCode() const { |
| 9976 | return true; |
| 9977 | } |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9978 | |
Richard Smith | 8583872 | 2015-11-24 03:09:01 +0000 | [diff] [blame] | 9979 | template <typename T, typename MapNodeTy, typename BaseTraverseFn, |
| 9980 | typename MapTy> |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9981 | bool TraverseNode(T Node, MapNodeTy MapNode, |
Richard Smith | 8583872 | 2015-11-24 03:09:01 +0000 | [diff] [blame] | 9982 | BaseTraverseFn BaseTraverse, MapTy *Parents) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 9983 | if (!Node) |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 9984 | return true; |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 9985 | if (ParentStack.size() > 0) { |
Benjamin Kramer | e8c51fd | 2015-10-21 10:07:26 +0000 | [diff] [blame] | 9986 | // FIXME: Currently we add the same parent multiple times, but only |
| 9987 | // when no memoization data is available for the type. |
| 9988 | // For example when we visit all subexpressions of template |
| 9989 | // instantiations; this is suboptimal, but benign: the only way to |
| 9990 | // visit those is with hasAncestor / hasParent, and those do not create |
| 9991 | // new matches. |
| 9992 | // The plan is to enable DynTypedNode to be storable in a map or hash |
| 9993 | // map. The main problem there is to implement hash functions / |
| 9994 | // comparison operators for all types that DynTypedNode supports that |
| 9995 | // do not have pointer identity. |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 9996 | auto &NodeOrVector = (*Parents)[MapNode]; |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 9997 | if (NodeOrVector.isNull()) { |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 9998 | if (const auto *D = ParentStack.back().get<Decl>()) |
| 9999 | NodeOrVector = D; |
| 10000 | else if (const auto *S = ParentStack.back().get<Stmt>()) |
| 10001 | NodeOrVector = S; |
| 10002 | else |
| 10003 | NodeOrVector = |
| 10004 | new ast_type_traits::DynTypedNode(ParentStack.back()); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 10005 | } else { |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 10006 | if (!NodeOrVector.template is<ASTContext::ParentVector *>()) { |
| 10007 | auto *Vector = new ASTContext::ParentVector( |
| 10008 | 1, getSingleDynTypedNodeFromParentMap(NodeOrVector)); |
Gabor Horvath | 10a837a | 2017-04-19 15:11:10 +0000 | [diff] [blame] | 10009 | delete NodeOrVector |
| 10010 | .template dyn_cast<ast_type_traits::DynTypedNode *>(); |
Benjamin Kramer | 422b3ff | 2015-10-23 13:24:18 +0000 | [diff] [blame] | 10011 | NodeOrVector = Vector; |
Samuel Benzaquen | 3ca0a7b | 2014-06-13 13:31:40 +0000 | [diff] [blame] | 10012 | } |
Samuel Benzaquen | 3ca0a7b | 2014-06-13 13:31:40 +0000 | [diff] [blame] | 10013 | |
| 10014 | auto *Vector = |
| 10015 | NodeOrVector.template get<ASTContext::ParentVector *>(); |
| 10016 | // Skip duplicates for types that have memoization data. |
| 10017 | // We must check that the type has memoization data before calling |
| 10018 | // std::find() because DynTypedNode::operator== can't compare all |
| 10019 | // types. |
| 10020 | bool Found = ParentStack.back().getMemoizationData() && |
| 10021 | std::find(Vector->begin(), Vector->end(), |
| 10022 | ParentStack.back()) != Vector->end(); |
| 10023 | if (!Found) |
| 10024 | Vector->push_back(ParentStack.back()); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 10025 | } |
| 10026 | } |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10027 | ParentStack.push_back(createDynTypedNode(Node)); |
Richard Smith | 8583872 | 2015-11-24 03:09:01 +0000 | [diff] [blame] | 10028 | bool Result = BaseTraverse(); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10029 | ParentStack.pop_back(); |
| 10030 | return Result; |
| 10031 | } |
| 10032 | |
| 10033 | bool TraverseDecl(Decl *DeclNode) { |
Richard Smith | 8583872 | 2015-11-24 03:09:01 +0000 | [diff] [blame] | 10034 | return TraverseNode(DeclNode, DeclNode, |
| 10035 | [&] { return VisitorBase::TraverseDecl(DeclNode); }, |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10036 | Parents); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10037 | } |
| 10038 | |
| 10039 | bool TraverseStmt(Stmt *StmtNode) { |
Richard Smith | 8583872 | 2015-11-24 03:09:01 +0000 | [diff] [blame] | 10040 | return TraverseNode(StmtNode, StmtNode, |
| 10041 | [&] { return VisitorBase::TraverseStmt(StmtNode); }, |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10042 | Parents); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10043 | } |
| 10044 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10045 | bool TraverseTypeLoc(TypeLoc TypeLocNode) { |
Richard Smith | 8583872 | 2015-11-24 03:09:01 +0000 | [diff] [blame] | 10046 | return TraverseNode( |
| 10047 | TypeLocNode, ast_type_traits::DynTypedNode::create(TypeLocNode), |
| 10048 | [&] { return VisitorBase::TraverseTypeLoc(TypeLocNode); }, |
| 10049 | OtherParents); |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10050 | } |
| 10051 | |
| 10052 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSLocNode) { |
| 10053 | return TraverseNode( |
| 10054 | NNSLocNode, ast_type_traits::DynTypedNode::create(NNSLocNode), |
Richard Smith | 8583872 | 2015-11-24 03:09:01 +0000 | [diff] [blame] | 10055 | [&] { |
| 10056 | return VisitorBase::TraverseNestedNameSpecifierLoc(NNSLocNode); |
| 10057 | }, |
| 10058 | OtherParents); |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10059 | } |
| 10060 | |
| 10061 | ASTContext::ParentMapPointers *Parents; |
| 10062 | ASTContext::ParentMapOtherNodes *OtherParents; |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10063 | llvm::SmallVector<ast_type_traits::DynTypedNode, 16> ParentStack; |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10064 | }; |
| 10065 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 10066 | } // namespace |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10067 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10068 | template <typename NodeTy, typename MapTy> |
| 10069 | static ASTContext::DynTypedNodeList getDynNodeFromMap(const NodeTy &Node, |
| 10070 | const MapTy &Map) { |
| 10071 | auto I = Map.find(Node); |
| 10072 | if (I == Map.end()) { |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 10073 | return llvm::ArrayRef<ast_type_traits::DynTypedNode>(); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10074 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 10075 | if (const auto *V = |
| 10076 | I->second.template dyn_cast<ASTContext::ParentVector *>()) { |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 10077 | return llvm::makeArrayRef(*V); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 10078 | } |
Benjamin Kramer | 8e4a176 | 2015-10-22 11:21:40 +0000 | [diff] [blame] | 10079 | return getSingleDynTypedNodeFromParentMap(I->second); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10080 | } |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10081 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10082 | ASTContext::DynTypedNodeList |
| 10083 | ASTContext::getParents(const ast_type_traits::DynTypedNode &Node) { |
| 10084 | if (!PointerParents) { |
| 10085 | // We always need to run over the whole translation unit, as |
| 10086 | // hasAncestor can escape any subtree. |
| 10087 | auto Maps = ParentMapASTVisitor::buildMap(*getTranslationUnitDecl()); |
| 10088 | PointerParents.reset(Maps.first); |
| 10089 | OtherParents.reset(Maps.second); |
| 10090 | } |
| 10091 | if (Node.getNodeKind().hasPointerIdentity()) |
| 10092 | return getDynNodeFromMap(Node.getMemoizationData(), *PointerParents); |
| 10093 | return getDynNodeFromMap(Node, *OtherParents); |
| 10094 | } |
| 10095 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10096 | bool |
| 10097 | ASTContext::ObjCMethodsAreEqual(const ObjCMethodDecl *MethodDecl, |
| 10098 | const ObjCMethodDecl *MethodImpl) { |
| 10099 | // No point trying to match an unavailable/deprecated mothod. |
| 10100 | if (MethodDecl->hasAttr<UnavailableAttr>() |
| 10101 | || MethodDecl->hasAttr<DeprecatedAttr>()) |
| 10102 | return false; |
| 10103 | if (MethodDecl->getObjCDeclQualifier() != |
| 10104 | MethodImpl->getObjCDeclQualifier()) |
| 10105 | return false; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10106 | if (!hasSameType(MethodDecl->getReturnType(), MethodImpl->getReturnType())) |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10107 | return false; |
| 10108 | |
| 10109 | if (MethodDecl->param_size() != MethodImpl->param_size()) |
| 10110 | return false; |
| 10111 | |
| 10112 | for (ObjCMethodDecl::param_const_iterator IM = MethodImpl->param_begin(), |
| 10113 | IF = MethodDecl->param_begin(), EM = MethodImpl->param_end(), |
| 10114 | EF = MethodDecl->param_end(); |
| 10115 | IM != EM && IF != EF; ++IM, ++IF) { |
| 10116 | const ParmVarDecl *DeclVar = (*IF); |
| 10117 | const ParmVarDecl *ImplVar = (*IM); |
| 10118 | if (ImplVar->getObjCDeclQualifier() != DeclVar->getObjCDeclQualifier()) |
| 10119 | return false; |
| 10120 | if (!hasSameType(DeclVar->getType(), ImplVar->getType())) |
| 10121 | return false; |
| 10122 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 10123 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10124 | return (MethodDecl->isVariadic() == MethodImpl->isVariadic()); |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10125 | } |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 10126 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 10127 | uint64_t ASTContext::getTargetNullPointerValue(QualType QT) const { |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 10128 | LangAS AS; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 10129 | if (QT->getUnqualifiedDesugaredType()->isNullPtrType()) |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 10130 | AS = LangAS::Default; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 10131 | else |
| 10132 | AS = QT->getPointeeType().getAddressSpace(); |
| 10133 | |
| 10134 | return getTargetInfo().getNullPointerValue(AS); |
| 10135 | } |
| 10136 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 10137 | unsigned ASTContext::getTargetAddressSpace(LangAS AS) const { |
| 10138 | if (isTargetAddressSpace(AS)) |
| 10139 | return toTargetAddressSpace(AS); |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 10140 | else |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 10141 | return (*AddrSpaceMap)[(unsigned)AS]; |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 10142 | } |
| 10143 | |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 10144 | QualType ASTContext::getCorrespondingSaturatedType(QualType Ty) const { |
| 10145 | assert(Ty->isFixedPointType()); |
| 10146 | |
| 10147 | if (Ty->isSaturatedFixedPointType()) return Ty; |
| 10148 | |
| 10149 | const auto &BT = Ty->getAs<BuiltinType>(); |
| 10150 | switch (BT->getKind()) { |
| 10151 | default: |
| 10152 | llvm_unreachable("Not a fixed point type!"); |
| 10153 | case BuiltinType::ShortAccum: |
| 10154 | return SatShortAccumTy; |
| 10155 | case BuiltinType::Accum: |
| 10156 | return SatAccumTy; |
| 10157 | case BuiltinType::LongAccum: |
| 10158 | return SatLongAccumTy; |
| 10159 | case BuiltinType::UShortAccum: |
| 10160 | return SatUnsignedShortAccumTy; |
| 10161 | case BuiltinType::UAccum: |
| 10162 | return SatUnsignedAccumTy; |
| 10163 | case BuiltinType::ULongAccum: |
| 10164 | return SatUnsignedLongAccumTy; |
| 10165 | case BuiltinType::ShortFract: |
| 10166 | return SatShortFractTy; |
| 10167 | case BuiltinType::Fract: |
| 10168 | return SatFractTy; |
| 10169 | case BuiltinType::LongFract: |
| 10170 | return SatLongFractTy; |
| 10171 | case BuiltinType::UShortFract: |
| 10172 | return SatUnsignedShortFractTy; |
| 10173 | case BuiltinType::UFract: |
| 10174 | return SatUnsignedFractTy; |
| 10175 | case BuiltinType::ULongFract: |
| 10176 | return SatUnsignedLongFractTy; |
| 10177 | } |
| 10178 | } |
| 10179 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 10180 | // Explicitly instantiate this in case a Redeclarable<T> is used from a TU that |
| 10181 | // doesn't include ASTContext.h |
| 10182 | template |
| 10183 | clang::LazyGenerationalUpdatePtr< |
| 10184 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::ValueType |
| 10185 | clang::LazyGenerationalUpdatePtr< |
| 10186 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::makeValue( |
| 10187 | const clang::ASTContext &Ctx, Decl *Value); |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 10188 | |
| 10189 | unsigned char ASTContext::getFixedPointScale(QualType Ty) const { |
| 10190 | assert(Ty->isFixedPointType()); |
| 10191 | |
| 10192 | const auto *BT = Ty->getAs<BuiltinType>(); |
| 10193 | const TargetInfo &Target = getTargetInfo(); |
| 10194 | switch (BT->getKind()) { |
| 10195 | default: |
| 10196 | llvm_unreachable("Not a fixed point type!"); |
| 10197 | case BuiltinType::ShortAccum: |
| 10198 | case BuiltinType::SatShortAccum: |
| 10199 | return Target.getShortAccumScale(); |
| 10200 | case BuiltinType::Accum: |
| 10201 | case BuiltinType::SatAccum: |
| 10202 | return Target.getAccumScale(); |
| 10203 | case BuiltinType::LongAccum: |
| 10204 | case BuiltinType::SatLongAccum: |
| 10205 | return Target.getLongAccumScale(); |
| 10206 | case BuiltinType::UShortAccum: |
| 10207 | case BuiltinType::SatUShortAccum: |
| 10208 | return Target.getUnsignedShortAccumScale(); |
| 10209 | case BuiltinType::UAccum: |
| 10210 | case BuiltinType::SatUAccum: |
| 10211 | return Target.getUnsignedAccumScale(); |
| 10212 | case BuiltinType::ULongAccum: |
| 10213 | case BuiltinType::SatULongAccum: |
| 10214 | return Target.getUnsignedLongAccumScale(); |
| 10215 | case BuiltinType::ShortFract: |
| 10216 | case BuiltinType::SatShortFract: |
| 10217 | return Target.getShortFractScale(); |
| 10218 | case BuiltinType::Fract: |
| 10219 | case BuiltinType::SatFract: |
| 10220 | return Target.getFractScale(); |
| 10221 | case BuiltinType::LongFract: |
| 10222 | case BuiltinType::SatLongFract: |
| 10223 | return Target.getLongFractScale(); |
| 10224 | case BuiltinType::UShortFract: |
| 10225 | case BuiltinType::SatUShortFract: |
| 10226 | return Target.getUnsignedShortFractScale(); |
| 10227 | case BuiltinType::UFract: |
| 10228 | case BuiltinType::SatUFract: |
| 10229 | return Target.getUnsignedFractScale(); |
| 10230 | case BuiltinType::ULongFract: |
| 10231 | case BuiltinType::SatULongFract: |
| 10232 | return Target.getUnsignedLongFractScale(); |
| 10233 | } |
| 10234 | } |
| 10235 | |
| 10236 | unsigned char ASTContext::getFixedPointIBits(QualType Ty) const { |
| 10237 | assert(Ty->isFixedPointType()); |
| 10238 | |
| 10239 | const auto *BT = Ty->getAs<BuiltinType>(); |
| 10240 | const TargetInfo &Target = getTargetInfo(); |
| 10241 | switch (BT->getKind()) { |
| 10242 | default: |
| 10243 | llvm_unreachable("Not a fixed point type!"); |
| 10244 | case BuiltinType::ShortAccum: |
| 10245 | case BuiltinType::SatShortAccum: |
| 10246 | return Target.getShortAccumIBits(); |
| 10247 | case BuiltinType::Accum: |
| 10248 | case BuiltinType::SatAccum: |
| 10249 | return Target.getAccumIBits(); |
| 10250 | case BuiltinType::LongAccum: |
| 10251 | case BuiltinType::SatLongAccum: |
| 10252 | return Target.getLongAccumIBits(); |
| 10253 | case BuiltinType::UShortAccum: |
| 10254 | case BuiltinType::SatUShortAccum: |
| 10255 | return Target.getUnsignedShortAccumIBits(); |
| 10256 | case BuiltinType::UAccum: |
| 10257 | case BuiltinType::SatUAccum: |
| 10258 | return Target.getUnsignedAccumIBits(); |
| 10259 | case BuiltinType::ULongAccum: |
| 10260 | case BuiltinType::SatULongAccum: |
| 10261 | return Target.getUnsignedLongAccumIBits(); |
| 10262 | case BuiltinType::ShortFract: |
| 10263 | case BuiltinType::SatShortFract: |
| 10264 | case BuiltinType::Fract: |
| 10265 | case BuiltinType::SatFract: |
| 10266 | case BuiltinType::LongFract: |
| 10267 | case BuiltinType::SatLongFract: |
| 10268 | case BuiltinType::UShortFract: |
| 10269 | case BuiltinType::SatUShortFract: |
| 10270 | case BuiltinType::UFract: |
| 10271 | case BuiltinType::SatUFract: |
| 10272 | case BuiltinType::ULongFract: |
| 10273 | case BuiltinType::SatULongFract: |
| 10274 | return 0; |
| 10275 | } |
| 10276 | } |