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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the ASTContext interface. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/AST/ASTContext.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "CXXABI.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/APValue.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTMutationListener.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTTypeTraits.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/Attr.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/AttrIterator.h" |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 20 | #include "clang/AST/CharUnits.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 21 | #include "clang/AST/Comment.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 22 | #include "clang/AST/Decl.h" |
| 23 | #include "clang/AST/DeclBase.h" |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclCXX.h" |
Chandler Carruth | aa36b89 | 2015-12-30 03:40:23 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclContextInternals.h" |
Steve Naroff | 67391b8 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 26 | #include "clang/AST/DeclObjC.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 27 | #include "clang/AST/DeclOpenMP.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 28 | #include "clang/AST/DeclTemplate.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 29 | #include "clang/AST/DeclarationName.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 30 | #include "clang/AST/Expr.h" |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 31 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 32 | #include "clang/AST/ExternalASTSource.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 33 | #include "clang/AST/Mangle.h" |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 34 | #include "clang/AST/MangleNumberingContext.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 35 | #include "clang/AST/NestedNameSpecifier.h" |
| 36 | #include "clang/AST/RawCommentList.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 37 | #include "clang/AST/RecordLayout.h" |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 38 | #include "clang/AST/RecursiveASTVisitor.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 39 | #include "clang/AST/Stmt.h" |
| 40 | #include "clang/AST/TemplateBase.h" |
| 41 | #include "clang/AST/TemplateName.h" |
| 42 | #include "clang/AST/Type.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 43 | #include "clang/AST/TypeLoc.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 44 | #include "clang/AST/UnresolvedSet.h" |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 45 | #include "clang/AST/VTableBuilder.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 46 | #include "clang/Basic/AddressSpaces.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 47 | #include "clang/Basic/Builtins.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 48 | #include "clang/Basic/CommentOptions.h" |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 49 | #include "clang/Basic/ExceptionSpecificationType.h" |
Leonard Chan | a677942 | 2018-08-06 16:42:37 +0000 | [diff] [blame] | 50 | #include "clang/Basic/FixedPoint.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)) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 195 | DeclLoc = D->getBeginLoc(); |
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". |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 203 | DeclLoc = D->getBeginLoc(); |
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); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +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); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 499 | |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 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 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 508 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 509 | const Decl *OriginalDecl; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +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; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 561 | |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 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 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 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 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 596 | void |
| 597 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 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()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 605 | for (TemplateParameterList::const_iterator P = Params->begin(), |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 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 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 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 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 625 | } else |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 626 | ID.AddBoolean(false); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 627 | continue; |
| 628 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 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(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 647 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 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()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 652 | for (TemplateParameterList::const_iterator P = Params->begin(), |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 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( |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 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 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 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 |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 707 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 708 | SourceLocation(), TTP->getDepth(), |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 709 | TTP->getPosition(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 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); |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 798 | TraversalScope = {TUDecl}; |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 801 | ASTContext::~ASTContext() { |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 802 | // Release the DenseMaps associated with DeclContext objects. |
| 803 | // FIXME: Is this the ideal solution? |
| 804 | ReleaseDeclContextMaps(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 805 | |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 806 | // Call all of the deallocation functions on all of their targets. |
Chandler Carruth | ff5a01a | 2015-12-30 03:00:23 +0000 | [diff] [blame] | 807 | for (auto &Pair : Deallocations) |
| 808 | (Pair.first)(Pair.second); |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 809 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 810 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 811 | // because they can contain DenseMaps. |
| 812 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 813 | const ASTRecordLayout*>::iterator |
| 814 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 815 | // Increment in loop to prevent using deallocated memory. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 816 | if (auto *R = const_cast<ASTRecordLayout *>((I++)->second)) |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 817 | R->Destroy(*this); |
| 818 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 819 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 820 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 821 | // Increment in loop to prevent using deallocated memory. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 822 | if (auto *R = const_cast<ASTRecordLayout *>((I++)->second)) |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 823 | R->Destroy(*this); |
| 824 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 825 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 826 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 827 | AEnd = DeclAttrs.end(); |
| 828 | A != AEnd; ++A) |
| 829 | A->second->~AttrVec(); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 830 | |
David Majnemer | e694f3e | 2015-08-14 14:43:50 +0000 | [diff] [blame] | 831 | for (std::pair<const MaterializeTemporaryExpr *, APValue *> &MTVPair : |
| 832 | MaterializedTemporaryValues) |
| 833 | MTVPair.second->~APValue(); |
| 834 | |
Richard Smith | 423f46f | 2016-07-20 21:38:26 +0000 | [diff] [blame] | 835 | for (const auto &Value : ModuleInitializers) |
| 836 | Value.second->~PerModuleInitializers(); |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 837 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 838 | |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 839 | class ASTContext::ParentMap { |
| 840 | /// Contains parents of a node. |
| 841 | using ParentVector = llvm::SmallVector<ast_type_traits::DynTypedNode, 2>; |
| 842 | |
| 843 | /// Maps from a node to its parents. This is used for nodes that have |
| 844 | /// pointer identity only, which are more common and we can save space by |
| 845 | /// only storing a unique pointer to them. |
| 846 | using ParentMapPointers = llvm::DenseMap< |
| 847 | const void *, |
| 848 | llvm::PointerUnion4<const Decl *, const Stmt *, |
| 849 | ast_type_traits::DynTypedNode *, ParentVector *>>; |
| 850 | |
| 851 | /// Parent map for nodes without pointer identity. We store a full |
| 852 | /// DynTypedNode for all keys. |
| 853 | using ParentMapOtherNodes = llvm::DenseMap< |
| 854 | ast_type_traits::DynTypedNode, |
| 855 | llvm::PointerUnion4<const Decl *, const Stmt *, |
| 856 | ast_type_traits::DynTypedNode *, ParentVector *>>; |
| 857 | |
| 858 | ParentMapPointers PointerParents; |
| 859 | ParentMapOtherNodes OtherParents; |
| 860 | class ASTVisitor; |
| 861 | |
| 862 | static ast_type_traits::DynTypedNode |
| 863 | getSingleDynTypedNodeFromParentMap(ParentMapPointers::mapped_type U) { |
| 864 | if (const auto *D = U.dyn_cast<const Decl *>()) |
| 865 | return ast_type_traits::DynTypedNode::create(*D); |
| 866 | if (const auto *S = U.dyn_cast<const Stmt *>()) |
| 867 | return ast_type_traits::DynTypedNode::create(*S); |
| 868 | return *U.get<ast_type_traits::DynTypedNode *>(); |
| 869 | } |
| 870 | |
| 871 | template <typename NodeTy, typename MapTy> |
| 872 | static ASTContext::DynTypedNodeList getDynNodeFromMap(const NodeTy &Node, |
| 873 | const MapTy &Map) { |
| 874 | auto I = Map.find(Node); |
| 875 | if (I == Map.end()) { |
| 876 | return llvm::ArrayRef<ast_type_traits::DynTypedNode>(); |
| 877 | } |
| 878 | if (const auto *V = I->second.template dyn_cast<ParentVector *>()) { |
| 879 | return llvm::makeArrayRef(*V); |
| 880 | } |
| 881 | return getSingleDynTypedNodeFromParentMap(I->second); |
| 882 | } |
| 883 | |
| 884 | public: |
| 885 | ParentMap(ASTContext &Ctx); |
| 886 | ~ParentMap() { |
| 887 | for (const auto &Entry : PointerParents) { |
| 888 | if (Entry.second.is<ast_type_traits::DynTypedNode *>()) { |
| 889 | delete Entry.second.get<ast_type_traits::DynTypedNode *>(); |
| 890 | } else if (Entry.second.is<ParentVector *>()) { |
| 891 | delete Entry.second.get<ParentVector *>(); |
| 892 | } |
| 893 | } |
| 894 | for (const auto &Entry : OtherParents) { |
| 895 | if (Entry.second.is<ast_type_traits::DynTypedNode *>()) { |
| 896 | delete Entry.second.get<ast_type_traits::DynTypedNode *>(); |
| 897 | } else if (Entry.second.is<ParentVector *>()) { |
| 898 | delete Entry.second.get<ParentVector *>(); |
| 899 | } |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 900 | } |
| 901 | } |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 902 | |
| 903 | DynTypedNodeList getParents(const ast_type_traits::DynTypedNode &Node) { |
| 904 | if (Node.getNodeKind().hasPointerIdentity()) |
| 905 | return getDynNodeFromMap(Node.getMemoizationData(), PointerParents); |
| 906 | return getDynNodeFromMap(Node, OtherParents); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 907 | } |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 908 | }; |
| 909 | |
| 910 | void ASTContext::setTraversalScope(const std::vector<Decl *> &TopLevelDecls) { |
| 911 | TraversalScope = TopLevelDecls; |
| 912 | Parents.reset(); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 915 | void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) { |
Chandler Carruth | ff5a01a | 2015-12-30 03:00:23 +0000 | [diff] [blame] | 916 | Deallocations.push_back({Callback, Data}); |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 919 | void |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 920 | ASTContext::setExternalSource(IntrusiveRefCntPtr<ExternalASTSource> Source) { |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 921 | ExternalSource = std::move(Source); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 924 | void ASTContext::PrintStats() const { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 925 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 926 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 927 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 928 | unsigned counts[] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 929 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 930 | #define ABSTRACT_TYPE(Name, Parent) |
| 931 | #include "clang/AST/TypeNodes.def" |
| 932 | 0 // Extra |
| 933 | }; |
Douglas Gregor | b1fe2c9 | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 934 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 935 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 936 | Type *T = Types[i]; |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 937 | counts[(unsigned)T->getTypeClass()]++; |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 940 | unsigned Idx = 0; |
| 941 | unsigned TotalBytes = 0; |
| 942 | #define TYPE(Name, Parent) \ |
| 943 | if (counts[Idx]) \ |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 944 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
Bruno Ricci | 58e0322 | 2018-08-06 15:17:32 +0000 | [diff] [blame] | 945 | << " types, " << sizeof(Name##Type) << " each " \ |
| 946 | << "(" << counts[Idx] * sizeof(Name##Type) \ |
| 947 | << " bytes)\n"; \ |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 948 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 949 | ++Idx; |
| 950 | #define ABSTRACT_TYPE(Name, Parent) |
| 951 | #include "clang/AST/TypeNodes.def" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 953 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 954 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 955 | // Implicit special member functions. |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 956 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 957 | << NumImplicitDefaultConstructors |
| 958 | << " implicit default constructors created\n"; |
| 959 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 960 | << NumImplicitCopyConstructors |
| 961 | << " implicit copy constructors created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 962 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 963 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 964 | << NumImplicitMoveConstructors |
| 965 | << " implicit move constructors created\n"; |
| 966 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 967 | << NumImplicitCopyAssignmentOperators |
| 968 | << " implicit copy assignment operators created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 969 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 970 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 971 | << NumImplicitMoveAssignmentOperators |
| 972 | << " implicit move assignment operators created\n"; |
| 973 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 974 | << NumImplicitDestructors |
| 975 | << " implicit destructors created\n"; |
| 976 | |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 977 | if (ExternalSource) { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 978 | llvm::errs() << "\n"; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 979 | ExternalSource->PrintStats(); |
| 980 | } |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 981 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 982 | BumpAlloc.PrintStats(); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 985 | void ASTContext::mergeDefinitionIntoModule(NamedDecl *ND, Module *M, |
| 986 | bool NotifyListeners) { |
| 987 | if (NotifyListeners) |
| 988 | if (auto *Listener = getASTMutationListener()) |
| 989 | Listener->RedefinedHiddenDefinition(ND, M); |
| 990 | |
Richard Smith | 13897eb | 2018-09-12 23:37:00 +0000 | [diff] [blame] | 991 | MergedDefModules[cast<NamedDecl>(ND->getCanonicalDecl())].push_back(M); |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | void ASTContext::deduplicateMergedDefinitonsFor(NamedDecl *ND) { |
Richard Smith | 20fbdb3 | 2018-09-12 02:13:46 +0000 | [diff] [blame] | 995 | auto It = MergedDefModules.find(cast<NamedDecl>(ND->getCanonicalDecl())); |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 996 | if (It == MergedDefModules.end()) |
| 997 | return; |
| 998 | |
Benjamin Kramer | a72a70a | 2016-10-17 13:00:44 +0000 | [diff] [blame] | 999 | auto &Merged = It->second; |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 1000 | llvm::DenseSet<Module*> Found; |
| 1001 | for (Module *&M : Merged) |
| 1002 | if (!Found.insert(M).second) |
| 1003 | M = nullptr; |
| 1004 | Merged.erase(std::remove(Merged.begin(), Merged.end(), nullptr), Merged.end()); |
| 1005 | } |
| 1006 | |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 1007 | void ASTContext::PerModuleInitializers::resolve(ASTContext &Ctx) { |
| 1008 | if (LazyInitializers.empty()) |
| 1009 | return; |
| 1010 | |
| 1011 | auto *Source = Ctx.getExternalSource(); |
| 1012 | assert(Source && "lazy initializers but no external source"); |
| 1013 | |
| 1014 | auto LazyInits = std::move(LazyInitializers); |
| 1015 | LazyInitializers.clear(); |
| 1016 | |
| 1017 | for (auto ID : LazyInits) |
| 1018 | Initializers.push_back(Source->GetExternalDecl(ID)); |
| 1019 | |
| 1020 | assert(LazyInitializers.empty() && |
| 1021 | "GetExternalDecl for lazy module initializer added more inits"); |
| 1022 | } |
| 1023 | |
| 1024 | void ASTContext::addModuleInitializer(Module *M, Decl *D) { |
| 1025 | // One special case: if we add a module initializer that imports another |
| 1026 | // module, and that module's only initializer is an ImportDecl, simplify. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1027 | if (const auto *ID = dyn_cast<ImportDecl>(D)) { |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 1028 | auto It = ModuleInitializers.find(ID->getImportedModule()); |
| 1029 | |
| 1030 | // Maybe the ImportDecl does nothing at all. (Common case.) |
| 1031 | if (It == ModuleInitializers.end()) |
| 1032 | return; |
| 1033 | |
| 1034 | // Maybe the ImportDecl only imports another ImportDecl. |
| 1035 | auto &Imported = *It->second; |
| 1036 | if (Imported.Initializers.size() + Imported.LazyInitializers.size() == 1) { |
| 1037 | Imported.resolve(*this); |
| 1038 | auto *OnlyDecl = Imported.Initializers.front(); |
| 1039 | if (isa<ImportDecl>(OnlyDecl)) |
| 1040 | D = OnlyDecl; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | auto *&Inits = ModuleInitializers[M]; |
| 1045 | if (!Inits) |
| 1046 | Inits = new (*this) PerModuleInitializers; |
| 1047 | Inits->Initializers.push_back(D); |
| 1048 | } |
| 1049 | |
| 1050 | void ASTContext::addLazyModuleInitializers(Module *M, ArrayRef<uint32_t> IDs) { |
| 1051 | auto *&Inits = ModuleInitializers[M]; |
| 1052 | if (!Inits) |
| 1053 | Inits = new (*this) PerModuleInitializers; |
| 1054 | Inits->LazyInitializers.insert(Inits->LazyInitializers.end(), |
| 1055 | IDs.begin(), IDs.end()); |
| 1056 | } |
| 1057 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1058 | ArrayRef<Decl *> ASTContext::getModuleInitializers(Module *M) { |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 1059 | auto It = ModuleInitializers.find(M); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1060 | if (It == ModuleInitializers.end()) |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 1061 | return None; |
| 1062 | |
| 1063 | auto *Inits = It->second; |
| 1064 | Inits->resolve(*this); |
| 1065 | return Inits->Initializers; |
| 1066 | } |
| 1067 | |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 1068 | ExternCContextDecl *ASTContext::getExternCContextDecl() const { |
| 1069 | if (!ExternCContext) |
| 1070 | ExternCContext = ExternCContextDecl::Create(*this, getTranslationUnitDecl()); |
| 1071 | |
| 1072 | return ExternCContext; |
| 1073 | } |
| 1074 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1075 | BuiltinTemplateDecl * |
| 1076 | ASTContext::buildBuiltinTemplateDecl(BuiltinTemplateKind BTK, |
| 1077 | const IdentifierInfo *II) const { |
| 1078 | auto *BuiltinTemplate = BuiltinTemplateDecl::Create(*this, TUDecl, II, BTK); |
| 1079 | BuiltinTemplate->setImplicit(); |
| 1080 | TUDecl->addDecl(BuiltinTemplate); |
| 1081 | |
| 1082 | return BuiltinTemplate; |
| 1083 | } |
| 1084 | |
| 1085 | BuiltinTemplateDecl * |
| 1086 | ASTContext::getMakeIntegerSeqDecl() const { |
| 1087 | if (!MakeIntegerSeqDecl) |
| 1088 | MakeIntegerSeqDecl = buildBuiltinTemplateDecl(BTK__make_integer_seq, |
| 1089 | getMakeIntegerSeqName()); |
| 1090 | return MakeIntegerSeqDecl; |
| 1091 | } |
| 1092 | |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 1093 | BuiltinTemplateDecl * |
| 1094 | ASTContext::getTypePackElementDecl() const { |
| 1095 | if (!TypePackElementDecl) |
| 1096 | TypePackElementDecl = buildBuiltinTemplateDecl(BTK__type_pack_element, |
| 1097 | getTypePackElementName()); |
| 1098 | return TypePackElementDecl; |
| 1099 | } |
| 1100 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 1101 | RecordDecl *ASTContext::buildImplicitRecord(StringRef Name, |
| 1102 | RecordDecl::TagKind TK) const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1103 | SourceLocation Loc; |
| 1104 | RecordDecl *NewDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 1105 | if (getLangOpts().CPlusPlus) |
| 1106 | NewDecl = CXXRecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, |
| 1107 | Loc, &Idents.get(Name)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1108 | else |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 1109 | NewDecl = RecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, Loc, |
| 1110 | &Idents.get(Name)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1111 | NewDecl->setImplicit(); |
David Majnemer | f863736 | 2015-01-15 08:41:25 +0000 | [diff] [blame] | 1112 | NewDecl->addAttr(TypeVisibilityAttr::CreateImplicit( |
| 1113 | const_cast<ASTContext &>(*this), TypeVisibilityAttr::Default)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1114 | return NewDecl; |
| 1115 | } |
| 1116 | |
| 1117 | TypedefDecl *ASTContext::buildImplicitTypedef(QualType T, |
| 1118 | StringRef Name) const { |
| 1119 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 1120 | TypedefDecl *NewDecl = TypedefDecl::Create( |
| 1121 | const_cast<ASTContext &>(*this), getTranslationUnitDecl(), |
| 1122 | SourceLocation(), SourceLocation(), &Idents.get(Name), TInfo); |
| 1123 | NewDecl->setImplicit(); |
| 1124 | return NewDecl; |
| 1125 | } |
| 1126 | |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 1127 | TypedefDecl *ASTContext::getInt128Decl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1128 | if (!Int128Decl) |
| 1129 | Int128Decl = buildImplicitTypedef(Int128Ty, "__int128_t"); |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 1130 | return Int128Decl; |
| 1131 | } |
| 1132 | |
| 1133 | TypedefDecl *ASTContext::getUInt128Decl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1134 | if (!UInt128Decl) |
| 1135 | UInt128Decl = buildImplicitTypedef(UnsignedInt128Ty, "__uint128_t"); |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 1136 | return UInt128Decl; |
| 1137 | } |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 1138 | |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 1139 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1140 | auto *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 1141 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1142 | Types.push_back(Ty); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 1145 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target, |
| 1146 | const TargetInfo *AuxTarget) { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1147 | assert((!this->Target || this->Target == &Target) && |
| 1148 | "Incorrect target reinitialization"); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1149 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1150 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1151 | this->Target = &Target; |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 1152 | this->AuxTarget = AuxTarget; |
| 1153 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1154 | ABI.reset(createCXXABI(Target)); |
| 1155 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 1156 | AddrSpaceMapMangling = isAddrSpaceMapManglingEnabled(Target, LangOpts); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1157 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1158 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1159 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1161 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1162 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1163 | // C99 6.2.5p3. |
Eli Friedman | 9ffd4a9 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 1164 | if (LangOpts.CharIsSigned) |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 1165 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 1166 | else |
| 1167 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1168 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1169 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 1170 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 1171 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 1172 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 1173 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1174 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1175 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1176 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 1177 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 1178 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 1179 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 1180 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1181 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1182 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1183 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 1184 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 1185 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1186 | |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1187 | // GNU extension, __float128 for IEEE quadruple precision |
| 1188 | InitBuiltinType(Float128Ty, BuiltinType::Float128); |
| 1189 | |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 1190 | // C11 extension ISO/IEC TS 18661-3 |
| 1191 | InitBuiltinType(Float16Ty, BuiltinType::Float16); |
| 1192 | |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1193 | // ISO/IEC JTC1 SC22 WG14 N1169 Extension |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1194 | InitBuiltinType(ShortAccumTy, BuiltinType::ShortAccum); |
| 1195 | InitBuiltinType(AccumTy, BuiltinType::Accum); |
| 1196 | InitBuiltinType(LongAccumTy, BuiltinType::LongAccum); |
| 1197 | InitBuiltinType(UnsignedShortAccumTy, BuiltinType::UShortAccum); |
| 1198 | InitBuiltinType(UnsignedAccumTy, BuiltinType::UAccum); |
| 1199 | InitBuiltinType(UnsignedLongAccumTy, BuiltinType::ULongAccum); |
| 1200 | InitBuiltinType(ShortFractTy, BuiltinType::ShortFract); |
| 1201 | InitBuiltinType(FractTy, BuiltinType::Fract); |
| 1202 | InitBuiltinType(LongFractTy, BuiltinType::LongFract); |
| 1203 | InitBuiltinType(UnsignedShortFractTy, BuiltinType::UShortFract); |
| 1204 | InitBuiltinType(UnsignedFractTy, BuiltinType::UFract); |
| 1205 | InitBuiltinType(UnsignedLongFractTy, BuiltinType::ULongFract); |
| 1206 | InitBuiltinType(SatShortAccumTy, BuiltinType::SatShortAccum); |
| 1207 | InitBuiltinType(SatAccumTy, BuiltinType::SatAccum); |
| 1208 | InitBuiltinType(SatLongAccumTy, BuiltinType::SatLongAccum); |
| 1209 | InitBuiltinType(SatUnsignedShortAccumTy, BuiltinType::SatUShortAccum); |
| 1210 | InitBuiltinType(SatUnsignedAccumTy, BuiltinType::SatUAccum); |
| 1211 | InitBuiltinType(SatUnsignedLongAccumTy, BuiltinType::SatULongAccum); |
| 1212 | InitBuiltinType(SatShortFractTy, BuiltinType::SatShortFract); |
| 1213 | InitBuiltinType(SatFractTy, BuiltinType::SatFract); |
| 1214 | InitBuiltinType(SatLongFractTy, BuiltinType::SatLongFract); |
| 1215 | InitBuiltinType(SatUnsignedShortFractTy, BuiltinType::SatUShortFract); |
| 1216 | InitBuiltinType(SatUnsignedFractTy, BuiltinType::SatUFract); |
| 1217 | InitBuiltinType(SatUnsignedLongFractTy, BuiltinType::SatULongFract); |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 1219 | // GNU extension, 128-bit integers. |
| 1220 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 1221 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 1222 | |
Hans Wennborg | 0d81e01 | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 1223 | // C++ 3.9.1p5 |
| 1224 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
| 1225 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 1226 | else // -fshort-wchar makes wchar_t be unsigned. |
| 1227 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 1228 | if (LangOpts.CPlusPlus && LangOpts.WChar) |
| 1229 | WideCharTy = WCharTy; |
| 1230 | else { |
| 1231 | // C99 (or C++ using -fno-wchar). |
| 1232 | WideCharTy = getFromTargetType(Target.getWCharType()); |
| 1233 | } |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1234 | |
James Molloy | 3636554 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 1235 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 1236 | |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 1237 | // C++20 (proposed) |
| 1238 | InitBuiltinType(Char8Ty, BuiltinType::Char8); |
| 1239 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1240 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 1241 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 1242 | else // C99 |
| 1243 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 1244 | |
| 1245 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 1246 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 1247 | else // C99 |
| 1248 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 1249 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1250 | // Placeholder type for type-dependent expressions whose type is |
| 1251 | // completely unknown. No code should ever check a type against |
| 1252 | // DependentTy and users should never see it; however, it is here to |
| 1253 | // help diagnose failures to properly check for type-dependent |
| 1254 | // expressions. |
| 1255 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1256 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 1257 | // Placeholder type for functions. |
| 1258 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 1259 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1260 | // Placeholder type for bound members. |
| 1261 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 1262 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1263 | // Placeholder type for pseudo-objects. |
| 1264 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 1265 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 1266 | // "any" type; useful for debugger-like clients. |
| 1267 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 1268 | |
John McCall | 8a6b59a | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 1269 | // Placeholder type for unbridged ARC casts. |
| 1270 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 1271 | |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 1272 | // Placeholder type for builtin functions. |
| 1273 | InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn); |
| 1274 | |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 1275 | // Placeholder type for OMP array sections. |
| 1276 | if (LangOpts.OpenMP) |
| 1277 | InitBuiltinType(OMPArraySectionTy, BuiltinType::OMPArraySection); |
| 1278 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1279 | // C99 6.2.5p11. |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1280 | FloatComplexTy = getComplexType(FloatTy); |
| 1281 | DoubleComplexTy = getComplexType(DoubleTy); |
| 1282 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1283 | Float128ComplexTy = getComplexType(Float128Ty); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1284 | |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1285 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1286 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 1287 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1288 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1289 | |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 1290 | if (LangOpts.OpenCL) { |
| 1291 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1292 | InitBuiltinType(SingletonId, BuiltinType::Id); |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 1293 | #include "clang/Basic/OpenCLImageTypes.def" |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1294 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1295 | InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1296 | InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1297 | InitBuiltinType(OCLClkEventTy, BuiltinType::OCLClkEvent); |
| 1298 | InitBuiltinType(OCLQueueTy, BuiltinType::OCLQueue); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1299 | InitBuiltinType(OCLReserveIDTy, BuiltinType::OCLReserveID); |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 1300 | |
| 1301 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
| 1302 | InitBuiltinType(Id##Ty, BuiltinType::Id); |
| 1303 | #include "clang/Basic/OpenCLExtensionTypes.def" |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1304 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1305 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1306 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 29898f4 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 1307 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 1308 | SignedCharTy : BoolTy); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1309 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1310 | ObjCConstantStringType = QualType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1311 | |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1312 | ObjCSuperType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1313 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1314 | // void * type |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 1315 | if (LangOpts.OpenCLVersion >= 200) { |
| 1316 | auto Q = VoidTy.getQualifiers(); |
| 1317 | Q.setAddressSpace(LangAS::opencl_generic); |
| 1318 | VoidPtrTy = getPointerType(getCanonicalType( |
| 1319 | getQualifiedType(VoidTy.getUnqualifiedType(), Q))); |
| 1320 | } else { |
| 1321 | VoidPtrTy = getPointerType(VoidTy); |
| 1322 | } |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1323 | |
| 1324 | // nullptr type (C++0x 2.14.7) |
| 1325 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1326 | |
| 1327 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 1328 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 1329 | |
| 1330 | // Builtin type used to help define __builtin_va_list. |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 1331 | VaListTagDecl = nullptr; |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1334 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1335 | return SourceMgr.getDiagnostics(); |
| 1336 | } |
| 1337 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 1338 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 1339 | AttrVec *&Result = DeclAttrs[D]; |
| 1340 | if (!Result) { |
| 1341 | void *Mem = Allocate(sizeof(AttrVec)); |
| 1342 | Result = new (Mem) AttrVec; |
| 1343 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1344 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 1345 | return *Result; |
| 1346 | } |
| 1347 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1348 | /// Erase the attributes corresponding to the given declaration. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 1349 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 1350 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 1351 | if (Pos != DeclAttrs.end()) { |
| 1352 | Pos->second->~AttrVec(); |
| 1353 | DeclAttrs.erase(Pos); |
| 1354 | } |
| 1355 | } |
| 1356 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1357 | // FIXME: Remove ? |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1358 | MemberSpecializationInfo * |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1359 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1360 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1361 | return getTemplateOrSpecializationInfo(Var) |
| 1362 | .dyn_cast<MemberSpecializationInfo *>(); |
| 1363 | } |
| 1364 | |
| 1365 | ASTContext::TemplateOrSpecializationInfo |
| 1366 | ASTContext::getTemplateOrSpecializationInfo(const VarDecl *Var) { |
| 1367 | llvm::DenseMap<const VarDecl *, TemplateOrSpecializationInfo>::iterator Pos = |
| 1368 | TemplateOrInstantiation.find(Var); |
| 1369 | if (Pos == TemplateOrInstantiation.end()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1370 | return {}; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1372 | return Pos->second; |
| 1373 | } |
| 1374 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1375 | void |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1376 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 1377 | TemplateSpecializationKind TSK, |
| 1378 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1379 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 1380 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1381 | setTemplateOrSpecializationInfo(Inst, new (*this) MemberSpecializationInfo( |
| 1382 | Tmpl, TSK, PointOfInstantiation)); |
| 1383 | } |
| 1384 | |
| 1385 | void |
| 1386 | ASTContext::setTemplateOrSpecializationInfo(VarDecl *Inst, |
| 1387 | TemplateOrSpecializationInfo TSI) { |
| 1388 | assert(!TemplateOrInstantiation[Inst] && |
| 1389 | "Already noted what the variable was instantiated from"); |
| 1390 | TemplateOrInstantiation[Inst] = TSI; |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1393 | FunctionDecl *ASTContext::getClassScopeSpecializationPattern( |
| 1394 | const FunctionDecl *FD){ |
| 1395 | assert(FD && "Specialization is 0"); |
| 1396 | llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1397 | = ClassScopeSpecializationPattern.find(FD); |
| 1398 | if (Pos == ClassScopeSpecializationPattern.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1399 | return nullptr; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1400 | |
| 1401 | return Pos->second; |
| 1402 | } |
| 1403 | |
| 1404 | void ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD, |
| 1405 | FunctionDecl *Pattern) { |
| 1406 | assert(FD && "Specialization is 0"); |
| 1407 | assert(Pattern && "Class scope specialization pattern is 0"); |
Francois Pichet | 5792825 | 2011-08-14 14:28:49 +0000 | [diff] [blame] | 1408 | ClassScopeSpecializationPattern[FD] = Pattern; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1411 | NamedDecl * |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1412 | ASTContext::getInstantiatedFromUsingDecl(NamedDecl *UUD) { |
| 1413 | auto Pos = InstantiatedFromUsingDecl.find(UUD); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1414 | if (Pos == InstantiatedFromUsingDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1415 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1417 | return Pos->second; |
| 1418 | } |
| 1419 | |
| 1420 | void |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1421 | ASTContext::setInstantiatedFromUsingDecl(NamedDecl *Inst, NamedDecl *Pattern) { |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1422 | assert((isa<UsingDecl>(Pattern) || |
| 1423 | isa<UnresolvedUsingValueDecl>(Pattern) || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1424 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1425 | "pattern decl is not a using decl"); |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1426 | assert((isa<UsingDecl>(Inst) || |
| 1427 | isa<UnresolvedUsingValueDecl>(Inst) || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1428 | isa<UnresolvedUsingTypenameDecl>(Inst)) && |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1429 | "instantiation did not produce a using decl"); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1430 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 1431 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 1432 | } |
| 1433 | |
| 1434 | UsingShadowDecl * |
| 1435 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 1436 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 1437 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 1438 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1439 | return nullptr; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1440 | |
| 1441 | return Pos->second; |
| 1442 | } |
| 1443 | |
| 1444 | void |
| 1445 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 1446 | UsingShadowDecl *Pattern) { |
| 1447 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 1448 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1451 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 1452 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 1453 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 1454 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1455 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1456 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1457 | return Pos->second; |
| 1458 | } |
| 1459 | |
| 1460 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 1461 | FieldDecl *Tmpl) { |
| 1462 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 1463 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 1464 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 1465 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1467 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 1468 | } |
| 1469 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1470 | ASTContext::overridden_cxx_method_iterator |
| 1471 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 1472 | return overridden_methods(Method).begin(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | ASTContext::overridden_cxx_method_iterator |
| 1476 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 1477 | return overridden_methods(Method).end(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1480 | unsigned |
| 1481 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 1482 | auto Range = overridden_methods(Method); |
| 1483 | return Range.end() - Range.begin(); |
Clement Courbet | 8251ebf | 2016-06-10 11:54:43 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Clement Courbet | 6ecaec8 | 2016-07-05 07:49:31 +0000 | [diff] [blame] | 1486 | ASTContext::overridden_method_range |
| 1487 | ASTContext::overridden_methods(const CXXMethodDecl *Method) const { |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 1488 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos = |
| 1489 | OverriddenMethods.find(Method->getCanonicalDecl()); |
| 1490 | if (Pos == OverriddenMethods.end()) |
| 1491 | return overridden_method_range(nullptr, nullptr); |
| 1492 | return overridden_method_range(Pos->second.begin(), Pos->second.end()); |
Clement Courbet | 6ecaec8 | 2016-07-05 07:49:31 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1495 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1496 | const CXXMethodDecl *Overridden) { |
Argyrios Kyrtzidis | cc4ca0a | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1497 | assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl()); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1498 | OverriddenMethods[Method].push_back(Overridden); |
| 1499 | } |
| 1500 | |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 1501 | void ASTContext::getOverriddenMethods( |
| 1502 | const NamedDecl *D, |
| 1503 | SmallVectorImpl<const NamedDecl *> &Overridden) const { |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1504 | assert(D); |
| 1505 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1506 | if (const auto *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | c8e7008 | 2013-04-17 00:09:03 +0000 | [diff] [blame] | 1507 | Overridden.append(overridden_methods_begin(CXXMethod), |
| 1508 | overridden_methods_end(CXXMethod)); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1509 | return; |
| 1510 | } |
| 1511 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1512 | const auto *Method = dyn_cast<ObjCMethodDecl>(D); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1513 | if (!Method) |
| 1514 | return; |
| 1515 | |
Argyrios Kyrtzidis | 353f6a4 | 2012-10-09 18:19:01 +0000 | [diff] [blame] | 1516 | SmallVector<const ObjCMethodDecl *, 8> OverDecls; |
| 1517 | Method->getOverriddenMethods(OverDecls); |
Argyrios Kyrtzidis | a7a1081 | 2012-10-09 20:08:43 +0000 | [diff] [blame] | 1518 | Overridden.append(OverDecls.begin(), OverDecls.end()); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 1521 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 1522 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 1523 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 1524 | if (!FirstLocalImport) { |
| 1525 | FirstLocalImport = Import; |
| 1526 | LastLocalImport = Import; |
| 1527 | return; |
| 1528 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1529 | |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 1530 | LastLocalImport->NextLocalImport = Import; |
| 1531 | LastLocalImport = Import; |
| 1532 | } |
| 1533 | |
Chris Lattner | 53cfe80 | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1534 | //===----------------------------------------------------------------------===// |
| 1535 | // Type Sizing and Analysis |
| 1536 | //===----------------------------------------------------------------------===// |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1537 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1538 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 1539 | /// scalar floating point type. |
| 1540 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1541 | const auto *BT = T->getAs<BuiltinType>(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1542 | assert(BT && "Not a floating point type!"); |
| 1543 | switch (BT->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1544 | default: llvm_unreachable("Not a floating point type!"); |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 1545 | case BuiltinType::Float16: |
| 1546 | case BuiltinType::Half: |
| 1547 | return Target->getHalfFormat(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1548 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 1549 | case BuiltinType::Double: return Target->getDoubleFormat(); |
| 1550 | case BuiltinType::LongDouble: return Target->getLongDoubleFormat(); |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1551 | case BuiltinType::Float128: return Target->getFloat128Format(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1555 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1556 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1557 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1558 | bool UseAlignAttrOnly = false; |
| 1559 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 1560 | Align = AlignFromAttr; |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1561 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1562 | // __attribute__((aligned)) can increase or decrease alignment |
| 1563 | // *except* on a struct or struct member, where it only increases |
| 1564 | // alignment unless 'packed' is also specified. |
| 1565 | // |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1566 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1567 | // ignore that possibility; Sema should diagnose it. |
| 1568 | if (isa<FieldDecl>(D)) { |
| 1569 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 1570 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 1571 | } else { |
| 1572 | UseAlignAttrOnly = true; |
| 1573 | } |
| 1574 | } |
Fariborz Jahanian | 9f10718 | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 1575 | else if (isa<FieldDecl>(D)) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1576 | UseAlignAttrOnly = |
Fariborz Jahanian | 9f10718 | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 1577 | D->hasAttr<PackedAttr>() || |
| 1578 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1579 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1580 | // If we're using the align attribute only, just ignore everything |
| 1581 | // else about the declaration and its type. |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1582 | if (UseAlignAttrOnly) { |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1583 | // do nothing |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1584 | } else if (const auto *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1585 | QualType T = VD->getType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1586 | if (const auto *RT = T->getAs<ReferenceType>()) { |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1587 | if (ForAlignof) |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1588 | T = RT->getPointeeType(); |
| 1589 | else |
| 1590 | T = getPointerType(RT->getPointeeType()); |
| 1591 | } |
Richard Smith | f6d7030 | 2014-06-10 23:34:28 +0000 | [diff] [blame] | 1592 | QualType BaseT = getBaseElementType(T); |
Akira Hatanaka | d62f2c8 | 2017-01-06 17:56:15 +0000 | [diff] [blame] | 1593 | if (T->isFunctionType()) |
| 1594 | Align = getTypeInfoImpl(T.getTypePtr()).Align; |
| 1595 | else if (!BaseT->isIncompleteType()) { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1596 | // Adjust alignments of declarations with array type by the |
| 1597 | // large-array alignment on the target. |
Rafael Espindola | 8857275 | 2013-08-07 18:08:19 +0000 | [diff] [blame] | 1598 | if (const ArrayType *arrayType = getAsArrayType(T)) { |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1599 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
| 1600 | if (!ForAlignof && MinWidth) { |
Rafael Espindola | 8857275 | 2013-08-07 18:08:19 +0000 | [diff] [blame] | 1601 | if (isa<VariableArrayType>(arrayType)) |
| 1602 | Align = std::max(Align, Target->getLargeArrayAlign()); |
| 1603 | else if (isa<ConstantArrayType>(arrayType) && |
| 1604 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
| 1605 | Align = std::max(Align, Target->getLargeArrayAlign()); |
| 1606 | } |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1607 | } |
Chad Rosier | 99ee782 | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 1608 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Roger Ferrer Ibanez | 3fa38a1 | 2017-03-08 14:00:44 +0000 | [diff] [blame] | 1609 | if (BaseT.getQualifiers().hasUnaligned()) |
| 1610 | Align = Target->getCharWidth(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1611 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Ulrich Weigand | b63f779 | 2015-04-21 17:26:18 +0000 | [diff] [blame] | 1612 | if (VD->hasGlobalStorage() && !ForAlignof) |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1613 | Align = std::max(Align, getTargetInfo().getMinGlobalAlign()); |
| 1614 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1615 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1616 | |
| 1617 | // Fields can be subject to extra alignment constraints, like if |
| 1618 | // the field is packed, the struct is packed, or the struct has a |
| 1619 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 1620 | // the actual alignment of the field within the struct, and then |
| 1621 | // (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] | 1622 | if (const auto *Field = dyn_cast<FieldDecl>(VD)) { |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1623 | const RecordDecl *Parent = Field->getParent(); |
| 1624 | // We can only produce a sensible answer if the record is valid. |
| 1625 | if (!Parent->isInvalidDecl()) { |
| 1626 | const ASTRecordLayout &Layout = getASTRecordLayout(Parent); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1627 | |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1628 | // Start with the record's overall alignment. |
| 1629 | unsigned FieldAlign = toBits(Layout.getAlignment()); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1630 | |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1631 | // Use the GCD of that and the offset within the record. |
| 1632 | uint64_t Offset = Layout.getFieldOffset(Field->getFieldIndex()); |
| 1633 | if (Offset > 0) { |
| 1634 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 1635 | // which means we get to do this crazy thing instead of Euclid's. |
| 1636 | uint64_t LowBitOfOffset = Offset & (~Offset + 1); |
| 1637 | if (LowBitOfOffset < FieldAlign) |
| 1638 | FieldAlign = static_cast<unsigned>(LowBitOfOffset); |
| 1639 | } |
| 1640 | |
| 1641 | Align = std::min(Align, FieldAlign); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1642 | } |
Charles Davis | 3fc5107 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 1643 | } |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1644 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1645 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1646 | return toCharUnitsFromBits(Align); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1647 | } |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1648 | |
John McCall | f124992 | 2012-08-21 04:10:00 +0000 | [diff] [blame] | 1649 | // getTypeInfoDataSizeInChars - Return the size of a type, in |
| 1650 | // chars. If the type is a record, its data size is returned. This is |
| 1651 | // the size of the memcpy that's performed when assigning this type |
| 1652 | // using a trivial copy/move assignment operator. |
| 1653 | std::pair<CharUnits, CharUnits> |
| 1654 | ASTContext::getTypeInfoDataSizeInChars(QualType T) const { |
| 1655 | std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T); |
| 1656 | |
| 1657 | // In C++, objects can sometimes be allocated into the tail padding |
| 1658 | // of a base-class subobject. We decide whether that's possible |
| 1659 | // during class layout, so here we can just trust the layout results. |
| 1660 | if (getLangOpts().CPlusPlus) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1661 | if (const auto *RT = T->getAs<RecordType>()) { |
John McCall | f124992 | 2012-08-21 04:10:00 +0000 | [diff] [blame] | 1662 | const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl()); |
| 1663 | sizeAndAlign.first = layout.getDataSize(); |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | return sizeAndAlign; |
| 1668 | } |
| 1669 | |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1670 | /// getConstantArrayInfoInChars - Performing the computation in CharUnits |
| 1671 | /// instead of in bits prevents overflowing the uint64_t for some large arrays. |
| 1672 | std::pair<CharUnits, CharUnits> |
| 1673 | static getConstantArrayInfoInChars(const ASTContext &Context, |
| 1674 | const ConstantArrayType *CAT) { |
| 1675 | std::pair<CharUnits, CharUnits> EltInfo = |
| 1676 | Context.getTypeInfoInChars(CAT->getElementType()); |
| 1677 | uint64_t Size = CAT->getSize().getZExtValue(); |
Richard Trieu | c750907 | 2013-05-14 23:41:50 +0000 | [diff] [blame] | 1678 | assert((Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity()) <= |
| 1679 | (uint64_t)(-1)/Size) && |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1680 | "Overflow in array type char size evaluation"); |
| 1681 | uint64_t Width = EltInfo.first.getQuantity() * Size; |
| 1682 | unsigned Align = EltInfo.second.getQuantity(); |
Warren Hunt | 5ae586a | 2013-11-01 23:59:41 +0000 | [diff] [blame] | 1683 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 1684 | Context.getTargetInfo().getPointerWidth(0) == 64) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1685 | Width = llvm::alignTo(Width, Align); |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1686 | return std::make_pair(CharUnits::fromQuantity(Width), |
| 1687 | CharUnits::fromQuantity(Align)); |
| 1688 | } |
| 1689 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1690 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1691 | ASTContext::getTypeInfoInChars(const Type *T) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1692 | if (const auto *CAT = dyn_cast<ConstantArrayType>(T)) |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1693 | return getConstantArrayInfoInChars(*this, CAT); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1694 | TypeInfo Info = getTypeInfo(T); |
| 1695 | return std::make_pair(toCharUnitsFromBits(Info.Width), |
| 1696 | toCharUnitsFromBits(Info.Align)); |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1700 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1701 | return getTypeInfoInChars(T.getTypePtr()); |
| 1702 | } |
| 1703 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1704 | bool ASTContext::isAlignmentRequired(const Type *T) const { |
| 1705 | return getTypeInfo(T).AlignIsRequired; |
| 1706 | } |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1707 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1708 | bool ASTContext::isAlignmentRequired(QualType T) const { |
| 1709 | return isAlignmentRequired(T.getTypePtr()); |
| 1710 | } |
| 1711 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1712 | unsigned ASTContext::getTypeAlignIfKnown(QualType T) const { |
| 1713 | // An alignment on a typedef overrides anything else. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1714 | if (const auto *TT = T->getAs<TypedefType>()) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1715 | if (unsigned Align = TT->getDecl()->getMaxAlignment()) |
| 1716 | return Align; |
| 1717 | |
| 1718 | // If we have an (array of) complete type, we're done. |
| 1719 | T = getBaseElementType(T); |
| 1720 | if (!T->isIncompleteType()) |
| 1721 | return getTypeAlign(T); |
| 1722 | |
| 1723 | // If we had an array type, its element type might be a typedef |
| 1724 | // type with an alignment attribute. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1725 | if (const auto *TT = T->getAs<TypedefType>()) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1726 | if (unsigned Align = TT->getDecl()->getMaxAlignment()) |
| 1727 | return Align; |
| 1728 | |
| 1729 | // Otherwise, see if the declaration of the type had an attribute. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1730 | if (const auto *TT = T->getAs<TagType>()) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1731 | return TT->getDecl()->getMaxAlignment(); |
| 1732 | |
| 1733 | return 0; |
| 1734 | } |
| 1735 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1736 | TypeInfo ASTContext::getTypeInfo(const Type *T) const { |
David Majnemer | f8d3864 | 2014-07-30 08:42:33 +0000 | [diff] [blame] | 1737 | TypeInfoMap::iterator I = MemoizedTypeInfo.find(T); |
| 1738 | if (I != MemoizedTypeInfo.end()) |
| 1739 | return I->second; |
| 1740 | |
| 1741 | // This call can invalidate MemoizedTypeInfo[T], so we need a second lookup. |
| 1742 | TypeInfo TI = getTypeInfoImpl(T); |
| 1743 | MemoizedTypeInfo[T] = TI; |
Rafael Espindola | eaa88c1 | 2014-07-30 04:40:23 +0000 | [diff] [blame] | 1744 | return TI; |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
| 1747 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 1748 | /// method does not work on incomplete types. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1749 | /// |
| 1750 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 1751 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 1752 | /// should take a QualType, &c. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1753 | TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const { |
| 1754 | uint64_t Width = 0; |
| 1755 | unsigned Align = 8; |
| 1756 | bool AlignIsRequired = false; |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 1757 | unsigned AS = 0; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1758 | switch (T->getTypeClass()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1759 | #define TYPE(Class, Base) |
| 1760 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1761 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1762 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
David Blaikie | ab277d6 | 2013-07-13 21:08:03 +0000 | [diff] [blame] | 1763 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) \ |
| 1764 | case Type::Class: \ |
| 1765 | assert(!T->isDependentType() && "should not see dependent types here"); \ |
| 1766 | return getTypeInfo(cast<Class##Type>(T)->desugar().getTypePtr()); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1767 | #include "clang/AST/TypeNodes.def" |
John McCall | 15547bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 1768 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1769 | |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1770 | case Type::FunctionNoProto: |
| 1771 | case Type::FunctionProto: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1772 | // GCC extension: alignof(function) = 32 bits |
| 1773 | Width = 0; |
| 1774 | Align = 32; |
| 1775 | break; |
| 1776 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1777 | case Type::IncompleteArray: |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1778 | case Type::VariableArray: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1779 | Width = 0; |
| 1780 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 1781 | break; |
| 1782 | |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1783 | case Type::ConstantArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1784 | const auto *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1786 | TypeInfo EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | d541a4c | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1787 | uint64_t Size = CAT->getSize().getZExtValue(); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1788 | assert((Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) && |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1789 | "Overflow in array type bit size evaluation"); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1790 | Width = EltInfo.Width * Size; |
| 1791 | Align = EltInfo.Align; |
Warren Hunt | 5ae586a | 2013-11-01 23:59:41 +0000 | [diff] [blame] | 1792 | if (!getTargetInfo().getCXXABI().isMicrosoft() || |
| 1793 | getTargetInfo().getPointerWidth(0) == 64) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1794 | Width = llvm::alignTo(Width, Align); |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1795 | break; |
Christopher Lamb | c5fafa2 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 1796 | } |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1797 | case Type::ExtVector: |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1798 | case Type::Vector: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1799 | const auto *VT = cast<VectorType>(T); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1800 | TypeInfo EltInfo = getTypeInfo(VT->getElementType()); |
| 1801 | Width = EltInfo.Width * VT->getNumElements(); |
Eli Friedman | 3df5efe | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1802 | Align = Width; |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1803 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 1804 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | ef78c8e | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 1805 | if (Align & (Align-1)) { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1806 | Align = llvm::NextPowerOf2(Align); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1807 | Width = llvm::alignTo(Width, Align); |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1808 | } |
Chad Rosier | cc40ea7 | 2012-07-13 23:57:43 +0000 | [diff] [blame] | 1809 | // Adjust the alignment based on the target max. |
| 1810 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
| 1811 | if (TargetVectorAlign && TargetVectorAlign < Align) |
| 1812 | Align = TargetVectorAlign; |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1813 | break; |
| 1814 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1815 | |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1816 | case Type::Builtin: |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1817 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1818 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1819 | case BuiltinType::Void: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1820 | // GCC extension: alignof(void) = 8 bits. |
| 1821 | Width = 0; |
| 1822 | Align = 8; |
| 1823 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1824 | case BuiltinType::Bool: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1825 | Width = Target->getBoolWidth(); |
| 1826 | Align = Target->getBoolAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1827 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1828 | case BuiltinType::Char_S: |
| 1829 | case BuiltinType::Char_U: |
| 1830 | case BuiltinType::UChar: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1831 | case BuiltinType::SChar: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 1832 | case BuiltinType::Char8: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1833 | Width = Target->getCharWidth(); |
| 1834 | Align = Target->getCharAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1835 | break; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1836 | case BuiltinType::WChar_S: |
| 1837 | case BuiltinType::WChar_U: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1838 | Width = Target->getWCharWidth(); |
| 1839 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1840 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1841 | case BuiltinType::Char16: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1842 | Width = Target->getChar16Width(); |
| 1843 | Align = Target->getChar16Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1844 | break; |
| 1845 | case BuiltinType::Char32: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1846 | Width = Target->getChar32Width(); |
| 1847 | Align = Target->getChar32Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1848 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1849 | case BuiltinType::UShort: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1850 | case BuiltinType::Short: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1851 | Width = Target->getShortWidth(); |
| 1852 | Align = Target->getShortAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1853 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1854 | case BuiltinType::UInt: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1855 | case BuiltinType::Int: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1856 | Width = Target->getIntWidth(); |
| 1857 | Align = Target->getIntAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1858 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1859 | case BuiltinType::ULong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1860 | case BuiltinType::Long: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1861 | Width = Target->getLongWidth(); |
| 1862 | Align = Target->getLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1863 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1864 | case BuiltinType::ULongLong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1865 | case BuiltinType::LongLong: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1866 | Width = Target->getLongLongWidth(); |
| 1867 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1868 | break; |
Chris Lattner | 0a415ec | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 1869 | case BuiltinType::Int128: |
| 1870 | case BuiltinType::UInt128: |
| 1871 | Width = 128; |
| 1872 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 1873 | break; |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1874 | case BuiltinType::ShortAccum: |
| 1875 | case BuiltinType::UShortAccum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1876 | case BuiltinType::SatShortAccum: |
| 1877 | case BuiltinType::SatUShortAccum: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1878 | Width = Target->getShortAccumWidth(); |
| 1879 | Align = Target->getShortAccumAlign(); |
| 1880 | break; |
| 1881 | case BuiltinType::Accum: |
| 1882 | case BuiltinType::UAccum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1883 | case BuiltinType::SatAccum: |
| 1884 | case BuiltinType::SatUAccum: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1885 | Width = Target->getAccumWidth(); |
| 1886 | Align = Target->getAccumAlign(); |
| 1887 | break; |
| 1888 | case BuiltinType::LongAccum: |
| 1889 | case BuiltinType::ULongAccum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1890 | case BuiltinType::SatLongAccum: |
| 1891 | case BuiltinType::SatULongAccum: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1892 | Width = Target->getLongAccumWidth(); |
| 1893 | Align = Target->getLongAccumAlign(); |
| 1894 | break; |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1895 | case BuiltinType::ShortFract: |
| 1896 | case BuiltinType::UShortFract: |
| 1897 | case BuiltinType::SatShortFract: |
| 1898 | case BuiltinType::SatUShortFract: |
| 1899 | Width = Target->getShortFractWidth(); |
| 1900 | Align = Target->getShortFractAlign(); |
| 1901 | break; |
| 1902 | case BuiltinType::Fract: |
| 1903 | case BuiltinType::UFract: |
| 1904 | case BuiltinType::SatFract: |
| 1905 | case BuiltinType::SatUFract: |
| 1906 | Width = Target->getFractWidth(); |
| 1907 | Align = Target->getFractAlign(); |
| 1908 | break; |
| 1909 | case BuiltinType::LongFract: |
| 1910 | case BuiltinType::ULongFract: |
| 1911 | case BuiltinType::SatLongFract: |
| 1912 | case BuiltinType::SatULongFract: |
| 1913 | Width = Target->getLongFractWidth(); |
| 1914 | Align = Target->getLongFractAlign(); |
| 1915 | break; |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 1916 | case BuiltinType::Float16: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1917 | case BuiltinType::Half: |
| 1918 | Width = Target->getHalfWidth(); |
| 1919 | Align = Target->getHalfAlign(); |
| 1920 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1921 | case BuiltinType::Float: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1922 | Width = Target->getFloatWidth(); |
| 1923 | Align = Target->getFloatAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1924 | break; |
| 1925 | case BuiltinType::Double: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1926 | Width = Target->getDoubleWidth(); |
| 1927 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1928 | break; |
| 1929 | case BuiltinType::LongDouble: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1930 | Width = Target->getLongDoubleWidth(); |
| 1931 | Align = Target->getLongDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1932 | break; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1933 | case BuiltinType::Float128: |
| 1934 | Width = Target->getFloat128Width(); |
| 1935 | Align = Target->getFloat128Align(); |
| 1936 | break; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1937 | case BuiltinType::NullPtr: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1938 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 1939 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | a81b0b7 | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 1940 | break; |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1941 | case BuiltinType::ObjCId: |
| 1942 | case BuiltinType::ObjCClass: |
| 1943 | case BuiltinType::ObjCSel: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1944 | Width = Target->getPointerWidth(0); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1945 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 1946 | break; |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 1947 | case BuiltinType::OCLSampler: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1948 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1949 | case BuiltinType::OCLClkEvent: |
| 1950 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1951 | case BuiltinType::OCLReserveID: |
Yaxun Liu | 99444cb | 2016-08-03 20:38:06 +0000 | [diff] [blame] | 1952 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1953 | case BuiltinType::Id: |
| 1954 | #include "clang/Basic/OpenCLImageTypes.def" |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 1955 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
| 1956 | case BuiltinType::Id: |
| 1957 | #include "clang/Basic/OpenCLExtensionTypes.def" |
Sven van Haastregt | 3bb7eaf | 2017-12-06 10:11:28 +0000 | [diff] [blame] | 1958 | AS = getTargetAddressSpace( |
| 1959 | Target->getOpenCLTypeAddrSpace(getOpenCLTypeKind(T))); |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 1960 | Width = Target->getPointerWidth(AS); |
| 1961 | Align = Target->getPointerAlign(AS); |
| 1962 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1963 | } |
Chris Lattner | 48f84b8 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 1964 | break; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1965 | case Type::ObjCObjectPointer: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1966 | Width = Target->getPointerWidth(0); |
| 1967 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1968 | break; |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 1969 | case Type::BlockPointer: |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 1970 | AS = getTargetAddressSpace(cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1971 | Width = Target->getPointerWidth(AS); |
| 1972 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 1973 | break; |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1974 | case Type::LValueReference: |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 1975 | case Type::RValueReference: |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1976 | // alignof and sizeof should never enter this code path here, so we go |
| 1977 | // the pointer route. |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 1978 | AS = getTargetAddressSpace(cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1979 | Width = Target->getPointerWidth(AS); |
| 1980 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1981 | break; |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 1982 | case Type::Pointer: |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 1983 | AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1984 | Width = Target->getPointerWidth(AS); |
| 1985 | Align = Target->getPointerAlign(AS); |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 1986 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1987 | case Type::MemberPointer: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1988 | const auto *MPT = cast<MemberPointerType>(T); |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 1989 | CXXABI::MemberPointerInfo MPI = ABI->getMemberPointerInfo(MPT); |
| 1990 | Width = MPI.Width; |
| 1991 | Align = MPI.Align; |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 1992 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 1993 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1994 | case Type::Complex: { |
| 1995 | // Complex types have the same alignment as their elements, but twice the |
| 1996 | // size. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1997 | TypeInfo EltInfo = getTypeInfo(cast<ComplexType>(T)->getElementType()); |
| 1998 | Width = EltInfo.Width * 2; |
| 1999 | Align = EltInfo.Align; |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 2000 | break; |
| 2001 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2002 | case Type::ObjCObject: |
| 2003 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2004 | case Type::Adjusted: |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2005 | case Type::Decayed: |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2006 | return getTypeInfo(cast<AdjustedType>(T)->getAdjustedType().getTypePtr()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 2007 | case Type::ObjCInterface: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2008 | const auto *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 2009 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 2010 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 2011 | Align = toBits(Layout.getAlignment()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 2012 | break; |
| 2013 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2014 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2015 | case Type::Enum: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2016 | const auto *TT = cast<TagType>(T); |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 2017 | |
| 2018 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 7f97189 | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 2019 | Width = 8; |
| 2020 | Align = 8; |
Chris Lattner | 572100b | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 2021 | break; |
| 2022 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2023 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2024 | if (const auto *ET = dyn_cast<EnumType>(TT)) { |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 2025 | const EnumDecl *ED = ET->getDecl(); |
| 2026 | TypeInfo Info = |
| 2027 | getTypeInfo(ED->getIntegerType()->getUnqualifiedDesugaredType()); |
| 2028 | if (unsigned AttrAlign = ED->getMaxAlignment()) { |
| 2029 | Info.Align = AttrAlign; |
| 2030 | Info.AlignIsRequired = true; |
| 2031 | } |
| 2032 | return Info; |
| 2033 | } |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 2034 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2035 | const auto *RT = cast<RecordType>(TT); |
David Majnemer | 5821ff7 | 2015-02-03 08:49:29 +0000 | [diff] [blame] | 2036 | const RecordDecl *RD = RT->getDecl(); |
| 2037 | const ASTRecordLayout &Layout = getASTRecordLayout(RD); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 2038 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 2039 | Align = toBits(Layout.getAlignment()); |
David Majnemer | 5821ff7 | 2015-02-03 08:49:29 +0000 | [diff] [blame] | 2040 | AlignIsRequired = RD->hasAttr<AlignedAttr>(); |
Chris Lattner | 49a953a | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 2041 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2042 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2043 | |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 2044 | case Type::SubstTemplateTypeParm: |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2045 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 2046 | getReplacementType().getTypePtr()); |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2047 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 2048 | case Type::Auto: |
| 2049 | case Type::DeducedTemplateSpecialization: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2050 | const auto *A = cast<DeducedType>(T); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 2051 | assert(!A->getDeducedType().isNull() && |
| 2052 | "cannot request the size of an undeduced or dependent auto type"); |
Matt Beaumont-Gay | 7a24210e | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 2053 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2056 | case Type::Paren: |
| 2057 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 2058 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 2059 | case Type::ObjCTypeParam: |
| 2060 | return getTypeInfo(cast<ObjCTypeParamType>(T)->desugar().getTypePtr()); |
| 2061 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 2062 | case Type::Typedef: { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2063 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2064 | TypeInfo Info = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 2065 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 2066 | // alignment we have. This violates the GCC documentation (which says that |
| 2067 | // attribute(aligned) can only round up) but matches its implementation. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2068 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) { |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 2069 | Align = AttrAlign; |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2070 | AlignIsRequired = true; |
David Majnemer | 37bffb6 | 2014-08-04 05:11:01 +0000 | [diff] [blame] | 2071 | } else { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2072 | Align = Info.Align; |
David Majnemer | 37bffb6 | 2014-08-04 05:11:01 +0000 | [diff] [blame] | 2073 | AlignIsRequired = Info.AlignIsRequired; |
| 2074 | } |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2075 | Width = Info.Width; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2076 | break; |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 2077 | } |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 2078 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2079 | case Type::Elaborated: |
| 2080 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2081 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2082 | case Type::Attributed: |
| 2083 | return getTypeInfo( |
| 2084 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 2085 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2086 | case Type::Atomic: { |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 2087 | // Start with the base type information. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2088 | TypeInfo Info = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 2089 | Width = Info.Width; |
| 2090 | Align = Info.Align; |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 2091 | |
JF Bastien | 801fca2 | 2018-05-09 03:51:12 +0000 | [diff] [blame] | 2092 | if (!Width) { |
| 2093 | // An otherwise zero-sized type should still generate an |
| 2094 | // atomic operation. |
| 2095 | Width = Target->getCharWidth(); |
| 2096 | assert(Align); |
| 2097 | } else if (Width <= Target->getMaxAtomicPromoteWidth()) { |
| 2098 | // If the size of the type doesn't exceed the platform's max |
| 2099 | // atomic promotion width, make the size and alignment more |
| 2100 | // favorable to atomic operations: |
| 2101 | |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 2102 | // Round the size up to a power of 2. |
| 2103 | if (!llvm::isPowerOf2_64(Width)) |
| 2104 | Width = llvm::NextPowerOf2(Width); |
| 2105 | |
| 2106 | // Set the alignment equal to the size. |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 2107 | Align = static_cast<unsigned>(Width); |
| 2108 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2109 | } |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 2110 | break; |
| 2111 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2112 | case Type::Pipe: |
Anastasia Stulova | b339893 | 2017-06-05 11:27:03 +0000 | [diff] [blame] | 2113 | Width = Target->getPointerWidth(getTargetAddressSpace(LangAS::opencl_global)); |
| 2114 | Align = Target->getPointerAlign(getTargetAddressSpace(LangAS::opencl_global)); |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2115 | break; |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 2116 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 2118 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2119 | return TypeInfo(Width, Align, AlignIsRequired); |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 2122 | unsigned ASTContext::getTypeUnadjustedAlign(const Type *T) const { |
| 2123 | UnadjustedAlignMap::iterator I = MemoizedUnadjustedAlign.find(T); |
| 2124 | if (I != MemoizedUnadjustedAlign.end()) |
| 2125 | return I->second; |
| 2126 | |
| 2127 | unsigned UnadjustedAlign; |
| 2128 | if (const auto *RT = T->getAs<RecordType>()) { |
| 2129 | const RecordDecl *RD = RT->getDecl(); |
| 2130 | const ASTRecordLayout &Layout = getASTRecordLayout(RD); |
| 2131 | UnadjustedAlign = toBits(Layout.getUnadjustedAlignment()); |
| 2132 | } else if (const auto *ObjCI = T->getAs<ObjCInterfaceType>()) { |
| 2133 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 2134 | UnadjustedAlign = toBits(Layout.getUnadjustedAlignment()); |
| 2135 | } else { |
| 2136 | UnadjustedAlign = getTypeAlign(T); |
| 2137 | } |
| 2138 | |
| 2139 | MemoizedUnadjustedAlign[T] = UnadjustedAlign; |
| 2140 | return UnadjustedAlign; |
| 2141 | } |
| 2142 | |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2143 | unsigned ASTContext::getOpenMPDefaultSimdAlign(QualType T) const { |
| 2144 | unsigned SimdAlign = getTargetInfo().getSimdDefaultAlign(); |
| 2145 | // Target ppc64 with QPX: simd default alignment for pointer to double is 32. |
| 2146 | if ((getTargetInfo().getTriple().getArch() == llvm::Triple::ppc64 || |
| 2147 | getTargetInfo().getTriple().getArch() == llvm::Triple::ppc64le) && |
| 2148 | getTargetInfo().getABI() == "elfv1-qpx" && |
| 2149 | T->isSpecificBuiltinType(BuiltinType::Double)) |
| 2150 | SimdAlign = 256; |
| 2151 | return SimdAlign; |
| 2152 | } |
| 2153 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 2154 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 2155 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 2156 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 2157 | } |
| 2158 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 2159 | /// toBits - Convert a size in characters to a size in characters. |
| 2160 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 2161 | return CharSize.getQuantity() * getCharWidth(); |
| 2162 | } |
| 2163 | |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 2164 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 2165 | /// This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2166 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 2167 | return getTypeInfoInChars(T).first; |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 2168 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2169 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 2170 | return getTypeInfoInChars(T).first; |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2173 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 2174 | /// characters. This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2175 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 2176 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 2177 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2178 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 2179 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 2182 | /// getTypeUnadjustedAlignInChars - Return the ABI-specified alignment of a |
| 2183 | /// type, in characters, before alignment adustments. This method does |
| 2184 | /// not work on incomplete types. |
| 2185 | CharUnits ASTContext::getTypeUnadjustedAlignInChars(QualType T) const { |
| 2186 | return toCharUnitsFromBits(getTypeUnadjustedAlign(T)); |
| 2187 | } |
| 2188 | CharUnits ASTContext::getTypeUnadjustedAlignInChars(const Type *T) const { |
| 2189 | return toCharUnitsFromBits(getTypeUnadjustedAlign(T)); |
| 2190 | } |
| 2191 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 2192 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 2193 | /// type for the current target in bits. This can be different than the ABI |
| 2194 | /// alignment in cases where it is beneficial for performance to overalign |
| 2195 | /// a data type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2196 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2197 | TypeInfo TI = getTypeInfo(T); |
| 2198 | unsigned ABIAlign = TI.Align; |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2199 | |
David Majnemer | e154456 | 2015-04-24 01:25:05 +0000 | [diff] [blame] | 2200 | T = T->getBaseElementTypeUnsafe(); |
| 2201 | |
| 2202 | // The preferred alignment of member pointers is that of a pointer. |
| 2203 | if (T->isMemberPointerType()) |
| 2204 | return getPreferredTypeAlign(getPointerDiffType().getTypePtr()); |
| 2205 | |
Andrey Turetskiy | db6655f | 2016-02-10 11:58:46 +0000 | [diff] [blame] | 2206 | if (!Target->allowsLargerPreferedTypeAlignment()) |
| 2207 | return ABIAlign; |
Robert Lytton | eaf6f36 | 2013-11-12 10:09:34 +0000 | [diff] [blame] | 2208 | |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2209 | // Double and long long should be naturally aligned if possible. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2210 | if (const auto *CT = T->getAs<ComplexType>()) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2211 | T = CT->getElementType().getTypePtr(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2212 | if (const auto *ET = T->getAs<EnumType>()) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 2213 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2214 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | b57321a | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 2215 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2216 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
David Majnemer | 8b6bd57 | 2014-02-24 23:34:17 +0000 | [diff] [blame] | 2217 | // Don't increase the alignment if an alignment attribute was specified on a |
| 2218 | // typedef declaration. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2219 | if (!TI.AlignIsRequired) |
David Majnemer | 8b6bd57 | 2014-02-24 23:34:17 +0000 | [diff] [blame] | 2220 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2221 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 2222 | return ABIAlign; |
| 2223 | } |
| 2224 | |
Ulrich Weigand | ca3cb7f | 2015-04-21 17:29:35 +0000 | [diff] [blame] | 2225 | /// getTargetDefaultAlignForAttributeAligned - Return the default alignment |
| 2226 | /// for __attribute__((aligned)) on this target, to be used if no alignment |
| 2227 | /// value is specified. |
Eugene Zelenko | d4304d2 | 2015-11-04 21:37:17 +0000 | [diff] [blame] | 2228 | unsigned ASTContext::getTargetDefaultAlignForAttributeAligned() const { |
Ulrich Weigand | ca3cb7f | 2015-04-21 17:29:35 +0000 | [diff] [blame] | 2229 | return getTargetInfo().getDefaultAlignForAttributeAligned(); |
| 2230 | } |
| 2231 | |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 2232 | /// getAlignOfGlobalVar - Return the alignment in bits that should be given |
| 2233 | /// to a global variable of the specified type. |
| 2234 | unsigned ASTContext::getAlignOfGlobalVar(QualType T) const { |
| 2235 | return std::max(getTypeAlign(T), getTargetInfo().getMinGlobalAlign()); |
| 2236 | } |
| 2237 | |
| 2238 | /// getAlignOfGlobalVarInChars - Return the alignment in characters that |
| 2239 | /// should be given to a global variable of the specified type. |
| 2240 | CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T) const { |
| 2241 | return toCharUnitsFromBits(getAlignOfGlobalVar(T)); |
| 2242 | } |
| 2243 | |
David Majnemer | 08ef2ba | 2015-06-23 20:34:18 +0000 | [diff] [blame] | 2244 | CharUnits ASTContext::getOffsetOfBaseWithVBPtr(const CXXRecordDecl *RD) const { |
| 2245 | CharUnits Offset = CharUnits::Zero(); |
| 2246 | const ASTRecordLayout *Layout = &getASTRecordLayout(RD); |
| 2247 | while (const CXXRecordDecl *Base = Layout->getBaseSharingVBPtr()) { |
| 2248 | Offset += Layout->getBaseClassOffset(Base); |
| 2249 | Layout = &getASTRecordLayout(Base); |
| 2250 | } |
| 2251 | return Offset; |
| 2252 | } |
| 2253 | |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2254 | /// DeepCollectObjCIvars - |
| 2255 | /// This routine first collects all declared, but not synthesized, ivars in |
| 2256 | /// super class and then collects all ivars, including those synthesized for |
| 2257 | /// current class. This routine is used for implementation of current class |
| 2258 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2259 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 2260 | bool leafClass, |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2261 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2262 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 2263 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 2264 | if (!leafClass) { |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 2265 | for (const auto *I : OI->ivars()) |
| 2266 | Ivars.push_back(I); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2267 | } else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2268 | auto *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2269 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 2270 | Iv= Iv->getNextIvar()) |
| 2271 | Ivars.push_back(Iv); |
| 2272 | } |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2275 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 2276 | /// those inherited by it. |
| 2277 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 2278 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2279 | if (const auto *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2280 | // We can use protocol_iterator here instead of |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2281 | // all_referenced_protocol_iterator since we are walking all categories. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2282 | for (auto *Proto : OI->all_referenced_protocols()) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 2283 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 2284 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2285 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2286 | // Categories of this Interface. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 2287 | for (const auto *Cat : OI->visible_categories()) |
| 2288 | CollectInheritedProtocols(Cat, Protocols); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2289 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2290 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 2291 | while (SD) { |
| 2292 | CollectInheritedProtocols(SD, Protocols); |
| 2293 | SD = SD->getSuperClass(); |
| 2294 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2295 | } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 2296 | for (auto *Proto : OC->protocols()) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 2297 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2298 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2299 | } else if (const auto *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 2300 | // Insert the protocol. |
| 2301 | if (!Protocols.insert( |
| 2302 | const_cast<ObjCProtocolDecl *>(OP->getCanonicalDecl())).second) |
| 2303 | return; |
| 2304 | |
| 2305 | for (auto *Proto : OP->protocols()) |
| 2306 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2307 | } |
| 2308 | } |
| 2309 | |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2310 | static bool unionHasUniqueObjectRepresentations(const ASTContext &Context, |
| 2311 | const RecordDecl *RD) { |
| 2312 | assert(RD->isUnion() && "Must be union type"); |
| 2313 | CharUnits UnionSize = Context.getTypeSizeInChars(RD->getTypeForDecl()); |
| 2314 | |
| 2315 | for (const auto *Field : RD->fields()) { |
| 2316 | if (!Context.hasUniqueObjectRepresentations(Field->getType())) |
| 2317 | return false; |
| 2318 | CharUnits FieldSize = Context.getTypeSizeInChars(Field->getType()); |
| 2319 | if (FieldSize != UnionSize) |
| 2320 | return false; |
| 2321 | } |
Eric Fiselier | 12a9f34 | 2018-02-02 20:30:39 +0000 | [diff] [blame] | 2322 | return !RD->field_empty(); |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
Benjamin Kramer | 802e625 | 2017-12-24 12:46:22 +0000 | [diff] [blame] | 2325 | static bool isStructEmpty(QualType Ty) { |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2326 | const RecordDecl *RD = Ty->castAs<RecordType>()->getDecl(); |
| 2327 | |
| 2328 | if (!RD->field_empty()) |
| 2329 | return false; |
| 2330 | |
| 2331 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RD)) |
| 2332 | return ClassDecl->isEmpty(); |
| 2333 | |
| 2334 | return true; |
| 2335 | } |
| 2336 | |
| 2337 | static llvm::Optional<int64_t> |
| 2338 | structHasUniqueObjectRepresentations(const ASTContext &Context, |
| 2339 | const RecordDecl *RD) { |
| 2340 | assert(!RD->isUnion() && "Must be struct/class type"); |
| 2341 | const auto &Layout = Context.getASTRecordLayout(RD); |
| 2342 | |
| 2343 | int64_t CurOffsetInBits = 0; |
| 2344 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RD)) { |
| 2345 | if (ClassDecl->isDynamicClass()) |
| 2346 | return llvm::None; |
| 2347 | |
| 2348 | SmallVector<std::pair<QualType, int64_t>, 4> Bases; |
| 2349 | for (const auto Base : ClassDecl->bases()) { |
| 2350 | // Empty types can be inherited from, and non-empty types can potentially |
| 2351 | // have tail padding, so just make sure there isn't an error. |
| 2352 | if (!isStructEmpty(Base.getType())) { |
| 2353 | llvm::Optional<int64_t> Size = structHasUniqueObjectRepresentations( |
| 2354 | Context, Base.getType()->getAs<RecordType>()->getDecl()); |
| 2355 | if (!Size) |
| 2356 | return llvm::None; |
| 2357 | Bases.emplace_back(Base.getType(), Size.getValue()); |
| 2358 | } |
| 2359 | } |
| 2360 | |
Fangrui Song | 1d38c13 | 2018-09-30 21:41:11 +0000 | [diff] [blame] | 2361 | llvm::sort(Bases, [&](const std::pair<QualType, int64_t> &L, |
| 2362 | const std::pair<QualType, int64_t> &R) { |
| 2363 | return Layout.getBaseClassOffset(L.first->getAsCXXRecordDecl()) < |
| 2364 | Layout.getBaseClassOffset(R.first->getAsCXXRecordDecl()); |
| 2365 | }); |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2366 | |
| 2367 | for (const auto Base : Bases) { |
| 2368 | int64_t BaseOffset = Context.toBits( |
| 2369 | Layout.getBaseClassOffset(Base.first->getAsCXXRecordDecl())); |
| 2370 | int64_t BaseSize = Base.second; |
| 2371 | if (BaseOffset != CurOffsetInBits) |
| 2372 | return llvm::None; |
| 2373 | CurOffsetInBits = BaseOffset + BaseSize; |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | for (const auto *Field : RD->fields()) { |
| 2378 | if (!Field->getType()->isReferenceType() && |
| 2379 | !Context.hasUniqueObjectRepresentations(Field->getType())) |
| 2380 | return llvm::None; |
| 2381 | |
| 2382 | int64_t FieldSizeInBits = |
| 2383 | Context.toBits(Context.getTypeSizeInChars(Field->getType())); |
| 2384 | if (Field->isBitField()) { |
| 2385 | int64_t BitfieldSize = Field->getBitWidthValue(Context); |
| 2386 | |
| 2387 | if (BitfieldSize > FieldSizeInBits) |
| 2388 | return llvm::None; |
| 2389 | FieldSizeInBits = BitfieldSize; |
| 2390 | } |
| 2391 | |
| 2392 | int64_t FieldOffsetInBits = Context.getFieldOffset(Field); |
| 2393 | |
| 2394 | if (FieldOffsetInBits != CurOffsetInBits) |
| 2395 | return llvm::None; |
| 2396 | |
| 2397 | CurOffsetInBits = FieldSizeInBits + FieldOffsetInBits; |
| 2398 | } |
| 2399 | |
| 2400 | return CurOffsetInBits; |
| 2401 | } |
| 2402 | |
| 2403 | bool ASTContext::hasUniqueObjectRepresentations(QualType Ty) const { |
| 2404 | // C++17 [meta.unary.prop]: |
| 2405 | // The predicate condition for a template specialization |
| 2406 | // has_unique_object_representations<T> shall be |
| 2407 | // satisfied if and only if: |
| 2408 | // (9.1) - T is trivially copyable, and |
| 2409 | // (9.2) - any two objects of type T with the same value have the same |
| 2410 | // object representation, where two objects |
| 2411 | // of array or non-union class type are considered to have the same value |
| 2412 | // if their respective sequences of |
| 2413 | // direct subobjects have the same values, and two objects of union type |
| 2414 | // are considered to have the same |
| 2415 | // value if they have the same active member and the corresponding members |
| 2416 | // have the same value. |
| 2417 | // The set of scalar types for which this condition holds is |
| 2418 | // implementation-defined. [ Note: If a type has padding |
| 2419 | // bits, the condition does not hold; otherwise, the condition holds true |
| 2420 | // for unsigned integral types. -- end note ] |
| 2421 | assert(!Ty.isNull() && "Null QualType sent to unique object rep check"); |
| 2422 | |
| 2423 | // Arrays are unique only if their element type is unique. |
| 2424 | if (Ty->isArrayType()) |
| 2425 | return hasUniqueObjectRepresentations(getBaseElementType(Ty)); |
| 2426 | |
| 2427 | // (9.1) - T is trivially copyable... |
| 2428 | if (!Ty.isTriviallyCopyableType(*this)) |
| 2429 | return false; |
| 2430 | |
| 2431 | // All integrals and enums are unique. |
| 2432 | if (Ty->isIntegralOrEnumerationType()) |
| 2433 | return true; |
| 2434 | |
| 2435 | // All other pointers are unique. |
| 2436 | if (Ty->isPointerType()) |
| 2437 | return true; |
| 2438 | |
| 2439 | if (Ty->isMemberPointerType()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2440 | const auto *MPT = Ty->getAs<MemberPointerType>(); |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2441 | return !ABI->getMemberPointerInfo(MPT).HasPadding; |
| 2442 | } |
| 2443 | |
| 2444 | if (Ty->isRecordType()) { |
| 2445 | const RecordDecl *Record = Ty->getAs<RecordType>()->getDecl(); |
| 2446 | |
Erich Keane | bd2197c | 2017-12-12 16:02:06 +0000 | [diff] [blame] | 2447 | if (Record->isInvalidDecl()) |
| 2448 | return false; |
| 2449 | |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2450 | if (Record->isUnion()) |
| 2451 | return unionHasUniqueObjectRepresentations(*this, Record); |
| 2452 | |
| 2453 | Optional<int64_t> StructSize = |
| 2454 | structHasUniqueObjectRepresentations(*this, Record); |
| 2455 | |
| 2456 | return StructSize && |
| 2457 | StructSize.getValue() == static_cast<int64_t>(getTypeSize(Ty)); |
| 2458 | } |
| 2459 | |
| 2460 | // FIXME: More cases to handle here (list by rsmith): |
| 2461 | // vectors (careful about, eg, vector of 3 foo) |
| 2462 | // _Complex int and friends |
| 2463 | // _Atomic T |
| 2464 | // Obj-C block pointers |
| 2465 | // Obj-C object pointers |
| 2466 | // and perhaps OpenCL's various builtin types (pipe, sampler_t, event_t, |
| 2467 | // clk_event_t, queue_t, reserve_id_t) |
| 2468 | // There're also Obj-C class types and the Obj-C selector type, but I think it |
| 2469 | // makes sense for those to return false here. |
| 2470 | |
| 2471 | return false; |
| 2472 | } |
| 2473 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2474 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2475 | unsigned count = 0; |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 2476 | // Count ivars declared in class extension. |
Aaron Ballman | b4a5345 | 2014-03-13 21:57:01 +0000 | [diff] [blame] | 2477 | for (const auto *Ext : OI->known_extensions()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2478 | count += Ext->ivar_size(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2479 | |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 2480 | // Count ivar defined in this class's implementation. This |
| 2481 | // includes synthesized ivars. |
| 2482 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 2483 | count += ImplDecl->ivar_size(); |
| 2484 | |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2485 | return count; |
| 2486 | } |
| 2487 | |
Argyrios Kyrtzidis | 2e809ce | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 2488 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 2489 | if (!E) |
| 2490 | return false; |
| 2491 | |
| 2492 | // nullptr_t is always treated as null. |
| 2493 | if (E->getType()->isNullPtrType()) return true; |
| 2494 | |
| 2495 | if (E->getType()->isAnyPointerType() && |
| 2496 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 2497 | Expr::NPC_ValueDependentIsNull)) |
| 2498 | return true; |
| 2499 | |
| 2500 | // Unfortunately, __null has type 'int'. |
| 2501 | if (isa<GNUNullExpr>(E)) return true; |
| 2502 | |
| 2503 | return false; |
| 2504 | } |
| 2505 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2506 | /// Get the implementation of ObjCInterfaceDecl, or nullptr if none |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2507 | /// exists. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2508 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 2509 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 2510 | I = ObjCImpls.find(D); |
| 2511 | if (I != ObjCImpls.end()) |
| 2512 | return cast<ObjCImplementationDecl>(I->second); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2513 | return nullptr; |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2514 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2515 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2516 | /// Get the implementation of ObjCCategoryDecl, or nullptr if none |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2517 | /// exists. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2518 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 2519 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 2520 | I = ObjCImpls.find(D); |
| 2521 | if (I != ObjCImpls.end()) |
| 2522 | return cast<ObjCCategoryImplDecl>(I->second); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2523 | return nullptr; |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2526 | /// Set the implementation of ObjCInterfaceDecl. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2527 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 2528 | ObjCImplementationDecl *ImplD) { |
| 2529 | assert(IFaceD && ImplD && "Passed null params"); |
| 2530 | ObjCImpls[IFaceD] = ImplD; |
| 2531 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2532 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2533 | /// Set the implementation of ObjCCategoryDecl. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2534 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 2535 | ObjCCategoryImplDecl *ImplD) { |
| 2536 | assert(CatD && ImplD && "Passed null params"); |
| 2537 | ObjCImpls[CatD] = ImplD; |
| 2538 | } |
| 2539 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 2540 | const ObjCMethodDecl * |
| 2541 | ASTContext::getObjCMethodRedeclaration(const ObjCMethodDecl *MD) const { |
| 2542 | return ObjCMethodRedecls.lookup(MD); |
| 2543 | } |
| 2544 | |
| 2545 | void ASTContext::setObjCMethodRedeclaration(const ObjCMethodDecl *MD, |
| 2546 | const ObjCMethodDecl *Redecl) { |
| 2547 | assert(!getObjCMethodRedeclaration(MD) && "MD already has a redeclaration"); |
| 2548 | ObjCMethodRedecls[MD] = Redecl; |
| 2549 | } |
| 2550 | |
Dmitri Gribenko | 37527c2 | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 2551 | const ObjCInterfaceDecl *ASTContext::getObjContainingInterface( |
| 2552 | const NamedDecl *ND) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2553 | if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2554 | return ID; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2555 | if (const auto *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2556 | return CD->getClassInterface(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2557 | if (const auto *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2558 | return IMD->getClassInterface(); |
| 2559 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2560 | return nullptr; |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2563 | /// Get the copy initialization expression of VarDecl, or nullptr if |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2564 | /// none exists. |
Akira Hatanaka | 9978da3 | 2018-08-10 15:09:24 +0000 | [diff] [blame] | 2565 | ASTContext::BlockVarCopyInit |
| 2566 | ASTContext::getBlockVarCopyInit(const VarDecl*VD) const { |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2567 | assert(VD && "Passed null params"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2568 | assert(VD->hasAttr<BlocksAttr>() && |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2569 | "getBlockVarCopyInits - not __block var"); |
Akira Hatanaka | 9978da3 | 2018-08-10 15:09:24 +0000 | [diff] [blame] | 2570 | auto I = BlockVarCopyInits.find(VD); |
| 2571 | if (I != BlockVarCopyInits.end()) |
| 2572 | return I->second; |
| 2573 | return {nullptr, false}; |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2576 | /// Set the copy inialization expression of a block var decl. |
Akira Hatanaka | 9978da3 | 2018-08-10 15:09:24 +0000 | [diff] [blame] | 2577 | void ASTContext::setBlockVarCopyInit(const VarDecl*VD, Expr *CopyExpr, |
| 2578 | bool CanThrow) { |
| 2579 | assert(VD && CopyExpr && "Passed null params"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2580 | assert(VD->hasAttr<BlocksAttr>() && |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2581 | "setBlockVarCopyInits - not __block var"); |
Akira Hatanaka | 9978da3 | 2018-08-10 15:09:24 +0000 | [diff] [blame] | 2582 | BlockVarCopyInits[VD].setExprAndFlag(CopyExpr, CanThrow); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2585 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2586 | unsigned DataSize) const { |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 2587 | if (!DataSize) |
| 2588 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 2589 | else |
| 2590 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2591 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 2592 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2593 | auto *TInfo = |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2594 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 2595 | new (TInfo) TypeSourceInfo(T); |
| 2596 | return TInfo; |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2599 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2600 | SourceLocation L) const { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2601 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | 2d525f0 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2602 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | 3665e00 | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 2603 | return DI; |
| 2604 | } |
| 2605 | |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2606 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2607 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2608 | return getObjCLayout(D, nullptr); |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2612 | ASTContext::getASTObjCImplementationLayout( |
| 2613 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2614 | return getObjCLayout(D->getClassInterface(), D); |
| 2615 | } |
| 2616 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2617 | //===----------------------------------------------------------------------===// |
| 2618 | // Type creation/memoization methods |
| 2619 | //===----------------------------------------------------------------------===// |
| 2620 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2621 | QualType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2622 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 2623 | unsigned fastQuals = quals.getFastQualifiers(); |
| 2624 | quals.removeFastQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2625 | |
| 2626 | // Check if we've already instantiated this type. |
| 2627 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2628 | ExtQuals::Profile(ID, baseType, quals); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2629 | void *insertPos = nullptr; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2630 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 2631 | assert(eq->getQualifiers() == quals); |
| 2632 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2635 | // If the base type is not canonical, make the appropriate canonical type. |
| 2636 | QualType canon; |
| 2637 | if (!baseType->isCanonicalUnqualified()) { |
| 2638 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2639 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 2640 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2641 | |
| 2642 | // Re-find the insert position. |
| 2643 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 2644 | } |
| 2645 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2646 | auto *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2647 | ExtQualNodes.InsertNode(eq, insertPos); |
| 2648 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 2651 | QualType ASTContext::getAddrSpaceQualType(QualType T, |
| 2652 | LangAS AddressSpace) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2653 | QualType CanT = getCanonicalType(T); |
| 2654 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | 445fcab | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2655 | return T; |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2656 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2657 | // If we are composing extended qualifiers together, merge together |
| 2658 | // into one ExtQuals node. |
| 2659 | QualifierCollector Quals; |
| 2660 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2661 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2662 | // If this type already has an address space specified, it cannot get |
| 2663 | // another one. |
| 2664 | assert(!Quals.hasAddressSpace() && |
| 2665 | "Type cannot be in multiple addr spaces!"); |
| 2666 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2667 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2668 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | 025b5fb | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 2669 | } |
| 2670 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 2671 | QualType ASTContext::removeAddrSpaceQualType(QualType T) const { |
| 2672 | // If we are composing extended qualifiers together, merge together |
| 2673 | // into one ExtQuals node. |
| 2674 | QualifierCollector Quals; |
| 2675 | const Type *TypeNode = Quals.strip(T); |
| 2676 | |
| 2677 | // If the qualifier doesn't have an address space just return it. |
| 2678 | if (!Quals.hasAddressSpace()) |
| 2679 | return T; |
| 2680 | |
| 2681 | Quals.removeAddressSpace(); |
| 2682 | |
| 2683 | // Removal of the address space can mean there are no longer any |
| 2684 | // non-fast qualifiers, so creating an ExtQualType isn't possible (asserts) |
| 2685 | // or required. |
| 2686 | if (Quals.hasNonFastQualifiers()) |
| 2687 | return getExtQualType(TypeNode, Quals); |
| 2688 | else |
| 2689 | return QualType(TypeNode, Quals.getFastQualifiers()); |
| 2690 | } |
| 2691 | |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2692 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2693 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2694 | QualType CanT = getCanonicalType(T); |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2695 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2696 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2697 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2698 | if (const auto *ptr = T->getAs<PointerType>()) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2699 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 2700 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | b68215c | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 2701 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 2702 | return getPointerType(ResultType); |
| 2703 | } |
| 2704 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2705 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2706 | // If we are composing extended qualifiers together, merge together |
| 2707 | // into one ExtQuals node. |
| 2708 | QualifierCollector Quals; |
| 2709 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2710 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2711 | // If this type already has an ObjCGC specified, it cannot get |
| 2712 | // another one. |
| 2713 | assert(!Quals.hasObjCGCAttr() && |
| 2714 | "Type cannot have multiple ObjCGCs!"); |
| 2715 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2717 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2718 | } |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2719 | |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2720 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 2721 | FunctionType::ExtInfo Info) { |
| 2722 | if (T->getExtInfo() == Info) |
| 2723 | return T; |
| 2724 | |
| 2725 | QualType Result; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2726 | if (const auto *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2727 | Result = getFunctionNoProtoType(FNPT->getReturnType(), Info); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2728 | } else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2729 | const auto *FPT = cast<FunctionProtoType>(T); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2730 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 2731 | EPI.ExtInfo = Info; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2732 | Result = getFunctionType(FPT->getReturnType(), FPT->getParamTypes(), EPI); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | return cast<FunctionType>(Result.getTypePtr()); |
| 2736 | } |
| 2737 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2738 | void ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD, |
| 2739 | QualType ResultType) { |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2740 | FD = FD->getMostRecentDecl(); |
| 2741 | while (true) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2742 | const auto *FPT = FD->getType()->castAs<FunctionProtoType>(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2743 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2744 | FD->setType(getFunctionType(ResultType, FPT->getParamTypes(), EPI)); |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2745 | if (FunctionDecl *Next = FD->getPreviousDecl()) |
| 2746 | FD = Next; |
| 2747 | else |
| 2748 | break; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2749 | } |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2750 | if (ASTMutationListener *L = getASTMutationListener()) |
| 2751 | L->DeducedReturnType(FD, ResultType); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2754 | /// Get a function type and produce the equivalent function type with the |
| 2755 | /// specified exception specification. Type sugar that can be present on a |
| 2756 | /// declaration of a function with an exception specification is permitted |
| 2757 | /// and preserved. Other type sugar (for instance, typedefs) is not. |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2758 | QualType ASTContext::getFunctionTypeWithExceptionSpec( |
| 2759 | QualType Orig, const FunctionProtoType::ExceptionSpecInfo &ESI) { |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2760 | // Might have some parens. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2761 | if (const auto *PT = dyn_cast<ParenType>(Orig)) |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2762 | return getParenType( |
| 2763 | getFunctionTypeWithExceptionSpec(PT->getInnerType(), ESI)); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2764 | |
| 2765 | // Might have a calling-convention attribute. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2766 | if (const auto *AT = dyn_cast<AttributedType>(Orig)) |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2767 | return getAttributedType( |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2768 | AT->getAttrKind(), |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2769 | getFunctionTypeWithExceptionSpec(AT->getModifiedType(), ESI), |
| 2770 | getFunctionTypeWithExceptionSpec(AT->getEquivalentType(), ESI)); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2771 | |
| 2772 | // Anything else must be a function type. Rebuild it with the new exception |
| 2773 | // specification. |
Stephan Bergmann | 84dcc8f | 2019-02-13 09:39:17 +0000 | [diff] [blame] | 2774 | const auto *Proto = Orig->getAs<FunctionProtoType>(); |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2775 | return getFunctionType( |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2776 | Proto->getReturnType(), Proto->getParamTypes(), |
| 2777 | Proto->getExtProtoInfo().withExceptionSpec(ESI)); |
| 2778 | } |
| 2779 | |
Richard Smith | dfe85e2 | 2016-12-15 02:35:39 +0000 | [diff] [blame] | 2780 | bool ASTContext::hasSameFunctionTypeIgnoringExceptionSpec(QualType T, |
| 2781 | QualType U) { |
| 2782 | return hasSameType(T, U) || |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2783 | (getLangOpts().CPlusPlus17 && |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2784 | hasSameType(getFunctionTypeWithExceptionSpec(T, EST_None), |
| 2785 | getFunctionTypeWithExceptionSpec(U, EST_None))); |
Richard Smith | dfe85e2 | 2016-12-15 02:35:39 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2788 | void ASTContext::adjustExceptionSpec( |
| 2789 | FunctionDecl *FD, const FunctionProtoType::ExceptionSpecInfo &ESI, |
| 2790 | bool AsWritten) { |
| 2791 | // Update the type. |
| 2792 | QualType Updated = |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2793 | getFunctionTypeWithExceptionSpec(FD->getType(), ESI); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2794 | FD->setType(Updated); |
| 2795 | |
| 2796 | if (!AsWritten) |
| 2797 | return; |
| 2798 | |
| 2799 | // Update the type in the type source information too. |
| 2800 | if (TypeSourceInfo *TSInfo = FD->getTypeSourceInfo()) { |
| 2801 | // If the type and the type-as-written differ, we may need to update |
| 2802 | // the type-as-written too. |
| 2803 | if (TSInfo->getType() != FD->getType()) |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2804 | Updated = getFunctionTypeWithExceptionSpec(TSInfo->getType(), ESI); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2805 | |
| 2806 | // FIXME: When we get proper type location information for exceptions, |
| 2807 | // we'll also have to rebuild the TypeSourceInfo. For now, we just patch |
| 2808 | // up the TypeSourceInfo; |
| 2809 | assert(TypeLoc::getFullDataSizeForType(Updated) == |
| 2810 | TypeLoc::getFullDataSizeForType(TSInfo->getType()) && |
| 2811 | "TypeLoc size mismatch from updating exception specification"); |
| 2812 | TSInfo->overrideType(Updated); |
| 2813 | } |
| 2814 | } |
| 2815 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2816 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 2817 | /// number with the specified element type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2818 | QualType ASTContext::getComplexType(QualType T) const { |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2819 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2820 | // structure. |
| 2821 | llvm::FoldingSetNodeID ID; |
| 2822 | ComplexType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2823 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2824 | void *InsertPos = nullptr; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2825 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2826 | return QualType(CT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2827 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2828 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2829 | // so fill in the canonical type field. |
| 2830 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2831 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2832 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2833 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2834 | // Get the new insert position for the node we care about. |
| 2835 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2836 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2837 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2838 | auto *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2839 | Types.push_back(New); |
| 2840 | ComplexTypes.InsertNode(New, InsertPos); |
| 2841 | return QualType(New, 0); |
| 2842 | } |
| 2843 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 2844 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 2845 | /// the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2846 | QualType ASTContext::getPointerType(QualType T) const { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 2847 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2848 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2849 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2850 | PointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2852 | void *InsertPos = nullptr; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2853 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2854 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 2856 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2857 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2858 | QualType Canonical; |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2859 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2860 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2862 | // Get the new insert position for the node we care about. |
| 2863 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2864 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2865 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2866 | auto *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2867 | Types.push_back(New); |
| 2868 | PointerTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2869 | return QualType(New, 0); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2872 | QualType ASTContext::getAdjustedType(QualType Orig, QualType New) const { |
| 2873 | llvm::FoldingSetNodeID ID; |
| 2874 | AdjustedType::Profile(ID, Orig, New); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2875 | void *InsertPos = nullptr; |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2876 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2877 | if (AT) |
| 2878 | return QualType(AT, 0); |
| 2879 | |
| 2880 | QualType Canonical = getCanonicalType(New); |
| 2881 | |
| 2882 | // Get the new insert position for the node we care about. |
| 2883 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2884 | assert(!AT && "Shouldn't be in the map!"); |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2885 | |
| 2886 | AT = new (*this, TypeAlignment) |
| 2887 | AdjustedType(Type::Adjusted, Orig, New, Canonical); |
| 2888 | Types.push_back(AT); |
| 2889 | AdjustedTypes.InsertNode(AT, InsertPos); |
| 2890 | return QualType(AT, 0); |
| 2891 | } |
| 2892 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2893 | QualType ASTContext::getDecayedType(QualType T) const { |
| 2894 | assert((T->isArrayType() || T->isFunctionType()) && "T does not decay"); |
| 2895 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2896 | QualType Decayed; |
| 2897 | |
| 2898 | // C99 6.7.5.3p7: |
| 2899 | // A declaration of a parameter as "array of type" shall be |
| 2900 | // adjusted to "qualified pointer to type", where the type |
| 2901 | // qualifiers (if any) are those specified within the [ and ] of |
| 2902 | // the array type derivation. |
| 2903 | if (T->isArrayType()) |
| 2904 | Decayed = getArrayDecayedType(T); |
| 2905 | |
| 2906 | // C99 6.7.5.3p8: |
| 2907 | // A declaration of a parameter as "function returning type" |
| 2908 | // shall be adjusted to "pointer to function returning type", as |
| 2909 | // in 6.3.2.1. |
| 2910 | if (T->isFunctionType()) |
| 2911 | Decayed = getPointerType(T); |
| 2912 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2913 | llvm::FoldingSetNodeID ID; |
| 2914 | AdjustedType::Profile(ID, T, Decayed); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2915 | void *InsertPos = nullptr; |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2916 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2917 | if (AT) |
| 2918 | return QualType(AT, 0); |
| 2919 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2920 | QualType Canonical = getCanonicalType(Decayed); |
| 2921 | |
| 2922 | // Get the new insert position for the node we care about. |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2923 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2924 | assert(!AT && "Shouldn't be in the map!"); |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2925 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2926 | AT = new (*this, TypeAlignment) DecayedType(T, Decayed, Canonical); |
| 2927 | Types.push_back(AT); |
| 2928 | AdjustedTypes.InsertNode(AT, InsertPos); |
| 2929 | return QualType(AT, 0); |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2932 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2933 | /// a pointer to the specified block. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2934 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 0ac01283 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 2935 | assert(T->isFunctionType() && "block of function types only"); |
| 2936 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2937 | // structure. |
| 2938 | llvm::FoldingSetNodeID ID; |
| 2939 | BlockPointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2940 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2941 | void *InsertPos = nullptr; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2942 | if (BlockPointerType *PT = |
| 2943 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2944 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2945 | |
| 2946 | // 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] | 2947 | // type either so fill in the canonical type field. |
| 2948 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2949 | if (!T.isCanonical()) { |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2950 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2951 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2952 | // Get the new insert position for the node we care about. |
| 2953 | BlockPointerType *NewIP = |
| 2954 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2955 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2956 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2957 | auto *New = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 2958 | Types.push_back(New); |
| 2959 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 2960 | return QualType(New, 0); |
| 2961 | } |
| 2962 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2963 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 2964 | /// lvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2965 | QualType |
| 2966 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2967 | assert(getCanonicalType(T) != OverloadTy && |
Douglas Gregor | 291e8ee | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 2968 | "Unresolved overloaded function type"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2969 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2970 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2971 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2972 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2973 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2974 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2975 | void *InsertPos = nullptr; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2976 | if (LValueReferenceType *RT = |
| 2977 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2978 | return QualType(RT, 0); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2979 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2980 | const auto *InnerRef = T->getAs<ReferenceType>(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2981 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2982 | // If the referencee type isn't canonical, this won't be a canonical type |
| 2983 | // either, so fill in the canonical type field. |
| 2984 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2985 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 2986 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 2987 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2988 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2989 | // Get the new insert position for the node we care about. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2990 | LValueReferenceType *NewIP = |
| 2991 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2992 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2995 | auto *New = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 2996 | SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 2997 | Types.push_back(New); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 2998 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 2999 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3000 | return QualType(New, 0); |
| 3001 | } |
| 3002 | |
| 3003 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 3004 | /// rvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3005 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3006 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3007 | // structure. |
| 3008 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3009 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3010 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3011 | void *InsertPos = nullptr; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3012 | if (RValueReferenceType *RT = |
| 3013 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3014 | return QualType(RT, 0); |
| 3015 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3016 | const auto *InnerRef = T->getAs<ReferenceType>(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3017 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3018 | // If the referencee type isn't canonical, this won't be a canonical type |
| 3019 | // either, so fill in the canonical type field. |
| 3020 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3021 | if (InnerRef || !T.isCanonical()) { |
| 3022 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 3023 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3024 | |
| 3025 | // Get the new insert position for the node we care about. |
| 3026 | RValueReferenceType *NewIP = |
| 3027 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3028 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3031 | auto *New = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3032 | Types.push_back(New); |
| 3033 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 3034 | return QualType(New, 0); |
| 3035 | } |
| 3036 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3037 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 3038 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3039 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3040 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3041 | // structure. |
| 3042 | llvm::FoldingSetNodeID ID; |
| 3043 | MemberPointerType::Profile(ID, T, Cls); |
| 3044 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3045 | void *InsertPos = nullptr; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3046 | if (MemberPointerType *PT = |
| 3047 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3048 | return QualType(PT, 0); |
| 3049 | |
| 3050 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 3051 | // type either, so fill in the canonical type field. |
| 3052 | QualType Canonical; |
Douglas Gregor | 615ac67 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 3053 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3054 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 3055 | |
| 3056 | // Get the new insert position for the node we care about. |
| 3057 | MemberPointerType *NewIP = |
| 3058 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3059 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3060 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3061 | auto *New = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3062 | Types.push_back(New); |
| 3063 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 3064 | return QualType(New, 0); |
| 3065 | } |
| 3066 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 3068 | /// array of the specified element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3069 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 3070 | const llvm::APInt &ArySizeIn, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 3071 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3072 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 2dfdb82 | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 3073 | assert((EltTy->isDependentType() || |
| 3074 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 3075 | "Constant array of VLAs is illegal!"); |
| 3076 | |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 3077 | // Convert the array size into a canonical width matching the pointer size for |
| 3078 | // the target. |
| 3079 | llvm::APInt ArySize(ArySizeIn); |
Konstantin Zhuravlyov | 9c1e310 | 2017-03-21 18:55:39 +0000 | [diff] [blame] | 3080 | ArySize = ArySize.zextOrTrunc(Target->getMaxPointerWidth()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3081 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 3082 | llvm::FoldingSetNodeID ID; |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3083 | ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3084 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3085 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3086 | if (ConstantArrayType *ATP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 3087 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3088 | return QualType(ATP, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3089 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3090 | // If the element type isn't canonical or has qualifiers, this won't |
| 3091 | // be a canonical type either, so fill in the canonical type field. |
| 3092 | QualType Canon; |
| 3093 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 3094 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3095 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3096 | ASM, IndexTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3097 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3098 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 3099 | // Get the new insert position for the node we care about. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3100 | ConstantArrayType *NewIP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 3101 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3102 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 3103 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3104 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3105 | auto *New = new (*this,TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3106 | ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals); |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 3107 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 3108 | Types.push_back(New); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3109 | return QualType(New, 0); |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3112 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 3113 | /// variably-modified, into the corresponding type with all the known |
| 3114 | /// sizes replaced with [*]. |
| 3115 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 3116 | // Vastly most common case. |
| 3117 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3118 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3119 | QualType result; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3120 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3121 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3122 | const Type *ty = split.Ty; |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3123 | switch (ty->getTypeClass()) { |
| 3124 | #define TYPE(Class, Base) |
| 3125 | #define ABSTRACT_TYPE(Class, Base) |
| 3126 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 3127 | #include "clang/AST/TypeNodes.def" |
| 3128 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 3129 | |
| 3130 | // These types should never be variably-modified. |
| 3131 | case Type::Builtin: |
| 3132 | case Type::Complex: |
| 3133 | case Type::Vector: |
Erich Keane | f702b02 | 2018-07-13 19:46:04 +0000 | [diff] [blame] | 3134 | case Type::DependentVector: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3135 | case Type::ExtVector: |
| 3136 | case Type::DependentSizedExtVector: |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3137 | case Type::DependentAddressSpace: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3138 | case Type::ObjCObject: |
| 3139 | case Type::ObjCInterface: |
| 3140 | case Type::ObjCObjectPointer: |
| 3141 | case Type::Record: |
| 3142 | case Type::Enum: |
| 3143 | case Type::UnresolvedUsing: |
| 3144 | case Type::TypeOfExpr: |
| 3145 | case Type::TypeOf: |
| 3146 | case Type::Decltype: |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3147 | case Type::UnaryTransform: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3148 | case Type::DependentName: |
| 3149 | case Type::InjectedClassName: |
| 3150 | case Type::TemplateSpecialization: |
| 3151 | case Type::DependentTemplateSpecialization: |
| 3152 | case Type::TemplateTypeParm: |
| 3153 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3154 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 3155 | case Type::DeducedTemplateSpecialization: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3156 | case Type::PackExpansion: |
| 3157 | llvm_unreachable("type should never be variably-modified"); |
| 3158 | |
| 3159 | // These types can be variably-modified but should never need to |
| 3160 | // further decay. |
| 3161 | case Type::FunctionNoProto: |
| 3162 | case Type::FunctionProto: |
| 3163 | case Type::BlockPointer: |
| 3164 | case Type::MemberPointer: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3165 | case Type::Pipe: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3166 | return type; |
| 3167 | |
| 3168 | // These types can be variably-modified. All these modifications |
| 3169 | // preserve structure except as noted by comments. |
| 3170 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 3171 | // optimizations available here. |
| 3172 | case Type::Pointer: |
| 3173 | result = getPointerType(getVariableArrayDecayedType( |
| 3174 | cast<PointerType>(ty)->getPointeeType())); |
| 3175 | break; |
| 3176 | |
| 3177 | case Type::LValueReference: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3178 | const auto *lv = cast<LValueReferenceType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3179 | result = getLValueReferenceType( |
| 3180 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 3181 | lv->isSpelledAsLValue()); |
| 3182 | break; |
| 3183 | } |
| 3184 | |
| 3185 | case Type::RValueReference: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3186 | const auto *lv = cast<RValueReferenceType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3187 | result = getRValueReferenceType( |
| 3188 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 3189 | break; |
| 3190 | } |
| 3191 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3192 | case Type::Atomic: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3193 | const auto *at = cast<AtomicType>(ty); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3194 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 3195 | break; |
| 3196 | } |
| 3197 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3198 | case Type::ConstantArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3199 | const auto *cat = cast<ConstantArrayType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3200 | result = getConstantArrayType( |
| 3201 | getVariableArrayDecayedType(cat->getElementType()), |
| 3202 | cat->getSize(), |
| 3203 | cat->getSizeModifier(), |
| 3204 | cat->getIndexTypeCVRQualifiers()); |
| 3205 | break; |
| 3206 | } |
| 3207 | |
| 3208 | case Type::DependentSizedArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3209 | const auto *dat = cast<DependentSizedArrayType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3210 | result = getDependentSizedArrayType( |
| 3211 | getVariableArrayDecayedType(dat->getElementType()), |
| 3212 | dat->getSizeExpr(), |
| 3213 | dat->getSizeModifier(), |
| 3214 | dat->getIndexTypeCVRQualifiers(), |
| 3215 | dat->getBracketsRange()); |
| 3216 | break; |
| 3217 | } |
| 3218 | |
| 3219 | // Turn incomplete types into [*] types. |
| 3220 | case Type::IncompleteArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3221 | const auto *iat = cast<IncompleteArrayType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3222 | result = getVariableArrayType( |
| 3223 | getVariableArrayDecayedType(iat->getElementType()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3224 | /*size*/ nullptr, |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3225 | ArrayType::Normal, |
| 3226 | iat->getIndexTypeCVRQualifiers(), |
| 3227 | SourceRange()); |
| 3228 | break; |
| 3229 | } |
| 3230 | |
| 3231 | // Turn VLA types into [*] types. |
| 3232 | case Type::VariableArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3233 | const auto *vat = cast<VariableArrayType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3234 | result = getVariableArrayType( |
| 3235 | getVariableArrayDecayedType(vat->getElementType()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3236 | /*size*/ nullptr, |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3237 | ArrayType::Star, |
| 3238 | vat->getIndexTypeCVRQualifiers(), |
| 3239 | vat->getBracketsRange()); |
| 3240 | break; |
| 3241 | } |
| 3242 | } |
| 3243 | |
| 3244 | // Apply the top-level qualifiers from the original. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3245 | return getQualifiedType(result, split.Quals); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3246 | } |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3247 | |
Steve Naroff | cadebd0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 3248 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 3249 | /// variable array of the specified element type. |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3250 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 3251 | Expr *NumElts, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 3252 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3253 | unsigned IndexTypeQuals, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3254 | SourceRange Brackets) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3255 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 3256 | // that have an expression provided for their size. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3257 | QualType Canon; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3258 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3259 | // Be sure to pull qualifiers off the element type. |
| 3260 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 3261 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3262 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3263 | IndexTypeQuals, Brackets); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3264 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 3265 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3266 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3267 | auto *New = new (*this, TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3268 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3269 | |
| 3270 | VariableArrayTypes.push_back(New); |
| 3271 | Types.push_back(New); |
| 3272 | return QualType(New, 0); |
| 3273 | } |
| 3274 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3275 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 3276 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 3277 | /// type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3278 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 3279 | Expr *numElements, |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3280 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3281 | unsigned elementTypeQuals, |
| 3282 | SourceRange brackets) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3283 | assert((!numElements || numElements->isTypeDependent() || |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3284 | numElements->isValueDependent()) && |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3285 | "Size must be type- or value-dependent!"); |
| 3286 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3287 | // Dependently-sized array types that do not have a specified number |
| 3288 | // of elements will have their sizes deduced from a dependent |
| 3289 | // initializer. We do no canonicalization here at all, which is okay |
| 3290 | // because they can't be used in most locations. |
| 3291 | if (!numElements) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3292 | auto *newType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3293 | = new (*this, TypeAlignment) |
| 3294 | DependentSizedArrayType(*this, elementType, QualType(), |
| 3295 | numElements, ASM, elementTypeQuals, |
| 3296 | brackets); |
| 3297 | Types.push_back(newType); |
| 3298 | return QualType(newType, 0); |
| 3299 | } |
| 3300 | |
| 3301 | // Otherwise, we actually build a new type every time, but we |
| 3302 | // also build a canonical type. |
| 3303 | |
| 3304 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 3305 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3306 | void *insertPos = nullptr; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3307 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3308 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3309 | QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3310 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 3311 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3312 | // Look for an existing type with these properties. |
| 3313 | DependentSizedArrayType *canonTy = |
| 3314 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 3315 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3316 | // If we don't have one, build one. |
| 3317 | if (!canonTy) { |
| 3318 | canonTy = new (*this, TypeAlignment) |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3319 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3320 | QualType(), numElements, ASM, elementTypeQuals, |
| 3321 | brackets); |
| 3322 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 3323 | Types.push_back(canonTy); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3326 | // Apply qualifiers from the element type to the array. |
| 3327 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3328 | canonElementType.Quals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3329 | |
David Majnemer | 16a7470 | 2015-07-24 05:54:19 +0000 | [diff] [blame] | 3330 | // If we didn't need extra canonicalization for the element type or the size |
| 3331 | // expression, then just use that as our result. |
| 3332 | if (QualType(canonElementType.Ty, 0) == elementType && |
| 3333 | canonTy->getSizeExpr() == numElements) |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3334 | return canon; |
| 3335 | |
| 3336 | // Otherwise, we need to build a type which follows the spelling |
| 3337 | // of the element type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3338 | auto *sugaredType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3339 | = new (*this, TypeAlignment) |
| 3340 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 3341 | ASM, elementTypeQuals, brackets); |
| 3342 | Types.push_back(sugaredType); |
| 3343 | return QualType(sugaredType, 0); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3346 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3347 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3348 | unsigned elementTypeQuals) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3349 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3350 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3351 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3352 | void *insertPos = nullptr; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3353 | if (IncompleteArrayType *iat = |
| 3354 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 3355 | return QualType(iat, 0); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3356 | |
| 3357 | // 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] | 3358 | // either, so fill in the canonical type field. We also have to pull |
| 3359 | // qualifiers off the element type. |
| 3360 | QualType canon; |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3361 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3362 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 3363 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3364 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3365 | ASM, elementTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3366 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3367 | |
| 3368 | // Get the new insert position for the node we care about. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3369 | IncompleteArrayType *existing = |
| 3370 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 3371 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 843ebedd | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 3372 | } |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3373 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3374 | auto *newType = new (*this, TypeAlignment) |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3375 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3376 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3377 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 3378 | Types.push_back(newType); |
| 3379 | return QualType(newType, 0); |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3382 | /// getVectorType - Return the unique reference to a vector type of |
| 3383 | /// 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] | 3384 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3385 | VectorType::VectorKind VecKind) const { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3386 | assert(vecType->isBuiltinType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3387 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3388 | // Check if we've already instantiated a vector of this type. |
| 3389 | llvm::FoldingSetNodeID ID; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 3390 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 3391 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3392 | void *InsertPos = nullptr; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3393 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3394 | return QualType(VTP, 0); |
| 3395 | |
| 3396 | // If the element type isn't canonical, this won't be a canonical type either, |
| 3397 | // so fill in the canonical type field. |
| 3398 | QualType Canonical; |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 3399 | if (!vecType.isCanonical()) { |
Bob Wilson | 7795480 | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 3400 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3401 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3402 | // Get the new insert position for the node we care about. |
| 3403 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3404 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3405 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3406 | auto *New = new (*this, TypeAlignment) |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 3407 | VectorType(vecType, NumElts, Canonical, VecKind); |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3408 | VectorTypes.InsertNode(New, InsertPos); |
| 3409 | Types.push_back(New); |
| 3410 | return QualType(New, 0); |
| 3411 | } |
| 3412 | |
Erich Keane | f702b02 | 2018-07-13 19:46:04 +0000 | [diff] [blame] | 3413 | QualType |
| 3414 | ASTContext::getDependentVectorType(QualType VecType, Expr *SizeExpr, |
| 3415 | SourceLocation AttrLoc, |
| 3416 | VectorType::VectorKind VecKind) const { |
| 3417 | llvm::FoldingSetNodeID ID; |
| 3418 | DependentVectorType::Profile(ID, *this, getCanonicalType(VecType), SizeExpr, |
| 3419 | VecKind); |
| 3420 | void *InsertPos = nullptr; |
| 3421 | DependentVectorType *Canon = |
| 3422 | DependentVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3423 | DependentVectorType *New; |
| 3424 | |
| 3425 | if (Canon) { |
| 3426 | New = new (*this, TypeAlignment) DependentVectorType( |
| 3427 | *this, VecType, QualType(Canon, 0), SizeExpr, AttrLoc, VecKind); |
| 3428 | } else { |
| 3429 | QualType CanonVecTy = getCanonicalType(VecType); |
| 3430 | if (CanonVecTy == VecType) { |
| 3431 | New = new (*this, TypeAlignment) DependentVectorType( |
| 3432 | *this, VecType, QualType(), SizeExpr, AttrLoc, VecKind); |
| 3433 | |
| 3434 | DependentVectorType *CanonCheck = |
| 3435 | DependentVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3436 | assert(!CanonCheck && |
| 3437 | "Dependent-sized vector_size canonical type broken"); |
| 3438 | (void)CanonCheck; |
| 3439 | DependentVectorTypes.InsertNode(New, InsertPos); |
| 3440 | } else { |
| 3441 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 3442 | SourceLocation()); |
| 3443 | New = new (*this, TypeAlignment) DependentVectorType( |
| 3444 | *this, VecType, Canon, SizeExpr, AttrLoc, VecKind); |
| 3445 | } |
| 3446 | } |
| 3447 | |
| 3448 | Types.push_back(New); |
| 3449 | return QualType(New, 0); |
| 3450 | } |
| 3451 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3452 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3453 | /// 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] | 3454 | QualType |
| 3455 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 3456 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3457 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3458 | // Check if we've already instantiated a vector of this type. |
| 3459 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 3460 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 3461 | VectorType::GenericVector); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3462 | void *InsertPos = nullptr; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3463 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3464 | return QualType(VTP, 0); |
| 3465 | |
| 3466 | // If the element type isn't canonical, this won't be a canonical type either, |
| 3467 | // so fill in the canonical type field. |
| 3468 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3469 | if (!vecType.isCanonical()) { |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3470 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3471 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3472 | // Get the new insert position for the node we care about. |
| 3473 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3474 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3475 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3476 | auto *New = new (*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3477 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3478 | VectorTypes.InsertNode(New, InsertPos); |
| 3479 | Types.push_back(New); |
| 3480 | return QualType(New, 0); |
| 3481 | } |
| 3482 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3483 | QualType |
| 3484 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 3485 | Expr *SizeExpr, |
| 3486 | SourceLocation AttrLoc) const { |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3487 | llvm::FoldingSetNodeID ID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3488 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3489 | SizeExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3490 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3491 | void *InsertPos = nullptr; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3492 | DependentSizedExtVectorType *Canon |
| 3493 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3494 | DependentSizedExtVectorType *New; |
| 3495 | if (Canon) { |
| 3496 | // We already have a canonical version of this array type; use it as |
| 3497 | // the canonical type for a newly-built type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3498 | New = new (*this, TypeAlignment) |
| 3499 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 3500 | SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3501 | } else { |
| 3502 | QualType CanonVecTy = getCanonicalType(vecType); |
| 3503 | if (CanonVecTy == vecType) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3504 | New = new (*this, TypeAlignment) |
| 3505 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 3506 | AttrLoc); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3507 | |
| 3508 | DependentSizedExtVectorType *CanonCheck |
| 3509 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3510 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 3511 | (void)CanonCheck; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3512 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 3513 | } else { |
| 3514 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 3515 | SourceLocation()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3516 | New = new (*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3517 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3518 | } |
| 3519 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3520 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 3521 | Types.push_back(New); |
| 3522 | return QualType(New, 0); |
| 3523 | } |
| 3524 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3525 | QualType ASTContext::getDependentAddressSpaceType(QualType PointeeType, |
| 3526 | Expr *AddrSpaceExpr, |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3527 | SourceLocation AttrLoc) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3528 | assert(AddrSpaceExpr->isInstantiationDependent()); |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3529 | |
| 3530 | QualType canonPointeeType = getCanonicalType(PointeeType); |
| 3531 | |
| 3532 | void *insertPos = nullptr; |
| 3533 | llvm::FoldingSetNodeID ID; |
| 3534 | DependentAddressSpaceType::Profile(ID, *this, canonPointeeType, |
| 3535 | AddrSpaceExpr); |
| 3536 | |
| 3537 | DependentAddressSpaceType *canonTy = |
| 3538 | DependentAddressSpaceTypes.FindNodeOrInsertPos(ID, insertPos); |
| 3539 | |
| 3540 | if (!canonTy) { |
| 3541 | canonTy = new (*this, TypeAlignment) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3542 | DependentAddressSpaceType(*this, canonPointeeType, |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3543 | QualType(), AddrSpaceExpr, AttrLoc); |
| 3544 | DependentAddressSpaceTypes.InsertNode(canonTy, insertPos); |
| 3545 | Types.push_back(canonTy); |
| 3546 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3547 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3548 | if (canonPointeeType == PointeeType && |
| 3549 | canonTy->getAddrSpaceExpr() == AddrSpaceExpr) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3550 | return QualType(canonTy, 0); |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3551 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3552 | auto *sugaredType |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3553 | = new (*this, TypeAlignment) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3554 | DependentAddressSpaceType(*this, PointeeType, QualType(canonTy, 0), |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3555 | AddrSpaceExpr, AttrLoc); |
| 3556 | Types.push_back(sugaredType); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3557 | return QualType(sugaredType, 0); |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3560 | /// 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] | 3561 | static bool isCanonicalResultType(QualType T) { |
| 3562 | return T.isCanonical() && |
| 3563 | (T.getObjCLifetime() == Qualifiers::OCL_None || |
| 3564 | T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone); |
| 3565 | } |
| 3566 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3567 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3568 | QualType |
| 3569 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 3570 | const FunctionType::ExtInfo &Info) const { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3571 | // Unique functions, to guarantee there is only one function of a particular |
| 3572 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 3573 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 3574 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3575 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3576 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3577 | if (FunctionNoProtoType *FT = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3578 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3579 | return QualType(FT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3580 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3581 | QualType Canonical; |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3582 | if (!isCanonicalResultType(ResultTy)) { |
| 3583 | Canonical = |
| 3584 | getFunctionNoProtoType(getCanonicalFunctionResultType(ResultTy), Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3585 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 3586 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3587 | FunctionNoProtoType *NewIP = |
| 3588 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3589 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 3590 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3591 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3592 | auto *New = new (*this, TypeAlignment) |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3593 | FunctionNoProtoType(ResultTy, Canonical, Info); |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 3594 | Types.push_back(New); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3595 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3596 | return QualType(New, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
Douglas Gregor | a602a15 | 2015-10-01 20:20:47 +0000 | [diff] [blame] | 3599 | CanQualType |
| 3600 | ASTContext::getCanonicalFunctionResultType(QualType ResultType) const { |
| 3601 | CanQualType CanResultType = getCanonicalType(ResultType); |
| 3602 | |
| 3603 | // Canonical result types do not have ARC lifetime qualifiers. |
| 3604 | if (CanResultType.getQualifiers().hasObjCLifetime()) { |
| 3605 | Qualifiers Qs = CanResultType.getQualifiers(); |
| 3606 | Qs.removeObjCLifetime(); |
| 3607 | return CanQualType::CreateUnsafe( |
| 3608 | getQualifiedType(CanResultType.getUnqualifiedType(), Qs)); |
| 3609 | } |
| 3610 | |
| 3611 | return CanResultType; |
| 3612 | } |
| 3613 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3614 | static bool isCanonicalExceptionSpecification( |
| 3615 | const FunctionProtoType::ExceptionSpecInfo &ESI, bool NoexceptInType) { |
| 3616 | if (ESI.Type == EST_None) |
| 3617 | return true; |
| 3618 | if (!NoexceptInType) |
| 3619 | return false; |
| 3620 | |
| 3621 | // C++17 onwards: exception specification is part of the type, as a simple |
| 3622 | // boolean "can this function type throw". |
| 3623 | if (ESI.Type == EST_BasicNoexcept) |
| 3624 | return true; |
| 3625 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3626 | // A noexcept(expr) specification is (possibly) canonical if expr is |
| 3627 | // value-dependent. |
| 3628 | if (ESI.Type == EST_DependentNoexcept) |
| 3629 | return true; |
| 3630 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3631 | // A dynamic exception specification is canonical if it only contains pack |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3632 | // expansions (so we can't tell whether it's non-throwing) and all its |
| 3633 | // contained types are canonical. |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3634 | if (ESI.Type == EST_Dynamic) { |
Richard Smith | fda59e5 | 2016-10-26 01:05:54 +0000 | [diff] [blame] | 3635 | bool AnyPackExpansions = false; |
| 3636 | for (QualType ET : ESI.Exceptions) { |
| 3637 | if (!ET.isCanonical()) |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3638 | return false; |
Richard Smith | fda59e5 | 2016-10-26 01:05:54 +0000 | [diff] [blame] | 3639 | if (ET->getAs<PackExpansionType>()) |
| 3640 | AnyPackExpansions = true; |
| 3641 | } |
| 3642 | return AnyPackExpansions; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3643 | } |
| 3644 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3645 | return false; |
| 3646 | } |
| 3647 | |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3648 | QualType ASTContext::getFunctionTypeInternal( |
| 3649 | QualType ResultTy, ArrayRef<QualType> ArgArray, |
| 3650 | const FunctionProtoType::ExtProtoInfo &EPI, bool OnlyWantCanonical) const { |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3651 | size_t NumArgs = ArgArray.size(); |
| 3652 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3653 | // Unique functions, to guarantee there is only one function of a particular |
| 3654 | // structure. |
| 3655 | llvm::FoldingSetNodeID ID; |
| 3656 | FunctionProtoType::Profile(ID, ResultTy, ArgArray.begin(), NumArgs, EPI, |
| 3657 | *this, true); |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3658 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3659 | QualType Canonical; |
| 3660 | bool Unique = false; |
| 3661 | |
| 3662 | void *InsertPos = nullptr; |
| 3663 | if (FunctionProtoType *FPT = |
| 3664 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 3665 | QualType Existing = QualType(FPT, 0); |
| 3666 | |
| 3667 | // If we find a pre-existing equivalent FunctionProtoType, we can just reuse |
| 3668 | // it so long as our exception specification doesn't contain a dependent |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3669 | // noexcept expression, or we're just looking for a canonical type. |
| 3670 | // Otherwise, we're going to need to create a type |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3671 | // sugar node to hold the concrete expression. |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3672 | if (OnlyWantCanonical || !isComputedNoexcept(EPI.ExceptionSpec.Type) || |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3673 | EPI.ExceptionSpec.NoexceptExpr == FPT->getNoexceptExpr()) |
| 3674 | return Existing; |
| 3675 | |
| 3676 | // We need a new type sugar node for this one, to hold the new noexcept |
| 3677 | // expression. We do no canonicalization here, but that's OK since we don't |
| 3678 | // expect to see the same noexcept expression much more than once. |
| 3679 | Canonical = getCanonicalType(Existing); |
| 3680 | Unique = true; |
| 3681 | } |
| 3682 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3683 | bool NoexceptInType = getLangOpts().CPlusPlus17; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3684 | bool IsCanonicalExceptionSpec = |
| 3685 | isCanonicalExceptionSpecification(EPI.ExceptionSpec, NoexceptInType); |
| 3686 | |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3687 | // Determine whether the type being created is already canonical or not. |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3688 | bool isCanonical = !Unique && IsCanonicalExceptionSpec && |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3689 | isCanonicalResultType(ResultTy) && !EPI.HasTrailingReturn; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3690 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3691 | if (!ArgArray[i].isCanonicalAsParam()) |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3692 | isCanonical = false; |
| 3693 | |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3694 | if (OnlyWantCanonical) |
| 3695 | assert(isCanonical && |
| 3696 | "given non-canonical parameters constructing canonical type"); |
| 3697 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3698 | // If this type isn't canonical, get the canonical version of it if we don't |
| 3699 | // already have it. The exception spec is only partially part of the |
| 3700 | // canonical type, and only in C++17 onwards. |
| 3701 | if (!isCanonical && Canonical.isNull()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3702 | SmallVector<QualType, 16> CanonicalArgs; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3703 | CanonicalArgs.reserve(NumArgs); |
| 3704 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3705 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3706 | |
Benjamin Kramer | 3f515cd | 2016-10-26 12:51:45 +0000 | [diff] [blame] | 3707 | llvm::SmallVector<QualType, 8> ExceptionTypeStorage; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3708 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 3709 | CanonicalEPI.HasTrailingReturn = false; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3710 | |
| 3711 | if (IsCanonicalExceptionSpec) { |
| 3712 | // Exception spec is already OK. |
| 3713 | } else if (NoexceptInType) { |
| 3714 | switch (EPI.ExceptionSpec.Type) { |
| 3715 | case EST_Unparsed: case EST_Unevaluated: case EST_Uninstantiated: |
| 3716 | // We don't know yet. It shouldn't matter what we pick here; no-one |
| 3717 | // should ever look at this. |
| 3718 | LLVM_FALLTHROUGH; |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3719 | case EST_None: case EST_MSAny: case EST_NoexceptFalse: |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3720 | CanonicalEPI.ExceptionSpec.Type = EST_None; |
| 3721 | break; |
| 3722 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3723 | // A dynamic exception specification is almost always "not noexcept", |
| 3724 | // with the exception that a pack expansion might expand to no types. |
| 3725 | case EST_Dynamic: { |
| 3726 | bool AnyPacks = false; |
| 3727 | for (QualType ET : EPI.ExceptionSpec.Exceptions) { |
| 3728 | if (ET->getAs<PackExpansionType>()) |
| 3729 | AnyPacks = true; |
| 3730 | ExceptionTypeStorage.push_back(getCanonicalType(ET)); |
| 3731 | } |
| 3732 | if (!AnyPacks) |
| 3733 | CanonicalEPI.ExceptionSpec.Type = EST_None; |
| 3734 | else { |
| 3735 | CanonicalEPI.ExceptionSpec.Type = EST_Dynamic; |
| 3736 | CanonicalEPI.ExceptionSpec.Exceptions = ExceptionTypeStorage; |
| 3737 | } |
| 3738 | break; |
| 3739 | } |
| 3740 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3741 | case EST_DynamicNone: case EST_BasicNoexcept: case EST_NoexceptTrue: |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3742 | CanonicalEPI.ExceptionSpec.Type = EST_BasicNoexcept; |
| 3743 | break; |
| 3744 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3745 | case EST_DependentNoexcept: |
| 3746 | llvm_unreachable("dependent noexcept is already canonical"); |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3747 | } |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3748 | } else { |
| 3749 | CanonicalEPI.ExceptionSpec = FunctionProtoType::ExceptionSpecInfo(); |
| 3750 | } |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3751 | |
Douglas Gregor | a602a15 | 2015-10-01 20:20:47 +0000 | [diff] [blame] | 3752 | // Adjust the canonical function result type. |
| 3753 | CanQualType CanResultTy = getCanonicalFunctionResultType(ResultTy); |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3754 | Canonical = |
| 3755 | getFunctionTypeInternal(CanResultTy, CanonicalArgs, CanonicalEPI, true); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3756 | |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 3757 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3758 | FunctionProtoType *NewIP = |
| 3759 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3760 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3761 | } |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3762 | |
Bruno Ricci | 26a2536 | 2018-10-02 11:46:38 +0000 | [diff] [blame] | 3763 | // Compute the needed size to hold this FunctionProtoType and the |
| 3764 | // various trailing objects. |
| 3765 | auto ESH = FunctionProtoType::getExceptionSpecSize( |
| 3766 | EPI.ExceptionSpec.Type, EPI.ExceptionSpec.Exceptions.size()); |
| 3767 | size_t Size = FunctionProtoType::totalSizeToAlloc< |
| 3768 | QualType, FunctionType::FunctionTypeExtraBitfields, |
| 3769 | FunctionType::ExceptionType, Expr *, FunctionDecl *, |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 3770 | FunctionProtoType::ExtParameterInfo, Qualifiers>( |
Bruno Ricci | 26a2536 | 2018-10-02 11:46:38 +0000 | [diff] [blame] | 3771 | NumArgs, FunctionProtoType::hasExtraBitfields(EPI.ExceptionSpec.Type), |
| 3772 | ESH.NumExceptionType, ESH.NumExprPtr, ESH.NumFunctionDeclPtr, |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 3773 | EPI.ExtParameterInfos ? NumArgs : 0, |
| 3774 | EPI.TypeQuals.hasNonFastQualifiers() ? 1 : 0); |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3775 | |
Bruno Ricci | 26a2536 | 2018-10-02 11:46:38 +0000 | [diff] [blame] | 3776 | auto *FTP = (FunctionProtoType *)Allocate(Size, TypeAlignment); |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 3777 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3778 | new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3779 | Types.push_back(FTP); |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3780 | if (!Unique) |
| 3781 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3782 | return QualType(FTP, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3783 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 3784 | |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3785 | QualType ASTContext::getPipeType(QualType T, bool ReadOnly) const { |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3786 | llvm::FoldingSetNodeID ID; |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3787 | PipeType::Profile(ID, T, ReadOnly); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3788 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 3789 | void *InsertPos = nullptr; |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3790 | if (PipeType *PT = PipeTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3791 | return QualType(PT, 0); |
| 3792 | |
| 3793 | // If the pipe element type isn't canonical, this won't be a canonical type |
| 3794 | // either, so fill in the canonical type field. |
| 3795 | QualType Canonical; |
| 3796 | if (!T.isCanonical()) { |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3797 | Canonical = getPipeType(getCanonicalType(T), ReadOnly); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3798 | |
| 3799 | // Get the new insert position for the node we care about. |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3800 | PipeType *NewIP = PipeTypes.FindNodeOrInsertPos(ID, InsertPos); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3801 | assert(!NewIP && "Shouldn't be in the map!"); |
| 3802 | (void)NewIP; |
| 3803 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3804 | auto *New = new (*this, TypeAlignment) PipeType(T, Canonical, ReadOnly); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3805 | Types.push_back(New); |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3806 | PipeTypes.InsertNode(New, InsertPos); |
Joey Gouly | 5788b78 | 2016-11-18 14:10:54 +0000 | [diff] [blame] | 3807 | return QualType(New, 0); |
| 3808 | } |
| 3809 | |
Anastasia Stulova | 59055b9 | 2018-05-09 13:23:26 +0000 | [diff] [blame] | 3810 | QualType ASTContext::adjustStringLiteralBaseType(QualType Ty) const { |
| 3811 | // OpenCL v1.1 s6.5.3: a string literal is in the constant address space. |
| 3812 | return LangOpts.OpenCL ? getAddrSpaceQualType(Ty, LangAS::opencl_constant) |
| 3813 | : Ty; |
| 3814 | } |
| 3815 | |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3816 | QualType ASTContext::getReadPipeType(QualType T) const { |
| 3817 | return getPipeType(T, true); |
| 3818 | } |
| 3819 | |
Joey Gouly | 5788b78 | 2016-11-18 14:10:54 +0000 | [diff] [blame] | 3820 | QualType ASTContext::getWritePipeType(QualType T) const { |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3821 | return getPipeType(T, false); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3822 | } |
| 3823 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3824 | #ifndef NDEBUG |
| 3825 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 3826 | if (!isa<CXXRecordDecl>(D)) return false; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3827 | const auto *RD = cast<CXXRecordDecl>(D); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3828 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 3829 | return true; |
| 3830 | if (RD->getDescribedClassTemplate() && |
| 3831 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 3832 | return true; |
| 3833 | return false; |
| 3834 | } |
| 3835 | #endif |
| 3836 | |
| 3837 | /// getInjectedClassNameType - Return the unique reference to the |
| 3838 | /// injected class name type for the specified templated declaration. |
| 3839 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3840 | QualType TST) const { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3841 | assert(NeedsInjectedClassNameType(Decl)); |
| 3842 | if (Decl->TypeForDecl) { |
| 3843 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3844 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3845 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 3846 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3847 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 3848 | } else { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3849 | Type *newType = |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3850 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3851 | Decl->TypeForDecl = newType; |
| 3852 | Types.push_back(newType); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3853 | } |
| 3854 | return QualType(Decl->TypeForDecl, 0); |
| 3855 | } |
| 3856 | |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3857 | /// getTypeDeclType - Return the unique reference to the type for the |
| 3858 | /// specified type declaration. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3859 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 89656d2 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 3860 | assert(Decl && "Passed null for Decl param"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3861 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3862 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3863 | if (const auto *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3864 | return getTypedefType(Typedef); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3865 | |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3866 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 3867 | "Template type parameter types are always available."); |
| 3868 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3869 | if (const auto *Record = dyn_cast<RecordDecl>(Decl)) { |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 3870 | assert(Record->isFirstDecl() && "struct/union has previous declaration"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3871 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3872 | return getRecordType(Record); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3873 | } else if (const auto *Enum = dyn_cast<EnumDecl>(Decl)) { |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 3874 | assert(Enum->isFirstDecl() && "enum has previous declaration"); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3875 | return getEnumType(Enum); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3876 | } else if (const auto *Using = dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3877 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 3878 | Decl->TypeForDecl = newType; |
| 3879 | Types.push_back(newType); |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3880 | } else |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3881 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 3882 | |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 3883 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3884 | } |
| 3885 | |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 3886 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3887 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3888 | QualType |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3889 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 3890 | QualType Canonical) const { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3891 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3892 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 3893 | if (Canonical.isNull()) |
| 3894 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3895 | auto *newType = new (*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3896 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3897 | Decl->TypeForDecl = newType; |
| 3898 | Types.push_back(newType); |
| 3899 | return QualType(newType, 0); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3902 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3903 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 3904 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3905 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3906 | if (PrevDecl->TypeForDecl) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3907 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3908 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3909 | auto *newType = new (*this, TypeAlignment) RecordType(Decl); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3910 | Decl->TypeForDecl = newType; |
| 3911 | Types.push_back(newType); |
| 3912 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3913 | } |
| 3914 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3915 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3916 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 3917 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3918 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3919 | if (PrevDecl->TypeForDecl) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3920 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3921 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3922 | auto *newType = new (*this, TypeAlignment) EnumType(Decl); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3923 | Decl->TypeForDecl = newType; |
| 3924 | Types.push_back(newType); |
| 3925 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3926 | } |
| 3927 | |
Richard Smith | e43e2b3 | 2018-08-20 21:47:29 +0000 | [diff] [blame] | 3928 | QualType ASTContext::getAttributedType(attr::Kind attrKind, |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 3929 | QualType modifiedType, |
| 3930 | QualType equivalentType) { |
| 3931 | llvm::FoldingSetNodeID id; |
| 3932 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 3933 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3934 | void *insertPos = nullptr; |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 3935 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 3936 | if (type) return QualType(type, 0); |
| 3937 | |
| 3938 | QualType canon = getCanonicalType(equivalentType); |
| 3939 | type = new (*this, TypeAlignment) |
| 3940 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
| 3941 | |
| 3942 | Types.push_back(type); |
| 3943 | AttributedTypes.InsertNode(type, insertPos); |
| 3944 | |
| 3945 | return QualType(type, 0); |
| 3946 | } |
| 3947 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3948 | /// Retrieve a substitution-result type. |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3949 | QualType |
| 3950 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3951 | QualType Replacement) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3952 | assert(Replacement.isCanonical() |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3953 | && "replacement types must always be canonical"); |
| 3954 | |
| 3955 | llvm::FoldingSetNodeID ID; |
| 3956 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3957 | void *InsertPos = nullptr; |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 3958 | SubstTemplateTypeParmType *SubstParm |
| 3959 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3960 | |
| 3961 | if (!SubstParm) { |
| 3962 | SubstParm = new (*this, TypeAlignment) |
| 3963 | SubstTemplateTypeParmType(Parm, Replacement); |
| 3964 | Types.push_back(SubstParm); |
| 3965 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 3966 | } |
| 3967 | |
| 3968 | return QualType(SubstParm, 0); |
| 3969 | } |
| 3970 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3971 | /// Retrieve a |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3972 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 3973 | const TemplateTypeParmType *Parm, |
| 3974 | const TemplateArgument &ArgPack) { |
| 3975 | #ifndef NDEBUG |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 3976 | for (const auto &P : ArgPack.pack_elements()) { |
| 3977 | assert(P.getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 3978 | assert(P.getAsType().isCanonical() && "Pack contains non-canonical type"); |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3979 | } |
| 3980 | #endif |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3981 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3982 | llvm::FoldingSetNodeID ID; |
| 3983 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3984 | void *InsertPos = nullptr; |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3985 | if (SubstTemplateTypeParmPackType *SubstParm |
| 3986 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3987 | return QualType(SubstParm, 0); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3988 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3989 | QualType Canon; |
| 3990 | if (!Parm->isCanonicalUnqualified()) { |
| 3991 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 3992 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 3993 | ArgPack); |
| 3994 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3995 | } |
| 3996 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3997 | auto *SubstParm |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 3998 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 3999 | ArgPack); |
| 4000 | Types.push_back(SubstParm); |
George Burgess IV | b5fe855 | 2017-06-12 17:44:30 +0000 | [diff] [blame] | 4001 | SubstTemplateTypeParmPackTypes.InsertNode(SubstParm, InsertPos); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4002 | return QualType(SubstParm, 0); |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4005 | /// Retrieve the template type parameter type for a template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4006 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 4007 | /// name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4008 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 4009 | bool ParameterPack, |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4010 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 4011 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4012 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4013 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4014 | TemplateTypeParmType *TypeParm |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 4015 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4016 | |
| 4017 | if (TypeParm) |
| 4018 | return QualType(TypeParm, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4019 | |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4020 | if (TTPDecl) { |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 4021 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4022 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4023 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4024 | TemplateTypeParmType *TypeCheck |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4025 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4026 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 4027 | (void)TypeCheck; |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 4028 | } else |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4029 | TypeParm = new (*this, TypeAlignment) |
| 4030 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 4031 | |
| 4032 | Types.push_back(TypeParm); |
| 4033 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 4034 | |
| 4035 | return QualType(TypeParm, 0); |
| 4036 | } |
| 4037 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4038 | TypeSourceInfo * |
| 4039 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 4040 | SourceLocation NameLoc, |
| 4041 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4042 | QualType Underlying) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4043 | assert(!Name.getAsDependentTemplateName() && |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 4044 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4045 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4046 | |
| 4047 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4048 | TemplateSpecializationTypeLoc TL = |
| 4049 | DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>(); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 4050 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4051 | TL.setTemplateNameLoc(NameLoc); |
| 4052 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 4053 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 4054 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 4055 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 4056 | return DI; |
| 4057 | } |
| 4058 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4059 | QualType |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4060 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4061 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4062 | QualType Underlying) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4063 | assert(!Template.getAsDependentTemplateName() && |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 4064 | "No dependent template names here!"); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4065 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4066 | SmallVector<TemplateArgument, 4> ArgVec; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4067 | ArgVec.reserve(Args.size()); |
| 4068 | for (const TemplateArgumentLoc &Arg : Args.arguments()) |
| 4069 | ArgVec.push_back(Arg.getArgument()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4070 | |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4071 | return getTemplateSpecializationType(Template, ArgVec, Underlying); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 4074 | #ifndef NDEBUG |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4075 | static bool hasAnyPackExpansions(ArrayRef<TemplateArgument> Args) { |
| 4076 | for (const TemplateArgument &Arg : Args) |
| 4077 | if (Arg.isPackExpansion()) |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 4078 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4079 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 4080 | return true; |
| 4081 | } |
| 4082 | #endif |
| 4083 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4084 | QualType |
| 4085 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4086 | ArrayRef<TemplateArgument> Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4087 | QualType Underlying) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4088 | assert(!Template.getAsDependentTemplateName() && |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 4089 | "No dependent template names here!"); |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 4090 | // Look through qualified template names. |
| 4091 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 4092 | Template = TemplateName(QTN->getTemplateDecl()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4093 | |
| 4094 | bool IsTypeAlias = |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4095 | Template.getAsTemplateDecl() && |
| 4096 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4097 | QualType CanonType; |
| 4098 | if (!Underlying.isNull()) |
| 4099 | CanonType = getCanonicalType(Underlying); |
| 4100 | else { |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 4101 | // We can get here with an alias template when the specialization contains |
| 4102 | // a pack expansion that does not match up with a parameter pack. |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4103 | assert((!IsTypeAlias || hasAnyPackExpansions(Args)) && |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 4104 | "Caller must compute aliased type"); |
| 4105 | IsTypeAlias = false; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4106 | CanonType = getCanonicalTemplateSpecializationType(Template, Args); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4107 | } |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 4108 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4109 | // Allocate the (non-canonical) template specialization type, but don't |
| 4110 | // try to unique it: these types typically have location information that |
| 4111 | // we don't unique and don't want to lose. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4112 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4113 | sizeof(TemplateArgument) * Args.size() + |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 4114 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4115 | TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4116 | auto *Spec |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4117 | = new (Mem) TemplateSpecializationType(Template, Args, CanonType, |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 4118 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4119 | |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 4120 | Types.push_back(Spec); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4121 | return QualType(Spec, 0); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4124 | QualType ASTContext::getCanonicalTemplateSpecializationType( |
| 4125 | TemplateName Template, ArrayRef<TemplateArgument> Args) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4126 | assert(!Template.getAsDependentTemplateName() && |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 4127 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4128 | |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 4129 | // Look through qualified template names. |
| 4130 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 4131 | Template = TemplateName(QTN->getTemplateDecl()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4132 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4133 | // Build the canonical template specialization type. |
| 4134 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4135 | SmallVector<TemplateArgument, 4> CanonArgs; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4136 | unsigned NumArgs = Args.size(); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4137 | CanonArgs.reserve(NumArgs); |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4138 | for (const TemplateArgument &Arg : Args) |
| 4139 | CanonArgs.push_back(getCanonicalTemplateArgument(Arg)); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4140 | |
| 4141 | // Determine whether this canonical template specialization type already |
| 4142 | // exists. |
| 4143 | llvm::FoldingSetNodeID ID; |
| 4144 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4145 | CanonArgs, *this); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4146 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4147 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4148 | TemplateSpecializationType *Spec |
| 4149 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4150 | |
| 4151 | if (!Spec) { |
| 4152 | // Allocate a new canonical template specialization type. |
| 4153 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 4154 | sizeof(TemplateArgument) * NumArgs), |
| 4155 | TypeAlignment); |
| 4156 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4157 | CanonArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4158 | QualType(), QualType()); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4159 | Types.push_back(Spec); |
| 4160 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 4161 | } |
| 4162 | |
| 4163 | assert(Spec->isDependentType() && |
| 4164 | "Non-dependent template-id type must have a canonical type"); |
| 4165 | return QualType(Spec, 0); |
| 4166 | } |
| 4167 | |
Joel E. Denny | 7509a2f | 2018-05-14 19:36:45 +0000 | [diff] [blame] | 4168 | QualType ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 4169 | NestedNameSpecifier *NNS, |
| 4170 | QualType NamedType, |
| 4171 | TagDecl *OwnedTagDecl) const { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4172 | llvm::FoldingSetNodeID ID; |
Joel E. Denny | 7509a2f | 2018-05-14 19:36:45 +0000 | [diff] [blame] | 4173 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType, OwnedTagDecl); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4174 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4175 | void *InsertPos = nullptr; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4176 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4177 | if (T) |
| 4178 | return QualType(T, 0); |
| 4179 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4180 | QualType Canon = NamedType; |
| 4181 | if (!Canon.isCanonical()) { |
| 4182 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4183 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4184 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4185 | (void)CheckT; |
| 4186 | } |
| 4187 | |
Bruno Ricci | d6bd598 | 2018-08-16 10:48:16 +0000 | [diff] [blame] | 4188 | void *Mem = Allocate(ElaboratedType::totalSizeToAlloc<TagDecl *>(!!OwnedTagDecl), |
| 4189 | TypeAlignment); |
| 4190 | T = new (Mem) ElaboratedType(Keyword, NNS, NamedType, Canon, OwnedTagDecl); |
| 4191 | |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4192 | Types.push_back(T); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4193 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4194 | return QualType(T, 0); |
| 4195 | } |
| 4196 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4197 | QualType |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4198 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4199 | llvm::FoldingSetNodeID ID; |
| 4200 | ParenType::Profile(ID, InnerType); |
| 4201 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4202 | void *InsertPos = nullptr; |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4203 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4204 | if (T) |
| 4205 | return QualType(T, 0); |
| 4206 | |
| 4207 | QualType Canon = InnerType; |
| 4208 | if (!Canon.isCanonical()) { |
| 4209 | Canon = getCanonicalType(InnerType); |
| 4210 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4211 | assert(!CheckT && "Paren canonical type broken"); |
| 4212 | (void)CheckT; |
| 4213 | } |
| 4214 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 4215 | T = new (*this, TypeAlignment) ParenType(InnerType, Canon); |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4216 | Types.push_back(T); |
| 4217 | ParenTypes.InsertNode(T, InsertPos); |
| 4218 | return QualType(T, 0); |
| 4219 | } |
| 4220 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 4221 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 4222 | NestedNameSpecifier *NNS, |
| 4223 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4224 | QualType Canon) const { |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4225 | if (Canon.isNull()) { |
| 4226 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 4227 | if (CanonNNS != NNS) |
| 4228 | Canon = getDependentNameType(Keyword, CanonNNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4229 | } |
| 4230 | |
| 4231 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 4232 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4233 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4234 | void *InsertPos = nullptr; |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4235 | DependentNameType *T |
| 4236 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4237 | if (T) |
| 4238 | return QualType(T, 0); |
| 4239 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 4240 | T = new (*this, TypeAlignment) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4241 | Types.push_back(T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4242 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4243 | return QualType(T, 0); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4244 | } |
| 4245 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4246 | QualType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4247 | ASTContext::getDependentTemplateSpecializationType( |
| 4248 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 4249 | NestedNameSpecifier *NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4250 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4251 | const TemplateArgumentListInfo &Args) const { |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4252 | // TODO: avoid this copy |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4253 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4254 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 4255 | ArgCopy.push_back(Args[I].getArgument()); |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4256 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, ArgCopy); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
| 4259 | QualType |
| 4260 | ASTContext::getDependentTemplateSpecializationType( |
| 4261 | ElaboratedTypeKeyword Keyword, |
| 4262 | NestedNameSpecifier *NNS, |
| 4263 | const IdentifierInfo *Name, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4264 | ArrayRef<TemplateArgument> Args) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4265 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 4266 | "nested-name-specifier must be dependent"); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4267 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4268 | llvm::FoldingSetNodeID ID; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4269 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4270 | Name, Args); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4271 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4272 | void *InsertPos = nullptr; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4273 | DependentTemplateSpecializationType *T |
| 4274 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4275 | if (T) |
| 4276 | return QualType(T, 0); |
| 4277 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4278 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4279 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4280 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 4281 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 4282 | |
| 4283 | bool AnyNonCanonArgs = false; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4284 | unsigned NumArgs = Args.size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4285 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4286 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4287 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 4288 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 4289 | AnyNonCanonArgs = true; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4292 | QualType Canon; |
| 4293 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 4294 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4295 | Name, |
| 4296 | CanonArgs); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4297 | |
| 4298 | // Find the insert position again. |
| 4299 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4300 | } |
| 4301 | |
| 4302 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 4303 | sizeof(TemplateArgument) * NumArgs), |
| 4304 | TypeAlignment); |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 4305 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4306 | Name, Args, Canon); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4307 | Types.push_back(T); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4308 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4309 | return QualType(T, 0); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 4312 | TemplateArgument ASTContext::getInjectedTemplateArg(NamedDecl *Param) { |
| 4313 | TemplateArgument Arg; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4314 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 4315 | QualType ArgType = getTypeDeclType(TTP); |
| 4316 | if (TTP->isParameterPack()) |
| 4317 | ArgType = getPackExpansionType(ArgType, None); |
| 4318 | |
| 4319 | Arg = TemplateArgument(ArgType); |
| 4320 | } else if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4321 | Expr *E = new (*this) DeclRefExpr( |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 4322 | *this, NTTP, /*enclosing*/ false, |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 4323 | NTTP->getType().getNonLValueExprType(*this), |
| 4324 | Expr::getValueKindForType(NTTP->getType()), NTTP->getLocation()); |
| 4325 | |
| 4326 | if (NTTP->isParameterPack()) |
| 4327 | E = new (*this) PackExpansionExpr(DependentTy, E, NTTP->getLocation(), |
| 4328 | None); |
| 4329 | Arg = TemplateArgument(E); |
| 4330 | } else { |
| 4331 | auto *TTP = cast<TemplateTemplateParmDecl>(Param); |
| 4332 | if (TTP->isParameterPack()) |
| 4333 | Arg = TemplateArgument(TemplateName(TTP), Optional<unsigned>()); |
| 4334 | else |
| 4335 | Arg = TemplateArgument(TemplateName(TTP)); |
| 4336 | } |
| 4337 | |
| 4338 | if (Param->isTemplateParameterPack()) |
| 4339 | Arg = TemplateArgument::CreatePackCopy(*this, Arg); |
| 4340 | |
| 4341 | return Arg; |
| 4342 | } |
| 4343 | |
Richard Smith | 43e14d2 | 2016-12-23 02:10:11 +0000 | [diff] [blame] | 4344 | void |
| 4345 | ASTContext::getInjectedTemplateArgs(const TemplateParameterList *Params, |
| 4346 | SmallVectorImpl<TemplateArgument> &Args) { |
| 4347 | Args.reserve(Args.size() + Params->size()); |
| 4348 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 4349 | for (NamedDecl *Param : *Params) |
| 4350 | Args.push_back(getInjectedTemplateArg(Param)); |
Richard Smith | 43e14d2 | 2016-12-23 02:10:11 +0000 | [diff] [blame] | 4351 | } |
| 4352 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 4353 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4354 | Optional<unsigned> NumExpansions) { |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4355 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 4356 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4357 | |
| 4358 | assert(Pattern->containsUnexpandedParameterPack() && |
| 4359 | "Pack expansions must expand one or more parameter packs"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4360 | void *InsertPos = nullptr; |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4361 | PackExpansionType *T |
| 4362 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4363 | if (T) |
| 4364 | return QualType(T, 0); |
| 4365 | |
| 4366 | QualType Canon; |
| 4367 | if (!Pattern.isCanonical()) { |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 4368 | Canon = getCanonicalType(Pattern); |
| 4369 | // The canonical type might not contain an unexpanded parameter pack, if it |
| 4370 | // contains an alias template specialization which ignores one of its |
| 4371 | // parameters. |
| 4372 | if (Canon->containsUnexpandedParameterPack()) { |
Richard Smith | 8b4e1e2 | 2014-07-10 01:20:17 +0000 | [diff] [blame] | 4373 | Canon = getPackExpansionType(Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4374 | |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 4375 | // Find the insert position again, in case we inserted an element into |
| 4376 | // PackExpansionTypes and invalidated our insert position. |
| 4377 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4378 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4379 | } |
| 4380 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 4381 | T = new (*this, TypeAlignment) |
| 4382 | PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4383 | Types.push_back(T); |
| 4384 | PackExpansionTypes.InsertNode(T, InsertPos); |
Richard Smith | 8b4e1e2 | 2014-07-10 01:20:17 +0000 | [diff] [blame] | 4385 | return QualType(T, 0); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4386 | } |
| 4387 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4388 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 4389 | /// alphabetically. |
Benjamin Kramer | 0eb262f | 2015-03-14 13:32:49 +0000 | [diff] [blame] | 4390 | static int CmpProtocolNames(ObjCProtocolDecl *const *LHS, |
| 4391 | ObjCProtocolDecl *const *RHS) { |
| 4392 | return DeclarationName::compare((*LHS)->getDeclName(), (*RHS)->getDeclName()); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4393 | } |
| 4394 | |
Craig Topper | 1f26d5b | 2016-01-03 19:43:23 +0000 | [diff] [blame] | 4395 | static bool areSortedAndUniqued(ArrayRef<ObjCProtocolDecl *> Protocols) { |
| 4396 | if (Protocols.empty()) return true; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4397 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 4398 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 4399 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4400 | |
Craig Topper | 1f26d5b | 2016-01-03 19:43:23 +0000 | [diff] [blame] | 4401 | for (unsigned i = 1; i != Protocols.size(); ++i) |
Benjamin Kramer | 0eb262f | 2015-03-14 13:32:49 +0000 | [diff] [blame] | 4402 | if (CmpProtocolNames(&Protocols[i - 1], &Protocols[i]) >= 0 || |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 4403 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4404 | return false; |
| 4405 | return true; |
| 4406 | } |
| 4407 | |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4408 | static void |
| 4409 | SortAndUniqueProtocols(SmallVectorImpl<ObjCProtocolDecl *> &Protocols) { |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4410 | // Sort protocols, keyed by name. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4411 | llvm::array_pod_sort(Protocols.begin(), Protocols.end(), CmpProtocolNames); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4412 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 4413 | // Canonicalize. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4414 | for (ObjCProtocolDecl *&P : Protocols) |
| 4415 | P = P->getCanonicalDecl(); |
| 4416 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4417 | // Remove duplicates. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4418 | auto ProtocolsEnd = std::unique(Protocols.begin(), Protocols.end()); |
| 4419 | Protocols.erase(ProtocolsEnd, Protocols.end()); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4420 | } |
| 4421 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4422 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 4423 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4424 | unsigned NumProtocols) const { |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 4425 | return getObjCObjectType(BaseType, {}, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4426 | llvm::makeArrayRef(Protocols, NumProtocols), |
| 4427 | /*isKindOf=*/false); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | QualType ASTContext::getObjCObjectType( |
| 4431 | QualType baseType, |
| 4432 | ArrayRef<QualType> typeArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4433 | ArrayRef<ObjCProtocolDecl *> protocols, |
| 4434 | bool isKindOf) const { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4435 | // If the base type is an interface and there aren't any protocols or |
| 4436 | // type arguments to add, then the interface type will do just fine. |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4437 | if (typeArgs.empty() && protocols.empty() && !isKindOf && |
| 4438 | isa<ObjCInterfaceType>(baseType)) |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4439 | return baseType; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4440 | |
| 4441 | // Look in the folding set for an existing type. |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4442 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4443 | ObjCObjectTypeImpl::Profile(ID, baseType, typeArgs, protocols, isKindOf); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4444 | void *InsertPos = nullptr; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4445 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4446 | return QualType(QT, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4447 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4448 | // Determine the type arguments to be used for canonicalization, |
| 4449 | // which may be explicitly specified here or written on the base |
| 4450 | // type. |
| 4451 | ArrayRef<QualType> effectiveTypeArgs = typeArgs; |
| 4452 | if (effectiveTypeArgs.empty()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4453 | if (const auto *baseObject = baseType->getAs<ObjCObjectType>()) |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4454 | effectiveTypeArgs = baseObject->getTypeArgs(); |
| 4455 | } |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4456 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4457 | // Build the canonical type, which has the canonical base type and a |
| 4458 | // sorted-and-uniqued list of protocols and the type arguments |
| 4459 | // canonicalized. |
| 4460 | QualType canonical; |
| 4461 | bool typeArgsAreCanonical = std::all_of(effectiveTypeArgs.begin(), |
| 4462 | effectiveTypeArgs.end(), |
| 4463 | [&](QualType type) { |
| 4464 | return type.isCanonical(); |
| 4465 | }); |
Craig Topper | 1f26d5b | 2016-01-03 19:43:23 +0000 | [diff] [blame] | 4466 | bool protocolsSorted = areSortedAndUniqued(protocols); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4467 | if (!typeArgsAreCanonical || !protocolsSorted || !baseType.isCanonical()) { |
| 4468 | // Determine the canonical type arguments. |
| 4469 | ArrayRef<QualType> canonTypeArgs; |
| 4470 | SmallVector<QualType, 4> canonTypeArgsVec; |
| 4471 | if (!typeArgsAreCanonical) { |
| 4472 | canonTypeArgsVec.reserve(effectiveTypeArgs.size()); |
| 4473 | for (auto typeArg : effectiveTypeArgs) |
| 4474 | canonTypeArgsVec.push_back(getCanonicalType(typeArg)); |
| 4475 | canonTypeArgs = canonTypeArgsVec; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4476 | } else { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4477 | canonTypeArgs = effectiveTypeArgs; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4480 | ArrayRef<ObjCProtocolDecl *> canonProtocols; |
| 4481 | SmallVector<ObjCProtocolDecl*, 8> canonProtocolsVec; |
| 4482 | if (!protocolsSorted) { |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4483 | canonProtocolsVec.append(protocols.begin(), protocols.end()); |
| 4484 | SortAndUniqueProtocols(canonProtocolsVec); |
| 4485 | canonProtocols = canonProtocolsVec; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4486 | } else { |
| 4487 | canonProtocols = protocols; |
| 4488 | } |
| 4489 | |
| 4490 | canonical = getObjCObjectType(getCanonicalType(baseType), canonTypeArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4491 | canonProtocols, isKindOf); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4492 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4493 | // Regenerate InsertPos. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4494 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4495 | } |
| 4496 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4497 | unsigned size = sizeof(ObjCObjectTypeImpl); |
| 4498 | size += typeArgs.size() * sizeof(QualType); |
| 4499 | size += protocols.size() * sizeof(ObjCProtocolDecl *); |
| 4500 | void *mem = Allocate(size, TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4501 | auto *T = |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4502 | new (mem) ObjCObjectTypeImpl(canonical, baseType, typeArgs, protocols, |
| 4503 | isKindOf); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4504 | |
| 4505 | Types.push_back(T); |
| 4506 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 4507 | return QualType(T, 0); |
| 4508 | } |
| 4509 | |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4510 | /// Apply Objective-C protocol qualifiers to the given type. |
| 4511 | /// If this is for the canonical type of a type parameter, we can apply |
| 4512 | /// protocol qualifiers on the ObjCObjectPointerType. |
| 4513 | QualType |
| 4514 | ASTContext::applyObjCProtocolQualifiers(QualType type, |
| 4515 | ArrayRef<ObjCProtocolDecl *> protocols, bool &hasError, |
| 4516 | bool allowOnPointerType) const { |
| 4517 | hasError = false; |
| 4518 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4519 | if (const auto *objT = dyn_cast<ObjCTypeParamType>(type.getTypePtr())) { |
Manman Ren | c5705ba | 2016-09-13 17:41:05 +0000 | [diff] [blame] | 4520 | return getObjCTypeParamType(objT->getDecl(), protocols); |
| 4521 | } |
| 4522 | |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4523 | // Apply protocol qualifiers to ObjCObjectPointerType. |
| 4524 | if (allowOnPointerType) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4525 | if (const auto *objPtr = |
| 4526 | dyn_cast<ObjCObjectPointerType>(type.getTypePtr())) { |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4527 | const ObjCObjectType *objT = objPtr->getObjectType(); |
| 4528 | // Merge protocol lists and construct ObjCObjectType. |
| 4529 | SmallVector<ObjCProtocolDecl*, 8> protocolsVec; |
| 4530 | protocolsVec.append(objT->qual_begin(), |
| 4531 | objT->qual_end()); |
| 4532 | protocolsVec.append(protocols.begin(), protocols.end()); |
| 4533 | ArrayRef<ObjCProtocolDecl *> protocols = protocolsVec; |
| 4534 | type = getObjCObjectType( |
| 4535 | objT->getBaseType(), |
| 4536 | objT->getTypeArgsAsWritten(), |
| 4537 | protocols, |
| 4538 | objT->isKindOfTypeAsWritten()); |
| 4539 | return getObjCObjectPointerType(type); |
| 4540 | } |
| 4541 | } |
| 4542 | |
| 4543 | // Apply protocol qualifiers to ObjCObjectType. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4544 | if (const auto *objT = dyn_cast<ObjCObjectType>(type.getTypePtr())){ |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4545 | // FIXME: Check for protocols to which the class type is already |
| 4546 | // known to conform. |
| 4547 | |
| 4548 | return getObjCObjectType(objT->getBaseType(), |
| 4549 | objT->getTypeArgsAsWritten(), |
| 4550 | protocols, |
| 4551 | objT->isKindOfTypeAsWritten()); |
| 4552 | } |
| 4553 | |
| 4554 | // If the canonical type is ObjCObjectType, ... |
| 4555 | if (type->isObjCObjectType()) { |
| 4556 | // Silently overwrite any existing protocol qualifiers. |
| 4557 | // TODO: determine whether that's the right thing to do. |
| 4558 | |
| 4559 | // FIXME: Check for protocols to which the class type is already |
| 4560 | // known to conform. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 4561 | return getObjCObjectType(type, {}, protocols, false); |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | // id<protocol-list> |
| 4565 | if (type->isObjCIdType()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4566 | const auto *objPtr = type->castAs<ObjCObjectPointerType>(); |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 4567 | type = getObjCObjectType(ObjCBuiltinIdTy, {}, protocols, |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4568 | objPtr->isKindOfType()); |
| 4569 | return getObjCObjectPointerType(type); |
| 4570 | } |
| 4571 | |
| 4572 | // Class<protocol-list> |
| 4573 | if (type->isObjCClassType()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4574 | const auto *objPtr = type->castAs<ObjCObjectPointerType>(); |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 4575 | type = getObjCObjectType(ObjCBuiltinClassTy, {}, protocols, |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4576 | objPtr->isKindOfType()); |
| 4577 | return getObjCObjectPointerType(type); |
| 4578 | } |
| 4579 | |
| 4580 | hasError = true; |
| 4581 | return type; |
| 4582 | } |
| 4583 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 4584 | QualType |
| 4585 | ASTContext::getObjCTypeParamType(const ObjCTypeParamDecl *Decl, |
| 4586 | ArrayRef<ObjCProtocolDecl *> protocols, |
| 4587 | QualType Canonical) const { |
| 4588 | // Look in the folding set for an existing type. |
| 4589 | llvm::FoldingSetNodeID ID; |
| 4590 | ObjCTypeParamType::Profile(ID, Decl, protocols); |
| 4591 | void *InsertPos = nullptr; |
| 4592 | if (ObjCTypeParamType *TypeParam = |
| 4593 | ObjCTypeParamTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4594 | return QualType(TypeParam, 0); |
| 4595 | |
| 4596 | if (Canonical.isNull()) { |
| 4597 | // We canonicalize to the underlying type. |
| 4598 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
| 4599 | if (!protocols.empty()) { |
| 4600 | // Apply the protocol qualifers. |
| 4601 | bool hasError; |
Richard Trieu | a986a31 | 2018-08-30 01:57:52 +0000 | [diff] [blame] | 4602 | Canonical = getCanonicalType(applyObjCProtocolQualifiers( |
| 4603 | Canonical, protocols, hasError, true /*allowOnPointerType*/)); |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 4604 | assert(!hasError && "Error when apply protocol qualifier to bound type"); |
| 4605 | } |
| 4606 | } |
| 4607 | |
| 4608 | unsigned size = sizeof(ObjCTypeParamType); |
| 4609 | size += protocols.size() * sizeof(ObjCProtocolDecl *); |
| 4610 | void *mem = Allocate(size, TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4611 | auto *newType = new (mem) ObjCTypeParamType(Decl, Canonical, protocols); |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 4612 | |
| 4613 | Types.push_back(newType); |
| 4614 | ObjCTypeParamTypes.InsertNode(newType, InsertPos); |
| 4615 | return QualType(newType, 0); |
| 4616 | } |
| 4617 | |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4618 | /// ObjCObjectAdoptsQTypeProtocols - Checks that protocols in IC's |
| 4619 | /// protocol list adopt all protocols in QT's qualified-id protocol |
| 4620 | /// list. |
| 4621 | bool ASTContext::ObjCObjectAdoptsQTypeProtocols(QualType QT, |
| 4622 | ObjCInterfaceDecl *IC) { |
| 4623 | if (!QT->isObjCQualifiedIdType()) |
| 4624 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4625 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4626 | if (const auto *OPT = QT->getAs<ObjCObjectPointerType>()) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4627 | // If both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4628 | for (auto *Proto : OPT->quals()) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4629 | if (!IC->ClassImplementsProtocol(Proto, false)) |
| 4630 | return false; |
| 4631 | } |
| 4632 | return true; |
| 4633 | } |
| 4634 | return false; |
| 4635 | } |
| 4636 | |
| 4637 | /// QIdProtocolsAdoptObjCObjectProtocols - Checks that protocols in |
| 4638 | /// QT's qualified-id protocol list adopt all protocols in IDecl's list |
| 4639 | /// of protocols. |
| 4640 | bool ASTContext::QIdProtocolsAdoptObjCObjectProtocols(QualType QT, |
| 4641 | ObjCInterfaceDecl *IDecl) { |
| 4642 | if (!QT->isObjCQualifiedIdType()) |
| 4643 | return false; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4644 | const auto *OPT = QT->getAs<ObjCObjectPointerType>(); |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4645 | if (!OPT) |
| 4646 | return false; |
| 4647 | if (!IDecl->hasDefinition()) |
| 4648 | return false; |
Fariborz Jahanian | 91fb0be | 2013-11-20 19:01:50 +0000 | [diff] [blame] | 4649 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocols; |
| 4650 | CollectInheritedProtocols(IDecl, InheritedProtocols); |
| 4651 | if (InheritedProtocols.empty()) |
| 4652 | return false; |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4653 | // 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] | 4654 | // of IDecl's, then bridge casting is ok. |
| 4655 | bool Conforms = false; |
| 4656 | for (auto *Proto : OPT->quals()) { |
| 4657 | Conforms = false; |
| 4658 | for (auto *PI : InheritedProtocols) { |
| 4659 | if (ProtocolCompatibleWithProtocol(Proto, PI)) { |
| 4660 | Conforms = true; |
| 4661 | break; |
| 4662 | } |
| 4663 | } |
| 4664 | if (!Conforms) |
| 4665 | break; |
| 4666 | } |
| 4667 | if (Conforms) |
| 4668 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4669 | |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4670 | for (auto *PI : InheritedProtocols) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4671 | // If both the right and left sides have qualifiers. |
| 4672 | bool Adopts = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4673 | for (auto *Proto : OPT->quals()) { |
Fariborz Jahanian | 48a01cb | 2014-04-04 23:53:45 +0000 | [diff] [blame] | 4674 | // return 'true' if 'PI' is in the inheritance hierarchy of Proto |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4675 | if ((Adopts = ProtocolCompatibleWithProtocol(PI, Proto))) |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4676 | break; |
| 4677 | } |
| 4678 | if (!Adopts) |
Fariborz Jahanian | 91fb0be | 2013-11-20 19:01:50 +0000 | [diff] [blame] | 4679 | return false; |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4680 | } |
| 4681 | return true; |
| 4682 | } |
| 4683 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4684 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 4685 | /// the given object type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4686 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4687 | llvm::FoldingSetNodeID ID; |
| 4688 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 4689 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4690 | void *InsertPos = nullptr; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4691 | if (ObjCObjectPointerType *QT = |
| 4692 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4693 | return QualType(QT, 0); |
| 4694 | |
| 4695 | // Find the canonical object type. |
| 4696 | QualType Canonical; |
| 4697 | if (!ObjectT.isCanonical()) { |
| 4698 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 4699 | |
| 4700 | // Regenerate InsertPos. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4701 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4702 | } |
| 4703 | |
Douglas Gregor | f85bee6 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 4704 | // No match. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4705 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4706 | auto *QType = |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4707 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4708 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4709 | Types.push_back(QType); |
| 4710 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4711 | return QualType(QType, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4712 | } |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4713 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4714 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 4715 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 4716 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 4717 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4718 | if (Decl->TypeForDecl) |
| 4719 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4720 | |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 4721 | if (PrevDecl) { |
| 4722 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 4723 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 4724 | return QualType(PrevDecl->TypeForDecl, 0); |
| 4725 | } |
| 4726 | |
Douglas Gregor | 7671e53 | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 4727 | // Prefer the definition, if there is one. |
| 4728 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 4729 | Decl = Def; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4730 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4731 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4732 | auto *T = new (Mem) ObjCInterfaceType(Decl); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4733 | Decl->TypeForDecl = T; |
| 4734 | Types.push_back(T); |
| 4735 | return QualType(T, 0); |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 4736 | } |
| 4737 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4738 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 4739 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4740 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4741 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4742 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4743 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4744 | TypeOfExprType *toe; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4745 | if (tofExpr->isTypeDependent()) { |
| 4746 | llvm::FoldingSetNodeID ID; |
| 4747 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4748 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4749 | void *InsertPos = nullptr; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4750 | DependentTypeOfExprType *Canon |
| 4751 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4752 | if (Canon) { |
| 4753 | // We already have a "canonical" version of an identical, dependent |
| 4754 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4755 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4756 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 4757 | } else { |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4758 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4759 | Canon |
| 4760 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4761 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 4762 | toe = Canon; |
| 4763 | } |
| 4764 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4765 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4766 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4767 | } |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4768 | Types.push_back(toe); |
| 4769 | return QualType(toe, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4770 | } |
| 4771 | |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4772 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4773 | /// TypeOfType nodes. The only motivation to unique these nodes would be |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4774 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4775 | /// an issue. This doesn't affect the type checker, since it operates |
| 4776 | /// on canonical types (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4777 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4778 | QualType Canonical = getCanonicalType(tofType); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4779 | auto *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4780 | Types.push_back(tot); |
| 4781 | return QualType(tot, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4784 | /// Unlike many "get<Type>" functions, we don't unique DecltypeType |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4785 | /// nodes. This would never be helpful, since each such type has its own |
| 4786 | /// expression, and would not give a significant memory saving, since there |
| 4787 | /// is an Expr tree under each such type. |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 4788 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4789 | DecltypeType *dt; |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4790 | |
| 4791 | // C++11 [temp.type]p2: |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4792 | // If an expression e involves a template parameter, decltype(e) denotes a |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4793 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 4794 | // type only if their expressions are equivalent (14.5.6.1). |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4795 | if (e->isInstantiationDependent()) { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4796 | llvm::FoldingSetNodeID ID; |
| 4797 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4798 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4799 | void *InsertPos = nullptr; |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4800 | DependentDecltypeType *Canon |
| 4801 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4802 | if (!Canon) { |
Yaron Keren | 633e14a | 2016-11-29 10:08:20 +0000 | [diff] [blame] | 4803 | // Build a new, canonical decltype(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4804 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4805 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4806 | } |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4807 | dt = new (*this, TypeAlignment) |
| 4808 | DecltypeType(e, UnderlyingType, QualType((DecltypeType *)Canon, 0)); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4809 | } else { |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4810 | dt = new (*this, TypeAlignment) |
| 4811 | DecltypeType(e, UnderlyingType, getCanonicalType(UnderlyingType)); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4812 | } |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 4813 | Types.push_back(dt); |
| 4814 | return QualType(dt, 0); |
| 4815 | } |
| 4816 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4817 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 4818 | /// savings are minimal and these are rare. |
| 4819 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 4820 | QualType UnderlyingType, |
| 4821 | UnaryTransformType::UTTKind Kind) |
| 4822 | const { |
Vassil Vassilev | bab6f96 | 2016-03-30 22:18:29 +0000 | [diff] [blame] | 4823 | UnaryTransformType *ut = nullptr; |
| 4824 | |
| 4825 | if (BaseType->isDependentType()) { |
| 4826 | // Look in the folding set for an existing type. |
| 4827 | llvm::FoldingSetNodeID ID; |
| 4828 | DependentUnaryTransformType::Profile(ID, getCanonicalType(BaseType), Kind); |
| 4829 | |
| 4830 | void *InsertPos = nullptr; |
| 4831 | DependentUnaryTransformType *Canon |
| 4832 | = DependentUnaryTransformTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4833 | |
| 4834 | if (!Canon) { |
| 4835 | // Build a new, canonical __underlying_type(type) type. |
| 4836 | Canon = new (*this, TypeAlignment) |
| 4837 | DependentUnaryTransformType(*this, getCanonicalType(BaseType), |
| 4838 | Kind); |
| 4839 | DependentUnaryTransformTypes.InsertNode(Canon, InsertPos); |
| 4840 | } |
| 4841 | ut = new (*this, TypeAlignment) UnaryTransformType (BaseType, |
| 4842 | QualType(), Kind, |
| 4843 | QualType(Canon, 0)); |
| 4844 | } else { |
| 4845 | QualType CanonType = getCanonicalType(UnderlyingType); |
| 4846 | ut = new (*this, TypeAlignment) UnaryTransformType (BaseType, |
| 4847 | UnderlyingType, Kind, |
| 4848 | CanonType); |
| 4849 | } |
| 4850 | Types.push_back(ut); |
| 4851 | return QualType(ut, 0); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4852 | } |
| 4853 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4854 | /// getAutoType - Return the uniqued reference to the 'auto' type which has been |
| 4855 | /// deduced to the given type, or to the canonical undeduced 'auto' type, or the |
| 4856 | /// canonical deduced-but-dependent 'auto' type. |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4857 | QualType ASTContext::getAutoType(QualType DeducedType, AutoTypeKeyword Keyword, |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4858 | bool IsDependent) const { |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4859 | if (DeducedType.isNull() && Keyword == AutoTypeKeyword::Auto && !IsDependent) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4860 | return getAutoDeductType(); |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4861 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4862 | // Look in the folding set for an existing type. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4863 | void *InsertPos = nullptr; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4864 | llvm::FoldingSetNodeID ID; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4865 | AutoType::Profile(ID, DeducedType, Keyword, IsDependent); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4866 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4867 | return QualType(AT, 0); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4868 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4869 | auto *AT = new (*this, TypeAlignment) |
| 4870 | AutoType(DeducedType, Keyword, IsDependent); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4871 | Types.push_back(AT); |
| 4872 | if (InsertPos) |
| 4873 | AutoTypes.InsertNode(AT, InsertPos); |
| 4874 | return QualType(AT, 0); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4875 | } |
| 4876 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 4877 | /// Return the uniqued reference to the deduced template specialization type |
| 4878 | /// which has been deduced to the given type, or to the canonical undeduced |
| 4879 | /// such type, or the canonical deduced-but-dependent such type. |
| 4880 | QualType ASTContext::getDeducedTemplateSpecializationType( |
| 4881 | TemplateName Template, QualType DeducedType, bool IsDependent) const { |
| 4882 | // Look in the folding set for an existing type. |
| 4883 | void *InsertPos = nullptr; |
| 4884 | llvm::FoldingSetNodeID ID; |
| 4885 | DeducedTemplateSpecializationType::Profile(ID, Template, DeducedType, |
| 4886 | IsDependent); |
| 4887 | if (DeducedTemplateSpecializationType *DTST = |
| 4888 | DeducedTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4889 | return QualType(DTST, 0); |
| 4890 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4891 | auto *DTST = new (*this, TypeAlignment) |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 4892 | DeducedTemplateSpecializationType(Template, DeducedType, IsDependent); |
| 4893 | Types.push_back(DTST); |
| 4894 | if (InsertPos) |
| 4895 | DeducedTemplateSpecializationTypes.InsertNode(DTST, InsertPos); |
| 4896 | return QualType(DTST, 0); |
| 4897 | } |
| 4898 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4899 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 4900 | /// the given value type. |
| 4901 | QualType ASTContext::getAtomicType(QualType T) const { |
| 4902 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 4903 | // structure. |
| 4904 | llvm::FoldingSetNodeID ID; |
| 4905 | AtomicType::Profile(ID, T); |
| 4906 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4907 | void *InsertPos = nullptr; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4908 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4909 | return QualType(AT, 0); |
| 4910 | |
| 4911 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 4912 | // either, so fill in the canonical type field. |
| 4913 | QualType Canonical; |
| 4914 | if (!T.isCanonical()) { |
| 4915 | Canonical = getAtomicType(getCanonicalType(T)); |
| 4916 | |
| 4917 | // Get the new insert position for the node we care about. |
| 4918 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4919 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4920 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4921 | auto *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4922 | Types.push_back(New); |
| 4923 | AtomicTypes.InsertNode(New, InsertPos); |
| 4924 | return QualType(New, 0); |
| 4925 | } |
| 4926 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 4927 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 4928 | QualType ASTContext::getAutoDeductType() const { |
| 4929 | if (AutoDeductTy.isNull()) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4930 | AutoDeductTy = QualType( |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4931 | new (*this, TypeAlignment) AutoType(QualType(), AutoTypeKeyword::Auto, |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4932 | /*dependent*/false), |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4933 | 0); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 4934 | return AutoDeductTy; |
| 4935 | } |
| 4936 | |
| 4937 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 4938 | QualType ASTContext::getAutoRRefDeductType() const { |
| 4939 | if (AutoRRefDeductTy.isNull()) |
| 4940 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 4941 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 4942 | return AutoRRefDeductTy; |
| 4943 | } |
| 4944 | |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 4945 | /// getTagDeclType - Return the unique reference to the type for the |
| 4946 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4947 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 4948 | assert(Decl); |
Mike Stump | b93185d | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 4949 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 4950 | // away const? mutable? |
| 4951 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4954 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 4955 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 4956 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 4957 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4958 | return getFromTargetType(Target->getSizeType()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 4959 | } |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 4960 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4961 | /// Return the unique signed counterpart of the integer type |
Alexander Shaposhnikov | 1e898d9 | 2017-07-14 17:30:14 +0000 | [diff] [blame] | 4962 | /// corresponding to size_t. |
| 4963 | CanQualType ASTContext::getSignedSizeType() const { |
| 4964 | return getFromTargetType(Target->getSignedSizeType()); |
| 4965 | } |
| 4966 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 4967 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 4968 | CanQualType ASTContext::getIntMaxType() const { |
| 4969 | return getFromTargetType(Target->getIntMaxType()); |
| 4970 | } |
| 4971 | |
| 4972 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 4973 | CanQualType ASTContext::getUIntMaxType() const { |
| 4974 | return getFromTargetType(Target->getUIntMaxType()); |
| 4975 | } |
| 4976 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4977 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 4978 | /// Used when in C++, as a GCC extension. |
| 4979 | QualType ASTContext::getSignedWCharType() const { |
| 4980 | // FIXME: derive from "Target" ? |
| 4981 | return WCharTy; |
| 4982 | } |
| 4983 | |
| 4984 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 4985 | /// Used when in C++, as a GCC extension. |
| 4986 | QualType ASTContext::getUnsignedWCharType() const { |
| 4987 | // FIXME: derive from "Target" ? |
| 4988 | return UnsignedIntTy; |
| 4989 | } |
| 4990 | |
Enea Zaffanella | f11ceb6 | 2013-01-26 17:08:37 +0000 | [diff] [blame] | 4991 | QualType ASTContext::getIntPtrType() const { |
| 4992 | return getFromTargetType(Target->getIntPtrType()); |
| 4993 | } |
| 4994 | |
| 4995 | QualType ASTContext::getUIntPtrType() const { |
| 4996 | return getCorrespondingUnsignedType(getIntPtrType()); |
| 4997 | } |
| 4998 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 4999 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 5000 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 5001 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 5002 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 5003 | } |
| 5004 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5005 | /// Return the unique unsigned counterpart of "ptrdiff_t" |
Alexander Shaposhnikov | 195b25c | 2017-09-28 23:11:31 +0000 | [diff] [blame] | 5006 | /// integer type. The standard (C11 7.21.6.1p7) refers to this type |
| 5007 | /// in the definition of %tu format specifier. |
| 5008 | QualType ASTContext::getUnsignedPointerDiffType() const { |
| 5009 | return getFromTargetType(Target->getUnsignedPtrDiffType(0)); |
| 5010 | } |
| 5011 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5012 | /// Return the unique type for "pid_t" defined in |
Eli Friedman | 4e91899e | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 5013 | /// <sys/types.h>. We need this to compute the correct type for vfork(). |
| 5014 | QualType ASTContext::getProcessIDType() const { |
| 5015 | return getFromTargetType(Target->getProcessIDType()); |
| 5016 | } |
| 5017 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 5018 | //===----------------------------------------------------------------------===// |
| 5019 | // Type Operators |
| 5020 | //===----------------------------------------------------------------------===// |
| 5021 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5022 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 5023 | // Push qualifiers into arrays, and then discard any remaining |
| 5024 | // qualifiers. |
| 5025 | T = getCanonicalType(T); |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 5026 | T = getVariableArrayDecayedType(T); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 5027 | const Type *Ty = T.getTypePtr(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 5028 | QualType Result; |
| 5029 | if (isa<ArrayType>(Ty)) { |
| 5030 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 5031 | } else if (isa<FunctionType>(Ty)) { |
| 5032 | Result = getPointerType(QualType(Ty, 0)); |
| 5033 | } else { |
| 5034 | Result = QualType(Ty, 0); |
| 5035 | } |
| 5036 | |
| 5037 | return CanQualType::CreateUnsafe(Result); |
| 5038 | } |
| 5039 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5040 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 5041 | Qualifiers &quals) { |
| 5042 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 5043 | |
| 5044 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 5045 | // the unqualified desugared type and then drops it on the floor. |
| 5046 | // We then have to strip that sugar back off with |
| 5047 | // getUnqualifiedDesugaredType(), which is silly. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5048 | const auto *AT = |
| 5049 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5050 | |
| 5051 | // 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] | 5052 | if (!AT) { |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5053 | quals = splitType.Quals; |
| 5054 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 5055 | } |
| 5056 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5057 | // Otherwise, recurse on the array's element type. |
| 5058 | QualType elementType = AT->getElementType(); |
| 5059 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 5060 | |
| 5061 | // If that didn't change the element type, AT has no qualifiers, so we |
| 5062 | // can just use the results in splitType. |
| 5063 | if (elementType == unqualElementType) { |
| 5064 | assert(quals.empty()); // from the recursive call |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5065 | quals = splitType.Quals; |
| 5066 | return QualType(splitType.Ty, 0); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | // Otherwise, add in the qualifiers from the outermost type, then |
| 5070 | // build the type back up. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5071 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 5072 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5073 | if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5074 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 5075 | CAT->getSizeModifier(), 0); |
| 5076 | } |
| 5077 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5078 | if (const auto *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5079 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5082 | if (const auto *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5083 | return getVariableArrayType(unqualElementType, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5084 | VAT->getSizeExpr(), |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 5085 | VAT->getSizeModifier(), |
| 5086 | VAT->getIndexTypeCVRQualifiers(), |
| 5087 | VAT->getBracketsRange()); |
| 5088 | } |
| 5089 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5090 | const auto *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5091 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 5092 | DSAT->getSizeModifier(), 0, |
| 5093 | SourceRange()); |
| 5094 | } |
| 5095 | |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5096 | /// Attempt to unwrap two types that may both be array types with the same bound |
| 5097 | /// (or both be array types of unknown bound) for the purpose of comparing the |
| 5098 | /// cv-decomposition of two types per C++ [conv.qual]. |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5099 | bool ASTContext::UnwrapSimilarArrayTypes(QualType &T1, QualType &T2) { |
| 5100 | bool UnwrappedAny = false; |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5101 | while (true) { |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5102 | auto *AT1 = getAsArrayType(T1); |
| 5103 | if (!AT1) return UnwrappedAny; |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5104 | |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5105 | auto *AT2 = getAsArrayType(T2); |
| 5106 | if (!AT2) return UnwrappedAny; |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5107 | |
| 5108 | // If we don't have two array types with the same constant bound nor two |
| 5109 | // incomplete array types, we've unwrapped everything we can. |
| 5110 | if (auto *CAT1 = dyn_cast<ConstantArrayType>(AT1)) { |
| 5111 | auto *CAT2 = dyn_cast<ConstantArrayType>(AT2); |
| 5112 | if (!CAT2 || CAT1->getSize() != CAT2->getSize()) |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5113 | return UnwrappedAny; |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5114 | } else if (!isa<IncompleteArrayType>(AT1) || |
| 5115 | !isa<IncompleteArrayType>(AT2)) { |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5116 | return UnwrappedAny; |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5117 | } |
| 5118 | |
| 5119 | T1 = AT1->getElementType(); |
| 5120 | T2 = AT2->getElementType(); |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5121 | UnwrappedAny = true; |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5122 | } |
| 5123 | } |
| 5124 | |
| 5125 | /// Attempt to unwrap two types that may be similar (C++ [conv.qual]). |
| 5126 | /// |
| 5127 | /// If T1 and T2 are both pointer types of the same kind, or both array types |
| 5128 | /// with the same bound, unwraps layers from T1 and T2 until a pointer type is |
| 5129 | /// unwrapped. Top-level qualifiers on T1 and T2 are ignored. |
| 5130 | /// |
| 5131 | /// This function will typically be called in a loop that successively |
| 5132 | /// "unwraps" pointer and pointer-to-member types to compare them at each |
| 5133 | /// level. |
| 5134 | /// |
| 5135 | /// \return \c true if a pointer type was unwrapped, \c false if we reached a |
| 5136 | /// pair of types that can't be unwrapped further. |
| 5137 | bool ASTContext::UnwrapSimilarTypes(QualType &T1, QualType &T2) { |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5138 | UnwrapSimilarArrayTypes(T1, T2); |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5139 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5140 | const auto *T1PtrType = T1->getAs<PointerType>(); |
| 5141 | const auto *T2PtrType = T2->getAs<PointerType>(); |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 5142 | if (T1PtrType && T2PtrType) { |
| 5143 | T1 = T1PtrType->getPointeeType(); |
| 5144 | T2 = T2PtrType->getPointeeType(); |
| 5145 | return true; |
| 5146 | } |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5147 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5148 | const auto *T1MPType = T1->getAs<MemberPointerType>(); |
| 5149 | const auto *T2MPType = T2->getAs<MemberPointerType>(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5150 | if (T1MPType && T2MPType && |
| 5151 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 5152 | QualType(T2MPType->getClass(), 0))) { |
| 5153 | T1 = T1MPType->getPointeeType(); |
| 5154 | T2 = T2MPType->getPointeeType(); |
| 5155 | return true; |
| 5156 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5157 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 5158 | if (getLangOpts().ObjC) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5159 | const auto *T1OPType = T1->getAs<ObjCObjectPointerType>(); |
| 5160 | const auto *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 5161 | if (T1OPType && T2OPType) { |
| 5162 | T1 = T1OPType->getPointeeType(); |
| 5163 | T2 = T2OPType->getPointeeType(); |
| 5164 | return true; |
| 5165 | } |
| 5166 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5167 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 5168 | // FIXME: Block pointers, too? |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5169 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 5170 | return false; |
| 5171 | } |
| 5172 | |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5173 | bool ASTContext::hasSimilarType(QualType T1, QualType T2) { |
| 5174 | while (true) { |
| 5175 | Qualifiers Quals; |
| 5176 | T1 = getUnqualifiedArrayType(T1, Quals); |
| 5177 | T2 = getUnqualifiedArrayType(T2, Quals); |
| 5178 | if (hasSameType(T1, T2)) |
| 5179 | return true; |
| 5180 | if (!UnwrapSimilarTypes(T1, T2)) |
| 5181 | return false; |
| 5182 | } |
| 5183 | } |
| 5184 | |
| 5185 | bool ASTContext::hasCvrSimilarType(QualType T1, QualType T2) { |
| 5186 | while (true) { |
| 5187 | Qualifiers Quals1, Quals2; |
| 5188 | T1 = getUnqualifiedArrayType(T1, Quals1); |
| 5189 | T2 = getUnqualifiedArrayType(T2, Quals2); |
| 5190 | |
| 5191 | Quals1.removeCVRQualifiers(); |
| 5192 | Quals2.removeCVRQualifiers(); |
| 5193 | if (Quals1 != Quals2) |
| 5194 | return false; |
| 5195 | |
| 5196 | if (hasSameType(T1, T2)) |
| 5197 | return true; |
| 5198 | |
| 5199 | if (!UnwrapSimilarTypes(T1, T2)) |
| 5200 | return false; |
| 5201 | } |
| 5202 | } |
| 5203 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5204 | DeclarationNameInfo |
| 5205 | ASTContext::getNameForTemplate(TemplateName Name, |
| 5206 | SourceLocation NameLoc) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5207 | switch (Name.getKind()) { |
| 5208 | case TemplateName::QualifiedTemplate: |
| 5209 | case TemplateName::Template: |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5210 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5211 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 5212 | NameLoc); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5213 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5214 | case TemplateName::OverloadedTemplate: { |
| 5215 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 5216 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 5217 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 5218 | } |
| 5219 | |
| 5220 | case TemplateName::DependentTemplate: { |
| 5221 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5222 | DeclarationName DName; |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 5223 | if (DTN->isIdentifier()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5224 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 5225 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 5226 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5227 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 5228 | // DNInfo work in progress: FIXME: source locations? |
| 5229 | DeclarationNameLoc DNLoc; |
| 5230 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 5231 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 5232 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 5233 | } |
| 5234 | } |
| 5235 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5236 | case TemplateName::SubstTemplateTemplateParm: { |
| 5237 | SubstTemplateTemplateParmStorage *subst |
| 5238 | = Name.getAsSubstTemplateTemplateParm(); |
| 5239 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 5240 | NameLoc); |
| 5241 | } |
| 5242 | |
| 5243 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 5244 | SubstTemplateTemplateParmPackStorage *subst |
| 5245 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 5246 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 5247 | NameLoc); |
| 5248 | } |
| 5249 | } |
| 5250 | |
| 5251 | llvm_unreachable("bad template name kind!"); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 5252 | } |
| 5253 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5254 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5255 | switch (Name.getKind()) { |
| 5256 | case TemplateName::QualifiedTemplate: |
| 5257 | case TemplateName::Template: { |
| 5258 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5259 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 5260 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5261 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 5262 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 5263 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 5264 | } |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 5265 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5266 | case TemplateName::OverloadedTemplate: |
| 5267 | llvm_unreachable("cannot canonicalize overloaded template"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5268 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5269 | case TemplateName::DependentTemplate: { |
| 5270 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 5271 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 5272 | return DTN->CanonicalTemplateName; |
| 5273 | } |
| 5274 | |
| 5275 | case TemplateName::SubstTemplateTemplateParm: { |
| 5276 | SubstTemplateTemplateParmStorage *subst |
| 5277 | = Name.getAsSubstTemplateTemplateParm(); |
| 5278 | return getCanonicalTemplateName(subst->getReplacement()); |
| 5279 | } |
| 5280 | |
| 5281 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 5282 | SubstTemplateTemplateParmPackStorage *subst |
| 5283 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 5284 | TemplateTemplateParmDecl *canonParameter |
| 5285 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 5286 | TemplateArgument canonArgPack |
| 5287 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 5288 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 5289 | } |
| 5290 | } |
| 5291 | |
| 5292 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 5293 | } |
| 5294 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 5295 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 5296 | X = getCanonicalTemplateName(X); |
| 5297 | Y = getCanonicalTemplateName(Y); |
| 5298 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 5299 | } |
| 5300 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5301 | TemplateArgument |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5302 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5303 | switch (Arg.getKind()) { |
| 5304 | case TemplateArgument::Null: |
| 5305 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5306 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5307 | case TemplateArgument::Expression: |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5308 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5309 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5310 | case TemplateArgument::Declaration: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5311 | auto *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl()); |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 5312 | return TemplateArgument(D, Arg.getParamTypeForDecl()); |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5313 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5314 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5315 | case TemplateArgument::NullPtr: |
| 5316 | return TemplateArgument(getCanonicalType(Arg.getNullPtrType()), |
| 5317 | /*isNullPtr*/true); |
| 5318 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5319 | case TemplateArgument::Template: |
| 5320 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5321 | |
| 5322 | case TemplateArgument::TemplateExpansion: |
| 5323 | return TemplateArgument(getCanonicalTemplateName( |
| 5324 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 5325 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5326 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5327 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5328 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5329 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5330 | case TemplateArgument::Type: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5331 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5332 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5333 | case TemplateArgument::Pack: { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 5334 | if (Arg.pack_size() == 0) |
| 5335 | return Arg; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5336 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5337 | auto *CanonArgs = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5338 | unsigned Idx = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5339 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5340 | AEnd = Arg.pack_end(); |
| 5341 | A != AEnd; (void)++A, ++Idx) |
| 5342 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5343 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5344 | return TemplateArgument(llvm::makeArrayRef(CanonArgs, Arg.pack_size())); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5345 | } |
| 5346 | } |
| 5347 | |
| 5348 | // Silence GCC warning |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5349 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5352 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5353 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5354 | if (!NNS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5355 | return nullptr; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5356 | |
| 5357 | switch (NNS->getKind()) { |
| 5358 | case NestedNameSpecifier::Identifier: |
| 5359 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5360 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5361 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 5362 | NNS->getAsIdentifier()); |
| 5363 | |
| 5364 | case NestedNameSpecifier::Namespace: |
| 5365 | // A namespace is canonical; build a nested-name-specifier with |
| 5366 | // this namespace and no prefix. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5367 | return NestedNameSpecifier::Create(*this, nullptr, |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 5368 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 5369 | |
| 5370 | case NestedNameSpecifier::NamespaceAlias: |
| 5371 | // A namespace is canonical; build a nested-name-specifier with |
| 5372 | // this namespace and no prefix. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5373 | return NestedNameSpecifier::Create(*this, nullptr, |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 5374 | NNS->getAsNamespaceAlias()->getNamespace() |
| 5375 | ->getOriginalNamespace()); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5376 | |
| 5377 | case NestedNameSpecifier::TypeSpec: |
| 5378 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 5379 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5380 | |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 5381 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 5382 | // break it apart into its prefix and identifier, then reconsititute those |
| 5383 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 5384 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 5385 | // types, e.g., |
| 5386 | // typedef typename T::type T1; |
| 5387 | // typedef typename T1::type T2; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5388 | if (const auto *DNT = T->getAs<DependentNameType>()) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5389 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 5390 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 5391 | |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 5392 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 5393 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 5394 | // first place? |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5395 | return NestedNameSpecifier::Create(*this, nullptr, false, |
| 5396 | const_cast<Type *>(T.getTypePtr())); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5397 | } |
| 5398 | |
| 5399 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 5400 | case NestedNameSpecifier::Super: |
| 5401 | // The global specifier and __super specifer are canonical and unique. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5402 | return NNS; |
| 5403 | } |
| 5404 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5405 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5406 | } |
| 5407 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5408 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5409 | // Handle the non-qualified case efficiently. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5410 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5411 | // Handle the common positive case fast. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5412 | if (const auto *AT = dyn_cast<ArrayType>(T)) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5413 | return AT; |
| 5414 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5415 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5416 | // Handle the common negative case fast. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5417 | if (!isa<ArrayType>(T.getCanonicalType())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5418 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5419 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5420 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5421 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 5422 | // 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] | 5423 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5424 | // If we get here, we either have type qualifiers on the type, or we have |
| 5425 | // 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] | 5426 | // we must propagate them down into the element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5427 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5428 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5429 | Qualifiers qs = split.Quals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5430 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5431 | // If we have a simple case, just return now. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5432 | const auto *ATy = dyn_cast<ArrayType>(split.Ty); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5433 | if (!ATy || qs.empty()) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5434 | return ATy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5435 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5436 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 5437 | // qualifiers into the array element type and return a new array type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5438 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5439 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5440 | if (const auto *CAT = dyn_cast<ConstantArrayType>(ATy)) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5441 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
| 5442 | CAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5443 | CAT->getIndexTypeCVRQualifiers())); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5444 | if (const auto *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5445 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 5446 | IAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5447 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5448 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5449 | if (const auto *DSAT = dyn_cast<DependentSizedArrayType>(ATy)) |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5450 | return cast<ArrayType>( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5451 | getDependentSizedArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5452 | DSAT->getSizeExpr(), |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5453 | DSAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5454 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5455 | DSAT->getBracketsRange())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5456 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5457 | const auto *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5458 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5459 | VAT->getSizeExpr(), |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5460 | VAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5461 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5462 | VAT->getBracketsRange())); |
Chris Lattner | ed0d079 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 5463 | } |
| 5464 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 5465 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 5466 | if (T->isArrayType() || T->isFunctionType()) |
| 5467 | return getDecayedType(T); |
| 5468 | return T; |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 5469 | } |
| 5470 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 5471 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 5472 | T = getVariableArrayDecayedType(T); |
| 5473 | T = getAdjustedParameterType(T); |
| 5474 | return T.getUnqualifiedType(); |
| 5475 | } |
| 5476 | |
David Majnemer | d09a51c | 2015-03-03 01:50:05 +0000 | [diff] [blame] | 5477 | QualType ASTContext::getExceptionObjectType(QualType T) const { |
| 5478 | // C++ [except.throw]p3: |
| 5479 | // A throw-expression initializes a temporary object, called the exception |
| 5480 | // object, the type of which is determined by removing any top-level |
| 5481 | // cv-qualifiers from the static type of the operand of throw and adjusting |
| 5482 | // the type from "array of T" or "function returning T" to "pointer to T" |
| 5483 | // or "pointer to function returning T", [...] |
| 5484 | T = getVariableArrayDecayedType(T); |
| 5485 | if (T->isArrayType() || T->isFunctionType()) |
| 5486 | T = getDecayedType(T); |
| 5487 | return T.getUnqualifiedType(); |
| 5488 | } |
| 5489 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 5490 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 5491 | /// specified array type to a pointer. This operation is non-trivial when |
| 5492 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 5493 | /// this returns a pointer to a properly qualified element of the array. |
| 5494 | /// |
| 5495 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5496 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5497 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 5498 | // typedefs in the element type of the array. This also handles propagation |
| 5499 | // of type qualifiers from the array type into the element type if present |
| 5500 | // (C99 6.7.3p8). |
| 5501 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 5502 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5503 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5504 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 5505 | |
| 5506 | // int x[restrict 4] -> int *restrict |
Jordan Rose | 303e2f1 | 2016-11-10 23:28:17 +0000 | [diff] [blame] | 5507 | QualType Result = getQualifiedType(PtrTy, |
| 5508 | PrettyArrayType->getIndexTypeQualifiers()); |
| 5509 | |
| 5510 | // int x[_Nullable] -> int * _Nullable |
| 5511 | if (auto Nullability = Ty->getNullability(*this)) { |
| 5512 | Result = const_cast<ASTContext *>(this)->getAttributedType( |
| 5513 | AttributedType::getNullabilityAttrKind(*Nullability), Result, Result); |
| 5514 | } |
| 5515 | return Result; |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 5516 | } |
| 5517 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5518 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 5519 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 5520 | } |
| 5521 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5522 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 5523 | Qualifiers qs; |
| 5524 | while (true) { |
| 5525 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5526 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5527 | if (!array) break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5528 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5529 | type = array->getElementType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5530 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5531 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5532 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5533 | return getQualifiedType(type, qs); |
Anders Carlsson | e0808df | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 5534 | } |
| 5535 | |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 5536 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5537 | uint64_t |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 5538 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 5539 | uint64_t ElementCount = 1; |
| 5540 | do { |
| 5541 | ElementCount *= CA->getSize().getZExtValue(); |
Richard Smith | 7808c6a | 2012-12-06 03:04:50 +0000 | [diff] [blame] | 5542 | CA = dyn_cast_or_null<ConstantArrayType>( |
| 5543 | CA->getElementType()->getAsArrayTypeUnsafe()); |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 5544 | } while (CA); |
| 5545 | return ElementCount; |
| 5546 | } |
| 5547 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 5548 | /// getFloatingRank - Return a relative rank for floating point types. |
| 5549 | /// 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] | 5550 | static FloatingRank getFloatingRank(QualType T) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5551 | if (const auto *CT = T->getAs<ComplexType>()) |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 5552 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5553 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5554 | assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type"); |
| 5555 | switch (T->getAs<BuiltinType>()->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5556 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 5557 | case BuiltinType::Float16: return Float16Rank; |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 5558 | case BuiltinType::Half: return HalfRank; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 5559 | case BuiltinType::Float: return FloatRank; |
| 5560 | case BuiltinType::Double: return DoubleRank; |
| 5561 | case BuiltinType::LongDouble: return LongDoubleRank; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5562 | case BuiltinType::Float128: return Float128Rank; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5563 | } |
| 5564 | } |
| 5565 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5566 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 5567 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | fc6ffa2 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 5568 | /// 'typeDomain' is a real floating point or complex type. |
| 5569 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 5570 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 5571 | QualType Domain) const { |
| 5572 | FloatingRank EltRank = getFloatingRank(Size); |
| 5573 | if (Domain->isComplexType()) { |
| 5574 | switch (EltRank) { |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 5575 | case Float16Rank: |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 5576 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 5577 | case FloatRank: return FloatComplexTy; |
| 5578 | case DoubleRank: return DoubleComplexTy; |
| 5579 | case LongDoubleRank: return LongDoubleComplexTy; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5580 | case Float128Rank: return Float128ComplexTy; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 5581 | } |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 5582 | } |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 5583 | |
| 5584 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 5585 | switch (EltRank) { |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 5586 | case Float16Rank: return HalfTy; |
Joey Gouly | dd7f456 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 5587 | case HalfRank: return HalfTy; |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 5588 | case FloatRank: return FloatTy; |
| 5589 | case DoubleRank: return DoubleTy; |
| 5590 | case LongDoubleRank: return LongDoubleTy; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5591 | case Float128Rank: return Float128Ty; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 5592 | } |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 5593 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5594 | } |
| 5595 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5596 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 5597 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 5598 | /// '_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] | 5599 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5600 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5601 | FloatingRank LHSR = getFloatingRank(LHS); |
| 5602 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5603 | |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5604 | if (LHSR == RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 5605 | return 0; |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5606 | if (LHSR > RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 5607 | return 1; |
| 5608 | return -1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5609 | } |
| 5610 | |
Erik Pilkington | eac7c3f | 2019-02-16 01:11:47 +0000 | [diff] [blame^] | 5611 | int ASTContext::getFloatingTypeSemanticOrder(QualType LHS, QualType RHS) const { |
| 5612 | if (&getFloatTypeSemantics(LHS) == &getFloatTypeSemantics(RHS)) |
| 5613 | return 0; |
| 5614 | return getFloatingTypeOrder(LHS, RHS); |
| 5615 | } |
| 5616 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5617 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 5618 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 5619 | /// or if it is not canonicalized. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5620 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 5621 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 5622 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5623 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5624 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5625 | case BuiltinType::Bool: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5626 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5627 | case BuiltinType::Char_S: |
| 5628 | case BuiltinType::Char_U: |
| 5629 | case BuiltinType::SChar: |
| 5630 | case BuiltinType::UChar: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5631 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5632 | case BuiltinType::Short: |
| 5633 | case BuiltinType::UShort: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5634 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5635 | case BuiltinType::Int: |
| 5636 | case BuiltinType::UInt: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5637 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5638 | case BuiltinType::Long: |
| 5639 | case BuiltinType::ULong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5640 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5641 | case BuiltinType::LongLong: |
| 5642 | case BuiltinType::ULongLong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5643 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 5644 | case BuiltinType::Int128: |
| 5645 | case BuiltinType::UInt128: |
| 5646 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5647 | } |
| 5648 | } |
| 5649 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5650 | /// Whether this is a promotable bitfield reference according |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5651 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 5652 | /// |
| 5653 | /// \returns the type this bit-field will promote to, or NULL if no |
| 5654 | /// promotion occurs. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5655 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | e05d3cb | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 5656 | if (E->isTypeDependent() || E->isValueDependent()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5657 | return {}; |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 5658 | |
Richard Smith | aadb254 | 2018-06-28 21:17:55 +0000 | [diff] [blame] | 5659 | // C++ [conv.prom]p5: |
| 5660 | // If the bit-field has an enumerated type, it is treated as any other |
| 5661 | // value of that type for promotion purposes. |
| 5662 | if (getLangOpts().CPlusPlus && E->getType()->isEnumeralType()) |
| 5663 | return {}; |
| 5664 | |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 5665 | // FIXME: We should not do this unless E->refersToBitField() is true. This |
| 5666 | // matters in C where getSourceBitField() will find bit-fields for various |
| 5667 | // cases where the source expression is not a bit-field designator. |
| 5668 | |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5669 | FieldDecl *Field = E->getSourceBitField(); // FIXME: conditional bit-fields? |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5670 | if (!Field) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5671 | return {}; |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5672 | |
| 5673 | QualType FT = Field->getType(); |
| 5674 | |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5675 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5676 | uint64_t IntSize = getTypeSize(IntTy); |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 5677 | // C++ [conv.prom]p5: |
| 5678 | // A prvalue for an integral bit-field can be converted to a prvalue of type |
| 5679 | // int if int can represent all the values of the bit-field; otherwise, it |
| 5680 | // can be converted to unsigned int if unsigned int can represent all the |
| 5681 | // values of the bit-field. If the bit-field is larger yet, no integral |
| 5682 | // promotion applies to it. |
| 5683 | // C11 6.3.1.1/2: |
| 5684 | // [For a bit-field of type _Bool, int, signed int, or unsigned int:] |
| 5685 | // If an int can represent all values of the original type (as restricted by |
| 5686 | // the width, for a bit-field), the value is converted to an int; otherwise, |
| 5687 | // it is converted to an unsigned int. |
| 5688 | // |
| 5689 | // FIXME: C does not permit promotion of a 'long : 3' bitfield to int. |
| 5690 | // We perform that promotion here to match GCC and C++. |
Richard Smith | aadb254 | 2018-06-28 21:17:55 +0000 | [diff] [blame] | 5691 | // FIXME: C does not permit promotion of an enum bit-field whose rank is |
| 5692 | // greater than that of 'int'. We perform that promotion to match GCC. |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5693 | if (BitWidth < IntSize) |
| 5694 | return IntTy; |
| 5695 | |
| 5696 | if (BitWidth == IntSize) |
| 5697 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 5698 | |
Richard Smith | aadb254 | 2018-06-28 21:17:55 +0000 | [diff] [blame] | 5699 | // Bit-fields wider than int are not subject to promotions, and therefore act |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 5700 | // like the base type. GCC has some weird bugs in this area that we |
| 5701 | // deliberately do not follow (GCC follows a pre-standard resolution to |
| 5702 | // C's DR315 which treats bit-width as being part of the type, and this leaks |
| 5703 | // into their semantics in some cases). |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5704 | return {}; |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5705 | } |
| 5706 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5707 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 5708 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 5709 | /// integer type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5710 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5711 | assert(!Promotable.isNull()); |
| 5712 | assert(Promotable->isPromotableIntegerType()); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5713 | if (const auto *ET = Promotable->getAs<EnumType>()) |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5714 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 5715 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5716 | if (const auto *BT = Promotable->getAs<BuiltinType>()) { |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 5717 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 5718 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 5719 | // types that can represent all the values of its underlying type: |
| 5720 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 5721 | // unsigned long long int [...] |
| 5722 | // FIXME: Is there some better way to compute this? |
| 5723 | if (BT->getKind() == BuiltinType::WChar_S || |
| 5724 | BT->getKind() == BuiltinType::WChar_U || |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 5725 | BT->getKind() == BuiltinType::Char8 || |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 5726 | BT->getKind() == BuiltinType::Char16 || |
| 5727 | BT->getKind() == BuiltinType::Char32) { |
| 5728 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 5729 | uint64_t FromSize = getTypeSize(BT); |
| 5730 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 5731 | LongLongTy, UnsignedLongLongTy }; |
| 5732 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 5733 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 5734 | if (FromSize < ToSize || |
| 5735 | (FromSize == ToSize && |
| 5736 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 5737 | return PromoteTypes[Idx]; |
| 5738 | } |
| 5739 | llvm_unreachable("char type should fit into long long"); |
| 5740 | } |
| 5741 | } |
| 5742 | |
| 5743 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5744 | if (Promotable->isSignedIntegerType()) |
| 5745 | return IntTy; |
Eli Friedman | 6745c3b | 2012-11-15 01:21:59 +0000 | [diff] [blame] | 5746 | uint64_t PromotableSize = getIntWidth(Promotable); |
| 5747 | uint64_t IntSize = getIntWidth(IntTy); |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5748 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 5749 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 5750 | } |
| 5751 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5752 | /// Recurses in pointer/array types until it finds an objc retainable |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 5753 | /// type and returns its ownership. |
| 5754 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 5755 | while (!T.isNull()) { |
| 5756 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 5757 | return T.getObjCLifetime(); |
| 5758 | if (T->isArrayType()) |
| 5759 | T = getBaseElementType(T); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5760 | else if (const auto *PT = T->getAs<PointerType>()) |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 5761 | T = PT->getPointeeType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5762 | else if (const auto *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 8e25253 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 5763 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 5764 | else |
| 5765 | break; |
| 5766 | } |
| 5767 | |
| 5768 | return Qualifiers::OCL_None; |
| 5769 | } |
| 5770 | |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5771 | static const Type *getIntegerTypeForEnum(const EnumType *ET) { |
| 5772 | // Incomplete enum types are not treated as integer types. |
| 5773 | // FIXME: In C++, enum types are never integer types. |
| 5774 | if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped()) |
| 5775 | return ET->getDecl()->getIntegerType().getTypePtr(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5776 | return nullptr; |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5777 | } |
| 5778 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5779 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5780 | /// 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] | 5781 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5782 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5783 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 5784 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5785 | |
| 5786 | // Unwrap enums to their underlying type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5787 | if (const auto *ET = dyn_cast<EnumType>(LHSC)) |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5788 | LHSC = getIntegerTypeForEnum(ET); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5789 | if (const auto *ET = dyn_cast<EnumType>(RHSC)) |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5790 | RHSC = getIntegerTypeForEnum(ET); |
| 5791 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5792 | if (LHSC == RHSC) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5793 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5794 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 5795 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5796 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5797 | unsigned LHSRank = getIntegerRank(LHSC); |
| 5798 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5799 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5800 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 5801 | if (LHSRank == RHSRank) return 0; |
| 5802 | return LHSRank > RHSRank ? 1 : -1; |
| 5803 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5804 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5805 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 5806 | if (LHSUnsigned) { |
| 5807 | // If the unsigned [LHS] type is larger, return it. |
| 5808 | if (LHSRank >= RHSRank) |
| 5809 | return 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5810 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5811 | // If the signed type can represent all values of the unsigned type, it |
| 5812 | // 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] | 5813 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5814 | return -1; |
| 5815 | } |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5816 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5817 | // If the unsigned [RHS] type is larger, return it. |
| 5818 | if (RHSRank >= LHSRank) |
| 5819 | return -1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5820 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5821 | // If the signed type can represent all values of the unsigned type, it |
| 5822 | // 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] | 5823 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5824 | return 1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5825 | } |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5826 | |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5827 | TypedefDecl *ASTContext::getCFConstantStringDecl() const { |
Saleem Abdulrasool | ef9b88a | 2018-10-24 16:38:16 +0000 | [diff] [blame] | 5828 | if (CFConstantStringTypeDecl) |
| 5829 | return CFConstantStringTypeDecl; |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 5830 | |
Saleem Abdulrasool | ef9b88a | 2018-10-24 16:38:16 +0000 | [diff] [blame] | 5831 | assert(!CFConstantStringTagDecl && |
| 5832 | "tag and typedef should be initialized together"); |
| 5833 | CFConstantStringTagDecl = buildImplicitRecord("__NSConstantString_tag"); |
| 5834 | CFConstantStringTagDecl->startDefinition(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5835 | |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 5836 | struct { |
| 5837 | QualType Type; |
| 5838 | const char *Name; |
| 5839 | } Fields[5]; |
| 5840 | unsigned Count = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5841 | |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 5842 | /// Objective-C ABI |
| 5843 | /// |
| 5844 | /// typedef struct __NSConstantString_tag { |
Saleem Abdulrasool | 1f6c41f | 2018-10-27 06:12:52 +0000 | [diff] [blame] | 5845 | /// const int *isa; |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 5846 | /// int flags; |
| 5847 | /// const char *str; |
| 5848 | /// long length; |
| 5849 | /// } __NSConstantString; |
| 5850 | /// |
| 5851 | /// Swift ABI (4.1, 4.2) |
| 5852 | /// |
| 5853 | /// typedef struct __NSConstantString_tag { |
| 5854 | /// uintptr_t _cfisa; |
| 5855 | /// uintptr_t _swift_rc; |
| 5856 | /// _Atomic(uint64_t) _cfinfoa; |
| 5857 | /// const char *_ptr; |
| 5858 | /// uint32_t _length; |
| 5859 | /// } __NSConstantString; |
| 5860 | /// |
| 5861 | /// Swift ABI (5.0) |
| 5862 | /// |
| 5863 | /// typedef struct __NSConstantString_tag { |
| 5864 | /// uintptr_t _cfisa; |
| 5865 | /// uintptr_t _swift_rc; |
| 5866 | /// _Atomic(uint64_t) _cfinfoa; |
| 5867 | /// const char *_ptr; |
| 5868 | /// uintptr_t _length; |
| 5869 | /// } __NSConstantString; |
| 5870 | |
| 5871 | const auto CFRuntime = getLangOpts().CFRuntime; |
| 5872 | if (static_cast<unsigned>(CFRuntime) < |
| 5873 | static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift)) { |
| 5874 | Fields[Count++] = { getPointerType(IntTy.withConst()), "isa" }; |
| 5875 | Fields[Count++] = { IntTy, "flags" }; |
| 5876 | Fields[Count++] = { getPointerType(CharTy.withConst()), "str" }; |
| 5877 | Fields[Count++] = { LongTy, "length" }; |
| 5878 | } else { |
| 5879 | Fields[Count++] = { getUIntPtrType(), "_cfisa" }; |
| 5880 | Fields[Count++] = { getUIntPtrType(), "_swift_rc" }; |
| 5881 | Fields[Count++] = { getFromTargetType(Target->getUInt64Type()), "_swift_rc" }; |
| 5882 | Fields[Count++] = { getPointerType(CharTy.withConst()), "_ptr" }; |
| 5883 | if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 || |
| 5884 | CFRuntime == LangOptions::CoreFoundationABI::Swift4_2) |
| 5885 | Fields[Count++] = { IntTy, "_ptr" }; |
| 5886 | else |
| 5887 | Fields[Count++] = { getUIntPtrType(), "_ptr" }; |
| 5888 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5889 | |
Saleem Abdulrasool | ef9b88a | 2018-10-24 16:38:16 +0000 | [diff] [blame] | 5890 | // Create fields |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 5891 | for (unsigned i = 0; i < Count; ++i) { |
Saleem Abdulrasool | ef9b88a | 2018-10-24 16:38:16 +0000 | [diff] [blame] | 5892 | FieldDecl *Field = |
| 5893 | FieldDecl::Create(*this, CFConstantStringTagDecl, SourceLocation(), |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 5894 | SourceLocation(), &Idents.get(Fields[i].Name), |
| 5895 | Fields[i].Type, /*TInfo=*/nullptr, |
Saleem Abdulrasool | ef9b88a | 2018-10-24 16:38:16 +0000 | [diff] [blame] | 5896 | /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit); |
| 5897 | Field->setAccess(AS_public); |
| 5898 | CFConstantStringTagDecl->addDecl(Field); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5899 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5900 | |
Saleem Abdulrasool | ef9b88a | 2018-10-24 16:38:16 +0000 | [diff] [blame] | 5901 | CFConstantStringTagDecl->completeDefinition(); |
| 5902 | // This type is designed to be compatible with NSConstantString, but cannot |
| 5903 | // use the same name, since NSConstantString is an interface. |
| 5904 | auto tagType = getTagDeclType(CFConstantStringTagDecl); |
| 5905 | CFConstantStringTypeDecl = |
| 5906 | buildImplicitTypedef(tagType, "__NSConstantString"); |
| 5907 | |
Quentin Colombet | 043406b | 2016-02-03 22:41:00 +0000 | [diff] [blame] | 5908 | return CFConstantStringTypeDecl; |
| 5909 | } |
| 5910 | |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5911 | RecordDecl *ASTContext::getCFConstantStringTagDecl() const { |
| 5912 | if (!CFConstantStringTagDecl) |
| 5913 | getCFConstantStringDecl(); // Build the tag and the typedef. |
| 5914 | return CFConstantStringTagDecl; |
| 5915 | } |
| 5916 | |
Quentin Colombet | 043406b | 2016-02-03 22:41:00 +0000 | [diff] [blame] | 5917 | // getCFConstantStringType - Return the type used for constant CFStrings. |
| 5918 | QualType ASTContext::getCFConstantStringType() const { |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5919 | return getTypedefType(getCFConstantStringDecl()); |
Gabor Greif | 412af03 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 5920 | } |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 5921 | |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 5922 | QualType ASTContext::getObjCSuperType() const { |
| 5923 | if (ObjCSuperType.isNull()) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5924 | RecordDecl *ObjCSuperTypeDecl = buildImplicitRecord("objc_super"); |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 5925 | TUDecl->addDecl(ObjCSuperTypeDecl); |
| 5926 | ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); |
| 5927 | } |
| 5928 | return ObjCSuperType; |
| 5929 | } |
| 5930 | |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 5931 | void ASTContext::setCFConstantStringType(QualType T) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5932 | const auto *TD = T->getAs<TypedefType>(); |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5933 | assert(TD && "Invalid CFConstantStringType"); |
| 5934 | CFConstantStringTypeDecl = cast<TypedefDecl>(TD->getDecl()); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5935 | const auto *TagType = |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5936 | CFConstantStringTypeDecl->getUnderlyingType()->getAs<RecordType>(); |
| 5937 | assert(TagType && "Invalid CFConstantStringType"); |
| 5938 | CFConstantStringTagDecl = TagType->getDecl(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 5939 | } |
| 5940 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5941 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5942 | if (BlockDescriptorType) |
| 5943 | return getTagDeclType(BlockDescriptorType); |
| 5944 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5945 | RecordDecl *RD; |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5946 | // FIXME: Needs the FlagAppleBlock bit. |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5947 | RD = buildImplicitRecord("__block_descriptor"); |
| 5948 | RD->startDefinition(); |
| 5949 | |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5950 | QualType FieldTypes[] = { |
| 5951 | UnsignedLongTy, |
| 5952 | UnsignedLongTy, |
| 5953 | }; |
| 5954 | |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 5955 | static const char *const FieldNames[] = { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5956 | "reserved", |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5957 | "Size" |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5958 | }; |
| 5959 | |
| 5960 | for (size_t i = 0; i < 2; ++i) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5961 | FieldDecl *Field = FieldDecl::Create( |
| 5962 | *this, RD, SourceLocation(), SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5963 | &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
| 5964 | /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 5965 | Field->setAccess(AS_public); |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5966 | RD->addDecl(Field); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5967 | } |
| 5968 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5969 | RD->completeDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5970 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5971 | BlockDescriptorType = RD; |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 5972 | |
| 5973 | return getTagDeclType(BlockDescriptorType); |
| 5974 | } |
| 5975 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5976 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5977 | if (BlockDescriptorExtendedType) |
| 5978 | return getTagDeclType(BlockDescriptorExtendedType); |
| 5979 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5980 | RecordDecl *RD; |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5981 | // FIXME: Needs the FlagAppleBlock bit. |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 5982 | RD = buildImplicitRecord("__block_descriptor_withcopydispose"); |
| 5983 | RD->startDefinition(); |
| 5984 | |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5985 | QualType FieldTypes[] = { |
| 5986 | UnsignedLongTy, |
| 5987 | UnsignedLongTy, |
| 5988 | getPointerType(VoidPtrTy), |
| 5989 | getPointerType(VoidPtrTy) |
| 5990 | }; |
| 5991 | |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 5992 | static const char *const FieldNames[] = { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 5993 | "reserved", |
| 5994 | "Size", |
| 5995 | "CopyFuncPtr", |
| 5996 | "DestroyFuncPtr" |
| 5997 | }; |
| 5998 | |
| 5999 | for (size_t i = 0; i < 4; ++i) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6000 | FieldDecl *Field = FieldDecl::Create( |
| 6001 | *this, RD, SourceLocation(), SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6002 | &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
| 6003 | /*BitWidth=*/nullptr, |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6004 | /*Mutable=*/false, ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 6005 | Field->setAccess(AS_public); |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6006 | RD->addDecl(Field); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 6007 | } |
| 6008 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6009 | RD->completeDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 6010 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6011 | BlockDescriptorExtendedType = RD; |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 6012 | return getTagDeclType(BlockDescriptorExtendedType); |
| 6013 | } |
| 6014 | |
Sven van Haastregt | 3bb7eaf | 2017-12-06 10:11:28 +0000 | [diff] [blame] | 6015 | TargetInfo::OpenCLTypeKind ASTContext::getOpenCLTypeKind(const Type *T) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6016 | const auto *BT = dyn_cast<BuiltinType>(T); |
Sven van Haastregt | 3bb7eaf | 2017-12-06 10:11:28 +0000 | [diff] [blame] | 6017 | |
| 6018 | if (!BT) { |
| 6019 | if (isa<PipeType>(T)) |
| 6020 | return TargetInfo::OCLTK_Pipe; |
| 6021 | |
| 6022 | return TargetInfo::OCLTK_Default; |
| 6023 | } |
| 6024 | |
| 6025 | switch (BT->getKind()) { |
| 6026 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 6027 | case BuiltinType::Id: \ |
| 6028 | return TargetInfo::OCLTK_Image; |
| 6029 | #include "clang/Basic/OpenCLImageTypes.def" |
| 6030 | |
| 6031 | case BuiltinType::OCLClkEvent: |
| 6032 | return TargetInfo::OCLTK_ClkEvent; |
| 6033 | |
| 6034 | case BuiltinType::OCLEvent: |
| 6035 | return TargetInfo::OCLTK_Event; |
| 6036 | |
| 6037 | case BuiltinType::OCLQueue: |
| 6038 | return TargetInfo::OCLTK_Queue; |
| 6039 | |
| 6040 | case BuiltinType::OCLReserveID: |
| 6041 | return TargetInfo::OCLTK_ReserveID; |
| 6042 | |
| 6043 | case BuiltinType::OCLSampler: |
| 6044 | return TargetInfo::OCLTK_Sampler; |
| 6045 | |
| 6046 | default: |
| 6047 | return TargetInfo::OCLTK_Default; |
| 6048 | } |
| 6049 | } |
| 6050 | |
| 6051 | LangAS ASTContext::getOpenCLTypeAddrSpace(const Type *T) const { |
| 6052 | return Target->getOpenCLTypeAddrSpace(getOpenCLTypeKind(T)); |
| 6053 | } |
| 6054 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6055 | /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty" |
| 6056 | /// requires copy/dispose. Note that this must match the logic |
| 6057 | /// in buildByrefHelpers. |
| 6058 | bool ASTContext::BlockRequiresCopying(QualType Ty, |
| 6059 | const VarDecl *D) { |
| 6060 | if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) { |
Akira Hatanaka | 9978da3 | 2018-08-10 15:09:24 +0000 | [diff] [blame] | 6061 | const Expr *copyExpr = getBlockVarCopyInit(D).getCopyExpr(); |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6062 | if (!copyExpr && record->hasTrivialDestructor()) return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6063 | |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 6064 | return true; |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 6065 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6066 | |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 6067 | // The block needs copy/destroy helpers if Ty is non-trivial to destructively |
| 6068 | // move or destroy. |
| 6069 | if (Ty.isNonTrivialToPrimitiveDestructiveMove() || Ty.isDestructedType()) |
| 6070 | return true; |
| 6071 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6072 | if (!Ty->isObjCRetainableType()) return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6073 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6074 | Qualifiers qs = Ty.getQualifiers(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6075 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6076 | // If we have lifetime, that dominates. |
| 6077 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6078 | switch (lifetime) { |
| 6079 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6080 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6081 | // These are just bits as far as the runtime is concerned. |
| 6082 | case Qualifiers::OCL_ExplicitNone: |
| 6083 | case Qualifiers::OCL_Autoreleasing: |
| 6084 | return false; |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 6085 | |
| 6086 | // These cases should have been taken care of when checking the type's |
| 6087 | // non-triviality. |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6088 | case Qualifiers::OCL_Weak: |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6089 | case Qualifiers::OCL_Strong: |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 6090 | llvm_unreachable("impossible"); |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6091 | } |
| 6092 | llvm_unreachable("fell out of lifetime switch!"); |
| 6093 | } |
| 6094 | return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) || |
| 6095 | Ty->isObjCObjectPointerType()); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 6096 | } |
| 6097 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 6098 | bool ASTContext::getByrefLifetime(QualType Ty, |
| 6099 | Qualifiers::ObjCLifetime &LifeTime, |
| 6100 | bool &HasByrefExtendedLayout) const { |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 6101 | if (!getLangOpts().ObjC || |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 6102 | getLangOpts().getGC() != LangOptions::NonGC) |
| 6103 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6104 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 6105 | HasByrefExtendedLayout = false; |
Fariborz Jahanian | f762b72 | 2012-12-11 19:58:01 +0000 | [diff] [blame] | 6106 | if (Ty->isRecordType()) { |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 6107 | HasByrefExtendedLayout = true; |
| 6108 | LifeTime = Qualifiers::OCL_None; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 6109 | } else if ((LifeTime = Ty.getObjCLifetime())) { |
| 6110 | // Honor the ARC qualifiers. |
| 6111 | } else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) { |
| 6112 | // The MRR rule. |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 6113 | LifeTime = Qualifiers::OCL_ExplicitNone; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 6114 | } else { |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 6115 | LifeTime = Qualifiers::OCL_None; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 6116 | } |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 6117 | return true; |
| 6118 | } |
| 6119 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 6120 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 6121 | if (!ObjCInstanceTypeDecl) |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6122 | ObjCInstanceTypeDecl = |
| 6123 | buildImplicitTypedef(getObjCIdType(), "instancetype"); |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 6124 | return ObjCInstanceTypeDecl; |
| 6125 | } |
| 6126 | |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 6127 | // This returns true if a type has been typedefed to BOOL: |
| 6128 | // typedef <type> BOOL; |
Chris Lattner | e021899 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 6129 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6130 | if (const auto *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 9b1f279 | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 6131 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 6132 | return II->isStr("BOOL"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6133 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6134 | return false; |
| 6135 | } |
| 6136 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6137 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6138 | /// purpose. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6139 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6140 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 6141 | return CharUnits::Zero(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6142 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6143 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6144 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6145 | // 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] | 6146 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6147 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6148 | // Treat arrays as pointers, since that's how they're passed in. |
| 6149 | else if (type->isArrayType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6150 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 6151 | return sz; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6152 | } |
| 6153 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 6154 | bool ASTContext::isMSStaticDataMemberInlineDefinition(const VarDecl *VD) const { |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 6155 | return getTargetInfo().getCXXABI().isMicrosoft() && |
| 6156 | VD->isStaticDataMember() && |
Hans Wennborg | b18da9b | 2018-02-20 12:43:02 +0000 | [diff] [blame] | 6157 | VD->getType()->isIntegralOrEnumerationType() && |
David Majnemer | fac5243 | 2015-10-19 23:22:49 +0000 | [diff] [blame] | 6158 | !VD->getFirstDecl()->isOutOfLine() && VD->getFirstDecl()->hasInit(); |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 6159 | } |
| 6160 | |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 6161 | ASTContext::InlineVariableDefinitionKind |
| 6162 | ASTContext::getInlineVariableDefinitionKind(const VarDecl *VD) const { |
| 6163 | if (!VD->isInline()) |
| 6164 | return InlineVariableDefinitionKind::None; |
| 6165 | |
| 6166 | // In almost all cases, it's a weak definition. |
| 6167 | auto *First = VD->getFirstDecl(); |
Richard Smith | 8910fe6 | 2017-10-23 03:58:34 +0000 | [diff] [blame] | 6168 | if (First->isInlineSpecified() || !First->isStaticDataMember()) |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 6169 | return InlineVariableDefinitionKind::Weak; |
| 6170 | |
| 6171 | // If there's a file-context declaration in this translation unit, it's a |
| 6172 | // non-discardable definition. |
| 6173 | for (auto *D : VD->redecls()) |
Richard Smith | 8910fe6 | 2017-10-23 03:58:34 +0000 | [diff] [blame] | 6174 | if (D->getLexicalDeclContext()->isFileContext() && |
| 6175 | !D->isInlineSpecified() && (D->isConstexpr() || First->isConstexpr())) |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 6176 | return InlineVariableDefinitionKind::Strong; |
| 6177 | |
| 6178 | // If we've not seen one yet, we don't know. |
| 6179 | return InlineVariableDefinitionKind::WeakUnknown; |
| 6180 | } |
| 6181 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6182 | static std::string charUnitsToString(const CharUnits &CU) { |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6183 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6184 | } |
| 6185 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 6186 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6187 | /// declaration. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 6188 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 6189 | std::string S; |
| 6190 | |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6191 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 6192 | QualType BlockTy = |
| 6193 | Expr->getType()->getAs<BlockPointerType>()->getPointeeType(); |
| 6194 | // Encode result type. |
Fariborz Jahanian | 0e3043b | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 6195 | if (getLangOpts().EncodeExtendedBlockSig) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6196 | getObjCEncodingForMethodParameter( |
| 6197 | Decl::OBJC_TQ_None, BlockTy->getAs<FunctionType>()->getReturnType(), S, |
| 6198 | true /*Extended*/); |
Fariborz Jahanian | 64223e6 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 6199 | else |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6200 | getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getReturnType(), S); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6201 | // Compute size of all parameters. |
| 6202 | // Start with computing size of a pointer in number of bytes. |
| 6203 | // FIXME: There might(should) be a better way of doing this computation! |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6204 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 6205 | CharUnits ParmOffset = PtrSize; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 6206 | for (auto PI : Decl->parameters()) { |
Aaron Ballman | b2b8b1d | 2014-03-07 16:09:59 +0000 | [diff] [blame] | 6207 | QualType PType = PI->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 6208 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | d487941 | 2012-06-30 00:48:59 +0000 | [diff] [blame] | 6209 | if (sz.isZero()) |
| 6210 | continue; |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 6211 | assert(sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6212 | ParmOffset += sz; |
| 6213 | } |
| 6214 | // Size of the argument frame |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6215 | S += charUnitsToString(ParmOffset); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6216 | // Block pointer and offset. |
| 6217 | S += "@?0"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6218 | |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6219 | // Argument types. |
| 6220 | ParmOffset = PtrSize; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 6221 | for (auto PVDecl : Decl->parameters()) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6222 | QualType PType = PVDecl->getOriginalType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6223 | if (const auto *AT = |
| 6224 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6225 | // Use array's original type only if it has known number of |
| 6226 | // elements. |
| 6227 | if (!isa<ConstantArrayType>(AT)) |
| 6228 | PType = PVDecl->getType(); |
| 6229 | } else if (PType->isFunctionType()) |
| 6230 | PType = PVDecl->getType(); |
Fariborz Jahanian | 0e3043b | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 6231 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 64223e6 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 6232 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType, |
| 6233 | S, true /*Extended*/); |
| 6234 | else |
| 6235 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6236 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 6237 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6238 | } |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 6239 | |
| 6240 | return S; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6241 | } |
| 6242 | |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6243 | std::string |
| 6244 | ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl) const { |
| 6245 | std::string S; |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6246 | // Encode result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6247 | getObjCEncodingForType(Decl->getReturnType(), S); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6248 | CharUnits ParmOffset; |
| 6249 | // Compute size of all parameters. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 6250 | for (auto PI : Decl->parameters()) { |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 6251 | QualType PType = PI->getType(); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6252 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6253 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 6254 | continue; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6255 | |
| 6256 | assert(sz.isPositive() && |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6257 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6258 | ParmOffset += sz; |
| 6259 | } |
| 6260 | S += charUnitsToString(ParmOffset); |
| 6261 | ParmOffset = CharUnits::Zero(); |
| 6262 | |
| 6263 | // Argument types. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 6264 | for (auto PVDecl : Decl->parameters()) { |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6265 | QualType PType = PVDecl->getOriginalType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6266 | if (const auto *AT = |
| 6267 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6268 | // Use array's original type only if it has known number of |
| 6269 | // elements. |
| 6270 | if (!isa<ConstantArrayType>(AT)) |
| 6271 | PType = PVDecl->getType(); |
| 6272 | } else if (PType->isFunctionType()) |
| 6273 | PType = PVDecl->getType(); |
| 6274 | getObjCEncodingForType(PType, S); |
| 6275 | S += charUnitsToString(ParmOffset); |
| 6276 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 6277 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6278 | |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6279 | return S; |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6280 | } |
| 6281 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6282 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6283 | /// method parameter or return type. If Extended, include class names and |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6284 | /// block object types. |
| 6285 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 6286 | QualType T, std::string& S, |
| 6287 | bool Extended) const { |
| 6288 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 6289 | getObjCEncodingForTypeQualifier(QT, S); |
| 6290 | // Encode parameter type. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6291 | getObjCEncodingForTypeImpl(T, S, true, true, nullptr, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6292 | true /*OutermostType*/, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6293 | false /*EncodingProperty*/, |
| 6294 | false /*StructField*/, |
| 6295 | Extended /*EncodeBlockParameters*/, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6296 | Extended /*EncodeClassNames*/); |
| 6297 | } |
| 6298 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6299 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6300 | /// declaration. |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6301 | std::string ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
| 6302 | bool Extended) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6303 | // FIXME: This is not very efficient. |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6304 | // Encode return type. |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6305 | std::string S; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6306 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 6307 | Decl->getReturnType(), S, Extended); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6308 | // Compute size of all parameters. |
| 6309 | // Start with computing size of a pointer in number of bytes. |
| 6310 | // FIXME: There might(should) be a better way of doing this computation! |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6311 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6312 | // The first two arguments (self and _cmd) are pointers; account for |
| 6313 | // their size. |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6314 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 6315 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 6316 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 6317 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 6318 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6319 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 6320 | continue; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6321 | |
| 6322 | assert(sz.isPositive() && |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 6323 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6324 | ParmOffset += sz; |
| 6325 | } |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6326 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6327 | S += "@0:"; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6328 | S += charUnitsToString(PtrSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6329 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6330 | // Argument types. |
| 6331 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 6332 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 6333 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 6334 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6335 | QualType PType = PVDecl->getOriginalType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6336 | if (const auto *AT = |
| 6337 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 6338 | // Use array's original type only if it has known number of |
| 6339 | // elements. |
Steve Naroff | 323827e | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 6340 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 6341 | PType = PVDecl->getType(); |
| 6342 | } else if (PType->isFunctionType()) |
| 6343 | PType = PVDecl->getType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6344 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6345 | PType, S, Extended); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6346 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 6347 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6348 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6349 | |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6350 | return S; |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6351 | } |
| 6352 | |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 6353 | ObjCPropertyImplDecl * |
| 6354 | ASTContext::getObjCPropertyImplDeclForPropertyDecl( |
| 6355 | const ObjCPropertyDecl *PD, |
| 6356 | const Decl *Container) const { |
| 6357 | if (!Container) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6358 | return nullptr; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6359 | if (const auto *CID = dyn_cast<ObjCCategoryImplDecl>(Container)) { |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 6360 | for (auto *PID : CID->property_impls()) |
| 6361 | if (PID->getPropertyDecl() == PD) |
| 6362 | return PID; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6363 | } else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6364 | const auto *OID = cast<ObjCImplementationDecl>(Container); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6365 | for (auto *PID : OID->property_impls()) |
| 6366 | if (PID->getPropertyDecl() == PD) |
| 6367 | return PID; |
| 6368 | } |
| 6369 | return nullptr; |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 6370 | } |
| 6371 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6372 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 6373 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6374 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 6375 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6376 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 6377 | /// attributes readonly and bycopy are encoded as single characters. The |
| 6378 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 6379 | /// encoded as single characters, followed by an identifier. Property types |
| 6380 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 6381 | /// these attributes are defined by the following enumeration: |
| 6382 | /// @code |
| 6383 | /// enum PropertyAttributes { |
| 6384 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 6385 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 6386 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 6387 | /// kPropertyDynamic = 'D', // property is dynamic |
| 6388 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 6389 | /// kPropertySetter = 'S', // followed by setter selector name |
| 6390 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 8cf6185 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 6391 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 6392 | /// kPropertyWeak = 'W' // 'weak' property |
| 6393 | /// kPropertyStrong = 'P' // property GC'able |
| 6394 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 6395 | /// }; |
| 6396 | /// @endcode |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6397 | std::string |
| 6398 | ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
| 6399 | const Decl *Container) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6400 | // Collect information from the property implementation decl(s). |
| 6401 | bool Dynamic = false; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6402 | ObjCPropertyImplDecl *SynthesizePID = nullptr; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6403 | |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 6404 | if (ObjCPropertyImplDecl *PropertyImpDecl = |
| 6405 | getObjCPropertyImplDeclForPropertyDecl(PD, Container)) { |
| 6406 | if (PropertyImpDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 6407 | Dynamic = true; |
| 6408 | else |
| 6409 | SynthesizePID = PropertyImpDecl; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6410 | } |
| 6411 | |
| 6412 | // FIXME: This is not very efficient. |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6413 | std::string S = "T"; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6414 | |
| 6415 | // Encode result type. |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 6416 | // GCC has some special rules regarding encoding of properties which |
| 6417 | // closely resembles encoding of ivars. |
Joe Groff | 98ac7d2 | 2014-07-07 22:25:15 +0000 | [diff] [blame] | 6418 | getObjCEncodingForPropertyType(PD->getType(), S); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6419 | |
| 6420 | if (PD->isReadOnly()) { |
| 6421 | S += ",R"; |
Nico Weber | 4e8626f | 2013-05-08 23:47:40 +0000 | [diff] [blame] | 6422 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) |
| 6423 | S += ",C"; |
| 6424 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) |
| 6425 | S += ",&"; |
Fariborz Jahanian | 33079ee | 2014-04-02 22:49:42 +0000 | [diff] [blame] | 6426 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) |
| 6427 | S += ",W"; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6428 | } else { |
| 6429 | switch (PD->getSetterKind()) { |
| 6430 | case ObjCPropertyDecl::Assign: break; |
| 6431 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6432 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 70a315c | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 6433 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6434 | } |
| 6435 | } |
| 6436 | |
| 6437 | // It really isn't clear at all what this means, since properties |
| 6438 | // are "dynamic by default". |
| 6439 | if (Dynamic) |
| 6440 | S += ",D"; |
| 6441 | |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 6442 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 6443 | S += ",N"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6444 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6445 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 6446 | S += ",G"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 6447 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6448 | } |
| 6449 | |
| 6450 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 6451 | S += ",S"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 6452 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6453 | } |
| 6454 | |
| 6455 | if (SynthesizePID) { |
| 6456 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 6457 | S += ",V"; |
Chris Lattner | 1cbaacc | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 6458 | S += OID->getNameAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6459 | } |
| 6460 | |
| 6461 | // FIXME: OBJCGC: weak & strong |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6462 | return S; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6463 | } |
| 6464 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6465 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6466 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 6467 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6468 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6469 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 6470 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6471 | if (const auto *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6472 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6473 | PointeeTy = UnsignedIntTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6474 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6475 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6476 | PointeeTy = IntTy; |
| 6477 | } |
| 6478 | } |
| 6479 | } |
| 6480 | |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 6481 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6482 | const FieldDecl *Field, |
| 6483 | QualType *NotEncodedT) const { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 6484 | // We follow the behavior of gcc, expanding structures which are |
| 6485 | // directly pointed to, and expanding embedded structures. Note that |
| 6486 | // these rules are sufficient to prevent recursive encoding of the |
| 6487 | // same type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6488 | getObjCEncodingForTypeImpl(T, S, true, true, Field, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6489 | true /* outermost type */, false, false, |
| 6490 | false, false, false, NotEncodedT); |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 6491 | } |
| 6492 | |
Joe Groff | 98ac7d2 | 2014-07-07 22:25:15 +0000 | [diff] [blame] | 6493 | void ASTContext::getObjCEncodingForPropertyType(QualType T, |
| 6494 | std::string& S) const { |
| 6495 | // Encode result type. |
| 6496 | // GCC has some special rules regarding encoding of properties which |
| 6497 | // closely resembles encoding of ivars. |
| 6498 | getObjCEncodingForTypeImpl(T, S, true, true, nullptr, |
| 6499 | true /* outermost type */, |
| 6500 | true /* encoding property */); |
| 6501 | } |
| 6502 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6503 | static char getObjCEncodingForPrimitiveKind(const ASTContext *C, |
| 6504 | BuiltinType::Kind kind) { |
| 6505 | switch (kind) { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6506 | case BuiltinType::Void: return 'v'; |
| 6507 | case BuiltinType::Bool: return 'B'; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 6508 | case BuiltinType::Char8: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6509 | case BuiltinType::Char_U: |
| 6510 | case BuiltinType::UChar: return 'C'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6511 | case BuiltinType::Char16: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6512 | case BuiltinType::UShort: return 'S'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6513 | case BuiltinType::Char32: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6514 | case BuiltinType::UInt: return 'I'; |
| 6515 | case BuiltinType::ULong: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6516 | return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6517 | case BuiltinType::UInt128: return 'T'; |
| 6518 | case BuiltinType::ULongLong: return 'Q'; |
| 6519 | case BuiltinType::Char_S: |
| 6520 | case BuiltinType::SChar: return 'c'; |
| 6521 | case BuiltinType::Short: return 's'; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 6522 | case BuiltinType::WChar_S: |
| 6523 | case BuiltinType::WChar_U: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6524 | case BuiltinType::Int: return 'i'; |
| 6525 | case BuiltinType::Long: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6526 | return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6527 | case BuiltinType::LongLong: return 'q'; |
| 6528 | case BuiltinType::Int128: return 't'; |
| 6529 | case BuiltinType::Float: return 'f'; |
| 6530 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 7cba5a7 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 6531 | case BuiltinType::LongDouble: return 'D'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6532 | case BuiltinType::NullPtr: return '*'; // like char* |
| 6533 | |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 6534 | case BuiltinType::Float16: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 6535 | case BuiltinType::Float128: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6536 | case BuiltinType::Half: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 6537 | case BuiltinType::ShortAccum: |
| 6538 | case BuiltinType::Accum: |
| 6539 | case BuiltinType::LongAccum: |
| 6540 | case BuiltinType::UShortAccum: |
| 6541 | case BuiltinType::UAccum: |
| 6542 | case BuiltinType::ULongAccum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 6543 | case BuiltinType::ShortFract: |
| 6544 | case BuiltinType::Fract: |
| 6545 | case BuiltinType::LongFract: |
| 6546 | case BuiltinType::UShortFract: |
| 6547 | case BuiltinType::UFract: |
| 6548 | case BuiltinType::ULongFract: |
| 6549 | case BuiltinType::SatShortAccum: |
| 6550 | case BuiltinType::SatAccum: |
| 6551 | case BuiltinType::SatLongAccum: |
| 6552 | case BuiltinType::SatUShortAccum: |
| 6553 | case BuiltinType::SatUAccum: |
| 6554 | case BuiltinType::SatULongAccum: |
| 6555 | case BuiltinType::SatShortFract: |
| 6556 | case BuiltinType::SatFract: |
| 6557 | case BuiltinType::SatLongFract: |
| 6558 | case BuiltinType::SatUShortFract: |
| 6559 | case BuiltinType::SatUFract: |
| 6560 | case BuiltinType::SatULongFract: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6561 | // FIXME: potentially need @encodes for these! |
| 6562 | return ' '; |
| 6563 | |
| 6564 | case BuiltinType::ObjCId: |
| 6565 | case BuiltinType::ObjCClass: |
| 6566 | case BuiltinType::ObjCSel: |
| 6567 | llvm_unreachable("@encoding ObjC primitive type"); |
| 6568 | |
| 6569 | // OpenCL and placeholder types don't need @encodings. |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 6570 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 6571 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 6572 | #include "clang/Basic/OpenCLImageTypes.def" |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 6573 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
| 6574 | case BuiltinType::Id: |
| 6575 | #include "clang/Basic/OpenCLExtensionTypes.def" |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6576 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 6577 | case BuiltinType::OCLClkEvent: |
| 6578 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 6579 | case BuiltinType::OCLReserveID: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6580 | case BuiltinType::OCLSampler: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6581 | case BuiltinType::Dependent: |
| 6582 | #define BUILTIN_TYPE(KIND, ID) |
| 6583 | #define PLACEHOLDER_TYPE(KIND, ID) \ |
| 6584 | case BuiltinType::KIND: |
| 6585 | #include "clang/AST/BuiltinTypes.def" |
| 6586 | llvm_unreachable("invalid builtin type for @encode"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6587 | } |
David Blaikie | 5a6a020 | 2013-01-09 17:48:41 +0000 | [diff] [blame] | 6588 | llvm_unreachable("invalid BuiltinType::Kind value"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6589 | } |
| 6590 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 6591 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 6592 | EnumDecl *Enum = ET->getDecl(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6593 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 6594 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 6595 | if (!Enum->isFixed()) |
| 6596 | return 'i'; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6597 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 6598 | // The encoding of a fixed enum type matches its fixed underlying type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6599 | const auto *BT = Enum->getIntegerType()->castAs<BuiltinType>(); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6600 | return getObjCEncodingForPrimitiveKind(C, BT->getKind()); |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 6601 | } |
| 6602 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6603 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6604 | QualType T, const FieldDecl *FD) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6605 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 6606 | S += 'b'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6607 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 6608 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 6609 | // then the offset (in bits) of the first element, then the type of the |
| 6610 | // bitfield, then the size in bits. For example, in this structure: |
| 6611 | // |
| 6612 | // struct |
| 6613 | // { |
| 6614 | // int integer; |
| 6615 | // int flags:2; |
| 6616 | // }; |
| 6617 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 6618 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 6619 | // information is not especially sensible, but we're stuck with it for |
| 6620 | // compatibility with GCC, although providing it breaks anything that |
| 6621 | // actually uses runtime introspection and wants to work on both runtimes... |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 6622 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
Akira Hatanaka | 4b1c484 | 2017-06-27 04:34:04 +0000 | [diff] [blame] | 6623 | uint64_t Offset; |
| 6624 | |
| 6625 | if (const auto *IVD = dyn_cast<ObjCIvarDecl>(FD)) { |
| 6626 | Offset = Ctx->lookupFieldBitOffset(IVD->getContainingInterface(), nullptr, |
| 6627 | IVD); |
| 6628 | } else { |
| 6629 | const RecordDecl *RD = FD->getParent(); |
| 6630 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
| 6631 | Offset = RL.getFieldOffset(FD->getFieldIndex()); |
| 6632 | } |
| 6633 | |
| 6634 | S += llvm::utostr(Offset); |
| 6635 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6636 | if (const auto *ET = T->getAs<EnumType>()) |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 6637 | S += ObjCEncodingForEnumType(Ctx, ET); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6638 | else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6639 | const auto *BT = T->castAs<BuiltinType>(); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6640 | S += getObjCEncodingForPrimitiveKind(Ctx, BT->getKind()); |
| 6641 | } |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6642 | } |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6643 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 6644 | } |
| 6645 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6646 | // FIXME: Use SmallString for accumulating string. |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 6647 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S, |
| 6648 | bool ExpandPointedToStructures, |
| 6649 | bool ExpandStructures, |
Daniel Dunbar | c040ce4 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 6650 | const FieldDecl *FD, |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 6651 | bool OutermostType, |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6652 | bool EncodingProperty, |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6653 | bool StructField, |
| 6654 | bool EncodeBlockParameters, |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 6655 | bool EncodeClassNames, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6656 | bool EncodePointerToObjCTypedef, |
| 6657 | QualType *NotEncodedT) const { |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6658 | CanQualType CT = getCanonicalType(T); |
| 6659 | switch (CT->getTypeClass()) { |
| 6660 | case Type::Builtin: |
| 6661 | case Type::Enum: |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6662 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6663 | return EncodeBitField(this, S, T, FD); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6664 | if (const auto *BT = dyn_cast<BuiltinType>(CT)) |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6665 | S += getObjCEncodingForPrimitiveKind(this, BT->getKind()); |
| 6666 | else |
| 6667 | S += ObjCEncodingForEnumType(this, cast<EnumType>(CT)); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6668 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6669 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6670 | case Type::Complex: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6671 | const auto *CT = T->castAs<ComplexType>(); |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 6672 | S += 'j'; |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6673 | getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, nullptr); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6674 | return; |
| 6675 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6676 | |
| 6677 | case Type::Atomic: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6678 | const auto *AT = T->castAs<AtomicType>(); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6679 | S += 'A'; |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6680 | getObjCEncodingForTypeImpl(AT->getValueType(), S, false, false, nullptr); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6681 | return; |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 6682 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6683 | |
| 6684 | // encoding for pointer or reference types. |
| 6685 | case Type::Pointer: |
| 6686 | case Type::LValueReference: |
| 6687 | case Type::RValueReference: { |
| 6688 | QualType PointeeTy; |
| 6689 | if (isa<PointerType>(CT)) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6690 | const auto *PT = T->castAs<PointerType>(); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6691 | if (PT->isObjCSelType()) { |
| 6692 | S += ':'; |
| 6693 | return; |
| 6694 | } |
| 6695 | PointeeTy = PT->getPointeeType(); |
| 6696 | } else { |
| 6697 | PointeeTy = T->castAs<ReferenceType>()->getPointeeType(); |
| 6698 | } |
| 6699 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6700 | bool isReadOnly = false; |
| 6701 | // For historical/compatibility reasons, the read-only qualifier of the |
| 6702 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 6703 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6704 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 6705 | if (isa<TypedefType>(T.getTypePtr())) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6706 | if (OutermostType && T.isConstQualified()) { |
| 6707 | isReadOnly = true; |
| 6708 | S += 'r'; |
| 6709 | } |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 6710 | } else if (OutermostType) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6711 | QualType P = PointeeTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6712 | while (P->getAs<PointerType>()) |
| 6713 | P = P->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6714 | if (P.isConstQualified()) { |
| 6715 | isReadOnly = true; |
| 6716 | S += 'r'; |
| 6717 | } |
| 6718 | } |
| 6719 | if (isReadOnly) { |
| 6720 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 6721 | // combinations need to be rearranged. |
| 6722 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6723 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 6724 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6725 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6726 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6727 | if (PointeeTy->isCharType()) { |
| 6728 | // char pointer types should be encoded as '*' unless it is a |
| 6729 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 6730 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6731 | S += '*'; |
| 6732 | return; |
| 6733 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6734 | } else if (const auto *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 3de6b70 | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 6735 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 6736 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 6737 | S += '#'; |
| 6738 | return; |
| 6739 | } |
| 6740 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 6741 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 6742 | S += '@'; |
| 6743 | return; |
| 6744 | } |
| 6745 | // fall through... |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6746 | } |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6747 | S += '^'; |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6748 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 6749 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6750 | getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6751 | nullptr, false, false, false, false, false, false, |
| 6752 | NotEncodedT); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6753 | return; |
| 6754 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6755 | |
| 6756 | case Type::ConstantArray: |
| 6757 | case Type::IncompleteArray: |
| 6758 | case Type::VariableArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6759 | const auto *AT = cast<ArrayType>(CT); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6760 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6761 | if (isa<IncompleteArrayType>(AT) && !StructField) { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6762 | // Incomplete arrays are encoded as a pointer to the array element. |
| 6763 | S += '^'; |
| 6764 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6765 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6766 | false, ExpandStructures, FD); |
| 6767 | } else { |
| 6768 | S += '['; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6769 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6770 | if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) |
Fariborz Jahanian | f0dc11a | 2013-06-04 16:04:37 +0000 | [diff] [blame] | 6771 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 6772 | else { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6773 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6774 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 6775 | "Unknown array type!"); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6776 | S += '0'; |
| 6777 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6778 | |
| 6779 | getObjCEncodingForTypeImpl(AT->getElementType(), S, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6780 | false, ExpandStructures, FD, |
| 6781 | false, false, false, false, false, false, |
| 6782 | NotEncodedT); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6783 | S += ']'; |
| 6784 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6785 | return; |
| 6786 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6787 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6788 | case Type::FunctionNoProto: |
| 6789 | case Type::FunctionProto: |
Anders Carlsson | df4cc61 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 6790 | S += '?'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6791 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6792 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6793 | case Type::Record: { |
| 6794 | RecordDecl *RDecl = cast<RecordType>(CT)->getDecl(); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 6795 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 6796 | // Anonymous structures print as '?' |
| 6797 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 6798 | S += II->getName(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6799 | if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 6800 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 6801 | llvm::raw_string_ostream OS(S); |
Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 6802 | printTemplateArgumentList(OS, TemplateArgs.asArray(), |
| 6803 | getPrintingPolicy()); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 6804 | } |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 6805 | } else { |
| 6806 | S += '?'; |
| 6807 | } |
Daniel Dunbar | fc1066d | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 6808 | if (ExpandStructures) { |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 6809 | S += '='; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6810 | if (!RDecl->isUnion()) { |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6811 | getObjCEncodingForStructureImpl(RDecl, S, FD, true, NotEncodedT); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6812 | } else { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6813 | for (const auto *Field : RDecl->fields()) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6814 | if (FD) { |
| 6815 | S += '"'; |
| 6816 | S += Field->getNameAsString(); |
| 6817 | S += '"'; |
| 6818 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6819 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6820 | // Special case bit-fields. |
| 6821 | if (Field->isBitField()) { |
| 6822 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6823 | Field); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6824 | } else { |
| 6825 | QualType qt = Field->getType(); |
| 6826 | getLegacyIntegralTypeEncoding(qt); |
| 6827 | getObjCEncodingForTypeImpl(qt, S, false, true, |
| 6828 | FD, /*OutermostType*/false, |
| 6829 | /*EncodingProperty*/false, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6830 | /*StructField*/true, |
| 6831 | false, false, false, NotEncodedT); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6832 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 6833 | } |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 6834 | } |
Fariborz Jahanian | bc92fd7 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 6835 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 6836 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6837 | return; |
| 6838 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6839 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6840 | case Type::BlockPointer: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6841 | const auto *BT = T->castAs<BlockPointerType>(); |
Steve Naroff | 49140cb | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 6842 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6843 | if (EncodeBlockParameters) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6844 | const auto *FT = BT->getPointeeType()->castAs<FunctionType>(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6845 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6846 | S += '<'; |
| 6847 | // Block return type |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6848 | getObjCEncodingForTypeImpl( |
| 6849 | FT->getReturnType(), S, ExpandPointedToStructures, ExpandStructures, |
| 6850 | FD, false /* OutermostType */, EncodingProperty, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6851 | false /* StructField */, EncodeBlockParameters, EncodeClassNames, false, |
| 6852 | NotEncodedT); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6853 | // Block self |
| 6854 | S += "@?"; |
| 6855 | // Block parameters |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6856 | if (const auto *FPT = dyn_cast<FunctionProtoType>(FT)) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 6857 | for (const auto &I : FPT->param_types()) |
| 6858 | getObjCEncodingForTypeImpl( |
| 6859 | I, S, ExpandPointedToStructures, ExpandStructures, FD, |
| 6860 | false /* OutermostType */, EncodingProperty, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6861 | false /* StructField */, EncodeBlockParameters, EncodeClassNames, |
| 6862 | false, NotEncodedT); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6863 | } |
| 6864 | S += '>'; |
| 6865 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6866 | return; |
| 6867 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6868 | |
Fariborz Jahanian | 33fa962 | 2014-01-28 20:41:15 +0000 | [diff] [blame] | 6869 | case Type::ObjCObject: { |
| 6870 | // hack to match legacy encoding of *id and *Class |
| 6871 | QualType Ty = getObjCObjectPointerType(CT); |
| 6872 | if (Ty->isObjCIdType()) { |
| 6873 | S += "{objc_object=}"; |
| 6874 | return; |
| 6875 | } |
| 6876 | else if (Ty->isObjCClassType()) { |
| 6877 | S += "{objc_class=}"; |
| 6878 | return; |
| 6879 | } |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 6880 | // TODO: Double check to make sure this intentionally falls through. |
Galina Kistanova | f87496d | 2017-06-03 06:31:42 +0000 | [diff] [blame] | 6881 | LLVM_FALLTHROUGH; |
Fariborz Jahanian | 33fa962 | 2014-01-28 20:41:15 +0000 | [diff] [blame] | 6882 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6883 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6884 | case Type::ObjCInterface: { |
| 6885 | // Ignore protocol qualifiers when mangling at this level. |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 6886 | // @encode(class_name) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 6887 | ObjCInterfaceDecl *OI = T->castAs<ObjCObjectType>()->getInterface(); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 6888 | S += '{'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6889 | S += OI->getObjCRuntimeNameAsString(); |
Akira Hatanaka | fd0fb20 | 2016-08-17 19:42:22 +0000 | [diff] [blame] | 6890 | if (ExpandStructures) { |
| 6891 | S += '='; |
| 6892 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
| 6893 | DeepCollectObjCIvars(OI, true, Ivars); |
| 6894 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6895 | const FieldDecl *Field = Ivars[i]; |
Akira Hatanaka | fd0fb20 | 2016-08-17 19:42:22 +0000 | [diff] [blame] | 6896 | if (Field->isBitField()) |
| 6897 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field); |
| 6898 | else |
| 6899 | getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD, |
| 6900 | false, false, false, false, false, |
| 6901 | EncodePointerToObjCTypedef, |
| 6902 | NotEncodedT); |
| 6903 | } |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 6904 | } |
| 6905 | S += '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6906 | return; |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 6907 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6908 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6909 | case Type::ObjCObjectPointer: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6910 | const auto *OPT = T->castAs<ObjCObjectPointerType>(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6911 | if (OPT->isObjCIdType()) { |
| 6912 | S += '@'; |
| 6913 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6914 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6915 | |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 6916 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 6917 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
| 6918 | // Since this is a binary compatibility issue, need to consult with runtime |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 6919 | // folks. Fortunately, this is a *very* obscure construct. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6920 | S += '#'; |
| 6921 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6922 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6923 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6924 | if (OPT->isObjCQualifiedIdType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6925 | getObjCEncodingForTypeImpl(getObjCIdType(), S, |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6926 | ExpandPointedToStructures, |
| 6927 | ExpandStructures, FD); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6928 | if (FD || EncodingProperty || EncodeClassNames) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6929 | // Note that we do extended encoding of protocol qualifer list |
| 6930 | // Only when doing ivar or property encoding. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6931 | S += '"'; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6932 | for (const auto *I : OPT->quals()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6933 | S += '<'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6934 | S += I->getObjCRuntimeNameAsString(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6935 | S += '>'; |
| 6936 | } |
| 6937 | S += '"'; |
| 6938 | } |
| 6939 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6940 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6941 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6942 | QualType PointeeTy = OPT->getPointeeType(); |
| 6943 | if (!EncodingProperty && |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 6944 | isa<TypedefType>(PointeeTy.getTypePtr()) && |
| 6945 | !EncodePointerToObjCTypedef) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6946 | // Another historical/compatibility reason. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6947 | // We encode the underlying type which comes out as |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6948 | // {...}; |
| 6949 | S += '^'; |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 6950 | if (FD && OPT->getInterfaceDecl()) { |
Fariborz Jahanian | c682ef5 | 2013-07-12 16:41:56 +0000 | [diff] [blame] | 6951 | // Prevent recursive encoding of fields in some rare cases. |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 6952 | ObjCInterfaceDecl *OI = OPT->getInterfaceDecl(); |
| 6953 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
| 6954 | DeepCollectObjCIvars(OI, true, Ivars); |
| 6955 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 6956 | if (Ivars[i] == FD) { |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 6957 | S += '{'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6958 | S += OI->getObjCRuntimeNameAsString(); |
Fariborz Jahanian | 88890e7 | 2013-07-12 16:19:11 +0000 | [diff] [blame] | 6959 | S += '}'; |
| 6960 | return; |
| 6961 | } |
| 6962 | } |
| 6963 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6964 | getObjCEncodingForTypeImpl(PointeeTy, S, |
| 6965 | false, ExpandPointedToStructures, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6966 | nullptr, |
Fariborz Jahanian | d4c1a20 | 2013-02-15 21:14:50 +0000 | [diff] [blame] | 6967 | false, false, false, false, false, |
| 6968 | /*EncodePointerToObjCTypedef*/true); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6969 | return; |
| 6970 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6971 | |
| 6972 | S += '@'; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6973 | if (OPT->getInterfaceDecl() && |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6974 | (FD || EncodingProperty || EncodeClassNames)) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6975 | S += '"'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6976 | S += OPT->getInterfaceDecl()->getObjCRuntimeNameAsString(); |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 6977 | for (const auto *I : OPT->quals()) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6978 | S += '<'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 6979 | S += I->getObjCRuntimeNameAsString(); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6980 | S += '>'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6981 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6982 | S += '"'; |
| 6983 | } |
| 6984 | return; |
| 6985 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6986 | |
John McCall | a9e6e8d | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 6987 | // gcc just blithely ignores member pointers. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6988 | // FIXME: we shoul do better than that. 'M' is available. |
| 6989 | case Type::MemberPointer: |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6990 | // This matches gcc's encoding, even though technically it is insufficient. |
| 6991 | //FIXME. We should do a better job than gcc. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6992 | case Type::Vector: |
| 6993 | case Type::ExtVector: |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6994 | // Until we have a coherent encoding of these three types, issue warning. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 6995 | if (NotEncodedT) |
| 6996 | *NotEncodedT = T; |
| 6997 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6998 | |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6999 | // We could see an undeduced auto type here during error recovery. |
| 7000 | // Just ignore it. |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 7001 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 7002 | case Type::DeducedTemplateSpecialization: |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 7003 | return; |
| 7004 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 7005 | case Type::Pipe: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 7006 | #define ABSTRACT_TYPE(KIND, BASE) |
| 7007 | #define TYPE(KIND, BASE) |
| 7008 | #define DEPENDENT_TYPE(KIND, BASE) \ |
| 7009 | case Type::KIND: |
| 7010 | #define NON_CANONICAL_TYPE(KIND, BASE) \ |
| 7011 | case Type::KIND: |
| 7012 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \ |
| 7013 | case Type::KIND: |
| 7014 | #include "clang/AST/TypeNodes.def" |
| 7015 | llvm_unreachable("@encode for dependent type!"); |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 7016 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 7017 | llvm_unreachable("bad type kind!"); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 7018 | } |
| 7019 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7020 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 7021 | std::string &S, |
| 7022 | const FieldDecl *FD, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 7023 | bool includeVBases, |
| 7024 | QualType *NotEncodedT) const { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7025 | assert(RDecl && "Expected non-null RecordDecl"); |
| 7026 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 7027 | if (!RDecl->getDefinition() || RDecl->getDefinition()->isInvalidDecl()) |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7028 | return; |
| 7029 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7030 | const auto *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7031 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 7032 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 7033 | |
| 7034 | if (CXXRec) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 7035 | for (const auto &BI : CXXRec->bases()) { |
| 7036 | if (!BI.isVirtual()) { |
| 7037 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 7038 | if (base->isEmpty()) |
| 7039 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 7040 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7041 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 7042 | std::make_pair(offs, base)); |
| 7043 | } |
| 7044 | } |
| 7045 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7046 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7047 | unsigned i = 0; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7048 | for (auto *Field : RDecl->fields()) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7049 | uint64_t offs = layout.getFieldOffset(i); |
| 7050 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7051 | std::make_pair(offs, Field)); |
| 7052 | ++i; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7053 | } |
| 7054 | |
| 7055 | if (CXXRec && includeVBases) { |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 7056 | for (const auto &BI : CXXRec->vbases()) { |
| 7057 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 7058 | if (base->isEmpty()) |
| 7059 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 7060 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); |
Eli Friedman | 1d24af8 | 2013-09-18 01:59:16 +0000 | [diff] [blame] | 7061 | if (offs >= uint64_t(toBits(layout.getNonVirtualSize())) && |
| 7062 | FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
Argyrios Kyrtzidis | 81c0b5c | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 7063 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 7064 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7065 | } |
| 7066 | } |
| 7067 | |
| 7068 | CharUnits size; |
| 7069 | if (CXXRec) { |
| 7070 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 7071 | } else { |
| 7072 | size = layout.getSize(); |
| 7073 | } |
| 7074 | |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7075 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7076 | uint64_t CurOffs = 0; |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7077 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7078 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 7079 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 7080 | |
Douglas Gregor | f5d6c74 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 7081 | if (CXXRec && CXXRec->isDynamicClass() && |
| 7082 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7083 | if (FD) { |
| 7084 | S += "\"_vptr$"; |
| 7085 | std::string recname = CXXRec->getNameAsString(); |
| 7086 | if (recname.empty()) recname = "?"; |
| 7087 | S += recname; |
| 7088 | S += '"'; |
| 7089 | } |
| 7090 | S += "^^?"; |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7091 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7092 | CurOffs += getTypeSize(VoidPtrTy); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7093 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7094 | } |
| 7095 | |
| 7096 | if (!RDecl->hasFlexibleArrayMember()) { |
| 7097 | // Mark the end of the structure. |
| 7098 | uint64_t offs = toBits(size); |
| 7099 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7100 | std::make_pair(offs, nullptr)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7101 | } |
| 7102 | |
| 7103 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7104 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7105 | assert(CurOffs <= CurLayObj->first); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7106 | if (CurOffs < CurLayObj->first) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7107 | uint64_t padding = CurLayObj->first - CurOffs; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7108 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 7109 | // packing/alignment of members is different that normal, in which case |
| 7110 | // the encoding will be out-of-sync with the real layout. |
| 7111 | // If the runtime switches to just consider the size of types without |
| 7112 | // taking into account alignment, we could make padding explicit in the |
| 7113 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 7114 | // longer then though. |
| 7115 | CurOffs += padding; |
| 7116 | } |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7117 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7118 | |
| 7119 | NamedDecl *dcl = CurLayObj->second; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7120 | if (!dcl) |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7121 | break; // reached end of structure. |
| 7122 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7123 | if (auto *base = dyn_cast<CXXRecordDecl>(dcl)) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7124 | // We expand the bases without their virtual bases since those are going |
| 7125 | // in the initial structure. Note that this differs from gcc which |
| 7126 | // expands virtual bases each time one is encountered in the hierarchy, |
| 7127 | // making the encoding type bigger than it really is. |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 7128 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false, |
| 7129 | NotEncodedT); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 7130 | assert(!base->isEmpty()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7131 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 7132 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7133 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7134 | } else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7135 | const auto *field = cast<FieldDecl>(dcl); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7136 | if (FD) { |
| 7137 | S += '"'; |
| 7138 | S += field->getNameAsString(); |
| 7139 | S += '"'; |
| 7140 | } |
| 7141 | |
| 7142 | if (field->isBitField()) { |
| 7143 | EncodeBitField(this, S, field->getType(), field); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7144 | #ifndef NDEBUG |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 7145 | CurOffs += field->getBitWidthValue(*this); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7146 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7147 | } else { |
| 7148 | QualType qt = field->getType(); |
| 7149 | getLegacyIntegralTypeEncoding(qt); |
| 7150 | getObjCEncodingForTypeImpl(qt, S, false, true, FD, |
| 7151 | /*OutermostType*/false, |
| 7152 | /*EncodingProperty*/false, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 7153 | /*StructField*/true, |
| 7154 | false, false, false, NotEncodedT); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7155 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7156 | CurOffs += getTypeSize(field->getType()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7157 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7158 | } |
| 7159 | } |
| 7160 | } |
| 7161 | } |
| 7162 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7163 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 7164 | std::string& S) const { |
| 7165 | if (QT & Decl::OBJC_TQ_In) |
| 7166 | S += 'n'; |
| 7167 | if (QT & Decl::OBJC_TQ_Inout) |
| 7168 | S += 'N'; |
| 7169 | if (QT & Decl::OBJC_TQ_Out) |
| 7170 | S += 'o'; |
| 7171 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 7172 | S += 'O'; |
| 7173 | if (QT & Decl::OBJC_TQ_Byref) |
| 7174 | S += 'R'; |
| 7175 | if (QT & Decl::OBJC_TQ_Oneway) |
| 7176 | S += 'V'; |
| 7177 | } |
| 7178 | |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 7179 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 7180 | if (!ObjCIdDecl) { |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 7181 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, {}, {}); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 7182 | T = getObjCObjectPointerType(T); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7183 | ObjCIdDecl = buildImplicitTypedef(T, "id"); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 7184 | } |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 7185 | return ObjCIdDecl; |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 7186 | } |
| 7187 | |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 7188 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 7189 | if (!ObjCSelDecl) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7190 | QualType T = getPointerType(ObjCBuiltinSelTy); |
| 7191 | ObjCSelDecl = buildImplicitTypedef(T, "SEL"); |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 7192 | } |
| 7193 | return ObjCSelDecl; |
Fariborz Jahanian | 4bef462 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 7194 | } |
| 7195 | |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 7196 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 7197 | if (!ObjCClassDecl) { |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 7198 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, {}, {}); |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 7199 | T = getObjCObjectPointerType(T); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7200 | ObjCClassDecl = buildImplicitTypedef(T, "Class"); |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 7201 | } |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 7202 | return ObjCClassDecl; |
Anders Carlsson | f56a7ae | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 7203 | } |
| 7204 | |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 7205 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 7206 | if (!ObjCProtocolClassDecl) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7207 | ObjCProtocolClassDecl |
| 7208 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 7209 | SourceLocation(), |
| 7210 | &Idents.get("Protocol"), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 7211 | /*typeParamList=*/nullptr, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7212 | /*PrevDecl=*/nullptr, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7213 | SourceLocation(), true); |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 7214 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7215 | |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 7216 | return ObjCProtocolClassDecl; |
| 7217 | } |
| 7218 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7219 | //===----------------------------------------------------------------------===// |
| 7220 | // __builtin_va_list Construction Functions |
| 7221 | //===----------------------------------------------------------------------===// |
| 7222 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 7223 | static TypedefDecl *CreateCharPtrNamedVaListDecl(const ASTContext *Context, |
| 7224 | StringRef Name) { |
| 7225 | // typedef char* __builtin[_ms]_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7226 | QualType T = Context->getPointerType(Context->CharTy); |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 7227 | return Context->buildImplicitTypedef(T, Name); |
| 7228 | } |
| 7229 | |
| 7230 | static TypedefDecl *CreateMSVaListDecl(const ASTContext *Context) { |
| 7231 | return CreateCharPtrNamedVaListDecl(Context, "__builtin_ms_va_list"); |
| 7232 | } |
| 7233 | |
| 7234 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 7235 | return CreateCharPtrNamedVaListDecl(Context, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7236 | } |
| 7237 | |
| 7238 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 7239 | // typedef void* __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7240 | QualType T = Context->getPointerType(Context->VoidTy); |
| 7241 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7242 | } |
| 7243 | |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7244 | static TypedefDecl * |
| 7245 | CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7246 | // struct __va_list |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7247 | RecordDecl *VaListTagDecl = Context->buildImplicitRecord("__va_list"); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7248 | if (Context->getLangOpts().CPlusPlus) { |
| 7249 | // namespace std { struct __va_list { |
| 7250 | NamespaceDecl *NS; |
| 7251 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 7252 | Context->getTranslationUnitDecl(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7253 | /*Inline*/ false, SourceLocation(), |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7254 | SourceLocation(), &Context->Idents.get("std"), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7255 | /*PrevDecl*/ nullptr); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7256 | NS->setImplicit(); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7257 | VaListTagDecl->setDeclContext(NS); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7258 | } |
| 7259 | |
| 7260 | VaListTagDecl->startDefinition(); |
| 7261 | |
| 7262 | const size_t NumFields = 5; |
| 7263 | QualType FieldTypes[NumFields]; |
| 7264 | const char *FieldNames[NumFields]; |
| 7265 | |
| 7266 | // void *__stack; |
| 7267 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); |
| 7268 | FieldNames[0] = "__stack"; |
| 7269 | |
| 7270 | // void *__gr_top; |
| 7271 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); |
| 7272 | FieldNames[1] = "__gr_top"; |
| 7273 | |
| 7274 | // void *__vr_top; |
| 7275 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 7276 | FieldNames[2] = "__vr_top"; |
| 7277 | |
| 7278 | // int __gr_offs; |
| 7279 | FieldTypes[3] = Context->IntTy; |
| 7280 | FieldNames[3] = "__gr_offs"; |
| 7281 | |
| 7282 | // int __vr_offs; |
| 7283 | FieldTypes[4] = Context->IntTy; |
| 7284 | FieldNames[4] = "__vr_offs"; |
| 7285 | |
| 7286 | // Create fields |
| 7287 | for (unsigned i = 0; i < NumFields; ++i) { |
| 7288 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 7289 | VaListTagDecl, |
| 7290 | SourceLocation(), |
| 7291 | SourceLocation(), |
| 7292 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7293 | FieldTypes[i], /*TInfo=*/nullptr, |
| 7294 | /*BitWidth=*/nullptr, |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7295 | /*Mutable=*/false, |
| 7296 | ICIS_NoInit); |
| 7297 | Field->setAccess(AS_public); |
| 7298 | VaListTagDecl->addDecl(Field); |
| 7299 | } |
| 7300 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7301 | Context->VaListTagDecl = VaListTagDecl; |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7302 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7303 | |
| 7304 | // } __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7305 | return Context->buildImplicitTypedef(VaListTagType, "__builtin_va_list"); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7306 | } |
| 7307 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7308 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
| 7309 | // typedef struct __va_list_tag { |
| 7310 | RecordDecl *VaListTagDecl; |
| 7311 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7312 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7313 | VaListTagDecl->startDefinition(); |
| 7314 | |
| 7315 | const size_t NumFields = 5; |
| 7316 | QualType FieldTypes[NumFields]; |
| 7317 | const char *FieldNames[NumFields]; |
| 7318 | |
| 7319 | // unsigned char gpr; |
| 7320 | FieldTypes[0] = Context->UnsignedCharTy; |
| 7321 | FieldNames[0] = "gpr"; |
| 7322 | |
| 7323 | // unsigned char fpr; |
| 7324 | FieldTypes[1] = Context->UnsignedCharTy; |
| 7325 | FieldNames[1] = "fpr"; |
| 7326 | |
| 7327 | // unsigned short reserved; |
| 7328 | FieldTypes[2] = Context->UnsignedShortTy; |
| 7329 | FieldNames[2] = "reserved"; |
| 7330 | |
| 7331 | // void* overflow_arg_area; |
| 7332 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 7333 | FieldNames[3] = "overflow_arg_area"; |
| 7334 | |
| 7335 | // void* reg_save_area; |
| 7336 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
| 7337 | FieldNames[4] = "reg_save_area"; |
| 7338 | |
| 7339 | // Create fields |
| 7340 | for (unsigned i = 0; i < NumFields; ++i) { |
| 7341 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
| 7342 | SourceLocation(), |
| 7343 | SourceLocation(), |
| 7344 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7345 | FieldTypes[i], /*TInfo=*/nullptr, |
| 7346 | /*BitWidth=*/nullptr, |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7347 | /*Mutable=*/false, |
| 7348 | ICIS_NoInit); |
| 7349 | Field->setAccess(AS_public); |
| 7350 | VaListTagDecl->addDecl(Field); |
| 7351 | } |
| 7352 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7353 | Context->VaListTagDecl = VaListTagDecl; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7354 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 7355 | |
| 7356 | // } __va_list_tag; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7357 | TypedefDecl *VaListTagTypedefDecl = |
| 7358 | Context->buildImplicitTypedef(VaListTagType, "__va_list_tag"); |
| 7359 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7360 | QualType VaListTagTypedefType = |
| 7361 | Context->getTypedefType(VaListTagTypedefDecl); |
| 7362 | |
| 7363 | // typedef __va_list_tag __builtin_va_list[1]; |
| 7364 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 7365 | QualType VaListTagArrayType |
| 7366 | = Context->getConstantArrayType(VaListTagTypedefType, |
| 7367 | Size, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7368 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7369 | } |
| 7370 | |
| 7371 | static TypedefDecl * |
| 7372 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7373 | // struct __va_list_tag { |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7374 | RecordDecl *VaListTagDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7375 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7376 | VaListTagDecl->startDefinition(); |
| 7377 | |
| 7378 | const size_t NumFields = 4; |
| 7379 | QualType FieldTypes[NumFields]; |
| 7380 | const char *FieldNames[NumFields]; |
| 7381 | |
| 7382 | // unsigned gp_offset; |
| 7383 | FieldTypes[0] = Context->UnsignedIntTy; |
| 7384 | FieldNames[0] = "gp_offset"; |
| 7385 | |
| 7386 | // unsigned fp_offset; |
| 7387 | FieldTypes[1] = Context->UnsignedIntTy; |
| 7388 | FieldNames[1] = "fp_offset"; |
| 7389 | |
| 7390 | // void* overflow_arg_area; |
| 7391 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 7392 | FieldNames[2] = "overflow_arg_area"; |
| 7393 | |
| 7394 | // void* reg_save_area; |
| 7395 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 7396 | FieldNames[3] = "reg_save_area"; |
| 7397 | |
| 7398 | // Create fields |
| 7399 | for (unsigned i = 0; i < NumFields; ++i) { |
| 7400 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 7401 | VaListTagDecl, |
| 7402 | SourceLocation(), |
| 7403 | SourceLocation(), |
| 7404 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7405 | FieldTypes[i], /*TInfo=*/nullptr, |
| 7406 | /*BitWidth=*/nullptr, |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7407 | /*Mutable=*/false, |
| 7408 | ICIS_NoInit); |
| 7409 | Field->setAccess(AS_public); |
| 7410 | VaListTagDecl->addDecl(Field); |
| 7411 | } |
| 7412 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7413 | Context->VaListTagDecl = VaListTagDecl; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7414 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 7415 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7416 | // }; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7417 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7418 | // typedef struct __va_list_tag __builtin_va_list[1]; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7419 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7420 | QualType VaListTagArrayType = |
| 7421 | Context->getConstantArrayType(VaListTagType, Size, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7422 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7423 | } |
| 7424 | |
| 7425 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
| 7426 | // typedef int __builtin_va_list[4]; |
| 7427 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); |
Yaron Keren | e0bcdd4 | 2016-10-08 06:45:10 +0000 | [diff] [blame] | 7428 | QualType IntArrayType = |
| 7429 | Context->getConstantArrayType(Context->IntTy, Size, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7430 | return Context->buildImplicitTypedef(IntArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7431 | } |
| 7432 | |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7433 | static TypedefDecl * |
| 7434 | CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7435 | // struct __va_list |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7436 | RecordDecl *VaListDecl = Context->buildImplicitRecord("__va_list"); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7437 | if (Context->getLangOpts().CPlusPlus) { |
| 7438 | // namespace std { struct __va_list { |
| 7439 | NamespaceDecl *NS; |
| 7440 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 7441 | Context->getTranslationUnitDecl(), |
| 7442 | /*Inline*/false, SourceLocation(), |
| 7443 | SourceLocation(), &Context->Idents.get("std"), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7444 | /*PrevDecl*/ nullptr); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7445 | NS->setImplicit(); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7446 | VaListDecl->setDeclContext(NS); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7447 | } |
| 7448 | |
| 7449 | VaListDecl->startDefinition(); |
| 7450 | |
| 7451 | // void * __ap; |
| 7452 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 7453 | VaListDecl, |
| 7454 | SourceLocation(), |
| 7455 | SourceLocation(), |
| 7456 | &Context->Idents.get("__ap"), |
| 7457 | Context->getPointerType(Context->VoidTy), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7458 | /*TInfo=*/nullptr, |
| 7459 | /*BitWidth=*/nullptr, |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7460 | /*Mutable=*/false, |
| 7461 | ICIS_NoInit); |
| 7462 | Field->setAccess(AS_public); |
| 7463 | VaListDecl->addDecl(Field); |
| 7464 | |
| 7465 | // }; |
| 7466 | VaListDecl->completeDefinition(); |
Oleg Ranevskyy | b88d247 | 2016-03-30 21:30:30 +0000 | [diff] [blame] | 7467 | Context->VaListTagDecl = VaListDecl; |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7468 | |
| 7469 | // typedef struct __va_list __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7470 | QualType T = Context->getRecordType(VaListDecl); |
| 7471 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7472 | } |
| 7473 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7474 | static TypedefDecl * |
| 7475 | CreateSystemZBuiltinVaListDecl(const ASTContext *Context) { |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7476 | // struct __va_list_tag { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7477 | RecordDecl *VaListTagDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7478 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7479 | VaListTagDecl->startDefinition(); |
| 7480 | |
| 7481 | const size_t NumFields = 4; |
| 7482 | QualType FieldTypes[NumFields]; |
| 7483 | const char *FieldNames[NumFields]; |
| 7484 | |
| 7485 | // long __gpr; |
| 7486 | FieldTypes[0] = Context->LongTy; |
| 7487 | FieldNames[0] = "__gpr"; |
| 7488 | |
| 7489 | // long __fpr; |
| 7490 | FieldTypes[1] = Context->LongTy; |
| 7491 | FieldNames[1] = "__fpr"; |
| 7492 | |
| 7493 | // void *__overflow_arg_area; |
| 7494 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 7495 | FieldNames[2] = "__overflow_arg_area"; |
| 7496 | |
| 7497 | // void *__reg_save_area; |
| 7498 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 7499 | FieldNames[3] = "__reg_save_area"; |
| 7500 | |
| 7501 | // Create fields |
| 7502 | for (unsigned i = 0; i < NumFields; ++i) { |
| 7503 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 7504 | VaListTagDecl, |
| 7505 | SourceLocation(), |
| 7506 | SourceLocation(), |
| 7507 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7508 | FieldTypes[i], /*TInfo=*/nullptr, |
| 7509 | /*BitWidth=*/nullptr, |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7510 | /*Mutable=*/false, |
| 7511 | ICIS_NoInit); |
| 7512 | Field->setAccess(AS_public); |
| 7513 | VaListTagDecl->addDecl(Field); |
| 7514 | } |
| 7515 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7516 | Context->VaListTagDecl = VaListTagDecl; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7517 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7518 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7519 | // }; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7520 | |
| 7521 | // typedef __va_list_tag __builtin_va_list[1]; |
| 7522 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7523 | QualType VaListTagArrayType = |
| 7524 | Context->getConstantArrayType(VaListTagType, Size, ArrayType::Normal, 0); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7525 | |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7526 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7527 | } |
| 7528 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7529 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
| 7530 | TargetInfo::BuiltinVaListKind Kind) { |
| 7531 | switch (Kind) { |
| 7532 | case TargetInfo::CharPtrBuiltinVaList: |
| 7533 | return CreateCharPtrBuiltinVaListDecl(Context); |
| 7534 | case TargetInfo::VoidPtrBuiltinVaList: |
| 7535 | return CreateVoidPtrBuiltinVaListDecl(Context); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7536 | case TargetInfo::AArch64ABIBuiltinVaList: |
| 7537 | return CreateAArch64ABIBuiltinVaListDecl(Context); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7538 | case TargetInfo::PowerABIBuiltinVaList: |
| 7539 | return CreatePowerABIBuiltinVaListDecl(Context); |
| 7540 | case TargetInfo::X86_64ABIBuiltinVaList: |
| 7541 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
| 7542 | case TargetInfo::PNaClABIBuiltinVaList: |
| 7543 | return CreatePNaClABIBuiltinVaListDecl(Context); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7544 | case TargetInfo::AAPCSABIBuiltinVaList: |
| 7545 | return CreateAAPCSABIBuiltinVaListDecl(Context); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7546 | case TargetInfo::SystemZBuiltinVaList: |
| 7547 | return CreateSystemZBuiltinVaListDecl(Context); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7548 | } |
| 7549 | |
| 7550 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
| 7551 | } |
| 7552 | |
| 7553 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7554 | if (!BuiltinVaListDecl) { |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7555 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7556 | assert(BuiltinVaListDecl->isImplicit()); |
| 7557 | } |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7558 | |
| 7559 | return BuiltinVaListDecl; |
| 7560 | } |
| 7561 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7562 | Decl *ASTContext::getVaListTagDecl() const { |
| 7563 | // Force the creation of VaListTagDecl by building the __builtin_va_list |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 7564 | // declaration. |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7565 | if (!VaListTagDecl) |
| 7566 | (void)getBuiltinVaListDecl(); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 7567 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7568 | return VaListTagDecl; |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 7569 | } |
| 7570 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 7571 | TypedefDecl *ASTContext::getBuiltinMSVaListDecl() const { |
| 7572 | if (!BuiltinMSVaListDecl) |
| 7573 | BuiltinMSVaListDecl = CreateMSVaListDecl(this); |
| 7574 | |
| 7575 | return BuiltinMSVaListDecl; |
| 7576 | } |
| 7577 | |
Erich Keane | 41af971 | 2018-04-16 21:30:08 +0000 | [diff] [blame] | 7578 | bool ASTContext::canBuiltinBeRedeclared(const FunctionDecl *FD) const { |
| 7579 | return BuiltinInfo.canBeRedeclared(FD->getBuiltinID()); |
| 7580 | } |
| 7581 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 7582 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7583 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 7584 | "'NSConstantString' type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7585 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 7586 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 7587 | } |
| 7588 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7589 | /// Retrieve the template name that corresponds to a non-empty |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7590 | /// lookup. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7591 | TemplateName |
| 7592 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 7593 | UnresolvedSetIterator End) const { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7594 | unsigned size = End - Begin; |
| 7595 | assert(size > 1 && "set is not overloaded!"); |
| 7596 | |
| 7597 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 7598 | size * sizeof(FunctionTemplateDecl*)); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7599 | auto *OT = new (memory) OverloadedTemplateStorage(size); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7600 | |
| 7601 | NamedDecl **Storage = OT->getStorage(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 7602 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7603 | NamedDecl *D = *I; |
| 7604 | assert(isa<FunctionTemplateDecl>(D) || |
Richard Smith | ef53a3e | 2018-06-06 16:36:56 +0000 | [diff] [blame] | 7605 | isa<UnresolvedUsingValueDecl>(D) || |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7606 | (isa<UsingShadowDecl>(D) && |
| 7607 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 7608 | *Storage++ = D; |
| 7609 | } |
| 7610 | |
| 7611 | return TemplateName(OT); |
| 7612 | } |
| 7613 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7614 | /// Retrieve the template name that represents a qualified |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7615 | /// template name such as \c std::vector. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7616 | TemplateName |
| 7617 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 7618 | bool TemplateKeyword, |
| 7619 | TemplateDecl *Template) const { |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 7620 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7621 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7622 | // FIXME: Canonicalization? |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7623 | llvm::FoldingSetNodeID ID; |
| 7624 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 7625 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7626 | void *InsertPos = nullptr; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7627 | QualifiedTemplateName *QTN = |
| 7628 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7629 | if (!QTN) { |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7630 | QTN = new (*this, alignof(QualifiedTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7631 | QualifiedTemplateName(NNS, TemplateKeyword, Template); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7632 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 7633 | } |
| 7634 | |
| 7635 | return TemplateName(QTN); |
| 7636 | } |
| 7637 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7638 | /// Retrieve the template name that represents a dependent |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7639 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7640 | TemplateName |
| 7641 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 7642 | const IdentifierInfo *Name) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7643 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 7644 | "Nested name specifier must be dependent"); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7645 | |
| 7646 | llvm::FoldingSetNodeID ID; |
| 7647 | DependentTemplateName::Profile(ID, NNS, Name); |
| 7648 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7649 | void *InsertPos = nullptr; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7650 | DependentTemplateName *QTN = |
| 7651 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7652 | |
| 7653 | if (QTN) |
| 7654 | return TemplateName(QTN); |
| 7655 | |
| 7656 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 7657 | if (CanonNNS == NNS) { |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7658 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7659 | DependentTemplateName(NNS, Name); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7660 | } else { |
| 7661 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7662 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7663 | DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7664 | DependentTemplateName *CheckQTN = |
| 7665 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7666 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 7667 | (void)CheckQTN; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7668 | } |
| 7669 | |
| 7670 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 7671 | return TemplateName(QTN); |
| 7672 | } |
| 7673 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7674 | /// Retrieve the template name that represents a dependent |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7675 | /// template name such as \c MetaFun::template operator+. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7676 | TemplateName |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7677 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7678 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7679 | assert((!NNS || NNS->isDependent()) && |
| 7680 | "Nested name specifier must be dependent"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7681 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7682 | llvm::FoldingSetNodeID ID; |
| 7683 | DependentTemplateName::Profile(ID, NNS, Operator); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7684 | |
| 7685 | void *InsertPos = nullptr; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7686 | DependentTemplateName *QTN |
| 7687 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7688 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7689 | if (QTN) |
| 7690 | return TemplateName(QTN); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7691 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7692 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 7693 | if (CanonNNS == NNS) { |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7694 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7695 | DependentTemplateName(NNS, Operator); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7696 | } else { |
| 7697 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7698 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7699 | DependentTemplateName(NNS, Operator, Canon); |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7700 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7701 | DependentTemplateName *CheckQTN |
| 7702 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7703 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 7704 | (void)CheckQTN; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7705 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7706 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7707 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 7708 | return TemplateName(QTN); |
| 7709 | } |
| 7710 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7711 | TemplateName |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 7712 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 7713 | TemplateName replacement) const { |
| 7714 | llvm::FoldingSetNodeID ID; |
| 7715 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7716 | |
| 7717 | void *insertPos = nullptr; |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 7718 | SubstTemplateTemplateParmStorage *subst |
| 7719 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7720 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 7721 | if (!subst) { |
| 7722 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 7723 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 7724 | } |
| 7725 | |
| 7726 | return TemplateName(subst); |
| 7727 | } |
| 7728 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7729 | TemplateName |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7730 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 7731 | const TemplateArgument &ArgPack) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7732 | auto &Self = const_cast<ASTContext &>(*this); |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7733 | llvm::FoldingSetNodeID ID; |
| 7734 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7735 | |
| 7736 | void *InsertPos = nullptr; |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7737 | SubstTemplateTemplateParmPackStorage *Subst |
| 7738 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7739 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7740 | if (!Subst) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7741 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7742 | ArgPack.pack_size(), |
| 7743 | ArgPack.pack_begin()); |
| 7744 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 7745 | } |
| 7746 | |
| 7747 | return TemplateName(Subst); |
| 7748 | } |
| 7749 | |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7750 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | ab13857 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 7751 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 7752 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 7753 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7754 | switch (Type) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7755 | case TargetInfo::NoInt: return {}; |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 7756 | case TargetInfo::SignedChar: return SignedCharTy; |
| 7757 | case TargetInfo::UnsignedChar: return UnsignedCharTy; |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7758 | case TargetInfo::SignedShort: return ShortTy; |
| 7759 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 7760 | case TargetInfo::SignedInt: return IntTy; |
| 7761 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 7762 | case TargetInfo::SignedLong: return LongTy; |
| 7763 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 7764 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 7765 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 7766 | } |
| 7767 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7768 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7769 | } |
Ted Kremenek | 77c51b2 | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 7770 | |
| 7771 | //===----------------------------------------------------------------------===// |
| 7772 | // Type Predicates. |
| 7773 | //===----------------------------------------------------------------------===// |
| 7774 | |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 7775 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 7776 | /// garbage collection attribute. |
| 7777 | /// |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7778 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7779 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7780 | return Qualifiers::GCNone; |
| 7781 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 7782 | assert(getLangOpts().ObjC); |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7783 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 7784 | |
| 7785 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 7786 | // (or pointers to them) be treated as though they were declared |
| 7787 | // as __strong. |
| 7788 | if (GCAttrs == Qualifiers::GCNone) { |
| 7789 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 7790 | return Qualifiers::Strong; |
| 7791 | else if (Ty->isPointerType()) |
| 7792 | return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType()); |
| 7793 | } else { |
| 7794 | // It's not valid to set GC attributes on anything that isn't a |
| 7795 | // pointer. |
| 7796 | #ifndef NDEBUG |
| 7797 | QualType CT = Ty->getCanonicalTypeInternal(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7798 | while (const auto *AT = dyn_cast<ArrayType>(CT)) |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7799 | CT = AT->getElementType(); |
| 7800 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 7801 | #endif |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 7802 | } |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 7803 | return GCAttrs; |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 7804 | } |
| 7805 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7806 | //===----------------------------------------------------------------------===// |
| 7807 | // Type Compatibility Testing |
| 7808 | //===----------------------------------------------------------------------===// |
Chris Lattner | b338a6b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 7809 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7810 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7811 | /// compatible. |
| 7812 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 7813 | const VectorType *RHS) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 7814 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7815 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7816 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7817 | } |
| 7818 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7819 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 7820 | QualType SecondVec) { |
| 7821 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 7822 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 7823 | |
| 7824 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 7825 | return true; |
| 7826 | |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 7827 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 7828 | // equivalent GCC vector types. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7829 | const auto *First = FirstVec->getAs<VectorType>(); |
| 7830 | const auto *Second = SecondVec->getAs<VectorType>(); |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 7831 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7832 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 7833 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 7834 | First->getVectorKind() != VectorType::AltiVecBool && |
| 7835 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 7836 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7837 | return true; |
| 7838 | |
| 7839 | return false; |
| 7840 | } |
| 7841 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7842 | //===----------------------------------------------------------------------===// |
| 7843 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 7844 | //===----------------------------------------------------------------------===// |
| 7845 | |
| 7846 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 7847 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7848 | bool |
| 7849 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 7850 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 7851 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7852 | return true; |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 7853 | for (auto *PI : rProto->protocols()) |
| 7854 | if (ProtocolCompatibleWithProtocol(lProto, PI)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7855 | return true; |
| 7856 | return false; |
| 7857 | } |
| 7858 | |
Dmitri Gribenko | 4364fcf | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 7859 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and |
| 7860 | /// Class<pr1, ...>. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7861 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs, |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 7862 | QualType rhs) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7863 | const auto *lhsQID = lhs->getAs<ObjCObjectPointerType>(); |
| 7864 | const auto *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 7865 | assert((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7866 | |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7867 | for (auto *lhsProto : lhsQID->quals()) { |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 7868 | bool match = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7869 | for (auto *rhsProto : rhsOPT->quals()) { |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 7870 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 7871 | match = true; |
| 7872 | break; |
| 7873 | } |
| 7874 | } |
| 7875 | if (!match) |
| 7876 | return false; |
| 7877 | } |
| 7878 | return true; |
| 7879 | } |
| 7880 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7881 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 7882 | /// ObjCQualifiedIDType. |
| 7883 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 7884 | bool compare) { |
| 7885 | // Allow id<P..> and an 'id' or void* type in all cases. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7886 | if (lhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7887 | lhs->isObjCIdType() || lhs->isObjCClassType()) |
| 7888 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7889 | else if (rhs->isVoidPointerType() || |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7890 | rhs->isObjCIdType() || rhs->isObjCClassType()) |
| 7891 | return true; |
| 7892 | |
| 7893 | if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7894 | const auto *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7895 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7896 | if (!rhsOPT) return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7897 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7898 | if (rhsOPT->qual_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7899 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7900 | // make sure we check the class hierarchy. |
| 7901 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7902 | for (auto *I : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7903 | // when comparing an id<P> on lhs with a static type on rhs, |
| 7904 | // see if static class implements all of id's protocols, directly or |
| 7905 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7906 | if (!rhsID->ClassImplementsProtocol(I, true)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7907 | return false; |
| 7908 | } |
| 7909 | } |
| 7910 | // If there are no qualifiers and no interface, we have an 'id'. |
| 7911 | return true; |
| 7912 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7913 | // Both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7914 | for (auto *lhsProto : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7915 | bool match = false; |
| 7916 | |
| 7917 | // when comparing an id<P> on lhs with a static type on rhs, |
| 7918 | // see if static class implements all of id's protocols, directly or |
| 7919 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7920 | for (auto *rhsProto : rhsOPT->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7921 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 7922 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 7923 | match = true; |
| 7924 | break; |
| 7925 | } |
| 7926 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7927 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7928 | // make sure we check the class hierarchy. |
| 7929 | if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) { |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7930 | for (auto *I : lhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7931 | // when comparing an id<P> on lhs with a static type on rhs, |
| 7932 | // see if static class implements all of id's protocols, directly or |
| 7933 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7934 | if (rhsID->ClassImplementsProtocol(I, true)) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7935 | match = true; |
| 7936 | break; |
| 7937 | } |
| 7938 | } |
| 7939 | } |
| 7940 | if (!match) |
| 7941 | return false; |
| 7942 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7943 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7944 | return true; |
| 7945 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7946 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7947 | const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 7948 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 7949 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7950 | if (const ObjCObjectPointerType *lhsOPT = |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7951 | lhs->getAsObjCInterfacePointerType()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7952 | // If both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7953 | for (auto *lhsProto : lhsOPT->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7954 | bool match = false; |
| 7955 | |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7956 | // 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] | 7957 | // see if static class implements all of id's protocols, directly or |
| 7958 | // through its super class and categories. |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7959 | // First, lhs protocols in the qualifier list must be found, direct |
| 7960 | // or indirect in rhs's qualifier list or it is a mismatch. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7961 | for (auto *rhsProto : rhsQID->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7962 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 7963 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 7964 | match = true; |
| 7965 | break; |
| 7966 | } |
| 7967 | } |
| 7968 | if (!match) |
| 7969 | return false; |
| 7970 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7971 | |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7972 | // Static class's protocols, or its super class or category protocols |
| 7973 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
| 7974 | if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) { |
| 7975 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 7976 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 7977 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 7978 | // no type qualifier and its class has no static protocol(s) |
| 7979 | // assume that it is mismatch. |
| 7980 | if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty()) |
| 7981 | return false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7982 | for (auto *lhsProto : LHSInheritedProtocols) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7983 | bool match = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7984 | for (auto *rhsProto : rhsQID->quals()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 7985 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 7986 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 7987 | match = true; |
| 7988 | break; |
| 7989 | } |
| 7990 | } |
| 7991 | if (!match) |
| 7992 | return false; |
| 7993 | } |
| 7994 | } |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7995 | return true; |
| 7996 | } |
| 7997 | return false; |
| 7998 | } |
| 7999 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8000 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 8001 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 8002 | /// protocol qualifiers on the LHS or RHS. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8003 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 8004 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8005 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 8006 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 8007 | |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 8008 | // 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] | 8009 | if (LHS->isObjCUnqualifiedIdOrClass() || |
| 8010 | RHS->isObjCUnqualifiedIdOrClass()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8011 | return true; |
| 8012 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8013 | // Function object that propagates a successful result or handles |
| 8014 | // __kindof types. |
| 8015 | auto finish = [&](bool succeeded) -> bool { |
| 8016 | if (succeeded) |
| 8017 | return true; |
| 8018 | |
| 8019 | if (!RHS->isKindOfType()) |
| 8020 | return false; |
| 8021 | |
| 8022 | // Strip off __kindof and protocol qualifiers, then check whether |
| 8023 | // we can assign the other way. |
| 8024 | return canAssignObjCInterfaces(RHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 8025 | LHSOPT->stripObjCKindOfTypeAndQuals(*this)); |
| 8026 | }; |
| 8027 | |
| 8028 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) { |
| 8029 | return finish(ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 8030 | QualType(RHSOPT,0), |
| 8031 | false)); |
| 8032 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8033 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8034 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) { |
| 8035 | return finish(ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0), |
| 8036 | QualType(RHSOPT,0))); |
| 8037 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8038 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8039 | // If we have 2 user-defined types, fall into that path. |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8040 | if (LHS->getInterface() && RHS->getInterface()) { |
| 8041 | return finish(canAssignObjCInterfaces(LHS, RHS)); |
| 8042 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8043 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8044 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8045 | } |
| 8046 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8047 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8048 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8049 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 8050 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 8051 | /// not OK. For the return type, the opposite is not OK. |
| 8052 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 8053 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 8054 | const ObjCObjectPointerType *RHSOPT, |
| 8055 | bool BlockReturnType) { |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8056 | |
| 8057 | // Function object that propagates a successful result or handles |
| 8058 | // __kindof types. |
| 8059 | auto finish = [&](bool succeeded) -> bool { |
| 8060 | if (succeeded) |
| 8061 | return true; |
| 8062 | |
| 8063 | const ObjCObjectPointerType *Expected = BlockReturnType ? RHSOPT : LHSOPT; |
| 8064 | if (!Expected->isKindOfType()) |
| 8065 | return false; |
| 8066 | |
| 8067 | // Strip off __kindof and protocol qualifiers, then check whether |
| 8068 | // we can assign the other way. |
| 8069 | return canAssignObjCInterfacesInBlockPointer( |
| 8070 | RHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 8071 | LHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 8072 | BlockReturnType); |
| 8073 | }; |
| 8074 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 8075 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8076 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8077 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8078 | if (LHSOPT->isObjCBuiltinType()) { |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8079 | return finish(RHSOPT->isObjCBuiltinType() || |
| 8080 | RHSOPT->isObjCQualifiedIdType()); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8081 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8082 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 8083 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8084 | return finish(ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0), |
| 8085 | QualType(RHSOPT,0), |
| 8086 | false)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8087 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8088 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 8089 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 8090 | if (LHS && RHS) { // We have 2 user-defined types. |
| 8091 | if (LHS != RHS) { |
| 8092 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8093 | return finish(BlockReturnType); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8094 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8095 | return finish(!BlockReturnType); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8096 | } |
| 8097 | else |
| 8098 | return true; |
| 8099 | } |
| 8100 | return false; |
| 8101 | } |
| 8102 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8103 | /// Comparison routine for Objective-C protocols to be used with |
| 8104 | /// llvm::array_pod_sort. |
| 8105 | static int compareObjCProtocolsByName(ObjCProtocolDecl * const *lhs, |
| 8106 | ObjCProtocolDecl * const *rhs) { |
| 8107 | return (*lhs)->getName().compare((*rhs)->getName()); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8108 | } |
| 8109 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8110 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8111 | /// of protocols inherited from two distinct objective-c pointer objects with |
| 8112 | /// the given common base. |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8113 | /// It is used to build composite qualifier list of the composite type of |
| 8114 | /// the conditional expression involving two objective-c pointer objects. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8115 | static |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8116 | void getIntersectionOfProtocols(ASTContext &Context, |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8117 | const ObjCInterfaceDecl *CommonBase, |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8118 | const ObjCObjectPointerType *LHSOPT, |
| 8119 | const ObjCObjectPointerType *RHSOPT, |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8120 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionSet) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8121 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8122 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 8123 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 8124 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 8125 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8126 | |
| 8127 | // Add all of the protocols for the LHS. |
| 8128 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSProtocolSet; |
| 8129 | |
| 8130 | // Start with the protocol qualifiers. |
| 8131 | for (auto proto : LHS->quals()) { |
| 8132 | Context.CollectInheritedProtocols(proto, LHSProtocolSet); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8133 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8134 | |
| 8135 | // Also add the protocols associated with the LHS interface. |
| 8136 | Context.CollectInheritedProtocols(LHS->getInterface(), LHSProtocolSet); |
| 8137 | |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 8138 | // Add all of the protocols for the RHS. |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8139 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSProtocolSet; |
| 8140 | |
| 8141 | // Start with the protocol qualifiers. |
| 8142 | for (auto proto : RHS->quals()) { |
| 8143 | Context.CollectInheritedProtocols(proto, RHSProtocolSet); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8144 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8145 | |
| 8146 | // Also add the protocols associated with the RHS interface. |
| 8147 | Context.CollectInheritedProtocols(RHS->getInterface(), RHSProtocolSet); |
| 8148 | |
| 8149 | // Compute the intersection of the collected protocol sets. |
| 8150 | for (auto proto : LHSProtocolSet) { |
| 8151 | if (RHSProtocolSet.count(proto)) |
| 8152 | IntersectionSet.push_back(proto); |
| 8153 | } |
| 8154 | |
| 8155 | // Compute the set of protocols that is implied by either the common type or |
| 8156 | // the protocols within the intersection. |
| 8157 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ImpliedProtocols; |
| 8158 | Context.CollectInheritedProtocols(CommonBase, ImpliedProtocols); |
| 8159 | |
| 8160 | // Remove any implied protocols from the list of inherited protocols. |
| 8161 | if (!ImpliedProtocols.empty()) { |
| 8162 | IntersectionSet.erase( |
| 8163 | std::remove_if(IntersectionSet.begin(), |
| 8164 | IntersectionSet.end(), |
| 8165 | [&](ObjCProtocolDecl *proto) -> bool { |
| 8166 | return ImpliedProtocols.count(proto) > 0; |
| 8167 | }), |
| 8168 | IntersectionSet.end()); |
| 8169 | } |
| 8170 | |
| 8171 | // Sort the remaining protocols by name. |
| 8172 | llvm::array_pod_sort(IntersectionSet.begin(), IntersectionSet.end(), |
| 8173 | compareObjCProtocolsByName); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8174 | } |
| 8175 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8176 | /// Determine whether the first type is a subtype of the second. |
| 8177 | static bool canAssignObjCObjectTypes(ASTContext &ctx, QualType lhs, |
| 8178 | QualType rhs) { |
| 8179 | // Common case: two object pointers. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8180 | const auto *lhsOPT = lhs->getAs<ObjCObjectPointerType>(); |
| 8181 | const auto *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8182 | if (lhsOPT && rhsOPT) |
| 8183 | return ctx.canAssignObjCInterfaces(lhsOPT, rhsOPT); |
| 8184 | |
| 8185 | // Two block pointers. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8186 | const auto *lhsBlock = lhs->getAs<BlockPointerType>(); |
| 8187 | const auto *rhsBlock = rhs->getAs<BlockPointerType>(); |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8188 | if (lhsBlock && rhsBlock) |
| 8189 | return ctx.typesAreBlockPointerCompatible(lhs, rhs); |
| 8190 | |
| 8191 | // If either is an unqualified 'id' and the other is a block, it's |
| 8192 | // acceptable. |
| 8193 | if ((lhsOPT && lhsOPT->isObjCIdType() && rhsBlock) || |
| 8194 | (rhsOPT && rhsOPT->isObjCIdType() && lhsBlock)) |
| 8195 | return true; |
| 8196 | |
| 8197 | return false; |
| 8198 | } |
| 8199 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8200 | // Check that the given Objective-C type argument lists are equivalent. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8201 | static bool sameObjCTypeArgs(ASTContext &ctx, |
| 8202 | const ObjCInterfaceDecl *iface, |
| 8203 | ArrayRef<QualType> lhsArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8204 | ArrayRef<QualType> rhsArgs, |
| 8205 | bool stripKindOf) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8206 | if (lhsArgs.size() != rhsArgs.size()) |
| 8207 | return false; |
| 8208 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8209 | ObjCTypeParamList *typeParams = iface->getTypeParamList(); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8210 | for (unsigned i = 0, n = lhsArgs.size(); i != n; ++i) { |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8211 | if (ctx.hasSameType(lhsArgs[i], rhsArgs[i])) |
| 8212 | continue; |
| 8213 | |
| 8214 | switch (typeParams->begin()[i]->getVariance()) { |
| 8215 | case ObjCTypeParamVariance::Invariant: |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8216 | if (!stripKindOf || |
| 8217 | !ctx.hasSameType(lhsArgs[i].stripObjCKindOfType(ctx), |
| 8218 | rhsArgs[i].stripObjCKindOfType(ctx))) { |
| 8219 | return false; |
| 8220 | } |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8221 | break; |
| 8222 | |
| 8223 | case ObjCTypeParamVariance::Covariant: |
| 8224 | if (!canAssignObjCObjectTypes(ctx, lhsArgs[i], rhsArgs[i])) |
| 8225 | return false; |
| 8226 | break; |
| 8227 | |
| 8228 | case ObjCTypeParamVariance::Contravariant: |
| 8229 | if (!canAssignObjCObjectTypes(ctx, rhsArgs[i], lhsArgs[i])) |
| 8230 | return false; |
| 8231 | break; |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8232 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8233 | } |
| 8234 | |
| 8235 | return true; |
| 8236 | } |
| 8237 | |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 8238 | QualType ASTContext::areCommonBaseCompatible( |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8239 | const ObjCObjectPointerType *Lptr, |
| 8240 | const ObjCObjectPointerType *Rptr) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8241 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 8242 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 8243 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 8244 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8245 | |
| 8246 | if (!LDecl || !RDecl) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8247 | return {}; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8248 | |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 8249 | // When either LHS or RHS is a kindof type, we should return a kindof type. |
| 8250 | // For example, for common base of kindof(ASub1) and kindof(ASub2), we return |
| 8251 | // kindof(A). |
| 8252 | bool anyKindOf = LHS->isKindOfType() || RHS->isKindOfType(); |
| 8253 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8254 | // Follow the left-hand side up the class hierarchy until we either hit a |
| 8255 | // root or find the RHS. Record the ancestors in case we don't find it. |
| 8256 | llvm::SmallDenseMap<const ObjCInterfaceDecl *, const ObjCObjectType *, 4> |
| 8257 | LHSAncestors; |
| 8258 | while (true) { |
| 8259 | // Record this ancestor. We'll need this if the common type isn't in the |
| 8260 | // path from the LHS to the root. |
| 8261 | LHSAncestors[LHS->getInterface()->getCanonicalDecl()] = LHS; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8262 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8263 | if (declaresSameEntity(LHS->getInterface(), RDecl)) { |
| 8264 | // Get the type arguments. |
| 8265 | ArrayRef<QualType> LHSTypeArgs = LHS->getTypeArgsAsWritten(); |
| 8266 | bool anyChanges = false; |
| 8267 | if (LHS->isSpecialized() && RHS->isSpecialized()) { |
| 8268 | // Both have type arguments, compare them. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8269 | if (!sameObjCTypeArgs(*this, LHS->getInterface(), |
| 8270 | LHS->getTypeArgs(), RHS->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8271 | /*stripKindOf=*/true)) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8272 | return {}; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8273 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { |
| 8274 | // If only one has type arguments, the result will not have type |
| 8275 | // arguments. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 8276 | LHSTypeArgs = {}; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8277 | anyChanges = true; |
| 8278 | } |
| 8279 | |
| 8280 | // Compute the intersection of protocols. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8281 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8282 | getIntersectionOfProtocols(*this, LHS->getInterface(), Lptr, Rptr, |
| 8283 | Protocols); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8284 | if (!Protocols.empty()) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8285 | anyChanges = true; |
| 8286 | |
| 8287 | // 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] | 8288 | // If we need to return a kindof type but LHS is not a kindof type, we |
| 8289 | // build a new result type. |
| 8290 | if (anyChanges || LHS->isKindOfType() != anyKindOf) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8291 | QualType Result = getObjCInterfaceType(LHS->getInterface()); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8292 | Result = getObjCObjectType(Result, LHSTypeArgs, Protocols, |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 8293 | anyKindOf || LHS->isKindOfType()); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8294 | return getObjCObjectPointerType(Result); |
| 8295 | } |
| 8296 | |
| 8297 | return getObjCObjectPointerType(QualType(LHS, 0)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8298 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8299 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8300 | // Find the superclass. |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8301 | QualType LHSSuperType = LHS->getSuperClassType(); |
| 8302 | if (LHSSuperType.isNull()) |
| 8303 | break; |
| 8304 | |
| 8305 | LHS = LHSSuperType->castAs<ObjCObjectType>(); |
| 8306 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8307 | |
| 8308 | // We didn't find anything by following the LHS to its root; now check |
| 8309 | // the RHS against the cached set of ancestors. |
| 8310 | while (true) { |
| 8311 | auto KnownLHS = LHSAncestors.find(RHS->getInterface()->getCanonicalDecl()); |
| 8312 | if (KnownLHS != LHSAncestors.end()) { |
| 8313 | LHS = KnownLHS->second; |
| 8314 | |
| 8315 | // Get the type arguments. |
| 8316 | ArrayRef<QualType> RHSTypeArgs = RHS->getTypeArgsAsWritten(); |
| 8317 | bool anyChanges = false; |
| 8318 | if (LHS->isSpecialized() && RHS->isSpecialized()) { |
| 8319 | // Both have type arguments, compare them. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8320 | if (!sameObjCTypeArgs(*this, LHS->getInterface(), |
| 8321 | LHS->getTypeArgs(), RHS->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8322 | /*stripKindOf=*/true)) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8323 | return {}; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8324 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { |
| 8325 | // If only one has type arguments, the result will not have type |
| 8326 | // arguments. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 8327 | RHSTypeArgs = {}; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8328 | anyChanges = true; |
| 8329 | } |
| 8330 | |
| 8331 | // Compute the intersection of protocols. |
| 8332 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 8333 | getIntersectionOfProtocols(*this, RHS->getInterface(), Lptr, Rptr, |
| 8334 | Protocols); |
| 8335 | if (!Protocols.empty()) |
| 8336 | anyChanges = true; |
| 8337 | |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 8338 | // If we need to return a kindof type but RHS is not a kindof type, we |
| 8339 | // build a new result type. |
| 8340 | if (anyChanges || RHS->isKindOfType() != anyKindOf) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8341 | QualType Result = getObjCInterfaceType(RHS->getInterface()); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8342 | Result = getObjCObjectType(Result, RHSTypeArgs, Protocols, |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 8343 | anyKindOf || RHS->isKindOfType()); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8344 | return getObjCObjectPointerType(Result); |
| 8345 | } |
| 8346 | |
| 8347 | return getObjCObjectPointerType(QualType(RHS, 0)); |
| 8348 | } |
| 8349 | |
| 8350 | // Find the superclass of the RHS. |
| 8351 | QualType RHSSuperType = RHS->getSuperClassType(); |
| 8352 | if (RHSSuperType.isNull()) |
| 8353 | break; |
| 8354 | |
| 8355 | RHS = RHSSuperType->castAs<ObjCObjectType>(); |
| 8356 | } |
| 8357 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8358 | return {}; |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 8359 | } |
| 8360 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8361 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 8362 | const ObjCObjectType *RHS) { |
| 8363 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 8364 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 8365 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 8366 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 8367 | // the LHS. |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8368 | ObjCInterfaceDecl *LHSInterface = LHS->getInterface(); |
| 8369 | bool IsSuperClass = LHSInterface->isSuperClassOf(RHS->getInterface()); |
| 8370 | if (!IsSuperClass) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 8371 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8372 | |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8373 | // If the LHS has protocol qualifiers, determine whether all of them are |
| 8374 | // satisfied by the RHS (i.e., the RHS has a superset of the protocols in the |
| 8375 | // LHS). |
| 8376 | if (LHS->getNumProtocols() > 0) { |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 8377 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 8378 | // ; i.e., SuperClass may implement at least one of the protocols |
| 8379 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 8380 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 8381 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
| 8382 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
| 8383 | // Also, if RHS has explicit quelifiers, include them for comparing with LHS's |
| 8384 | // qualifiers. |
| 8385 | for (auto *RHSPI : RHS->quals()) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8386 | CollectInheritedProtocols(RHSPI, SuperClassInheritedProtocols); |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 8387 | // If there is no protocols associated with RHS, it is not a match. |
| 8388 | if (SuperClassInheritedProtocols.empty()) |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 8389 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8390 | |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 8391 | for (const auto *LHSProto : LHS->quals()) { |
| 8392 | bool SuperImplementsProtocol = false; |
| 8393 | for (auto *SuperClassProto : SuperClassInheritedProtocols) |
| 8394 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 8395 | SuperImplementsProtocol = true; |
| 8396 | break; |
| 8397 | } |
| 8398 | if (!SuperImplementsProtocol) |
| 8399 | return false; |
| 8400 | } |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 8401 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8402 | |
| 8403 | // If the LHS is specialized, we may need to check type arguments. |
| 8404 | if (LHS->isSpecialized()) { |
| 8405 | // Follow the superclass chain until we've matched the LHS class in the |
| 8406 | // hierarchy. This substitutes type arguments through. |
| 8407 | const ObjCObjectType *RHSSuper = RHS; |
| 8408 | while (!declaresSameEntity(RHSSuper->getInterface(), LHSInterface)) |
| 8409 | RHSSuper = RHSSuper->getSuperClassType()->castAs<ObjCObjectType>(); |
| 8410 | |
| 8411 | // If the RHS is specializd, compare type arguments. |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8412 | if (RHSSuper->isSpecialized() && |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8413 | !sameObjCTypeArgs(*this, LHS->getInterface(), |
| 8414 | LHS->getTypeArgs(), RHSSuper->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8415 | /*stripKindOf=*/true)) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8416 | return false; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8417 | } |
| 8418 | } |
| 8419 | |
| 8420 | return true; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 8421 | } |
| 8422 | |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 8423 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 8424 | // get the "pointed to" types |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8425 | const auto *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 8426 | const auto *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8427 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8428 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 8429 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8430 | |
| 8431 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 8432 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 8433 | } |
| 8434 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 8435 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 8436 | return canAssignObjCInterfaces( |
| 8437 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 8438 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 8439 | } |
| 8440 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8441 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 8442 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8443 | /// 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] | 8444 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8445 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 8446 | bool CompareUnqualified) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8447 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 8448 | return hasSameType(LHS, RHS); |
Joey Gouly | 5788b78 | 2016-11-18 14:10:54 +0000 | [diff] [blame] | 8449 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8450 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8451 | } |
| 8452 | |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 8453 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | c87c879 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 8454 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 8455 | } |
| 8456 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8457 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 8458 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 8459 | } |
| 8460 | |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 8461 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 8462 | /// of T is compatible with SubType, return the merged type, else return |
| 8463 | /// QualType() |
| 8464 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 8465 | bool OfBlockPointer, |
| 8466 | bool Unqualified) { |
| 8467 | if (const RecordType *UT = T->getAsUnionType()) { |
| 8468 | RecordDecl *UD = UT->getDecl(); |
| 8469 | if (UD->hasAttr<TransparentUnionAttr>()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8470 | for (const auto *I : UD->fields()) { |
| 8471 | QualType ET = I->getType().getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 8472 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 8473 | if (!MT.isNull()) |
| 8474 | return MT; |
| 8475 | } |
| 8476 | } |
| 8477 | } |
| 8478 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8479 | return {}; |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 8480 | } |
| 8481 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8482 | /// mergeFunctionParameterTypes - merge two types which appear as function |
| 8483 | /// parameter types |
| 8484 | QualType ASTContext::mergeFunctionParameterTypes(QualType lhs, QualType rhs, |
| 8485 | bool OfBlockPointer, |
| 8486 | bool Unqualified) { |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 8487 | // GNU extension: two types are compatible if they appear as a function |
| 8488 | // argument, one of the types is a transparent union type and the other |
| 8489 | // type is compatible with a union member |
| 8490 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 8491 | Unqualified); |
| 8492 | if (!lmerge.isNull()) |
| 8493 | return lmerge; |
| 8494 | |
| 8495 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 8496 | Unqualified); |
| 8497 | if (!rmerge.isNull()) |
| 8498 | return rmerge; |
| 8499 | |
| 8500 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 8501 | } |
| 8502 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8503 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8504 | bool OfBlockPointer, |
| 8505 | bool Unqualified) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8506 | const auto *lbase = lhs->getAs<FunctionType>(); |
| 8507 | const auto *rbase = rhs->getAs<FunctionType>(); |
| 8508 | const auto *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 8509 | const auto *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8510 | bool allLTypes = true; |
| 8511 | bool allRTypes = true; |
| 8512 | |
| 8513 | // Check return type |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8514 | QualType retType; |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 8515 | if (OfBlockPointer) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8516 | QualType RHS = rbase->getReturnType(); |
| 8517 | QualType LHS = lbase->getReturnType(); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 8518 | bool UnqualifiedResult = Unqualified; |
| 8519 | if (!UnqualifiedResult) |
| 8520 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 8521 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 8522 | } |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8523 | else |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8524 | retType = mergeTypes(lbase->getReturnType(), rbase->getReturnType(), false, |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8525 | Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8526 | if (retType.isNull()) |
| 8527 | return {}; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8528 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8529 | if (Unqualified) |
| 8530 | retType = retType.getUnqualifiedType(); |
| 8531 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8532 | CanQualType LRetType = getCanonicalType(lbase->getReturnType()); |
| 8533 | CanQualType RRetType = getCanonicalType(rbase->getReturnType()); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8534 | if (Unqualified) { |
| 8535 | LRetType = LRetType.getUnqualifiedType(); |
| 8536 | RRetType = RRetType.getUnqualifiedType(); |
| 8537 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8538 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8539 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8540 | allLTypes = false; |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8541 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8542 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8543 | |
Daniel Dunbar | edd5bae | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 8544 | // FIXME: double check this |
| 8545 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 8546 | // rbase->getRegParmAttr() != 0 && |
| 8547 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 8548 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 8549 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8550 | |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 8551 | // Compatible functions must have compatible calling conventions |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 8552 | if (lbaseInfo.getCC() != rbaseInfo.getCC()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8553 | return {}; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8554 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8555 | // Regparm is part of the calling convention. |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 8556 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8557 | return {}; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8558 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8559 | return {}; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8560 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8561 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8562 | return {}; |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 8563 | if (lbaseInfo.getNoCallerSavedRegs() != rbaseInfo.getNoCallerSavedRegs()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8564 | return {}; |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 8565 | if (lbaseInfo.getNoCfCheck() != rbaseInfo.getNoCfCheck()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8566 | return {}; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8567 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8568 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 8569 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8570 | |
Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 8571 | if (lbaseInfo.getNoReturn() != NoReturn) |
| 8572 | allLTypes = false; |
| 8573 | if (rbaseInfo.getNoReturn() != NoReturn) |
| 8574 | allRTypes = false; |
| 8575 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8576 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8577 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8578 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 8579 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 8580 | "C++ shouldn't be here"); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8581 | // Compatible functions must have the same number of parameters |
| 8582 | if (lproto->getNumParams() != rproto->getNumParams()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8583 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8584 | |
| 8585 | // Variadic and non-variadic functions aren't compatible |
| 8586 | if (lproto->isVariadic() != rproto->isVariadic()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8587 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8588 | |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 8589 | if (lproto->getMethodQuals() != rproto->getMethodQuals()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8590 | return {}; |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 8591 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8592 | SmallVector<FunctionProtoType::ExtParameterInfo, 4> newParamInfos; |
| 8593 | bool canUseLeft, canUseRight; |
| 8594 | if (!mergeExtParameterInfo(lproto, rproto, canUseLeft, canUseRight, |
| 8595 | newParamInfos)) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8596 | return {}; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8597 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8598 | if (!canUseLeft) |
| 8599 | allLTypes = false; |
| 8600 | if (!canUseRight) |
| 8601 | allRTypes = false; |
| 8602 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8603 | // Check parameter type compatibility |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8604 | SmallVector<QualType, 10> types; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8605 | for (unsigned i = 0, n = lproto->getNumParams(); i < n; i++) { |
| 8606 | QualType lParamType = lproto->getParamType(i).getUnqualifiedType(); |
| 8607 | QualType rParamType = rproto->getParamType(i).getUnqualifiedType(); |
| 8608 | QualType paramType = mergeFunctionParameterTypes( |
| 8609 | lParamType, rParamType, OfBlockPointer, Unqualified); |
| 8610 | if (paramType.isNull()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8611 | return {}; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8612 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8613 | if (Unqualified) |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8614 | paramType = paramType.getUnqualifiedType(); |
| 8615 | |
| 8616 | types.push_back(paramType); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8617 | if (Unqualified) { |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8618 | lParamType = lParamType.getUnqualifiedType(); |
| 8619 | rParamType = rParamType.getUnqualifiedType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8620 | } |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8621 | |
| 8622 | if (getCanonicalType(paramType) != getCanonicalType(lParamType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8623 | allLTypes = false; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8624 | if (getCanonicalType(paramType) != getCanonicalType(rParamType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8625 | allRTypes = false; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8626 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8627 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8628 | if (allLTypes) return lhs; |
| 8629 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8630 | |
| 8631 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 8632 | EPI.ExtInfo = einfo; |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8633 | EPI.ExtParameterInfos = |
| 8634 | newParamInfos.empty() ? nullptr : newParamInfos.data(); |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 8635 | return getFunctionType(retType, types, EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8636 | } |
| 8637 | |
| 8638 | if (lproto) allRTypes = false; |
| 8639 | if (rproto) allLTypes = false; |
| 8640 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8641 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8642 | if (proto) { |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 8643 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8644 | if (proto->isVariadic()) |
| 8645 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8646 | // Check that the types are compatible with the types that |
| 8647 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 8648 | // The only types actually affected are promotable integer |
| 8649 | // types and floats, which would be passed as a different |
| 8650 | // type depending on whether the prototype is visible. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8651 | for (unsigned i = 0, n = proto->getNumParams(); i < n; ++i) { |
| 8652 | QualType paramTy = proto->getParamType(i); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8653 | |
Eli Friedman | 448ce40 | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 8654 | // 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] | 8655 | // to pass enum values. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8656 | if (const auto *Enum = paramTy->getAs<EnumType>()) { |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8657 | paramTy = Enum->getDecl()->getIntegerType(); |
| 8658 | if (paramTy.isNull()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8659 | return {}; |
Eli Friedman | 448ce40 | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 8660 | } |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8661 | |
| 8662 | if (paramTy->isPromotableIntegerType() || |
| 8663 | getCanonicalType(paramTy).getUnqualifiedType() == FloatTy) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8664 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8665 | } |
| 8666 | |
| 8667 | if (allLTypes) return lhs; |
| 8668 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8669 | |
| 8670 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 8671 | EPI.ExtInfo = einfo; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8672 | return getFunctionType(retType, proto->getParamTypes(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8673 | } |
| 8674 | |
| 8675 | if (allLTypes) return lhs; |
| 8676 | if (allRTypes) return rhs; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8677 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8678 | } |
| 8679 | |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8680 | /// Given that we have an enum type and a non-enum type, try to merge them. |
| 8681 | static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET, |
| 8682 | QualType other, bool isBlockReturnType) { |
| 8683 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 8684 | // a signed integer type, or an unsigned integer type. |
| 8685 | // Compatibility is based on the underlying type, not the promotion |
| 8686 | // type. |
| 8687 | QualType underlyingType = ET->getDecl()->getIntegerType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8688 | if (underlyingType.isNull()) |
| 8689 | return {}; |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8690 | if (Context.hasSameType(underlyingType, other)) |
| 8691 | return other; |
| 8692 | |
| 8693 | // In block return types, we're more permissive and accept any |
| 8694 | // integral type of the same size. |
| 8695 | if (isBlockReturnType && other->isIntegerType() && |
| 8696 | Context.getTypeSize(underlyingType) == Context.getTypeSize(other)) |
| 8697 | return other; |
| 8698 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8699 | return {}; |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8700 | } |
| 8701 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8702 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8703 | bool OfBlockPointer, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 8704 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | db4e349 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 8705 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 8706 | // type is adjusted to "T" prior to any further analysis, the expression |
| 8707 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 8708 | // expression is an lvalue unless the reference is an rvalue reference and |
| 8709 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 8710 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 8711 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8712 | |
| 8713 | if (Unqualified) { |
| 8714 | LHS = LHS.getUnqualifiedType(); |
| 8715 | RHS = RHS.getUnqualifiedType(); |
| 8716 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8717 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8718 | QualType LHSCan = getCanonicalType(LHS), |
| 8719 | RHSCan = getCanonicalType(RHS); |
| 8720 | |
| 8721 | // If two types are identical, they are compatible. |
| 8722 | if (LHSCan == RHSCan) |
| 8723 | return LHS; |
| 8724 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8725 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 8726 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 8727 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8728 | if (LQuals != RQuals) { |
| 8729 | // If any of these qualifiers are different, we have a type |
| 8730 | // mismatch. |
| 8731 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8732 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
Roger Ferrer Ibanez | d93add3 | 2017-02-24 08:41:09 +0000 | [diff] [blame] | 8733 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime() || |
| 8734 | LQuals.hasUnaligned() != RQuals.hasUnaligned()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8735 | return {}; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8736 | |
| 8737 | // Exactly one GC qualifier difference is allowed: __strong is |
| 8738 | // okay if the other type has no GC qualifier but is an Objective |
| 8739 | // C object pointer (i.e. implicitly strong by default). We fix |
| 8740 | // this by pretending that the unqualified type was actually |
| 8741 | // qualified __strong. |
| 8742 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 8743 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 8744 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 8745 | |
| 8746 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8747 | return {}; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8748 | |
| 8749 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 8750 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 8751 | } |
| 8752 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 8753 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 8754 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8755 | return {}; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8756 | } |
| 8757 | |
| 8758 | // Okay, qualifiers are equal. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8759 | |
Eli Friedman | dcca633 | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 8760 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 8761 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8762 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8763 | // We want to consider the two function types to be the same for these |
| 8764 | // comparisons, just force one to the other. |
| 8765 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 8766 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 16f9096 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 8767 | |
| 8768 | // Same as above for arrays |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 8769 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 8770 | LHSClass = Type::ConstantArray; |
| 8771 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 8772 | RHSClass = Type::ConstantArray; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8773 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8774 | // ObjCInterfaces are just specialized ObjCObjects. |
| 8775 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 8776 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 8777 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 8778 | // Canonicalize ExtVector -> Vector. |
| 8779 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 8780 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8781 | |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 8782 | // If the canonical type classes don't match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8783 | if (LHSClass != RHSClass) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8784 | // Note that we only have special rules for turning block enum |
| 8785 | // returns into block int returns, not vice-versa. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8786 | if (const auto *ETy = LHS->getAs<EnumType>()) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8787 | return mergeEnumWithInteger(*this, ETy, RHS, false); |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 8788 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 8789 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8790 | return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType); |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 8791 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 8792 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 194904e | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 8793 | if (OfBlockPointer && !BlockReturnType) { |
| 8794 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 8795 | return LHS; |
| 8796 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 8797 | return RHS; |
| 8798 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8799 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8800 | return {}; |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 8801 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8802 | |
Steve Naroff | c6edcbd | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 8803 | // The canonical type classes match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8804 | switch (LHSClass) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8805 | #define TYPE(Class, Base) |
| 8806 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 8807 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8808 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 8809 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 8810 | #include "clang/AST/TypeNodes.def" |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8811 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8812 | |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 8813 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 8814 | case Type::DeducedTemplateSpecialization: |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 8815 | case Type::LValueReference: |
| 8816 | case Type::RValueReference: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8817 | case Type::MemberPointer: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8818 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8819 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8820 | case Type::ObjCInterface: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8821 | case Type::IncompleteArray: |
| 8822 | case Type::VariableArray: |
| 8823 | case Type::FunctionProto: |
| 8824 | case Type::ExtVector: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8825 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8826 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8827 | case Type::Pointer: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8828 | { |
| 8829 | // Merge two pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 8830 | QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType(); |
| 8831 | QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8832 | if (Unqualified) { |
| 8833 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 8834 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 8835 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8836 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8837 | Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8838 | if (ResultType.isNull()) |
| 8839 | return {}; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 8840 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8841 | return LHS; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 8842 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8843 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8844 | return getPointerType(ResultType); |
| 8845 | } |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 8846 | case Type::BlockPointer: |
| 8847 | { |
| 8848 | // Merge two block pointer types, while trying to preserve typedef info |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 8849 | QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType(); |
| 8850 | QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8851 | if (Unqualified) { |
| 8852 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 8853 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 8854 | } |
Anastasia Stulova | 81a25e35 | 2017-03-10 15:23:07 +0000 | [diff] [blame] | 8855 | if (getLangOpts().OpenCL) { |
| 8856 | Qualifiers LHSPteeQual = LHSPointee.getQualifiers(); |
| 8857 | Qualifiers RHSPteeQual = RHSPointee.getQualifiers(); |
| 8858 | // Blocks can't be an expression in a ternary operator (OpenCL v2.0 |
| 8859 | // 6.12.5) thus the following check is asymmetric. |
| 8860 | if (!LHSPteeQual.isAddressSpaceSupersetOf(RHSPteeQual)) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8861 | return {}; |
Anastasia Stulova | 81a25e35 | 2017-03-10 15:23:07 +0000 | [diff] [blame] | 8862 | LHSPteeQual.removeAddressSpace(); |
| 8863 | RHSPteeQual.removeAddressSpace(); |
| 8864 | LHSPointee = |
| 8865 | QualType(LHSPointee.getTypePtr(), LHSPteeQual.getAsOpaqueValue()); |
| 8866 | RHSPointee = |
| 8867 | QualType(RHSPointee.getTypePtr(), RHSPteeQual.getAsOpaqueValue()); |
| 8868 | } |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8869 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 8870 | Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8871 | if (ResultType.isNull()) |
| 8872 | return {}; |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 8873 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 8874 | return LHS; |
| 8875 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 8876 | return RHS; |
| 8877 | return getBlockPointerType(ResultType); |
| 8878 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 8879 | case Type::Atomic: |
| 8880 | { |
| 8881 | // Merge two pointer types, while trying to preserve typedef info |
| 8882 | QualType LHSValue = LHS->getAs<AtomicType>()->getValueType(); |
| 8883 | QualType RHSValue = RHS->getAs<AtomicType>()->getValueType(); |
| 8884 | if (Unqualified) { |
| 8885 | LHSValue = LHSValue.getUnqualifiedType(); |
| 8886 | RHSValue = RHSValue.getUnqualifiedType(); |
| 8887 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8888 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 8889 | Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8890 | if (ResultType.isNull()) |
| 8891 | return {}; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 8892 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 8893 | return LHS; |
| 8894 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 8895 | return RHS; |
| 8896 | return getAtomicType(ResultType); |
| 8897 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8898 | case Type::ConstantArray: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8899 | { |
| 8900 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 8901 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 8902 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8903 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8904 | |
| 8905 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 8906 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8907 | if (Unqualified) { |
| 8908 | LHSElem = LHSElem.getUnqualifiedType(); |
| 8909 | RHSElem = RHSElem.getUnqualifiedType(); |
| 8910 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8911 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8912 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8913 | if (ResultType.isNull()) |
| 8914 | return {}; |
Jeremy Morse | 8129c5c | 2018-06-05 09:18:26 +0000 | [diff] [blame] | 8915 | |
| 8916 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 8917 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
| 8918 | |
| 8919 | // If either side is a variable array, and both are complete, check whether |
| 8920 | // the current dimension is definite. |
| 8921 | if (LVAT || RVAT) { |
| 8922 | auto SizeFetch = [this](const VariableArrayType* VAT, |
| 8923 | const ConstantArrayType* CAT) |
| 8924 | -> std::pair<bool,llvm::APInt> { |
| 8925 | if (VAT) { |
| 8926 | llvm::APSInt TheInt; |
| 8927 | Expr *E = VAT->getSizeExpr(); |
| 8928 | if (E && E->isIntegerConstantExpr(TheInt, *this)) |
| 8929 | return std::make_pair(true, TheInt); |
| 8930 | else |
| 8931 | return std::make_pair(false, TheInt); |
| 8932 | } else if (CAT) { |
| 8933 | return std::make_pair(true, CAT->getSize()); |
| 8934 | } else { |
| 8935 | return std::make_pair(false, llvm::APInt()); |
| 8936 | } |
| 8937 | }; |
| 8938 | |
| 8939 | bool HaveLSize, HaveRSize; |
| 8940 | llvm::APInt LSize, RSize; |
| 8941 | std::tie(HaveLSize, LSize) = SizeFetch(LVAT, LCAT); |
| 8942 | std::tie(HaveRSize, RSize) = SizeFetch(RVAT, RCAT); |
| 8943 | if (HaveLSize && HaveRSize && !llvm::APInt::isSameValue(LSize, RSize)) |
| 8944 | return {}; // Definite, but unequal, array dimension |
| 8945 | } |
| 8946 | |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8947 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 8948 | return LHS; |
| 8949 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 8950 | return RHS; |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 8951 | if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(), |
| 8952 | ArrayType::ArraySizeModifier(), 0); |
| 8953 | if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(), |
| 8954 | ArrayType::ArraySizeModifier(), 0); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8955 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 8956 | return LHS; |
| 8957 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 8958 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8959 | if (LVAT) { |
| 8960 | // FIXME: This isn't correct! But tricky to implement because |
| 8961 | // the array's size has to be the size of LHS, but the type |
| 8962 | // has to be different. |
| 8963 | return LHS; |
| 8964 | } |
| 8965 | if (RVAT) { |
| 8966 | // FIXME: This isn't correct! But tricky to implement because |
| 8967 | // the array's size has to be the size of RHS, but the type |
| 8968 | // has to be different. |
| 8969 | return RHS; |
| 8970 | } |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 8971 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 8972 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 8973 | return getIncompleteArrayType(ResultType, |
| 8974 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8975 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8976 | case Type::FunctionNoProto: |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8977 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8978 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8979 | case Type::Enum: |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8980 | return {}; |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8981 | case Type::Builtin: |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 8982 | // Only exactly equal builtin types are compatible, which is tested above. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8983 | return {}; |
Daniel Dunbar | 804c044 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 8984 | case Type::Complex: |
| 8985 | // Distinct complex types are incompatible. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8986 | return {}; |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 8987 | case Type::Vector: |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 8988 | // FIXME: The merged type should be an ExtVector! |
John McCall | 44c064b | 2010-03-12 23:14:13 +0000 | [diff] [blame] | 8989 | if (areCompatVectorTypes(LHSCan->getAs<VectorType>(), |
| 8990 | RHSCan->getAs<VectorType>())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8991 | return LHS; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8992 | return {}; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8993 | case Type::ObjCObject: { |
| 8994 | // Check if the types are assignment compatible. |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 8995 | // FIXME: This should be type compatibility, e.g. whether |
| 8996 | // "LHS x; RHS x;" at global scope is legal. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8997 | const auto *LHSIface = LHS->getAs<ObjCObjectType>(); |
| 8998 | const auto *RHSIface = RHS->getAs<ObjCObjectType>(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8999 | if (canAssignObjCInterfaces(LHSIface, RHSIface)) |
Steve Naroff | 7a7814c | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 9000 | return LHS; |
| 9001 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9002 | return {}; |
Cedric Venet | 4fc88b7 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 9003 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9004 | case Type::ObjCObjectPointer: |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 9005 | if (OfBlockPointer) { |
| 9006 | if (canAssignObjCInterfacesInBlockPointer( |
| 9007 | LHS->getAs<ObjCObjectPointerType>(), |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 9008 | RHS->getAs<ObjCObjectPointerType>(), |
| 9009 | BlockReturnType)) |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 9010 | return LHS; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9011 | return {}; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 9012 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 9013 | if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(), |
| 9014 | RHS->getAs<ObjCObjectPointerType>())) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 9015 | return LHS; |
| 9016 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9017 | return {}; |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 9018 | case Type::Pipe: |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 9019 | assert(LHS != RHS && |
| 9020 | "Equivalent pipe types should have already been handled!"); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9021 | return {}; |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 9022 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 9023 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 9024 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 9025 | } |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 9026 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 9027 | bool ASTContext::mergeExtParameterInfo( |
| 9028 | const FunctionProtoType *FirstFnType, const FunctionProtoType *SecondFnType, |
| 9029 | bool &CanUseFirst, bool &CanUseSecond, |
| 9030 | SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &NewParamInfos) { |
| 9031 | assert(NewParamInfos.empty() && "param info list not empty"); |
| 9032 | CanUseFirst = CanUseSecond = true; |
| 9033 | bool FirstHasInfo = FirstFnType->hasExtParameterInfos(); |
| 9034 | bool SecondHasInfo = SecondFnType->hasExtParameterInfos(); |
| 9035 | |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 9036 | // Fast path: if the first type doesn't have ext parameter infos, |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 9037 | // we match if and only if the second type also doesn't have them. |
| 9038 | if (!FirstHasInfo && !SecondHasInfo) |
| 9039 | return true; |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 9040 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 9041 | bool NeedParamInfo = false; |
| 9042 | size_t E = FirstHasInfo ? FirstFnType->getExtParameterInfos().size() |
| 9043 | : SecondFnType->getExtParameterInfos().size(); |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 9044 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 9045 | for (size_t I = 0; I < E; ++I) { |
| 9046 | FunctionProtoType::ExtParameterInfo FirstParam, SecondParam; |
| 9047 | if (FirstHasInfo) |
| 9048 | FirstParam = FirstFnType->getExtParameterInfo(I); |
| 9049 | if (SecondHasInfo) |
| 9050 | SecondParam = SecondFnType->getExtParameterInfo(I); |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 9051 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 9052 | // Cannot merge unless everything except the noescape flag matches. |
| 9053 | if (FirstParam.withIsNoEscape(false) != SecondParam.withIsNoEscape(false)) |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 9054 | return false; |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 9055 | |
| 9056 | bool FirstNoEscape = FirstParam.isNoEscape(); |
| 9057 | bool SecondNoEscape = SecondParam.isNoEscape(); |
| 9058 | bool IsNoEscape = FirstNoEscape && SecondNoEscape; |
| 9059 | NewParamInfos.push_back(FirstParam.withIsNoEscape(IsNoEscape)); |
| 9060 | if (NewParamInfos.back().getOpaqueValue()) |
| 9061 | NeedParamInfo = true; |
| 9062 | if (FirstNoEscape != IsNoEscape) |
| 9063 | CanUseFirst = false; |
| 9064 | if (SecondNoEscape != IsNoEscape) |
| 9065 | CanUseSecond = false; |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 9066 | } |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 9067 | |
| 9068 | if (!NeedParamInfo) |
| 9069 | NewParamInfos.clear(); |
| 9070 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 9071 | return true; |
| 9072 | } |
| 9073 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 9074 | void ASTContext::ResetObjCLayout(const ObjCContainerDecl *CD) { |
| 9075 | ObjCLayouts[CD] = nullptr; |
| 9076 | } |
| 9077 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9078 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 9079 | /// 'RHS' attributes and returns the merged version; including for function |
| 9080 | /// return types. |
| 9081 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 9082 | QualType LHSCan = getCanonicalType(LHS), |
| 9083 | RHSCan = getCanonicalType(RHS); |
| 9084 | // If two types are identical, they are compatible. |
| 9085 | if (LHSCan == RHSCan) |
| 9086 | return LHS; |
| 9087 | if (RHSCan->isFunctionType()) { |
| 9088 | if (!LHSCan->isFunctionType()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9089 | return {}; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9090 | QualType OldReturnType = |
| 9091 | cast<FunctionType>(RHSCan.getTypePtr())->getReturnType(); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9092 | QualType NewReturnType = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9093 | cast<FunctionType>(LHSCan.getTypePtr())->getReturnType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9094 | QualType ResReturnType = |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9095 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 9096 | if (ResReturnType.isNull()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9097 | return {}; |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9098 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 9099 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 9100 | // In either case, use OldReturnType to build the new function type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9101 | const auto *F = LHS->getAs<FunctionType>(); |
| 9102 | if (const auto *FPT = cast<FunctionProtoType>(F)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9103 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 9104 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Reid Kleckner | 896b32f | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 9105 | QualType ResultType = |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 9106 | getFunctionType(OldReturnType, FPT->getParamTypes(), EPI); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9107 | return ResultType; |
| 9108 | } |
| 9109 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9110 | return {}; |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9111 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9112 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9113 | // If the qualifiers are different, the types can still be merged. |
| 9114 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 9115 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 9116 | if (LQuals != RQuals) { |
| 9117 | // If any of these qualifiers are different, we have a type mismatch. |
| 9118 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 9119 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9120 | return {}; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9121 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9122 | // Exactly one GC qualifier difference is allowed: __strong is |
| 9123 | // okay if the other type has no GC qualifier but is an Objective |
| 9124 | // C object pointer (i.e. implicitly strong by default). We fix |
| 9125 | // this by pretending that the unqualified type was actually |
| 9126 | // qualified __strong. |
| 9127 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 9128 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 9129 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9130 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9131 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9132 | return {}; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9133 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9134 | if (GC_L == Qualifiers::Strong) |
| 9135 | return LHS; |
| 9136 | if (GC_R == Qualifiers::Strong) |
| 9137 | return RHS; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9138 | return {}; |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9139 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9140 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9141 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
| 9142 | QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 9143 | QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 9144 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 9145 | if (ResQT == LHSBaseQT) |
| 9146 | return LHS; |
| 9147 | if (ResQT == RHSBaseQT) |
| 9148 | return RHS; |
| 9149 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9150 | return {}; |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9151 | } |
| 9152 | |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 9153 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 9154 | // Integer Predicates |
| 9155 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3c91971 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 9156 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 9157 | unsigned ASTContext::getIntWidth(QualType T) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9158 | if (const auto *ET = T->getAs<EnumType>()) |
Eli Friedman | ee275c8 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 9159 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 9160 | if (T->isBooleanType()) |
| 9161 | return 1; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 9162 | // For builtin types, just use the standard type sizing method |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 9163 | return (unsigned)getTypeSize(T); |
| 9164 | } |
| 9165 | |
Abramo Bagnara | 1364049 | 2012-09-09 10:21:24 +0000 | [diff] [blame] | 9166 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) const { |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 9167 | assert((T->hasSignedIntegerRepresentation() || T->isSignedFixedPointType()) && |
| 9168 | "Unexpected type"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9169 | |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 9170 | // Turn <4 x signed int> -> <4 x unsigned int> |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9171 | if (const auto *VTy = T->getAs<VectorType>()) |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 9172 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 9173 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 9174 | |
| 9175 | // For enums, we return the unsigned version of the base type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9176 | if (const auto *ETy = T->getAs<EnumType>()) |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 9177 | T = ETy->getDecl()->getIntegerType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9178 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9179 | const auto *BTy = T->getAs<BuiltinType>(); |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 9180 | assert(BTy && "Unexpected signed integer or fixed point type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 9181 | switch (BTy->getKind()) { |
| 9182 | case BuiltinType::Char_S: |
| 9183 | case BuiltinType::SChar: |
| 9184 | return UnsignedCharTy; |
| 9185 | case BuiltinType::Short: |
| 9186 | return UnsignedShortTy; |
| 9187 | case BuiltinType::Int: |
| 9188 | return UnsignedIntTy; |
| 9189 | case BuiltinType::Long: |
| 9190 | return UnsignedLongTy; |
| 9191 | case BuiltinType::LongLong: |
| 9192 | return UnsignedLongLongTy; |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 9193 | case BuiltinType::Int128: |
| 9194 | return UnsignedInt128Ty; |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 9195 | |
| 9196 | case BuiltinType::ShortAccum: |
| 9197 | return UnsignedShortAccumTy; |
| 9198 | case BuiltinType::Accum: |
| 9199 | return UnsignedAccumTy; |
| 9200 | case BuiltinType::LongAccum: |
| 9201 | return UnsignedLongAccumTy; |
| 9202 | case BuiltinType::SatShortAccum: |
| 9203 | return SatUnsignedShortAccumTy; |
| 9204 | case BuiltinType::SatAccum: |
| 9205 | return SatUnsignedAccumTy; |
| 9206 | case BuiltinType::SatLongAccum: |
| 9207 | return SatUnsignedLongAccumTy; |
| 9208 | case BuiltinType::ShortFract: |
| 9209 | return UnsignedShortFractTy; |
| 9210 | case BuiltinType::Fract: |
| 9211 | return UnsignedFractTy; |
| 9212 | case BuiltinType::LongFract: |
| 9213 | return UnsignedLongFractTy; |
| 9214 | case BuiltinType::SatShortFract: |
| 9215 | return SatUnsignedShortFractTy; |
| 9216 | case BuiltinType::SatFract: |
| 9217 | return SatUnsignedFractTy; |
| 9218 | case BuiltinType::SatLongFract: |
| 9219 | return SatUnsignedLongFractTy; |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 9220 | default: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 9221 | llvm_unreachable("Unexpected signed integer or fixed point type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 9222 | } |
| 9223 | } |
| 9224 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9225 | ASTMutationListener::~ASTMutationListener() = default; |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 9226 | |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 9227 | void ASTMutationListener::DeducedReturnType(const FunctionDecl *FD, |
| 9228 | QualType ReturnType) {} |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9229 | |
| 9230 | //===----------------------------------------------------------------------===// |
| 9231 | // Builtin Type Computation |
| 9232 | //===----------------------------------------------------------------------===// |
| 9233 | |
| 9234 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9235 | /// pointer over the consumed characters. This returns the resultant type. If |
| 9236 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 9237 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 9238 | /// a vector of "i*". |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9239 | /// |
| 9240 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 9241 | /// to be an Integer Constant Expression. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 9242 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9243 | ASTContext::GetBuiltinTypeError &Error, |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9244 | bool &RequiresICE, |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9245 | bool AllowTypeModifiers) { |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9246 | // Modifiers. |
| 9247 | int HowLong = 0; |
| 9248 | bool Signed = false, Unsigned = false; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9249 | RequiresICE = false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9250 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9251 | // Read the prefixed modifiers first. |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9252 | bool Done = false; |
| 9253 | #ifndef NDEBUG |
| 9254 | bool IsSpecialLong = false; |
| 9255 | #endif |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9256 | while (!Done) { |
| 9257 | switch (*Str++) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9258 | default: Done = true; --Str; break; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 9259 | case 'I': |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9260 | RequiresICE = true; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 9261 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9262 | case 'S': |
| 9263 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 9264 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 9265 | Signed = true; |
| 9266 | break; |
| 9267 | case 'U': |
| 9268 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
Sean Silva | 2a99514 | 2015-01-16 21:44:26 +0000 | [diff] [blame] | 9269 | assert(!Unsigned && "Can't use 'U' modifier multiple times!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9270 | Unsigned = true; |
| 9271 | break; |
| 9272 | case 'L': |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9273 | assert(!IsSpecialLong && "Can't use 'L' with 'W' or 'N' modifiers"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9274 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 9275 | ++HowLong; |
| 9276 | break; |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9277 | case 'N': |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9278 | // 'N' behaves like 'L' for all non LP64 targets and 'int' otherwise. |
| 9279 | assert(!IsSpecialLong && "Can't use two 'N' or 'W' modifiers!"); |
| 9280 | assert(HowLong == 0 && "Can't use both 'L' and 'N' modifiers!"); |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9281 | #ifndef NDEBUG |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9282 | IsSpecialLong = true; |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9283 | #endif |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9284 | if (Context.getTargetInfo().getLongWidth() == 32) |
| 9285 | ++HowLong; |
| 9286 | break; |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 9287 | case 'W': |
| 9288 | // This modifier represents int64 type. |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9289 | assert(!IsSpecialLong && "Can't use two 'N' or 'W' modifiers!"); |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 9290 | assert(HowLong == 0 && "Can't use both 'L' and 'W' modifiers!"); |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9291 | #ifndef NDEBUG |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9292 | IsSpecialLong = true; |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9293 | #endif |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 9294 | switch (Context.getTargetInfo().getInt64Type()) { |
| 9295 | default: |
| 9296 | llvm_unreachable("Unexpected integer type"); |
| 9297 | case TargetInfo::SignedLong: |
| 9298 | HowLong = 1; |
| 9299 | break; |
| 9300 | case TargetInfo::SignedLongLong: |
| 9301 | HowLong = 2; |
| 9302 | break; |
| 9303 | } |
Duncan P. N. Exon Smith | eae8caa | 2017-06-14 21:26:31 +0000 | [diff] [blame] | 9304 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9305 | } |
| 9306 | } |
| 9307 | |
| 9308 | QualType Type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9309 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9310 | // Read the base type. |
| 9311 | switch (*Str++) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 9312 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9313 | case 'v': |
| 9314 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 9315 | "Bad modifiers used with 'v'!"); |
| 9316 | Type = Context.VoidTy; |
| 9317 | break; |
Jack Carter | 24bef98 | 2013-08-15 15:16:57 +0000 | [diff] [blame] | 9318 | case 'h': |
| 9319 | assert(HowLong == 0 && !Signed && !Unsigned && |
Sean Silva | 2a99514 | 2015-01-16 21:44:26 +0000 | [diff] [blame] | 9320 | "Bad modifiers used with 'h'!"); |
Jack Carter | 24bef98 | 2013-08-15 15:16:57 +0000 | [diff] [blame] | 9321 | Type = Context.HalfTy; |
| 9322 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9323 | case 'f': |
| 9324 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 9325 | "Bad modifiers used with 'f'!"); |
| 9326 | Type = Context.FloatTy; |
| 9327 | break; |
| 9328 | case 'd': |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9329 | assert(HowLong < 3 && !Signed && !Unsigned && |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9330 | "Bad modifiers used with 'd'!"); |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9331 | if (HowLong == 1) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9332 | Type = Context.LongDoubleTy; |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9333 | else if (HowLong == 2) |
| 9334 | Type = Context.Float128Ty; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9335 | else |
| 9336 | Type = Context.DoubleTy; |
| 9337 | break; |
| 9338 | case 's': |
| 9339 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 9340 | if (Unsigned) |
| 9341 | Type = Context.UnsignedShortTy; |
| 9342 | else |
| 9343 | Type = Context.ShortTy; |
| 9344 | break; |
| 9345 | case 'i': |
| 9346 | if (HowLong == 3) |
| 9347 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 9348 | else if (HowLong == 2) |
| 9349 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 9350 | else if (HowLong == 1) |
| 9351 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 9352 | else |
| 9353 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 9354 | break; |
| 9355 | case 'c': |
| 9356 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 9357 | if (Signed) |
| 9358 | Type = Context.SignedCharTy; |
| 9359 | else if (Unsigned) |
| 9360 | Type = Context.UnsignedCharTy; |
| 9361 | else |
| 9362 | Type = Context.CharTy; |
| 9363 | break; |
| 9364 | case 'b': // boolean |
| 9365 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 9366 | Type = Context.BoolTy; |
| 9367 | break; |
| 9368 | case 'z': // size_t. |
| 9369 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 9370 | Type = Context.getSizeType(); |
| 9371 | break; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 9372 | case 'w': // wchar_t. |
| 9373 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'w'!"); |
| 9374 | Type = Context.getWideCharType(); |
| 9375 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9376 | case 'F': |
| 9377 | Type = Context.getCFConstantStringType(); |
| 9378 | break; |
Fariborz Jahanian | d11da7e | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 9379 | case 'G': |
| 9380 | Type = Context.getObjCIdType(); |
| 9381 | break; |
| 9382 | case 'H': |
| 9383 | Type = Context.getObjCSelType(); |
| 9384 | break; |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 9385 | case 'M': |
| 9386 | Type = Context.getObjCSuperType(); |
| 9387 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9388 | case 'a': |
| 9389 | Type = Context.getBuiltinVaListType(); |
| 9390 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 9391 | break; |
| 9392 | case 'A': |
| 9393 | // This is a "reference" to a va_list; however, what exactly |
| 9394 | // this means depends on how va_list is defined. There are two |
| 9395 | // different kinds of va_list: ones passed by value, and ones |
| 9396 | // passed by reference. An example of a by-value va_list is |
| 9397 | // x86, where va_list is a char*. An example of by-ref va_list |
| 9398 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 9399 | // we want this argument to be a char*&; for x86-64, we want |
| 9400 | // it to be a __va_list_tag*. |
| 9401 | Type = Context.getBuiltinVaListType(); |
| 9402 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9403 | if (Type->isArrayType()) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9404 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9405 | else |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9406 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9407 | break; |
| 9408 | case 'V': { |
| 9409 | char *End; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9410 | unsigned NumElements = strtoul(Str, &End, 10); |
| 9411 | assert(End != Str && "Missing vector size"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9412 | Str = End; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9413 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9414 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9415 | RequiresICE, false); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9416 | assert(!RequiresICE && "Can't require vector ICE"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9417 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9418 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 9419 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 9420 | VectorType::GenericVector); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9421 | break; |
| 9422 | } |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9423 | case 'E': { |
| 9424 | char *End; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9425 | |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9426 | unsigned NumElements = strtoul(Str, &End, 10); |
| 9427 | assert(End != Str && "Missing vector size"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9428 | |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9429 | Str = End; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9430 | |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9431 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9432 | false); |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9433 | Type = Context.getExtVectorType(ElementType, NumElements); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9434 | break; |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9435 | } |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 9436 | case 'X': { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9437 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9438 | false); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9439 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 9440 | Type = Context.getComplexType(ElementType); |
| 9441 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9442 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9443 | case 'Y': |
Fariborz Jahanian | 73952fc | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 9444 | Type = Context.getPointerDiffType(); |
| 9445 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 9446 | case 'P': |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 9447 | Type = Context.getFILEType(); |
| 9448 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 9449 | Error = ASTContext::GE_Missing_stdio; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9450 | return {}; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9451 | } |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 9452 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 9453 | case 'J': |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 9454 | if (Signed) |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 9455 | Type = Context.getsigjmp_bufType(); |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 9456 | else |
| 9457 | Type = Context.getjmp_bufType(); |
| 9458 | |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 9459 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 9460 | Error = ASTContext::GE_Missing_setjmp; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9461 | return {}; |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 9462 | } |
| 9463 | break; |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 9464 | case 'K': |
| 9465 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 9466 | Type = Context.getucontext_tType(); |
| 9467 | |
| 9468 | if (Type.isNull()) { |
| 9469 | Error = ASTContext::GE_Missing_ucontext; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9470 | return {}; |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 9471 | } |
| 9472 | break; |
Eli Friedman | 4e91899e | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 9473 | case 'p': |
| 9474 | Type = Context.getProcessIDType(); |
| 9475 | break; |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 9476 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9477 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9478 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 9479 | Done = !AllowTypeModifiers; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9480 | while (!Done) { |
John McCall | b8b9466 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 9481 | switch (char c = *Str++) { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9482 | default: Done = true; --Str; break; |
| 9483 | case '*': |
| 9484 | case '&': { |
| 9485 | // Both pointers and references can have their pointee types |
| 9486 | // qualified with an address space. |
| 9487 | char *End; |
| 9488 | unsigned AddrSpace = strtoul(Str, &End, 10); |
Matt Arsenault | c65f966 | 2018-08-02 12:14:28 +0000 | [diff] [blame] | 9489 | if (End != Str) { |
| 9490 | // Note AddrSpace == 0 is not the same as an unspecified address space. |
| 9491 | Type = Context.getAddrSpaceQualType( |
| 9492 | Type, |
| 9493 | Context.getLangASForBuiltinAddressSpace(AddrSpace)); |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9494 | Str = End; |
| 9495 | } |
| 9496 | if (c == '*') |
| 9497 | Type = Context.getPointerType(Type); |
| 9498 | else |
| 9499 | Type = Context.getLValueReferenceType(Type); |
| 9500 | break; |
| 9501 | } |
| 9502 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 9503 | case 'C': |
| 9504 | Type = Type.withConst(); |
| 9505 | break; |
| 9506 | case 'D': |
| 9507 | Type = Context.getVolatileType(Type); |
| 9508 | break; |
Ted Kremenek | f2a2f5f | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 9509 | case 'R': |
| 9510 | Type = Type.withRestrict(); |
| 9511 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9512 | } |
| 9513 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9514 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9515 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9516 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9517 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9518 | return Type; |
| 9519 | } |
| 9520 | |
| 9521 | /// GetBuiltinType - Return the type for the specified builtin. |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9522 | QualType ASTContext::GetBuiltinType(unsigned Id, |
| 9523 | GetBuiltinTypeError &Error, |
| 9524 | unsigned *IntegerConstantArgs) const { |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 9525 | const char *TypeStr = BuiltinInfo.getTypeString(Id); |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 9526 | if (TypeStr[0] == '\0') { |
| 9527 | Error = GE_Missing_type; |
| 9528 | return {}; |
| 9529 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9530 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9531 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9532 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9533 | bool RequiresICE = false; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9534 | Error = GE_None; |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9535 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 9536 | RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9537 | if (Error != GE_None) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9538 | return {}; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9539 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9540 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9541 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9542 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9543 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9544 | if (Error != GE_None) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9545 | return {}; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9546 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9547 | // If this argument is required to be an IntegerConstantExpression and the |
| 9548 | // caller cares, fill in the bitmask we return. |
| 9549 | if (RequiresICE && IntegerConstantArgs) |
| 9550 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9551 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9552 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 9553 | if (Ty->isArrayType()) |
| 9554 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9555 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9556 | ArgTypes.push_back(Ty); |
| 9557 | } |
| 9558 | |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 9559 | if (Id == Builtin::BI__GetExceptionInfo) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9560 | return {}; |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 9561 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9562 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 9563 | "'.' should only occur at end of builtin type list!"); |
| 9564 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 9565 | FunctionType::ExtInfo EI(CC_C); |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 9566 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 9567 | |
| 9568 | bool Variadic = (TypeStr[0] == '.'); |
| 9569 | |
Richard Smith | 836de6b | 2016-12-19 23:59:34 +0000 | [diff] [blame] | 9570 | // We really shouldn't be making a no-proto type here. |
| 9571 | if (ArgTypes.empty() && Variadic && !getLangOpts().CPlusPlus) |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 9572 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 9573 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9574 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 9575 | EPI.ExtInfo = EI; |
| 9576 | EPI.Variadic = Variadic; |
Richard Smith | 391fb86 | 2016-10-18 07:13:55 +0000 | [diff] [blame] | 9577 | if (getLangOpts().CPlusPlus && BuiltinInfo.isNoThrow(Id)) |
| 9578 | EPI.ExceptionSpec.Type = |
| 9579 | getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9580 | |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 9581 | return getFunctionType(ResType, ArgTypes, EPI); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9582 | } |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 9583 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9584 | static GVALinkage basicGVALinkageForFunction(const ASTContext &Context, |
| 9585 | const FunctionDecl *FD) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9586 | if (!FD->isExternallyVisible()) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9587 | return GVA_Internal; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9588 | |
Richard Smith | e2467b7 | 2017-11-16 23:54:56 +0000 | [diff] [blame] | 9589 | // Non-user-provided functions get emitted as weak definitions with every |
| 9590 | // use, no matter whether they've been explicitly instantiated etc. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9591 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
Richard Smith | e2467b7 | 2017-11-16 23:54:56 +0000 | [diff] [blame] | 9592 | if (!MD->isUserProvided()) |
| 9593 | return GVA_DiscardableODR; |
| 9594 | |
Yaron Keren | 4cd211b | 2017-02-22 14:32:39 +0000 | [diff] [blame] | 9595 | GVALinkage External; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9596 | switch (FD->getTemplateSpecializationKind()) { |
| 9597 | case TSK_Undeclared: |
| 9598 | case TSK_ExplicitSpecialization: |
| 9599 | External = GVA_StrongExternal; |
| 9600 | break; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9601 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9602 | case TSK_ExplicitInstantiationDefinition: |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 9603 | return GVA_StrongODR; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9604 | |
David Majnemer | c3d0733 | 2014-05-15 06:25:57 +0000 | [diff] [blame] | 9605 | // C++11 [temp.explicit]p10: |
| 9606 | // [ Note: The intent is that an inline function that is the subject of |
| 9607 | // an explicit instantiation declaration will still be implicitly |
| 9608 | // instantiated when used so that the body can be considered for |
| 9609 | // inlining, but that no out-of-line copy of the inline function would be |
| 9610 | // generated in the translation unit. -- end note ] |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9611 | case TSK_ExplicitInstantiationDeclaration: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9612 | return GVA_AvailableExternally; |
| 9613 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9614 | case TSK_ImplicitInstantiation: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9615 | External = GVA_DiscardableODR; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9616 | break; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9617 | } |
| 9618 | |
| 9619 | if (!FD->isInlined()) |
| 9620 | return External; |
David Majnemer | 62f0ffd | 2013-08-01 17:26:42 +0000 | [diff] [blame] | 9621 | |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 9622 | if ((!Context.getLangOpts().CPlusPlus && |
| 9623 | !Context.getTargetInfo().getCXXABI().isMicrosoft() && |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9624 | !FD->hasAttr<DLLExportAttr>()) || |
David Majnemer | 62f0ffd | 2013-08-01 17:26:42 +0000 | [diff] [blame] | 9625 | FD->hasAttr<GNUInlineAttr>()) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9626 | // FIXME: This doesn't match gcc's behavior for dllexport inline functions. |
| 9627 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9628 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 9629 | // externally visible. |
| 9630 | if (FD->isInlineDefinitionExternallyVisible()) |
| 9631 | return External; |
| 9632 | |
| 9633 | // C99 inline semantics, where the symbol is not externally visible. |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9634 | return GVA_AvailableExternally; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9635 | } |
| 9636 | |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 9637 | // Functions specified with extern and inline in -fms-compatibility mode |
| 9638 | // forcibly get emitted. While the body of the function cannot be later |
| 9639 | // replaced, the function definition cannot be discarded. |
David Majnemer | 7376870 | 2015-03-20 00:02:27 +0000 | [diff] [blame] | 9640 | if (FD->isMSExternInline()) |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 9641 | return GVA_StrongODR; |
| 9642 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9643 | return GVA_DiscardableODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9644 | } |
| 9645 | |
Artem Belevich | ca2b951 | 2016-05-02 20:30:03 +0000 | [diff] [blame] | 9646 | static GVALinkage adjustGVALinkageForAttributes(const ASTContext &Context, |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9647 | const Decl *D, GVALinkage L) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9648 | // See http://msdn.microsoft.com/en-us/library/xa0d9ste.aspx |
| 9649 | // dllexport/dllimport on inline functions. |
| 9650 | if (D->hasAttr<DLLImportAttr>()) { |
| 9651 | if (L == GVA_DiscardableODR || L == GVA_StrongODR) |
| 9652 | return GVA_AvailableExternally; |
Artem Belevich | ca2b951 | 2016-05-02 20:30:03 +0000 | [diff] [blame] | 9653 | } else if (D->hasAttr<DLLExportAttr>()) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9654 | if (L == GVA_DiscardableODR) |
| 9655 | return GVA_StrongODR; |
Artem Belevich | ca2b951 | 2016-05-02 20:30:03 +0000 | [diff] [blame] | 9656 | } else if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice && |
| 9657 | D->hasAttr<CUDAGlobalAttr>()) { |
| 9658 | // Device-side functions with __global__ attribute must always be |
| 9659 | // visible externally so they can be launched from host. |
| 9660 | if (L == GVA_DiscardableODR || L == GVA_Internal) |
| 9661 | return GVA_StrongODR; |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9662 | } |
| 9663 | return L; |
| 9664 | } |
| 9665 | |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9666 | /// Adjust the GVALinkage for a declaration based on what an external AST source |
| 9667 | /// knows about whether there can be other definitions of this declaration. |
| 9668 | static GVALinkage |
| 9669 | adjustGVALinkageForExternalDefinitionKind(const ASTContext &Ctx, const Decl *D, |
| 9670 | GVALinkage L) { |
| 9671 | ExternalASTSource *Source = Ctx.getExternalSource(); |
| 9672 | if (!Source) |
| 9673 | return L; |
| 9674 | |
| 9675 | switch (Source->hasExternalDefinitions(D)) { |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9676 | case ExternalASTSource::EK_Never: |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9677 | // Other translation units rely on us to provide the definition. |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9678 | if (L == GVA_DiscardableODR) |
| 9679 | return GVA_StrongODR; |
| 9680 | break; |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9681 | |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9682 | case ExternalASTSource::EK_Always: |
| 9683 | return GVA_AvailableExternally; |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9684 | |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9685 | case ExternalASTSource::EK_ReplyHazy: |
| 9686 | break; |
| 9687 | } |
| 9688 | return L; |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9689 | } |
| 9690 | |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9691 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const { |
| 9692 | return adjustGVALinkageForExternalDefinitionKind(*this, FD, |
| 9693 | adjustGVALinkageForAttributes(*this, FD, |
| 9694 | basicGVALinkageForFunction(*this, FD))); |
| 9695 | } |
| 9696 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9697 | static GVALinkage basicGVALinkageForVariable(const ASTContext &Context, |
| 9698 | const VarDecl *VD) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9699 | if (!VD->isExternallyVisible()) |
| 9700 | return GVA_Internal; |
| 9701 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9702 | if (VD->isStaticLocal()) { |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9703 | const DeclContext *LexicalContext = VD->getParentFunctionOrMethod(); |
| 9704 | while (LexicalContext && !isa<FunctionDecl>(LexicalContext)) |
| 9705 | LexicalContext = LexicalContext->getLexicalParent(); |
| 9706 | |
David Blaikie | eb21001 | 2017-01-27 23:11:10 +0000 | [diff] [blame] | 9707 | // ObjC Blocks can create local variables that don't have a FunctionDecl |
| 9708 | // LexicalContext. |
| 9709 | if (!LexicalContext) |
| 9710 | return GVA_DiscardableODR; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9711 | |
David Blaikie | eb21001 | 2017-01-27 23:11:10 +0000 | [diff] [blame] | 9712 | // Otherwise, let the static local variable inherit its linkage from the |
| 9713 | // nearest enclosing function. |
| 9714 | auto StaticLocalLinkage = |
| 9715 | Context.GetGVALinkageForFunction(cast<FunctionDecl>(LexicalContext)); |
| 9716 | |
| 9717 | // Itanium ABI 5.2.2: "Each COMDAT group [for a static local variable] must |
| 9718 | // be emitted in any object with references to the symbol for the object it |
| 9719 | // contains, whether inline or out-of-line." |
| 9720 | // Similar behavior is observed with MSVC. An alternative ABI could use |
| 9721 | // StrongODR/AvailableExternally to match the function, but none are |
| 9722 | // known/supported currently. |
| 9723 | if (StaticLocalLinkage == GVA_StrongODR || |
| 9724 | StaticLocalLinkage == GVA_AvailableExternally) |
| 9725 | return GVA_DiscardableODR; |
| 9726 | return StaticLocalLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9727 | } |
| 9728 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 9729 | // MSVC treats in-class initialized static data members as definitions. |
| 9730 | // By giving them non-strong linkage, out-of-line definitions won't |
| 9731 | // cause link errors. |
| 9732 | if (Context.isMSStaticDataMemberInlineDefinition(VD)) |
| 9733 | return GVA_DiscardableODR; |
| 9734 | |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 9735 | // Most non-template variables have strong linkage; inline variables are |
| 9736 | // linkonce_odr or (occasionally, for compatibility) weak_odr. |
| 9737 | GVALinkage StrongLinkage; |
| 9738 | switch (Context.getInlineVariableDefinitionKind(VD)) { |
| 9739 | case ASTContext::InlineVariableDefinitionKind::None: |
| 9740 | StrongLinkage = GVA_StrongExternal; |
| 9741 | break; |
| 9742 | case ASTContext::InlineVariableDefinitionKind::Weak: |
| 9743 | case ASTContext::InlineVariableDefinitionKind::WeakUnknown: |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 9744 | StrongLinkage = GVA_DiscardableODR; |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 9745 | break; |
| 9746 | case ASTContext::InlineVariableDefinitionKind::Strong: |
| 9747 | StrongLinkage = GVA_StrongODR; |
| 9748 | break; |
| 9749 | } |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 9750 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 9751 | switch (VD->getTemplateSpecializationKind()) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9752 | case TSK_Undeclared: |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 9753 | return StrongLinkage; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9754 | |
David Majnemer | 6d1780c | 2015-07-17 23:36:49 +0000 | [diff] [blame] | 9755 | case TSK_ExplicitSpecialization: |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 9756 | return Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 9757 | VD->isStaticDataMember() |
David Majnemer | 6d1780c | 2015-07-17 23:36:49 +0000 | [diff] [blame] | 9758 | ? GVA_StrongODR |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 9759 | : StrongLinkage; |
David Majnemer | 6d1780c | 2015-07-17 23:36:49 +0000 | [diff] [blame] | 9760 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9761 | case TSK_ExplicitInstantiationDefinition: |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 9762 | return GVA_StrongODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9763 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9764 | case TSK_ExplicitInstantiationDeclaration: |
| 9765 | return GVA_AvailableExternally; |
| 9766 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9767 | case TSK_ImplicitInstantiation: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9768 | return GVA_DiscardableODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9769 | } |
Rafael Espindola | 27699c8 | 2013-05-13 14:05:53 +0000 | [diff] [blame] | 9770 | |
| 9771 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9772 | } |
| 9773 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9774 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9775 | return adjustGVALinkageForExternalDefinitionKind(*this, VD, |
| 9776 | adjustGVALinkageForAttributes(*this, VD, |
| 9777 | basicGVALinkageForVariable(*this, VD))); |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9778 | } |
| 9779 | |
David Blaikie | e6b7c28 | 2017-04-11 20:46:34 +0000 | [diff] [blame] | 9780 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9781 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9782 | if (!VD->isFileVarDecl()) |
| 9783 | return false; |
Renato Golin | 9258aa5 | 2014-05-21 10:40:27 +0000 | [diff] [blame] | 9784 | // Global named register variables (GNU extension) are never emitted. |
| 9785 | if (VD->getStorageClass() == SC_Register) |
| 9786 | return false; |
Richard Smith | 7747ce2 | 2015-08-19 20:49:38 +0000 | [diff] [blame] | 9787 | if (VD->getDescribedVarTemplate() || |
| 9788 | isa<VarTemplatePartialSpecializationDecl>(VD)) |
| 9789 | return false; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9790 | } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 9791 | // We never need to emit an uninstantiated function template. |
| 9792 | if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) |
| 9793 | return false; |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 9794 | } else if (isa<PragmaCommentDecl>(D)) |
| 9795 | return true; |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 9796 | else if (isa<OMPThreadPrivateDecl>(D)) |
Dmitry Polukhin | 0b0da29 | 2016-04-06 11:38:59 +0000 | [diff] [blame] | 9797 | return true; |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 9798 | else if (isa<PragmaDetectMismatchDecl>(D)) |
| 9799 | return true; |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 9800 | else if (isa<OMPThreadPrivateDecl>(D)) |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 9801 | return !D->getDeclContext()->isDependentContext(); |
| 9802 | else if (isa<OMPDeclareReductionDecl>(D)) |
| 9803 | return !D->getDeclContext()->isDependentContext(); |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 9804 | else if (isa<ImportDecl>(D)) |
| 9805 | return true; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 9806 | else |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 9807 | return false; |
| 9808 | |
Hans Wennborg | 08c5a7b | 2018-06-25 13:23:49 +0000 | [diff] [blame] | 9809 | if (D->isFromASTFile() && !LangOpts.BuildingPCHWithObjectFile) { |
| 9810 | assert(getExternalSource() && "It's from an AST file; must have a source."); |
| 9811 | // On Windows, PCH files are built together with an object file. If this |
| 9812 | // declaration comes from such a PCH and DeclMustBeEmitted would return |
| 9813 | // true, it would have returned true and the decl would have been emitted |
| 9814 | // into that object file, so it doesn't need to be emitted here. |
| 9815 | // Note that decls are still emitted if they're referenced, as usual; |
| 9816 | // DeclMustBeEmitted is used to decide whether a decl must be emitted even |
| 9817 | // if it's not referenced. |
| 9818 | // |
| 9819 | // Explicit template instantiation definitions are tricky. If there was an |
| 9820 | // explicit template instantiation decl in the PCH before, it will look like |
| 9821 | // the definition comes from there, even if that was just the declaration. |
| 9822 | // (Explicit instantiation defs of variable templates always get emitted.) |
| 9823 | bool IsExpInstDef = |
| 9824 | isa<FunctionDecl>(D) && |
| 9825 | cast<FunctionDecl>(D)->getTemplateSpecializationKind() == |
| 9826 | TSK_ExplicitInstantiationDefinition; |
| 9827 | |
Hans Wennborg | b51a703 | 2018-09-14 15:18:30 +0000 | [diff] [blame] | 9828 | // Implicit member function definitions, such as operator= might not be |
| 9829 | // marked as template specializations, since they're not coming from a |
| 9830 | // template but synthesized directly on the class. |
| 9831 | IsExpInstDef |= |
| 9832 | isa<CXXMethodDecl>(D) && |
| 9833 | cast<CXXMethodDecl>(D)->getParent()->getTemplateSpecializationKind() == |
| 9834 | TSK_ExplicitInstantiationDefinition; |
| 9835 | |
Hans Wennborg | 08c5a7b | 2018-06-25 13:23:49 +0000 | [diff] [blame] | 9836 | if (getExternalSource()->DeclIsFromPCHWithObjectFile(D) && !IsExpInstDef) |
| 9837 | return false; |
| 9838 | } |
| 9839 | |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 9840 | // If this is a member of a class template, we do not need to emit it. |
| 9841 | if (D->getDeclContext()->isDependentContext()) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9842 | return false; |
| 9843 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 9844 | // Weak references don't produce any output by themselves. |
| 9845 | if (D->hasAttr<WeakRefAttr>()) |
| 9846 | return false; |
| 9847 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9848 | // Aliases and used decls are required. |
| 9849 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 9850 | return true; |
| 9851 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9852 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9853 | // Forward declarations aren't required. |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 9854 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 9855 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9856 | |
| 9857 | // Constructors and destructors are required. |
| 9858 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 9859 | return true; |
Hans Wennborg | 08c5a7b | 2018-06-25 13:23:49 +0000 | [diff] [blame] | 9860 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 9861 | // The key function for a class is required. This rule only comes |
| 9862 | // into play when inline functions can be key functions, though. |
| 9863 | if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9864 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 9865 | const CXXRecordDecl *RD = MD->getParent(); |
| 9866 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 9867 | const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD); |
| 9868 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 9869 | return true; |
| 9870 | } |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9871 | } |
| 9872 | } |
| 9873 | |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9874 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 9875 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9876 | // static, static inline, always_inline, and extern inline functions can |
| 9877 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 9878 | // Implicit template instantiations can also be deferred in C++. |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9879 | return !isDiscardableGVALinkage(Linkage); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9880 | } |
Hans Wennborg | 08c5a7b | 2018-06-25 13:23:49 +0000 | [diff] [blame] | 9881 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9882 | const auto *VD = cast<VarDecl>(D); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9883 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 9884 | |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 9885 | // If the decl is marked as `declare target to`, it should be emitted for the |
| 9886 | // host and for the device. |
| 9887 | if (LangOpts.OpenMP && |
| 9888 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) |
| 9889 | return true; |
| 9890 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 9891 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly && |
| 9892 | !isMSStaticDataMemberInlineDefinition(VD)) |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 9893 | return false; |
| 9894 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9895 | // Variables that can be needed in other TUs are required. |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9896 | auto Linkage = GetGVALinkageForVariable(VD); |
| 9897 | if (!isDiscardableGVALinkage(Linkage)) |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9898 | return true; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9899 | |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9900 | // We never need to emit a variable that is available in another TU. |
| 9901 | if (Linkage == GVA_AvailableExternally) |
| 9902 | return false; |
| 9903 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9904 | // Variables that have destruction with side-effects are required. |
| 9905 | if (VD->getType().isDestructedType()) |
| 9906 | return true; |
| 9907 | |
| 9908 | // Variables that have initialization with side-effects are required. |
Richard Smith | 7747ce2 | 2015-08-19 20:49:38 +0000 | [diff] [blame] | 9909 | if (VD->getInit() && VD->getInit()->HasSideEffects(*this) && |
Richard Smith | 187ffb4 | 2017-01-20 01:19:46 +0000 | [diff] [blame] | 9910 | // We can get a value-dependent initializer during error recovery. |
| 9911 | (VD->getInit()->isValueDependent() || !VD->evaluateValue())) |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9912 | return true; |
| 9913 | |
Richard Smith | da38363 | 2016-08-15 01:33:41 +0000 | [diff] [blame] | 9914 | // Likewise, variables with tuple-like bindings are required if their |
| 9915 | // bindings have side-effects. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9916 | if (const auto *DD = dyn_cast<DecompositionDecl>(VD)) |
| 9917 | for (const auto *BD : DD->bindings()) |
| 9918 | if (const auto *BindingVD = BD->getHoldingVar()) |
Richard Smith | da38363 | 2016-08-15 01:33:41 +0000 | [diff] [blame] | 9919 | if (DeclMustBeEmitted(BindingVD)) |
| 9920 | return true; |
| 9921 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 9922 | return false; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9923 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 9924 | |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 9925 | void ASTContext::forEachMultiversionedFunctionVersion( |
| 9926 | const FunctionDecl *FD, |
Erich Keane | 0fb1648 | 2018-08-13 18:33:20 +0000 | [diff] [blame] | 9927 | llvm::function_ref<void(FunctionDecl *)> Pred) const { |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 9928 | assert(FD->isMultiVersion() && "Only valid for multiversioned functions"); |
| 9929 | llvm::SmallDenseSet<const FunctionDecl*, 4> SeenDecls; |
Erich Keane | 7304f0a | 2018-11-28 20:58:43 +0000 | [diff] [blame] | 9930 | FD = FD->getMostRecentDecl(); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 9931 | for (auto *CurDecl : |
| 9932 | FD->getDeclContext()->getRedeclContext()->lookup(FD->getDeclName())) { |
Erich Keane | 7304f0a | 2018-11-28 20:58:43 +0000 | [diff] [blame] | 9933 | FunctionDecl *CurFD = CurDecl->getAsFunction()->getMostRecentDecl(); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 9934 | if (CurFD && hasSameType(CurFD->getType(), FD->getType()) && |
| 9935 | std::end(SeenDecls) == llvm::find(SeenDecls, CurFD)) { |
| 9936 | SeenDecls.insert(CurFD); |
| 9937 | Pred(CurFD); |
| 9938 | } |
| 9939 | } |
| 9940 | } |
| 9941 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 9942 | CallingConv ASTContext::getDefaultCallingConvention(bool IsVariadic, |
| 9943 | bool IsCXXMethod) const { |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 9944 | // Pass through to the C++ ABI object |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 9945 | if (IsCXXMethod) |
| 9946 | return ABI->getDefaultMethodCallConv(IsVariadic); |
Timur Iskhodzhanov | c5098ad | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 9947 | |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 9948 | switch (LangOpts.getDefaultCallingConv()) { |
| 9949 | case LangOptions::DCC_None: |
| 9950 | break; |
| 9951 | case LangOptions::DCC_CDecl: |
| 9952 | return CC_C; |
| 9953 | case LangOptions::DCC_FastCall: |
Erich Keane | 5759fa7 | 2017-10-24 23:12:01 +0000 | [diff] [blame] | 9954 | if (getTargetInfo().hasFeature("sse2") && !IsVariadic) |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 9955 | return CC_X86FastCall; |
| 9956 | break; |
| 9957 | case LangOptions::DCC_StdCall: |
| 9958 | if (!IsVariadic) |
| 9959 | return CC_X86StdCall; |
| 9960 | break; |
| 9961 | case LangOptions::DCC_VectorCall: |
| 9962 | // __vectorcall cannot be applied to variadic functions. |
| 9963 | if (!IsVariadic) |
| 9964 | return CC_X86VectorCall; |
| 9965 | break; |
Erich Keane | a957ffb | 2017-11-02 21:08:00 +0000 | [diff] [blame] | 9966 | case LangOptions::DCC_RegCall: |
| 9967 | // __regcall cannot be applied to variadic functions. |
| 9968 | if (!IsVariadic) |
| 9969 | return CC_X86RegCall; |
| 9970 | break; |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 9971 | } |
Alexander Kornienko | 21de0ae | 2015-01-20 11:20:41 +0000 | [diff] [blame] | 9972 | return Target->getDefaultCallingConv(TargetInfo::CCMT_Unknown); |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 9973 | } |
| 9974 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 9975 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 9976 | // Pass through to the C++ ABI object |
| 9977 | return ABI->isNearlyEmpty(RD); |
| 9978 | } |
| 9979 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 9980 | VTableContextBase *ASTContext::getVTableContext() { |
| 9981 | if (!VTContext.get()) { |
| 9982 | if (Target->getCXXABI().isMicrosoft()) |
| 9983 | VTContext.reset(new MicrosoftVTableContext(*this)); |
| 9984 | else |
| 9985 | VTContext.reset(new ItaniumVTableContext(*this)); |
| 9986 | } |
| 9987 | return VTContext.get(); |
| 9988 | } |
| 9989 | |
Yaxun Liu | c18e9ec | 2019-02-14 02:00:09 +0000 | [diff] [blame] | 9990 | MangleContext *ASTContext::createMangleContext(const TargetInfo *T) { |
| 9991 | if (!T) |
| 9992 | T = Target; |
| 9993 | switch (T->getCXXABI().getKind()) { |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 9994 | case TargetCXXABI::GenericAArch64: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 9995 | case TargetCXXABI::GenericItanium: |
| 9996 | case TargetCXXABI::GenericARM: |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 9997 | case TargetCXXABI::GenericMIPS: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 9998 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 9999 | case TargetCXXABI::iOS64: |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 10000 | case TargetCXXABI::WebAssembly: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 10001 | case TargetCXXABI::WatchOS: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 10002 | return ItaniumMangleContext::create(*this, getDiagnostics()); |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 10003 | case TargetCXXABI::Microsoft: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 10004 | return MicrosoftMangleContext::create(*this, getDiagnostics()); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 10005 | } |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 10006 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 10007 | } |
| 10008 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 10009 | CXXABI::~CXXABI() = default; |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 10010 | |
| 10011 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 10012 | return ASTRecordLayouts.getMemorySize() + |
| 10013 | llvm::capacity_in_bytes(ObjCLayouts) + |
| 10014 | llvm::capacity_in_bytes(KeyFunctions) + |
| 10015 | llvm::capacity_in_bytes(ObjCImpls) + |
| 10016 | llvm::capacity_in_bytes(BlockVarCopyInits) + |
| 10017 | llvm::capacity_in_bytes(DeclAttrs) + |
| 10018 | llvm::capacity_in_bytes(TemplateOrInstantiation) + |
| 10019 | llvm::capacity_in_bytes(InstantiatedFromUsingDecl) + |
| 10020 | llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) + |
| 10021 | llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) + |
| 10022 | llvm::capacity_in_bytes(OverriddenMethods) + |
| 10023 | llvm::capacity_in_bytes(Types) + |
| 10024 | llvm::capacity_in_bytes(VariableArrayTypes) + |
| 10025 | llvm::capacity_in_bytes(ClassScopeSpecializationPattern); |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 10026 | } |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 10027 | |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 10028 | /// getIntTypeForBitwidth - |
| 10029 | /// sets integer QualTy according to specified details: |
| 10030 | /// bitwidth, signed/unsigned. |
| 10031 | /// Returns empty type if there is no appropriate target types. |
| 10032 | QualType ASTContext::getIntTypeForBitwidth(unsigned DestWidth, |
| 10033 | unsigned Signed) const { |
| 10034 | TargetInfo::IntType Ty = getTargetInfo().getIntTypeByWidth(DestWidth, Signed); |
| 10035 | CanQualType QualTy = getFromTargetType(Ty); |
| 10036 | if (!QualTy && DestWidth == 128) |
| 10037 | return Signed ? Int128Ty : UnsignedInt128Ty; |
| 10038 | return QualTy; |
| 10039 | } |
| 10040 | |
| 10041 | /// getRealTypeForBitwidth - |
| 10042 | /// sets floating point QualTy according to specified bitwidth. |
| 10043 | /// Returns empty type if there is no appropriate target types. |
| 10044 | QualType ASTContext::getRealTypeForBitwidth(unsigned DestWidth) const { |
| 10045 | TargetInfo::RealType Ty = getTargetInfo().getRealTypeByWidth(DestWidth); |
| 10046 | switch (Ty) { |
| 10047 | case TargetInfo::Float: |
| 10048 | return FloatTy; |
| 10049 | case TargetInfo::Double: |
| 10050 | return DoubleTy; |
| 10051 | case TargetInfo::LongDouble: |
| 10052 | return LongDoubleTy; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 10053 | case TargetInfo::Float128: |
| 10054 | return Float128Ty; |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 10055 | case TargetInfo::NoFloat: |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 10056 | return {}; |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 10057 | } |
| 10058 | |
| 10059 | llvm_unreachable("Unhandled TargetInfo::RealType value"); |
| 10060 | } |
| 10061 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 10062 | void ASTContext::setManglingNumber(const NamedDecl *ND, unsigned Number) { |
| 10063 | if (Number > 1) |
| 10064 | MangleNumbers[ND] = Number; |
David Blaikie | 095deba | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 10065 | } |
| 10066 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 10067 | unsigned ASTContext::getManglingNumber(const NamedDecl *ND) const { |
Richard Smith | e9b02d6 | 2016-03-21 22:33:02 +0000 | [diff] [blame] | 10068 | auto I = MangleNumbers.find(ND); |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 10069 | return I != MangleNumbers.end() ? I->second : 1; |
David Blaikie | 095deba | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 10070 | } |
| 10071 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 10072 | void ASTContext::setStaticLocalNumber(const VarDecl *VD, unsigned Number) { |
| 10073 | if (Number > 1) |
| 10074 | StaticLocalNumbers[VD] = Number; |
| 10075 | } |
| 10076 | |
| 10077 | unsigned ASTContext::getStaticLocalNumber(const VarDecl *VD) const { |
Richard Smith | e9b02d6 | 2016-03-21 22:33:02 +0000 | [diff] [blame] | 10078 | auto I = StaticLocalNumbers.find(VD); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 10079 | return I != StaticLocalNumbers.end() ? I->second : 1; |
| 10080 | } |
| 10081 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 10082 | MangleNumberingContext & |
| 10083 | ASTContext::getManglingNumberContext(const DeclContext *DC) { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 10084 | assert(LangOpts.CPlusPlus); // We don't need mangling numbers for plain C. |
Justin Lebar | 20ebffc | 2016-10-10 16:26:19 +0000 | [diff] [blame] | 10085 | std::unique_ptr<MangleNumberingContext> &MCtx = MangleNumberingContexts[DC]; |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 10086 | if (!MCtx) |
| 10087 | MCtx = createMangleNumberingContext(); |
| 10088 | return *MCtx; |
| 10089 | } |
| 10090 | |
Justin Lebar | 20ebffc | 2016-10-10 16:26:19 +0000 | [diff] [blame] | 10091 | std::unique_ptr<MangleNumberingContext> |
| 10092 | ASTContext::createMangleNumberingContext() const { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 10093 | return ABI->createMangleNumberingContext(); |
Douglas Gregor | 6379854 | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 10094 | } |
| 10095 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 10096 | const CXXConstructorDecl * |
| 10097 | ASTContext::getCopyConstructorForExceptionObject(CXXRecordDecl *RD) { |
| 10098 | return ABI->getCopyConstructorForExceptionObject( |
| 10099 | cast<CXXRecordDecl>(RD->getFirstDecl())); |
| 10100 | } |
| 10101 | |
| 10102 | void ASTContext::addCopyConstructorForExceptionObject(CXXRecordDecl *RD, |
| 10103 | CXXConstructorDecl *CD) { |
| 10104 | return ABI->addCopyConstructorForExceptionObject( |
| 10105 | cast<CXXRecordDecl>(RD->getFirstDecl()), |
| 10106 | cast<CXXConstructorDecl>(CD->getFirstDecl())); |
| 10107 | } |
| 10108 | |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 10109 | void ASTContext::addTypedefNameForUnnamedTagDecl(TagDecl *TD, |
| 10110 | TypedefNameDecl *DD) { |
| 10111 | return ABI->addTypedefNameForUnnamedTagDecl(TD, DD); |
| 10112 | } |
| 10113 | |
| 10114 | TypedefNameDecl * |
| 10115 | ASTContext::getTypedefNameForUnnamedTagDecl(const TagDecl *TD) { |
| 10116 | return ABI->getTypedefNameForUnnamedTagDecl(TD); |
| 10117 | } |
| 10118 | |
| 10119 | void ASTContext::addDeclaratorForUnnamedTagDecl(TagDecl *TD, |
| 10120 | DeclaratorDecl *DD) { |
| 10121 | return ABI->addDeclaratorForUnnamedTagDecl(TD, DD); |
| 10122 | } |
| 10123 | |
| 10124 | DeclaratorDecl *ASTContext::getDeclaratorForUnnamedTagDecl(const TagDecl *TD) { |
| 10125 | return ABI->getDeclaratorForUnnamedTagDecl(TD); |
| 10126 | } |
| 10127 | |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 10128 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 10129 | ParamIndices[D] = index; |
| 10130 | } |
| 10131 | |
| 10132 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 10133 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 10134 | assert(I != ParamIndices.end() && |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 10135 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 10136 | return I->second; |
| 10137 | } |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 10138 | |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 10139 | APValue * |
| 10140 | ASTContext::getMaterializedTemporaryValue(const MaterializeTemporaryExpr *E, |
| 10141 | bool MayCreate) { |
| 10142 | assert(E && E->getStorageDuration() == SD_Static && |
| 10143 | "don't need to cache the computed value for this temporary"); |
David Majnemer | 2dcef9e | 2015-08-13 23:50:15 +0000 | [diff] [blame] | 10144 | if (MayCreate) { |
| 10145 | APValue *&MTVI = MaterializedTemporaryValues[E]; |
| 10146 | if (!MTVI) |
| 10147 | MTVI = new (*this) APValue; |
| 10148 | return MTVI; |
| 10149 | } |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 10150 | |
David Majnemer | 2dcef9e | 2015-08-13 23:50:15 +0000 | [diff] [blame] | 10151 | return MaterializedTemporaryValues.lookup(E); |
Richard Smith | e6c0144 | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 10152 | } |
| 10153 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 10154 | bool ASTContext::AtomicUsesUnsupportedLibcall(const AtomicExpr *E) const { |
| 10155 | const llvm::Triple &T = getTargetInfo().getTriple(); |
| 10156 | if (!T.isOSDarwin()) |
| 10157 | return false; |
| 10158 | |
Bob Wilson | 2c82c3d | 2013-11-02 23:27:49 +0000 | [diff] [blame] | 10159 | if (!(T.isiOS() && T.isOSVersionLT(7)) && |
| 10160 | !(T.isMacOSX() && T.isOSVersionLT(10, 9))) |
| 10161 | return false; |
| 10162 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 10163 | QualType AtomicTy = E->getPtr()->getType()->getPointeeType(); |
| 10164 | CharUnits sizeChars = getTypeSizeInChars(AtomicTy); |
| 10165 | uint64_t Size = sizeChars.getQuantity(); |
| 10166 | CharUnits alignChars = getTypeAlignInChars(AtomicTy); |
| 10167 | unsigned Align = alignChars.getQuantity(); |
| 10168 | unsigned MaxInlineWidthInBits = getTargetInfo().getMaxAtomicInlineWidth(); |
| 10169 | return (Size != Align || toBits(sizeChars) > MaxInlineWidthInBits); |
| 10170 | } |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10171 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10172 | /// Template specializations to abstract away from pointers and TypeLocs. |
| 10173 | /// @{ |
| 10174 | template <typename T> |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10175 | static ast_type_traits::DynTypedNode createDynTypedNode(const T &Node) { |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10176 | return ast_type_traits::DynTypedNode::create(*Node); |
| 10177 | } |
| 10178 | template <> |
| 10179 | ast_type_traits::DynTypedNode createDynTypedNode(const TypeLoc &Node) { |
| 10180 | return ast_type_traits::DynTypedNode::create(Node); |
| 10181 | } |
| 10182 | template <> |
| 10183 | ast_type_traits::DynTypedNode |
| 10184 | createDynTypedNode(const NestedNameSpecifierLoc &Node) { |
| 10185 | return ast_type_traits::DynTypedNode::create(Node); |
| 10186 | } |
| 10187 | /// @} |
| 10188 | |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10189 | /// A \c RecursiveASTVisitor that builds a map from nodes to their |
| 10190 | /// parents as defined by the \c RecursiveASTVisitor. |
| 10191 | /// |
| 10192 | /// Note that the relationship described here is purely in terms of AST |
| 10193 | /// traversal - there are other relationships (for example declaration context) |
| 10194 | /// in the AST that are better modeled by special matchers. |
| 10195 | /// |
| 10196 | /// FIXME: Currently only builds up the map using \c Stmt and \c Decl nodes. |
| 10197 | class ASTContext::ParentMap::ASTVisitor |
| 10198 | : public RecursiveASTVisitor<ASTVisitor> { |
| 10199 | public: |
| 10200 | ASTVisitor(ParentMap &Map) : Map(Map) {} |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10201 | |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10202 | private: |
| 10203 | friend class RecursiveASTVisitor<ASTVisitor>; |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 10204 | |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10205 | using VisitorBase = RecursiveASTVisitor<ASTVisitor>; |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10206 | |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10207 | bool shouldVisitTemplateInstantiations() const { return true; } |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10208 | |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10209 | bool shouldVisitImplicitCode() const { return true; } |
| 10210 | |
| 10211 | template <typename T, typename MapNodeTy, typename BaseTraverseFn, |
| 10212 | typename MapTy> |
| 10213 | bool TraverseNode(T Node, MapNodeTy MapNode, BaseTraverseFn BaseTraverse, |
| 10214 | MapTy *Parents) { |
| 10215 | if (!Node) |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10216 | return true; |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10217 | if (ParentStack.size() > 0) { |
| 10218 | // FIXME: Currently we add the same parent multiple times, but only |
| 10219 | // when no memoization data is available for the type. |
| 10220 | // For example when we visit all subexpressions of template |
| 10221 | // instantiations; this is suboptimal, but benign: the only way to |
| 10222 | // visit those is with hasAncestor / hasParent, and those do not create |
| 10223 | // new matches. |
| 10224 | // The plan is to enable DynTypedNode to be storable in a map or hash |
| 10225 | // map. The main problem there is to implement hash functions / |
| 10226 | // comparison operators for all types that DynTypedNode supports that |
| 10227 | // do not have pointer identity. |
| 10228 | auto &NodeOrVector = (*Parents)[MapNode]; |
| 10229 | if (NodeOrVector.isNull()) { |
| 10230 | if (const auto *D = ParentStack.back().get<Decl>()) |
| 10231 | NodeOrVector = D; |
| 10232 | else if (const auto *S = ParentStack.back().get<Stmt>()) |
| 10233 | NodeOrVector = S; |
| 10234 | else |
| 10235 | NodeOrVector = new ast_type_traits::DynTypedNode(ParentStack.back()); |
| 10236 | } else { |
| 10237 | if (!NodeOrVector.template is<ParentVector *>()) { |
| 10238 | auto *Vector = new ParentVector( |
| 10239 | 1, getSingleDynTypedNodeFromParentMap(NodeOrVector)); |
| 10240 | delete NodeOrVector |
| 10241 | .template dyn_cast<ast_type_traits::DynTypedNode *>(); |
| 10242 | NodeOrVector = Vector; |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 10243 | } |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10244 | |
| 10245 | auto *Vector = NodeOrVector.template get<ParentVector *>(); |
| 10246 | // Skip duplicates for types that have memoization data. |
| 10247 | // We must check that the type has memoization data before calling |
| 10248 | // std::find() because DynTypedNode::operator== can't compare all |
| 10249 | // types. |
| 10250 | bool Found = ParentStack.back().getMemoizationData() && |
| 10251 | std::find(Vector->begin(), Vector->end(), |
| 10252 | ParentStack.back()) != Vector->end(); |
| 10253 | if (!Found) |
| 10254 | Vector->push_back(ParentStack.back()); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 10255 | } |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10256 | } |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10257 | ParentStack.push_back(createDynTypedNode(Node)); |
| 10258 | bool Result = BaseTraverse(); |
| 10259 | ParentStack.pop_back(); |
| 10260 | return Result; |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10261 | } |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10262 | |
| 10263 | bool TraverseDecl(Decl *DeclNode) { |
| 10264 | return TraverseNode( |
| 10265 | DeclNode, DeclNode, [&] { return VisitorBase::TraverseDecl(DeclNode); }, |
| 10266 | &Map.PointerParents); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 10267 | } |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10268 | |
| 10269 | bool TraverseStmt(Stmt *StmtNode) { |
| 10270 | return TraverseNode( |
| 10271 | StmtNode, StmtNode, [&] { return VisitorBase::TraverseStmt(StmtNode); }, |
| 10272 | &Map.PointerParents); |
| 10273 | } |
| 10274 | |
| 10275 | bool TraverseTypeLoc(TypeLoc TypeLocNode) { |
| 10276 | return TraverseNode( |
| 10277 | TypeLocNode, ast_type_traits::DynTypedNode::create(TypeLocNode), |
| 10278 | [&] { return VisitorBase::TraverseTypeLoc(TypeLocNode); }, |
| 10279 | &Map.OtherParents); |
| 10280 | } |
| 10281 | |
| 10282 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSLocNode) { |
| 10283 | return TraverseNode( |
| 10284 | NNSLocNode, ast_type_traits::DynTypedNode::create(NNSLocNode), |
| 10285 | [&] { return VisitorBase::TraverseNestedNameSpecifierLoc(NNSLocNode); }, |
| 10286 | &Map.OtherParents); |
| 10287 | } |
| 10288 | |
| 10289 | ParentMap ⤅ |
| 10290 | llvm::SmallVector<ast_type_traits::DynTypedNode, 16> ParentStack; |
| 10291 | }; |
| 10292 | |
| 10293 | ASTContext::ParentMap::ParentMap(ASTContext &Ctx) { |
| 10294 | ASTVisitor(*this).TraverseAST(Ctx); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10295 | } |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10296 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10297 | ASTContext::DynTypedNodeList |
| 10298 | ASTContext::getParents(const ast_type_traits::DynTypedNode &Node) { |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10299 | if (!Parents) |
| 10300 | // We build the parent map for the traversal scope (usually whole TU), as |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10301 | // hasAncestor can escape any subtree. |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10302 | Parents = llvm::make_unique<ParentMap>(*this); |
| 10303 | return Parents->getParents(Node); |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10304 | } |
| 10305 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10306 | bool |
| 10307 | ASTContext::ObjCMethodsAreEqual(const ObjCMethodDecl *MethodDecl, |
| 10308 | const ObjCMethodDecl *MethodImpl) { |
| 10309 | // No point trying to match an unavailable/deprecated mothod. |
| 10310 | if (MethodDecl->hasAttr<UnavailableAttr>() |
| 10311 | || MethodDecl->hasAttr<DeprecatedAttr>()) |
| 10312 | return false; |
| 10313 | if (MethodDecl->getObjCDeclQualifier() != |
| 10314 | MethodImpl->getObjCDeclQualifier()) |
| 10315 | return false; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10316 | if (!hasSameType(MethodDecl->getReturnType(), MethodImpl->getReturnType())) |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10317 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 10318 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10319 | if (MethodDecl->param_size() != MethodImpl->param_size()) |
| 10320 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 10321 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10322 | for (ObjCMethodDecl::param_const_iterator IM = MethodImpl->param_begin(), |
| 10323 | IF = MethodDecl->param_begin(), EM = MethodImpl->param_end(), |
| 10324 | EF = MethodDecl->param_end(); |
| 10325 | IM != EM && IF != EF; ++IM, ++IF) { |
| 10326 | const ParmVarDecl *DeclVar = (*IF); |
| 10327 | const ParmVarDecl *ImplVar = (*IM); |
| 10328 | if (ImplVar->getObjCDeclQualifier() != DeclVar->getObjCDeclQualifier()) |
| 10329 | return false; |
| 10330 | if (!hasSameType(DeclVar->getType(), ImplVar->getType())) |
| 10331 | return false; |
| 10332 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 10333 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10334 | return (MethodDecl->isVariadic() == MethodImpl->isVariadic()); |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10335 | } |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 10336 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 10337 | uint64_t ASTContext::getTargetNullPointerValue(QualType QT) const { |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 10338 | LangAS AS; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 10339 | if (QT->getUnqualifiedDesugaredType()->isNullPtrType()) |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 10340 | AS = LangAS::Default; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 10341 | else |
| 10342 | AS = QT->getPointeeType().getAddressSpace(); |
| 10343 | |
| 10344 | return getTargetInfo().getNullPointerValue(AS); |
| 10345 | } |
| 10346 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 10347 | unsigned ASTContext::getTargetAddressSpace(LangAS AS) const { |
| 10348 | if (isTargetAddressSpace(AS)) |
| 10349 | return toTargetAddressSpace(AS); |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 10350 | else |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 10351 | return (*AddrSpaceMap)[(unsigned)AS]; |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 10352 | } |
| 10353 | |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 10354 | QualType ASTContext::getCorrespondingSaturatedType(QualType Ty) const { |
| 10355 | assert(Ty->isFixedPointType()); |
| 10356 | |
| 10357 | if (Ty->isSaturatedFixedPointType()) return Ty; |
| 10358 | |
| 10359 | const auto &BT = Ty->getAs<BuiltinType>(); |
| 10360 | switch (BT->getKind()) { |
| 10361 | default: |
| 10362 | llvm_unreachable("Not a fixed point type!"); |
| 10363 | case BuiltinType::ShortAccum: |
| 10364 | return SatShortAccumTy; |
| 10365 | case BuiltinType::Accum: |
| 10366 | return SatAccumTy; |
| 10367 | case BuiltinType::LongAccum: |
| 10368 | return SatLongAccumTy; |
| 10369 | case BuiltinType::UShortAccum: |
| 10370 | return SatUnsignedShortAccumTy; |
| 10371 | case BuiltinType::UAccum: |
| 10372 | return SatUnsignedAccumTy; |
| 10373 | case BuiltinType::ULongAccum: |
| 10374 | return SatUnsignedLongAccumTy; |
| 10375 | case BuiltinType::ShortFract: |
| 10376 | return SatShortFractTy; |
| 10377 | case BuiltinType::Fract: |
| 10378 | return SatFractTy; |
| 10379 | case BuiltinType::LongFract: |
| 10380 | return SatLongFractTy; |
| 10381 | case BuiltinType::UShortFract: |
| 10382 | return SatUnsignedShortFractTy; |
| 10383 | case BuiltinType::UFract: |
| 10384 | return SatUnsignedFractTy; |
| 10385 | case BuiltinType::ULongFract: |
| 10386 | return SatUnsignedLongFractTy; |
| 10387 | } |
| 10388 | } |
| 10389 | |
Matt Arsenault | c65f966 | 2018-08-02 12:14:28 +0000 | [diff] [blame] | 10390 | LangAS ASTContext::getLangASForBuiltinAddressSpace(unsigned AS) const { |
| 10391 | if (LangOpts.OpenCL) |
| 10392 | return getTargetInfo().getOpenCLBuiltinAddressSpace(AS); |
| 10393 | |
| 10394 | if (LangOpts.CUDA) |
| 10395 | return getTargetInfo().getCUDABuiltinAddressSpace(AS); |
| 10396 | |
| 10397 | return getLangASFromTargetAS(AS); |
| 10398 | } |
| 10399 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 10400 | // Explicitly instantiate this in case a Redeclarable<T> is used from a TU that |
| 10401 | // doesn't include ASTContext.h |
| 10402 | template |
| 10403 | clang::LazyGenerationalUpdatePtr< |
| 10404 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::ValueType |
| 10405 | clang::LazyGenerationalUpdatePtr< |
| 10406 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::makeValue( |
| 10407 | const clang::ASTContext &Ctx, Decl *Value); |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 10408 | |
| 10409 | unsigned char ASTContext::getFixedPointScale(QualType Ty) const { |
| 10410 | assert(Ty->isFixedPointType()); |
| 10411 | |
| 10412 | const auto *BT = Ty->getAs<BuiltinType>(); |
| 10413 | const TargetInfo &Target = getTargetInfo(); |
| 10414 | switch (BT->getKind()) { |
| 10415 | default: |
| 10416 | llvm_unreachable("Not a fixed point type!"); |
| 10417 | case BuiltinType::ShortAccum: |
| 10418 | case BuiltinType::SatShortAccum: |
| 10419 | return Target.getShortAccumScale(); |
| 10420 | case BuiltinType::Accum: |
| 10421 | case BuiltinType::SatAccum: |
| 10422 | return Target.getAccumScale(); |
| 10423 | case BuiltinType::LongAccum: |
| 10424 | case BuiltinType::SatLongAccum: |
| 10425 | return Target.getLongAccumScale(); |
| 10426 | case BuiltinType::UShortAccum: |
| 10427 | case BuiltinType::SatUShortAccum: |
| 10428 | return Target.getUnsignedShortAccumScale(); |
| 10429 | case BuiltinType::UAccum: |
| 10430 | case BuiltinType::SatUAccum: |
| 10431 | return Target.getUnsignedAccumScale(); |
| 10432 | case BuiltinType::ULongAccum: |
| 10433 | case BuiltinType::SatULongAccum: |
| 10434 | return Target.getUnsignedLongAccumScale(); |
| 10435 | case BuiltinType::ShortFract: |
| 10436 | case BuiltinType::SatShortFract: |
| 10437 | return Target.getShortFractScale(); |
| 10438 | case BuiltinType::Fract: |
| 10439 | case BuiltinType::SatFract: |
| 10440 | return Target.getFractScale(); |
| 10441 | case BuiltinType::LongFract: |
| 10442 | case BuiltinType::SatLongFract: |
| 10443 | return Target.getLongFractScale(); |
| 10444 | case BuiltinType::UShortFract: |
| 10445 | case BuiltinType::SatUShortFract: |
| 10446 | return Target.getUnsignedShortFractScale(); |
| 10447 | case BuiltinType::UFract: |
| 10448 | case BuiltinType::SatUFract: |
| 10449 | return Target.getUnsignedFractScale(); |
| 10450 | case BuiltinType::ULongFract: |
| 10451 | case BuiltinType::SatULongFract: |
| 10452 | return Target.getUnsignedLongFractScale(); |
| 10453 | } |
| 10454 | } |
| 10455 | |
| 10456 | unsigned char ASTContext::getFixedPointIBits(QualType Ty) const { |
| 10457 | assert(Ty->isFixedPointType()); |
| 10458 | |
| 10459 | const auto *BT = Ty->getAs<BuiltinType>(); |
| 10460 | const TargetInfo &Target = getTargetInfo(); |
| 10461 | switch (BT->getKind()) { |
| 10462 | default: |
| 10463 | llvm_unreachable("Not a fixed point type!"); |
| 10464 | case BuiltinType::ShortAccum: |
| 10465 | case BuiltinType::SatShortAccum: |
| 10466 | return Target.getShortAccumIBits(); |
| 10467 | case BuiltinType::Accum: |
| 10468 | case BuiltinType::SatAccum: |
| 10469 | return Target.getAccumIBits(); |
| 10470 | case BuiltinType::LongAccum: |
| 10471 | case BuiltinType::SatLongAccum: |
| 10472 | return Target.getLongAccumIBits(); |
| 10473 | case BuiltinType::UShortAccum: |
| 10474 | case BuiltinType::SatUShortAccum: |
| 10475 | return Target.getUnsignedShortAccumIBits(); |
| 10476 | case BuiltinType::UAccum: |
| 10477 | case BuiltinType::SatUAccum: |
| 10478 | return Target.getUnsignedAccumIBits(); |
| 10479 | case BuiltinType::ULongAccum: |
| 10480 | case BuiltinType::SatULongAccum: |
| 10481 | return Target.getUnsignedLongAccumIBits(); |
| 10482 | case BuiltinType::ShortFract: |
| 10483 | case BuiltinType::SatShortFract: |
| 10484 | case BuiltinType::Fract: |
| 10485 | case BuiltinType::SatFract: |
| 10486 | case BuiltinType::LongFract: |
| 10487 | case BuiltinType::SatLongFract: |
| 10488 | case BuiltinType::UShortFract: |
| 10489 | case BuiltinType::SatUShortFract: |
| 10490 | case BuiltinType::UFract: |
| 10491 | case BuiltinType::SatUFract: |
| 10492 | case BuiltinType::ULongFract: |
| 10493 | case BuiltinType::SatULongFract: |
| 10494 | return 0; |
| 10495 | } |
| 10496 | } |
Leonard Chan | a677942 | 2018-08-06 16:42:37 +0000 | [diff] [blame] | 10497 | |
| 10498 | FixedPointSemantics ASTContext::getFixedPointSemantics(QualType Ty) const { |
Erich Keane | 8e77216 | 2019-01-18 19:31:54 +0000 | [diff] [blame] | 10499 | assert((Ty->isFixedPointType() || Ty->isIntegerType()) && |
| 10500 | "Can only get the fixed point semantics for a " |
| 10501 | "fixed point or integer type."); |
Leonard Chan | 2044ac8 | 2019-01-16 18:13:59 +0000 | [diff] [blame] | 10502 | if (Ty->isIntegerType()) |
| 10503 | return FixedPointSemantics::GetIntegerSemantics(getIntWidth(Ty), |
| 10504 | Ty->isSignedIntegerType()); |
| 10505 | |
Leonard Chan | a677942 | 2018-08-06 16:42:37 +0000 | [diff] [blame] | 10506 | bool isSigned = Ty->isSignedFixedPointType(); |
| 10507 | return FixedPointSemantics( |
| 10508 | static_cast<unsigned>(getTypeSize(Ty)), getFixedPointScale(Ty), isSigned, |
| 10509 | Ty->isSaturatedFixedPointType(), |
| 10510 | !isSigned && getTargetInfo().doUnsignedFixedPointTypesHavePadding()); |
| 10511 | } |
| 10512 | |
| 10513 | APFixedPoint ASTContext::getFixedPointMax(QualType Ty) const { |
| 10514 | assert(Ty->isFixedPointType()); |
| 10515 | return APFixedPoint::getMax(getFixedPointSemantics(Ty)); |
| 10516 | } |
| 10517 | |
| 10518 | APFixedPoint ASTContext::getFixedPointMin(QualType Ty) const { |
| 10519 | assert(Ty->isFixedPointType()); |
| 10520 | return APFixedPoint::getMin(getFixedPointSemantics(Ty)); |
| 10521 | } |
Leonard Chan | 2044ac8 | 2019-01-16 18:13:59 +0000 | [diff] [blame] | 10522 | |
| 10523 | QualType ASTContext::getCorrespondingSignedFixedPointType(QualType Ty) const { |
| 10524 | assert(Ty->isUnsignedFixedPointType() && |
| 10525 | "Expected unsigned fixed point type"); |
| 10526 | const auto *BTy = Ty->getAs<BuiltinType>(); |
| 10527 | |
| 10528 | switch (BTy->getKind()) { |
| 10529 | case BuiltinType::UShortAccum: |
| 10530 | return ShortAccumTy; |
| 10531 | case BuiltinType::UAccum: |
| 10532 | return AccumTy; |
| 10533 | case BuiltinType::ULongAccum: |
| 10534 | return LongAccumTy; |
| 10535 | case BuiltinType::SatUShortAccum: |
| 10536 | return SatShortAccumTy; |
| 10537 | case BuiltinType::SatUAccum: |
| 10538 | return SatAccumTy; |
| 10539 | case BuiltinType::SatULongAccum: |
| 10540 | return SatLongAccumTy; |
| 10541 | case BuiltinType::UShortFract: |
| 10542 | return ShortFractTy; |
| 10543 | case BuiltinType::UFract: |
| 10544 | return FractTy; |
| 10545 | case BuiltinType::ULongFract: |
| 10546 | return LongFractTy; |
| 10547 | case BuiltinType::SatUShortFract: |
| 10548 | return SatShortFractTy; |
| 10549 | case BuiltinType::SatUFract: |
| 10550 | return SatFractTy; |
| 10551 | case BuiltinType::SatULongFract: |
| 10552 | return SatLongFractTy; |
| 10553 | default: |
| 10554 | llvm_unreachable("Unexpected unsigned fixed point type"); |
| 10555 | } |
| 10556 | } |