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" |
Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 15 | #include "Interp/Context.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 16 | #include "clang/AST/APValue.h" |
Saar Raz | fdf80e8 | 2019-12-06 01:30:21 +0200 | [diff] [blame^] | 17 | #include "clang/AST/ASTConcept.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTMutationListener.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTTypeTraits.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 20 | #include "clang/AST/Attr.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 21 | #include "clang/AST/AttrIterator.h" |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 22 | #include "clang/AST/CharUnits.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 23 | #include "clang/AST/Comment.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 24 | #include "clang/AST/Decl.h" |
| 25 | #include "clang/AST/DeclBase.h" |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 26 | #include "clang/AST/DeclCXX.h" |
Chandler Carruth | aa36b89 | 2015-12-30 03:40:23 +0000 | [diff] [blame] | 27 | #include "clang/AST/DeclContextInternals.h" |
Steve Naroff | 67391b8 | 2007-10-01 19:00:59 +0000 | [diff] [blame] | 28 | #include "clang/AST/DeclObjC.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 29 | #include "clang/AST/DeclOpenMP.h" |
Douglas Gregor | ded2d7b | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 30 | #include "clang/AST/DeclTemplate.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 31 | #include "clang/AST/DeclarationName.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 32 | #include "clang/AST/Expr.h" |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 33 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 34 | #include "clang/AST/ExternalASTSource.h" |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 35 | #include "clang/AST/Mangle.h" |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 36 | #include "clang/AST/MangleNumberingContext.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 37 | #include "clang/AST/NestedNameSpecifier.h" |
| 38 | #include "clang/AST/RawCommentList.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 39 | #include "clang/AST/RecordLayout.h" |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 40 | #include "clang/AST/RecursiveASTVisitor.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 41 | #include "clang/AST/Stmt.h" |
| 42 | #include "clang/AST/TemplateBase.h" |
| 43 | #include "clang/AST/TemplateName.h" |
| 44 | #include "clang/AST/Type.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 45 | #include "clang/AST/TypeLoc.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 46 | #include "clang/AST/UnresolvedSet.h" |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 47 | #include "clang/AST/VTableBuilder.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 48 | #include "clang/Basic/AddressSpaces.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 49 | #include "clang/Basic/Builtins.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 50 | #include "clang/Basic/CommentOptions.h" |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 51 | #include "clang/Basic/ExceptionSpecificationType.h" |
Leonard Chan | a677942 | 2018-08-06 16:42:37 +0000 | [diff] [blame] | 52 | #include "clang/Basic/FixedPoint.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 53 | #include "clang/Basic/IdentifierTable.h" |
| 54 | #include "clang/Basic/LLVM.h" |
| 55 | #include "clang/Basic/LangOptions.h" |
| 56 | #include "clang/Basic/Linkage.h" |
| 57 | #include "clang/Basic/ObjCRuntime.h" |
| 58 | #include "clang/Basic/SanitizerBlacklist.h" |
| 59 | #include "clang/Basic/SourceLocation.h" |
Chris Lattner | d286851 | 2009-03-28 03:45:20 +0000 | [diff] [blame] | 60 | #include "clang/Basic/SourceManager.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 61 | #include "clang/Basic/Specifiers.h" |
| 62 | #include "clang/Basic/TargetCXXABI.h" |
Chris Lattner | 4dc8a6f | 2007-05-20 23:50:58 +0000 | [diff] [blame] | 63 | #include "clang/Basic/TargetInfo.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 64 | #include "clang/Basic/XRayLists.h" |
| 65 | #include "llvm/ADT/APInt.h" |
| 66 | #include "llvm/ADT/APSInt.h" |
| 67 | #include "llvm/ADT/ArrayRef.h" |
| 68 | #include "llvm/ADT/DenseMap.h" |
| 69 | #include "llvm/ADT/DenseSet.h" |
| 70 | #include "llvm/ADT/FoldingSet.h" |
| 71 | #include "llvm/ADT/None.h" |
| 72 | #include "llvm/ADT/Optional.h" |
| 73 | #include "llvm/ADT/PointerUnion.h" |
| 74 | #include "llvm/ADT/STLExtras.h" |
| 75 | #include "llvm/ADT/SmallPtrSet.h" |
| 76 | #include "llvm/ADT/SmallVector.h" |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 77 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 78 | #include "llvm/ADT/StringRef.h" |
Robert Lytton | eaf6f36 | 2013-11-12 10:09:34 +0000 | [diff] [blame] | 79 | #include "llvm/ADT/Triple.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 80 | #include "llvm/Support/Capacity.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 81 | #include "llvm/Support/Casting.h" |
| 82 | #include "llvm/Support/Compiler.h" |
| 83 | #include "llvm/Support/ErrorHandling.h" |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 84 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 1402ce3 | 2009-10-24 09:57:09 +0000 | [diff] [blame] | 85 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 86 | #include <algorithm> |
| 87 | #include <cassert> |
| 88 | #include <cstddef> |
| 89 | #include <cstdint> |
| 90 | #include <cstdlib> |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 91 | #include <map> |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 92 | #include <memory> |
| 93 | #include <string> |
| 94 | #include <tuple> |
| 95 | #include <utility> |
Anders Carlsson | a4267a6 | 2009-07-18 21:19:52 +0000 | [diff] [blame] | 96 | |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 97 | using namespace clang; |
| 98 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 99 | enum FloatingRank { |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 100 | Float16Rank, HalfRank, FloatRank, DoubleRank, LongDoubleRank, Float128Rank |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 103 | /// \returns location that is relevant when searching for Doc comments related |
| 104 | /// to \p D. |
| 105 | static SourceLocation getDeclLocForCommentSearch(const Decl *D, |
| 106 | SourceManager &SourceMgr) { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 107 | assert(D); |
| 108 | |
Dmitri Gribenko | df17d64 | 2012-06-28 16:19:39 +0000 | [diff] [blame] | 109 | // User can not attach documentation to implicit declarations. |
| 110 | if (D->isImplicit()) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 111 | return {}; |
Dmitri Gribenko | df17d64 | 2012-06-28 16:19:39 +0000 | [diff] [blame] | 112 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 113 | // User can not attach documentation to implicit instantiations. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 114 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 115 | if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 116 | return {}; |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 119 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 120 | if (VD->isStaticDataMember() && |
| 121 | VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 122 | return {}; |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 125 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(D)) { |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 126 | if (CRD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 127 | return {}; |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 130 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(D)) { |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 131 | TemplateSpecializationKind TSK = CTSD->getSpecializationKind(); |
| 132 | if (TSK == TSK_ImplicitInstantiation || |
| 133 | TSK == TSK_Undeclared) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 134 | return {}; |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 137 | if (const auto *ED = dyn_cast<EnumDecl>(D)) { |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 138 | if (ED->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 139 | return {}; |
Dmitri Gribenko | b1ad993 | 2012-08-20 22:36:31 +0000 | [diff] [blame] | 140 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 141 | if (const auto *TD = dyn_cast<TagDecl>(D)) { |
Fariborz Jahanian | 799a403 | 2013-04-17 21:05:20 +0000 | [diff] [blame] | 142 | // When tag declaration (but not definition!) is part of the |
| 143 | // decl-specifier-seq of some other declaration, it doesn't get comment |
| 144 | if (TD->isEmbeddedInDeclarator() && !TD->isCompleteDefinition()) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 145 | return {}; |
Fariborz Jahanian | 799a403 | 2013-04-17 21:05:20 +0000 | [diff] [blame] | 146 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 147 | // TODO: handle comments for function parameters properly. |
| 148 | if (isa<ParmVarDecl>(D)) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 149 | return {}; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 150 | |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 151 | // TODO: we could look up template parameter documentation in the template |
| 152 | // documentation. |
| 153 | if (isa<TemplateTypeParmDecl>(D) || |
| 154 | isa<NonTypeTemplateParmDecl>(D) || |
| 155 | isa<TemplateTemplateParmDecl>(D)) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 156 | return {}; |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 157 | |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 158 | // Find declaration location. |
| 159 | // For Objective-C declarations we generally don't expect to have multiple |
| 160 | // declarators, thus use declaration starting location as the "declaration |
| 161 | // location". |
| 162 | // For all other declarations multiple declarators are used quite frequently, |
| 163 | // so we use the location of the identifier as the "declaration location". |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 164 | if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) || |
Dmitri Gribenko | 34df220 | 2012-07-31 22:37:06 +0000 | [diff] [blame] | 165 | isa<ObjCPropertyDecl>(D) || |
Dmitri Gribenko | 7f4b377 | 2012-08-02 20:49:51 +0000 | [diff] [blame] | 166 | isa<RedeclarableTemplateDecl>(D) || |
Sam McCall | a433e71 | 2019-11-13 21:30:31 +0100 | [diff] [blame] | 167 | isa<ClassTemplateSpecializationDecl>(D) || |
| 168 | // Allow association with Y across {} in `typedef struct X {} Y`. |
| 169 | isa<TypedefDecl>(D)) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 170 | return D->getBeginLoc(); |
Fariborz Jahanian | b64e95f | 2013-07-24 22:58:51 +0000 | [diff] [blame] | 171 | else { |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 172 | const SourceLocation DeclLoc = D->getLocation(); |
Dmitri Gribenko | ef099dc | 2014-03-27 16:40:51 +0000 | [diff] [blame] | 173 | if (DeclLoc.isMacroID()) { |
| 174 | if (isa<TypedefDecl>(D)) { |
| 175 | // If location of the typedef name is in a macro, it is because being |
| 176 | // declared via a macro. Try using declaration's starting location as |
| 177 | // the "declaration location". |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 178 | return D->getBeginLoc(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 179 | } else if (const auto *TD = dyn_cast<TagDecl>(D)) { |
Dmitri Gribenko | ef099dc | 2014-03-27 16:40:51 +0000 | [diff] [blame] | 180 | // If location of the tag decl is inside a macro, but the spelling of |
| 181 | // the tag name comes from a macro argument, it looks like a special |
| 182 | // macro like NS_ENUM is being used to define the tag decl. In that |
| 183 | // case, adjust the source location to the expansion loc so that we can |
| 184 | // attach the comment to the tag decl. |
| 185 | if (SourceMgr.isMacroArgExpansion(DeclLoc) && |
| 186 | TD->isCompleteDefinition()) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 187 | return SourceMgr.getExpansionLoc(DeclLoc); |
Dmitri Gribenko | ef099dc | 2014-03-27 16:40:51 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 190 | return DeclLoc; |
Fariborz Jahanian | b64e95f | 2013-07-24 22:58:51 +0000 | [diff] [blame] | 191 | } |
Dmitri Gribenko | e7bb944 | 2012-07-13 01:06:46 +0000 | [diff] [blame] | 192 | |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 193 | return {}; |
| 194 | } |
| 195 | |
| 196 | RawComment *ASTContext::getRawCommentForDeclNoCacheImpl( |
| 197 | const Decl *D, const SourceLocation RepresentativeLocForDecl, |
| 198 | const std::map<unsigned, RawComment *> &CommentsInTheFile) const { |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 199 | // If the declaration doesn't map directly to a location in a file, we |
| 200 | // can't find the comment. |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 201 | if (RepresentativeLocForDecl.isInvalid() || |
| 202 | !RepresentativeLocForDecl.isFileID()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 203 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 204 | |
Jan Korous | 6644d01 | 2019-04-10 20:23:33 +0000 | [diff] [blame] | 205 | // If there are no comments anywhere, we won't find anything. |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 206 | if (CommentsInTheFile.empty()) |
Jan Korous | 6644d01 | 2019-04-10 20:23:33 +0000 | [diff] [blame] | 207 | return nullptr; |
| 208 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 209 | // Decompose the location for the declaration and find the beginning of the |
| 210 | // file buffer. |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 211 | const std::pair<FileID, unsigned> DeclLocDecomp = |
| 212 | SourceMgr.getDecomposedLoc(RepresentativeLocForDecl); |
| 213 | |
| 214 | // Slow path. |
| 215 | auto OffsetCommentBehindDecl = |
| 216 | CommentsInTheFile.lower_bound(DeclLocDecomp.second); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 217 | |
| 218 | // First check whether we have a trailing comment. |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 219 | if (OffsetCommentBehindDecl != CommentsInTheFile.end()) { |
| 220 | RawComment *CommentBehindDecl = OffsetCommentBehindDecl->second; |
| 221 | if ((CommentBehindDecl->isDocumentation() || |
| 222 | LangOpts.CommentOpts.ParseAllComments) && |
| 223 | CommentBehindDecl->isTrailingComment() && |
| 224 | (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D) || |
| 225 | isa<ObjCMethodDecl>(D) || isa<ObjCPropertyDecl>(D))) { |
| 226 | |
| 227 | // Check that Doxygen trailing comment comes after the declaration, starts |
| 228 | // on the same line and in the same file as the declaration. |
| 229 | if (SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second) == |
| 230 | Comments.getCommentBeginLine(CommentBehindDecl, DeclLocDecomp.first, |
| 231 | OffsetCommentBehindDecl->first)) { |
| 232 | return CommentBehindDecl; |
| 233 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
| 237 | // The comment just after the declaration was not a trailing comment. |
| 238 | // Let's look at the previous comment. |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 239 | if (OffsetCommentBehindDecl == CommentsInTheFile.begin()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 240 | return nullptr; |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 241 | |
| 242 | auto OffsetCommentBeforeDecl = --OffsetCommentBehindDecl; |
| 243 | RawComment *CommentBeforeDecl = OffsetCommentBeforeDecl->second; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 244 | |
| 245 | // Check that we actually have a non-member Doxygen comment. |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 246 | if (!(CommentBeforeDecl->isDocumentation() || |
David L. Jones | 13d5a87 | 2018-03-02 00:07:45 +0000 | [diff] [blame] | 247 | LangOpts.CommentOpts.ParseAllComments) || |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 248 | CommentBeforeDecl->isTrailingComment()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 249 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 250 | |
| 251 | // Decompose the end of the comment. |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 252 | const unsigned CommentEndOffset = |
| 253 | Comments.getCommentEndOffset(CommentBeforeDecl); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 254 | |
| 255 | // Get the corresponding buffer. |
| 256 | bool Invalid = false; |
| 257 | const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first, |
| 258 | &Invalid).data(); |
| 259 | if (Invalid) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 260 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 261 | |
| 262 | // Extract text between the comment and declaration. |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 263 | StringRef Text(Buffer + CommentEndOffset, |
| 264 | DeclLocDecomp.second - CommentEndOffset); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 265 | |
Dmitri Gribenko | 7e8729b | 2012-06-27 23:43:37 +0000 | [diff] [blame] | 266 | // There should be no other declarations or preprocessor directives between |
| 267 | // comment and declaration. |
Argyrios Kyrtzidis | b534d3a | 2013-07-26 18:38:12 +0000 | [diff] [blame] | 268 | if (Text.find_first_of(";{}#@") != StringRef::npos) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 269 | return nullptr; |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 270 | |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 271 | return CommentBeforeDecl; |
| 272 | } |
| 273 | |
| 274 | RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { |
| 275 | const SourceLocation DeclLoc = getDeclLocForCommentSearch(D, SourceMgr); |
| 276 | |
| 277 | // If the declaration doesn't map directly to a location in a file, we |
| 278 | // can't find the comment. |
| 279 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) |
| 280 | return nullptr; |
| 281 | |
| 282 | if (ExternalSource && !CommentsLoaded) { |
| 283 | ExternalSource->ReadComments(); |
| 284 | CommentsLoaded = true; |
| 285 | } |
| 286 | |
| 287 | if (Comments.empty()) |
| 288 | return nullptr; |
| 289 | |
| 290 | const FileID File = SourceMgr.getDecomposedLoc(DeclLoc).first; |
| 291 | const auto CommentsInThisFile = Comments.getCommentsInFile(File); |
| 292 | if (!CommentsInThisFile || CommentsInThisFile->empty()) |
| 293 | return nullptr; |
| 294 | |
| 295 | return getRawCommentForDeclNoCacheImpl(D, DeclLoc, *CommentsInThisFile); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 298 | /// If we have a 'templated' declaration for a template, adjust 'D' to |
| 299 | /// refer to the actual template. |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 300 | /// If we have an implicit instantiation, adjust 'D' to refer to template. |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 301 | static const Decl &adjustDeclToTemplate(const Decl &D) { |
| 302 | if (const auto *FD = dyn_cast<FunctionDecl>(&D)) { |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 303 | // Is this function declaration part of a function template? |
Douglas Gregor | 35ceb27 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 304 | if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate()) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 305 | return *FTD; |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 306 | |
| 307 | // Nothing to do if function is not an implicit instantiation. |
| 308 | if (FD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
| 309 | return D; |
| 310 | |
| 311 | // Function is an implicit instantiation of a function template? |
| 312 | if (const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate()) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 313 | return *FTD; |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 314 | |
| 315 | // Function is instantiated from a member definition of a class template? |
| 316 | if (const FunctionDecl *MemberDecl = |
| 317 | FD->getInstantiatedFromMemberFunction()) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 318 | return *MemberDecl; |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 319 | |
| 320 | return D; |
Douglas Gregor | 35ceb27 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 321 | } |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 322 | if (const auto *VD = dyn_cast<VarDecl>(&D)) { |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 323 | // Static data member is instantiated from a member definition of a class |
| 324 | // template? |
| 325 | if (VD->isStaticDataMember()) |
| 326 | if (const VarDecl *MemberDecl = VD->getInstantiatedFromStaticDataMember()) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 327 | return *MemberDecl; |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 328 | |
| 329 | return D; |
| 330 | } |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 331 | if (const auto *CRD = dyn_cast<CXXRecordDecl>(&D)) { |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 332 | // Is this class declaration part of a class template? |
| 333 | if (const ClassTemplateDecl *CTD = CRD->getDescribedClassTemplate()) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 334 | return *CTD; |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 335 | |
| 336 | // Class is an implicit instantiation of a class template or partial |
| 337 | // specialization? |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 338 | if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(CRD)) { |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 339 | if (CTSD->getSpecializationKind() != TSK_ImplicitInstantiation) |
| 340 | return D; |
| 341 | llvm::PointerUnion<ClassTemplateDecl *, |
| 342 | ClassTemplatePartialSpecializationDecl *> |
| 343 | PU = CTSD->getSpecializedTemplateOrPartial(); |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 344 | return PU.is<ClassTemplateDecl *>() |
| 345 | ? *static_cast<const Decl *>(PU.get<ClassTemplateDecl *>()) |
| 346 | : *static_cast<const Decl *>( |
| 347 | PU.get<ClassTemplatePartialSpecializationDecl *>()); |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // Class is instantiated from a member definition of a class template? |
| 351 | if (const MemberSpecializationInfo *Info = |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 352 | CRD->getMemberSpecializationInfo()) |
| 353 | return *Info->getInstantiatedFrom(); |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 354 | |
| 355 | return D; |
| 356 | } |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 357 | if (const auto *ED = dyn_cast<EnumDecl>(&D)) { |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 358 | // Enum is instantiated from a member definition of a class template? |
| 359 | if (const EnumDecl *MemberDecl = ED->getInstantiatedFromMemberEnum()) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 360 | return *MemberDecl; |
Dmitri Gribenko | 9063180 | 2012-08-22 17:44:32 +0000 | [diff] [blame] | 361 | |
| 362 | return D; |
| 363 | } |
| 364 | // FIXME: Adjust alias templates? |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 365 | return D; |
| 366 | } |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 367 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 368 | const RawComment *ASTContext::getRawCommentForAnyRedecl( |
| 369 | const Decl *D, |
| 370 | const Decl **OriginalDecl) const { |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 371 | if (!D) { |
| 372 | if (OriginalDecl) |
| 373 | OriginalDecl = nullptr; |
| 374 | return nullptr; |
| 375 | } |
Douglas Gregor | 35ceb27 | 2012-08-13 16:37:30 +0000 | [diff] [blame] | 376 | |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 377 | D = &adjustDeclToTemplate(*D); |
| 378 | |
| 379 | // Any comment directly attached to D? |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 380 | { |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 381 | auto DeclComment = DeclRawComments.find(D); |
| 382 | if (DeclComment != DeclRawComments.end()) { |
| 383 | if (OriginalDecl) |
| 384 | *OriginalDecl = D; |
| 385 | return DeclComment->second; |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 386 | } |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 387 | } |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 388 | |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 389 | // Any comment attached to any redeclaration of D? |
| 390 | const Decl *CanonicalD = D->getCanonicalDecl(); |
| 391 | if (!CanonicalD) |
| 392 | return nullptr; |
| 393 | |
| 394 | { |
| 395 | auto RedeclComment = RedeclChainComments.find(CanonicalD); |
| 396 | if (RedeclComment != RedeclChainComments.end()) { |
| 397 | if (OriginalDecl) |
| 398 | *OriginalDecl = RedeclComment->second; |
| 399 | auto CommentAtRedecl = DeclRawComments.find(RedeclComment->second); |
| 400 | assert(CommentAtRedecl != DeclRawComments.end() && |
| 401 | "This decl is supposed to have comment attached."); |
| 402 | return CommentAtRedecl->second; |
Dmitri Gribenko | a43ec18 | 2012-08-11 00:51:43 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 406 | // Any redeclarations of D that we haven't checked for comments yet? |
| 407 | // We can't use DenseMap::iterator directly since it'd get invalid. |
| 408 | auto LastCheckedRedecl = [this, CanonicalD]() -> const Decl * { |
| 409 | auto LookupRes = CommentlessRedeclChains.find(CanonicalD); |
| 410 | if (LookupRes != CommentlessRedeclChains.end()) |
| 411 | return LookupRes->second; |
| 412 | return nullptr; |
| 413 | }(); |
| 414 | |
| 415 | for (const auto Redecl : D->redecls()) { |
| 416 | assert(Redecl); |
| 417 | // Skip all redeclarations that have been checked previously. |
| 418 | if (LastCheckedRedecl) { |
| 419 | if (LastCheckedRedecl == Redecl) { |
| 420 | LastCheckedRedecl = nullptr; |
| 421 | } |
| 422 | continue; |
| 423 | } |
| 424 | const RawComment *RedeclComment = getRawCommentForDeclNoCache(Redecl); |
| 425 | if (RedeclComment) { |
| 426 | cacheRawCommentForDecl(*Redecl, *RedeclComment); |
| 427 | if (OriginalDecl) |
| 428 | *OriginalDecl = Redecl; |
| 429 | return RedeclComment; |
| 430 | } |
| 431 | CommentlessRedeclChains[CanonicalD] = Redecl; |
| 432 | } |
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) |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 435 | *OriginalDecl = nullptr; |
| 436 | return nullptr; |
| 437 | } |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 438 | |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 439 | void ASTContext::cacheRawCommentForDecl(const Decl &OriginalD, |
| 440 | const RawComment &Comment) const { |
| 441 | assert(Comment.isDocumentation() || LangOpts.CommentOpts.ParseAllComments); |
| 442 | DeclRawComments.try_emplace(&OriginalD, &Comment); |
| 443 | const Decl *const CanonicalDecl = OriginalD.getCanonicalDecl(); |
| 444 | RedeclChainComments.try_emplace(CanonicalDecl, &OriginalD); |
| 445 | CommentlessRedeclChains.erase(CanonicalDecl); |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 448 | static void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod, |
| 449 | SmallVectorImpl<const NamedDecl *> &Redeclared) { |
| 450 | const DeclContext *DC = ObjCMethod->getDeclContext(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 451 | if (const auto *IMD = dyn_cast<ObjCImplDecl>(DC)) { |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 452 | const ObjCInterfaceDecl *ID = IMD->getClassInterface(); |
| 453 | if (!ID) |
| 454 | return; |
| 455 | // Add redeclared method here. |
Aaron Ballman | b4a5345 | 2014-03-13 21:57:01 +0000 | [diff] [blame] | 456 | for (const auto *Ext : ID->known_extensions()) { |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 457 | if (ObjCMethodDecl *RedeclaredMethod = |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 458 | Ext->getMethod(ObjCMethod->getSelector(), |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 459 | ObjCMethod->isInstanceMethod())) |
| 460 | Redeclared.push_back(RedeclaredMethod); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 465 | void ASTContext::attachCommentsToJustParsedDecls(ArrayRef<Decl *> Decls, |
| 466 | const Preprocessor *PP) { |
| 467 | if (Comments.empty() || Decls.empty()) |
| 468 | return; |
| 469 | |
| 470 | // See if there are any new comments that are not attached to a decl. |
| 471 | // The location doesn't have to be precise - we care only about the file. |
| 472 | const FileID File = |
| 473 | SourceMgr.getDecomposedLoc((*Decls.begin())->getLocation()).first; |
| 474 | auto CommentsInThisFile = Comments.getCommentsInFile(File); |
| 475 | if (!CommentsInThisFile || CommentsInThisFile->empty() || |
| 476 | CommentsInThisFile->rbegin()->second->isAttached()) |
| 477 | return; |
| 478 | |
| 479 | // There is at least one comment not attached to a decl. |
| 480 | // Maybe it should be attached to one of Decls? |
| 481 | // |
| 482 | // Note that this way we pick up not only comments that precede the |
| 483 | // declaration, but also comments that *follow* the declaration -- thanks to |
| 484 | // the lookahead in the lexer: we've consumed the semicolon and looked |
| 485 | // ahead through comments. |
| 486 | |
| 487 | for (const Decl *D : Decls) { |
| 488 | assert(D); |
| 489 | if (D->isInvalidDecl()) |
| 490 | continue; |
| 491 | |
| 492 | D = &adjustDeclToTemplate(*D); |
| 493 | |
| 494 | const SourceLocation DeclLoc = getDeclLocForCommentSearch(D, SourceMgr); |
| 495 | |
| 496 | if (DeclLoc.isInvalid() || !DeclLoc.isFileID()) |
| 497 | continue; |
| 498 | |
| 499 | if (DeclRawComments.count(D) > 0) |
| 500 | continue; |
| 501 | |
| 502 | if (RawComment *const DocComment = |
| 503 | getRawCommentForDeclNoCacheImpl(D, DeclLoc, *CommentsInThisFile)) { |
| 504 | cacheRawCommentForDecl(*D, *DocComment); |
| 505 | comments::FullComment *FC = DocComment->parse(*this, PP, D); |
| 506 | ParsedComments[D->getCanonicalDecl()] = FC; |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 511 | comments::FullComment *ASTContext::cloneFullComment(comments::FullComment *FC, |
| 512 | const Decl *D) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 513 | auto *ThisDeclInfo = new (*this) comments::DeclInfo; |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 514 | ThisDeclInfo->CommentDecl = D; |
| 515 | ThisDeclInfo->IsFilled = false; |
| 516 | ThisDeclInfo->fill(); |
| 517 | ThisDeclInfo->CommentDecl = FC->getDecl(); |
Argyrios Kyrtzidis | 7daabbd | 2014-04-27 22:53:03 +0000 | [diff] [blame] | 518 | if (!ThisDeclInfo->TemplateParameters) |
| 519 | ThisDeclInfo->TemplateParameters = FC->getDeclInfo()->TemplateParameters; |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 520 | comments::FullComment *CFC = |
| 521 | new (*this) comments::FullComment(FC->getBlocks(), |
| 522 | ThisDeclInfo); |
| 523 | return CFC; |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Richard Smith | b39b9d5 | 2013-05-21 05:24:00 +0000 | [diff] [blame] | 526 | comments::FullComment *ASTContext::getLocalCommentForDeclUncached(const Decl *D) const { |
| 527 | const RawComment *RC = getRawCommentForDeclNoCache(D); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 528 | return RC ? RC->parse(*this, nullptr, D) : nullptr; |
Richard Smith | b39b9d5 | 2013-05-21 05:24:00 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 531 | comments::FullComment *ASTContext::getCommentForDecl( |
| 532 | const Decl *D, |
| 533 | const Preprocessor *PP) const { |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 534 | if (!D || D->isInvalidDecl()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 535 | return nullptr; |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 536 | D = &adjustDeclToTemplate(*D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 537 | |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 538 | const Decl *Canonical = D->getCanonicalDecl(); |
| 539 | llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos = |
| 540 | ParsedComments.find(Canonical); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 541 | |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 542 | if (Pos != ParsedComments.end()) { |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 543 | if (Canonical != D) { |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 544 | comments::FullComment *FC = Pos->second; |
Fariborz Jahanian | 42e3132 | 2012-10-11 23:52:50 +0000 | [diff] [blame] | 545 | comments::FullComment *CFC = cloneFullComment(FC, D); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 546 | return CFC; |
| 547 | } |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 548 | return Pos->second; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 549 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 550 | |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 551 | const Decl *OriginalDecl = nullptr; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 552 | |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 553 | const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 554 | if (!RC) { |
| 555 | if (isa<ObjCMethodDecl>(D) || isa<FunctionDecl>(D)) { |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 556 | SmallVector<const NamedDecl*, 8> Overridden; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 557 | const auto *OMD = dyn_cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 558 | if (OMD && OMD->isPropertyAccessor()) |
| 559 | if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl()) |
| 560 | if (comments::FullComment *FC = getCommentForDecl(PDecl, PP)) |
| 561 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 37494a1 | 2013-01-12 00:28:34 +0000 | [diff] [blame] | 562 | if (OMD) |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 563 | addRedeclaredMethods(OMD, Overridden); |
| 564 | getOverriddenMethods(dyn_cast<NamedDecl>(D), Overridden); |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 565 | for (unsigned i = 0, e = Overridden.size(); i < e; i++) |
| 566 | if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP)) |
| 567 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 568 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 569 | else if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 570 | // Attach any tag type's documentation to its typedef if latter |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 571 | // does not have one of its own. |
Fariborz Jahanian | 40abf34 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 572 | QualType QT = TD->getUnderlyingType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 573 | if (const auto *TT = QT->getAs<TagType>()) |
Dmitri Gribenko | 01b0651 | 2013-01-27 21:18:39 +0000 | [diff] [blame] | 574 | if (const Decl *TD = TT->getDecl()) |
| 575 | if (comments::FullComment *FC = getCommentForDecl(TD, PP)) |
Fariborz Jahanian | 66024d0 | 2013-01-25 23:08:39 +0000 | [diff] [blame] | 576 | return cloneFullComment(FC, D); |
Fariborz Jahanian | 40abf34 | 2013-01-25 22:48:32 +0000 | [diff] [blame] | 577 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 578 | else if (const auto *IC = dyn_cast<ObjCInterfaceDecl>(D)) { |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 579 | while (IC->getSuperClass()) { |
| 580 | IC = IC->getSuperClass(); |
| 581 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
| 582 | return cloneFullComment(FC, D); |
| 583 | } |
| 584 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 585 | else if (const auto *CD = dyn_cast<ObjCCategoryDecl>(D)) { |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 586 | if (const ObjCInterfaceDecl *IC = CD->getClassInterface()) |
| 587 | if (comments::FullComment *FC = getCommentForDecl(IC, PP)) |
| 588 | return cloneFullComment(FC, D); |
| 589 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 590 | else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 591 | if (!(RD = RD->getDefinition())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 592 | return nullptr; |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 593 | // Check non-virtual bases. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 594 | for (const auto &I : RD->bases()) { |
| 595 | if (I.isVirtual() || (I.getAccessSpecifier() != AS_public)) |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 596 | continue; |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 597 | QualType Ty = I.getType(); |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 598 | if (Ty.isNull()) |
| 599 | continue; |
| 600 | if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) { |
| 601 | if (!(NonVirtualBase= NonVirtualBase->getDefinition())) |
| 602 | continue; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 603 | |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 604 | if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP)) |
| 605 | return cloneFullComment(FC, D); |
| 606 | } |
| 607 | } |
| 608 | // Check virtual bases. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 609 | for (const auto &I : RD->vbases()) { |
| 610 | if (I.getAccessSpecifier() != AS_public) |
Fariborz Jahanian | 5a2e4a2 | 2013-04-26 23:34:36 +0000 | [diff] [blame] | 611 | continue; |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 612 | QualType Ty = I.getType(); |
Fariborz Jahanian | e970c1b | 2013-04-26 20:55:38 +0000 | [diff] [blame] | 613 | if (Ty.isNull()) |
| 614 | continue; |
| 615 | if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) { |
| 616 | if (!(VirtualBase= VirtualBase->getDefinition())) |
| 617 | continue; |
| 618 | if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP)) |
| 619 | return cloneFullComment(FC, D); |
| 620 | } |
| 621 | } |
| 622 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 623 | return nullptr; |
Fariborz Jahanian | 1c883b9 | 2012-10-10 18:34:52 +0000 | [diff] [blame] | 624 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 625 | |
Dmitri Gribenko | bfda9f7 | 2012-08-22 18:12:19 +0000 | [diff] [blame] | 626 | // If the RawComment was attached to other redeclaration of this Decl, we |
| 627 | // should parse the comment in context of that other Decl. This is important |
| 628 | // because comments can contain references to parameter names which can be |
| 629 | // different across redeclarations. |
Jan Korous | f31d8df | 2019-08-13 18:11:44 +0000 | [diff] [blame] | 630 | if (D != OriginalDecl && OriginalDecl) |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 631 | return getCommentForDecl(OriginalDecl, PP); |
Dmitri Gribenko | 4ae66a3 | 2012-08-16 18:19:43 +0000 | [diff] [blame] | 632 | |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 633 | comments::FullComment *FC = RC->parse(*this, PP, D); |
Dmitri Gribenko | b261088 | 2012-08-14 17:17:18 +0000 | [diff] [blame] | 634 | ParsedComments[Canonical] = FC; |
| 635 | return FC; |
Dmitri Gribenko | ec92531 | 2012-07-06 00:28:32 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 638 | void |
| 639 | ASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 640 | TemplateTemplateParmDecl *Parm) { |
| 641 | ID.AddInteger(Parm->getDepth()); |
| 642 | ID.AddInteger(Parm->getPosition()); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 643 | ID.AddBoolean(Parm->isParameterPack()); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 644 | |
| 645 | TemplateParameterList *Params = Parm->getTemplateParameters(); |
| 646 | ID.AddInteger(Params->size()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 647 | for (TemplateParameterList::const_iterator P = Params->begin(), |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 648 | PEnd = Params->end(); |
| 649 | P != PEnd; ++P) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 650 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 651 | ID.AddInteger(0); |
| 652 | ID.AddBoolean(TTP->isParameterPack()); |
| 653 | continue; |
| 654 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 655 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 656 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 657 | ID.AddInteger(1); |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 658 | ID.AddBoolean(NTTP->isParameterPack()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 659 | ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr()); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 660 | if (NTTP->isExpandedParameterPack()) { |
| 661 | ID.AddBoolean(true); |
| 662 | ID.AddInteger(NTTP->getNumExpansionTypes()); |
Eli Friedman | 205a429 | 2012-03-07 01:09:33 +0000 | [diff] [blame] | 663 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 664 | QualType T = NTTP->getExpansionType(I); |
| 665 | ID.AddPointer(T.getCanonicalType().getAsOpaquePtr()); |
| 666 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 667 | } else |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 668 | ID.AddBoolean(false); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 669 | continue; |
| 670 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 671 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 672 | auto *TTP = cast<TemplateTemplateParmDecl>(*P); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 673 | ID.AddInteger(2); |
| 674 | Profile(ID, TTP); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | TemplateTemplateParmDecl * |
| 679 | ASTContext::getCanonicalTemplateTemplateParmDecl( |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 680 | TemplateTemplateParmDecl *TTP) const { |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 681 | // Check if we already have a canonical template template parameter. |
| 682 | llvm::FoldingSetNodeID ID; |
| 683 | CanonicalTemplateTemplateParm::Profile(ID, TTP); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 684 | void *InsertPos = nullptr; |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 685 | CanonicalTemplateTemplateParm *Canonical |
| 686 | = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
| 687 | if (Canonical) |
| 688 | return Canonical->getParam(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 689 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 690 | // Build a canonical template parameter list. |
| 691 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 692 | SmallVector<NamedDecl *, 4> CanonParams; |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 693 | CanonParams.reserve(Params->size()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 694 | for (TemplateParameterList::const_iterator P = Params->begin(), |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 695 | PEnd = Params->end(); |
| 696 | P != PEnd; ++P) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 697 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 698 | CanonParams.push_back( |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 699 | TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 700 | SourceLocation(), |
| 701 | SourceLocation(), |
| 702 | TTP->getDepth(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 703 | TTP->getIndex(), nullptr, false, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 704 | TTP->isParameterPack())); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 705 | else if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 706 | QualType T = getCanonicalType(NTTP->getType()); |
| 707 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 708 | NonTypeTemplateParmDecl *Param; |
| 709 | if (NTTP->isExpandedParameterPack()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 710 | SmallVector<QualType, 2> ExpandedTypes; |
| 711 | SmallVector<TypeSourceInfo *, 2> ExpandedTInfos; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 712 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 713 | ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I))); |
| 714 | ExpandedTInfos.push_back( |
| 715 | getTrivialTypeSourceInfo(ExpandedTypes.back())); |
| 716 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 717 | |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 718 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 719 | SourceLocation(), |
| 720 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 721 | NTTP->getDepth(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 722 | NTTP->getPosition(), nullptr, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 723 | T, |
| 724 | TInfo, |
David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 725 | ExpandedTypes, |
| 726 | ExpandedTInfos); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 727 | } else { |
| 728 | Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 729 | SourceLocation(), |
| 730 | SourceLocation(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 731 | NTTP->getDepth(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 732 | NTTP->getPosition(), nullptr, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 733 | T, |
| 734 | NTTP->isParameterPack(), |
| 735 | TInfo); |
| 736 | } |
| 737 | CanonParams.push_back(Param); |
| 738 | |
| 739 | } else |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 740 | CanonParams.push_back(getCanonicalTemplateTemplateParmDecl( |
| 741 | cast<TemplateTemplateParmDecl>(*P))); |
| 742 | } |
| 743 | |
Saar Raz | 0330fba | 2019-10-15 18:44:06 +0000 | [diff] [blame] | 744 | assert(!TTP->getTemplateParameters()->getRequiresClause() && |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 745 | "Unexpected requires-clause on template template-parameter"); |
George Burgess IV | b7e4e48 | 2016-08-25 01:54:37 +0000 | [diff] [blame] | 746 | Expr *const CanonRequiresClause = nullptr; |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 747 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 748 | TemplateTemplateParmDecl *CanonTTP |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 749 | = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(), |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 750 | SourceLocation(), TTP->getDepth(), |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 751 | TTP->getPosition(), |
Douglas Gregor | f550077 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 752 | TTP->isParameterPack(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 753 | nullptr, |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 754 | TemplateParameterList::Create(*this, SourceLocation(), |
| 755 | SourceLocation(), |
David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 756 | CanonParams, |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 757 | SourceLocation(), |
| 758 | CanonRequiresClause)); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 759 | |
| 760 | // Get the new insert position for the node we care about. |
| 761 | Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 762 | assert(!Canonical && "Shouldn't be in the map!"); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 763 | (void)Canonical; |
| 764 | |
| 765 | // Create the canonical template template parameter entry. |
| 766 | Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP); |
| 767 | CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos); |
| 768 | return CanonTTP; |
| 769 | } |
| 770 | |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 771 | CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 772 | if (!LangOpts.CPlusPlus) return nullptr; |
John McCall | 8635341 | 2010-08-21 22:46:04 +0000 | [diff] [blame] | 773 | |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 774 | switch (T.getCXXABI().getKind()) { |
Petr Hosek | 9c3f9b9 | 2019-09-06 18:59:43 -0700 | [diff] [blame] | 775 | case TargetCXXABI::Fuchsia: |
Joerg Sonnenberger | daa13aa | 2014-05-13 11:20:16 +0000 | [diff] [blame] | 776 | case TargetCXXABI::GenericARM: // Same as Itanium at this level |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 777 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 778 | case TargetCXXABI::iOS64: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 779 | case TargetCXXABI::WatchOS: |
Joerg Sonnenberger | daa13aa | 2014-05-13 11:20:16 +0000 | [diff] [blame] | 780 | case TargetCXXABI::GenericAArch64: |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 781 | case TargetCXXABI::GenericMIPS: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 782 | case TargetCXXABI::GenericItanium: |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 783 | case TargetCXXABI::WebAssembly: |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 784 | return CreateItaniumCXXABI(*this); |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 785 | case TargetCXXABI::Microsoft: |
Charles Davis | 6bcb07a | 2010-08-19 02:18:14 +0000 | [diff] [blame] | 786 | return CreateMicrosoftCXXABI(*this); |
| 787 | } |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 788 | llvm_unreachable("Invalid CXXABI type!"); |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Nandor Licker | 950b70d | 2019-09-13 09:46:16 +0000 | [diff] [blame] | 791 | interp::Context &ASTContext::getInterpContext() { |
| 792 | if (!InterpContext) { |
| 793 | InterpContext.reset(new interp::Context(*this)); |
| 794 | } |
| 795 | return *InterpContext.get(); |
| 796 | } |
| 797 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 798 | static const LangASMap *getAddressSpaceMap(const TargetInfo &T, |
| 799 | const LangOptions &LOpts) { |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 800 | if (LOpts.FakeAddressSpaceMap) { |
| 801 | // The fake address space map must have a distinct entry for each |
| 802 | // language-specific address space. |
| 803 | static const unsigned FakeAddrSpaceMap[] = { |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 804 | 0, // Default |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 805 | 1, // opencl_global |
Egor Churaev | 28f00aa | 2016-12-23 16:11:25 +0000 | [diff] [blame] | 806 | 3, // opencl_local |
| 807 | 2, // opencl_constant |
Yaxun Liu | b7318e0 | 2017-10-13 03:37:48 +0000 | [diff] [blame] | 808 | 0, // opencl_private |
Anastasia Stulova | 2c8dcfb | 2014-11-26 14:10:06 +0000 | [diff] [blame] | 809 | 4, // opencl_generic |
| 810 | 5, // cuda_device |
| 811 | 6, // cuda_constant |
| 812 | 7 // cuda_shared |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 813 | }; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 814 | return &FakeAddrSpaceMap; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 815 | } else { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 816 | return &T.getAddressSpaceMap(); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 820 | static bool isAddrSpaceMapManglingEnabled(const TargetInfo &TI, |
| 821 | const LangOptions &LangOpts) { |
| 822 | switch (LangOpts.getAddressSpaceMapMangling()) { |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 823 | case LangOptions::ASMM_Target: |
| 824 | return TI.useAddressSpaceMapMangling(); |
| 825 | case LangOptions::ASMM_On: |
| 826 | return true; |
| 827 | case LangOptions::ASMM_Off: |
| 828 | return false; |
| 829 | } |
NAKAMURA Takumi | 5c81ca4 | 2013-09-13 17:12:09 +0000 | [diff] [blame] | 830 | llvm_unreachable("getAddressSpaceMapMangling() doesn't cover anything."); |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Alexey Samsonov | 0b15e34 | 2014-10-15 22:17:27 +0000 | [diff] [blame] | 833 | ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM, |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 834 | IdentifierTable &idents, SelectorTable &sels, |
Alp Toker | 0804343 | 2014-05-03 03:46:04 +0000 | [diff] [blame] | 835 | Builtin::Context &builtins) |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 836 | : ConstantArrayTypes(this_()), FunctionProtoTypes(this_()), |
| 837 | TemplateSpecializationTypes(this_()), |
Alexey Samsonov | 0b15e34 | 2014-10-15 22:17:27 +0000 | [diff] [blame] | 838 | DependentTemplateSpecializationTypes(this_()), |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 839 | SubstTemplateTemplateParmPacks(this_()), SourceMgr(SM), LangOpts(LOpts), |
Ilya Biryukov | df7ea71 | 2019-10-09 09:40:22 +0000 | [diff] [blame] | 840 | SanitizerBL(new SanitizerBlacklist(LangOpts.SanitizerBlacklistFiles, SM)), |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 841 | XRayFilter(new XRayFunctionFilter(LangOpts.XRayAlwaysInstrumentFiles, |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame] | 842 | LangOpts.XRayNeverInstrumentFiles, |
| 843 | LangOpts.XRayAttrListFiles, SM)), |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 844 | PrintingPolicy(LOpts), Idents(idents), Selectors(sels), |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 845 | BuiltinInfo(builtins), DeclarationNames(*this), Comments(SM), |
Eric Fiselier | 0683c0e | 2018-05-07 21:07:10 +0000 | [diff] [blame] | 846 | CommentCommandTraits(BumpAlloc, LOpts.CommentOpts), |
| 847 | CompCategories(this_()), LastSDM(nullptr, 0) { |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 848 | TUDecl = TranslationUnitDecl::Create(*this); |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 849 | TraversalScope = {TUDecl}; |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 852 | ASTContext::~ASTContext() { |
Ted Kremenek | da4e0d3 | 2010-02-11 07:12:28 +0000 | [diff] [blame] | 853 | // Release the DenseMaps associated with DeclContext objects. |
| 854 | // FIXME: Is this the ideal solution? |
| 855 | ReleaseDeclContextMaps(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 856 | |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 857 | // Call all of the deallocation functions on all of their targets. |
Chandler Carruth | ff5a01a | 2015-12-30 03:00:23 +0000 | [diff] [blame] | 858 | for (auto &Pair : Deallocations) |
| 859 | (Pair.first)(Pair.second); |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 860 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 861 | // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 862 | // because they can contain DenseMaps. |
| 863 | for (llvm::DenseMap<const ObjCContainerDecl*, |
| 864 | const ASTRecordLayout*>::iterator |
| 865 | I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) |
| 866 | // Increment in loop to prevent using deallocated memory. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 867 | if (auto *R = const_cast<ASTRecordLayout *>((I++)->second)) |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 868 | R->Destroy(*this); |
| 869 | |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 870 | for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator |
| 871 | I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) { |
| 872 | // Increment in loop to prevent using deallocated memory. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 873 | if (auto *R = const_cast<ASTRecordLayout *>((I++)->second)) |
Ted Kremenek | 076baeb | 2010-06-08 23:00:58 +0000 | [diff] [blame] | 874 | R->Destroy(*this); |
| 875 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 876 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 877 | for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(), |
| 878 | AEnd = DeclAttrs.end(); |
| 879 | A != AEnd; ++A) |
| 880 | A->second->~AttrVec(); |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 881 | |
Richard Smith | 423f46f | 2016-07-20 21:38:26 +0000 | [diff] [blame] | 882 | for (const auto &Value : ModuleInitializers) |
| 883 | Value.second->~PerModuleInitializers(); |
Gauthier Harnisch | 83c7b61 | 2019-06-15 10:24:47 +0000 | [diff] [blame] | 884 | |
| 885 | for (APValue *Value : APValueCleanups) |
| 886 | Value->~APValue(); |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 887 | } |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 888 | |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 889 | class ASTContext::ParentMap { |
| 890 | /// Contains parents of a node. |
| 891 | using ParentVector = llvm::SmallVector<ast_type_traits::DynTypedNode, 2>; |
| 892 | |
| 893 | /// Maps from a node to its parents. This is used for nodes that have |
| 894 | /// pointer identity only, which are more common and we can save space by |
| 895 | /// only storing a unique pointer to them. |
| 896 | using ParentMapPointers = llvm::DenseMap< |
| 897 | const void *, |
| 898 | llvm::PointerUnion4<const Decl *, const Stmt *, |
| 899 | ast_type_traits::DynTypedNode *, ParentVector *>>; |
| 900 | |
| 901 | /// Parent map for nodes without pointer identity. We store a full |
| 902 | /// DynTypedNode for all keys. |
| 903 | using ParentMapOtherNodes = llvm::DenseMap< |
| 904 | ast_type_traits::DynTypedNode, |
| 905 | llvm::PointerUnion4<const Decl *, const Stmt *, |
| 906 | ast_type_traits::DynTypedNode *, ParentVector *>>; |
| 907 | |
| 908 | ParentMapPointers PointerParents; |
| 909 | ParentMapOtherNodes OtherParents; |
| 910 | class ASTVisitor; |
| 911 | |
| 912 | static ast_type_traits::DynTypedNode |
| 913 | getSingleDynTypedNodeFromParentMap(ParentMapPointers::mapped_type U) { |
| 914 | if (const auto *D = U.dyn_cast<const Decl *>()) |
| 915 | return ast_type_traits::DynTypedNode::create(*D); |
| 916 | if (const auto *S = U.dyn_cast<const Stmt *>()) |
| 917 | return ast_type_traits::DynTypedNode::create(*S); |
| 918 | return *U.get<ast_type_traits::DynTypedNode *>(); |
| 919 | } |
| 920 | |
| 921 | template <typename NodeTy, typename MapTy> |
| 922 | static ASTContext::DynTypedNodeList getDynNodeFromMap(const NodeTy &Node, |
| 923 | const MapTy &Map) { |
| 924 | auto I = Map.find(Node); |
| 925 | if (I == Map.end()) { |
| 926 | return llvm::ArrayRef<ast_type_traits::DynTypedNode>(); |
| 927 | } |
| 928 | if (const auto *V = I->second.template dyn_cast<ParentVector *>()) { |
| 929 | return llvm::makeArrayRef(*V); |
| 930 | } |
| 931 | return getSingleDynTypedNodeFromParentMap(I->second); |
| 932 | } |
| 933 | |
| 934 | public: |
| 935 | ParentMap(ASTContext &Ctx); |
| 936 | ~ParentMap() { |
| 937 | for (const auto &Entry : PointerParents) { |
| 938 | if (Entry.second.is<ast_type_traits::DynTypedNode *>()) { |
| 939 | delete Entry.second.get<ast_type_traits::DynTypedNode *>(); |
| 940 | } else if (Entry.second.is<ParentVector *>()) { |
| 941 | delete Entry.second.get<ParentVector *>(); |
| 942 | } |
| 943 | } |
| 944 | for (const auto &Entry : OtherParents) { |
| 945 | if (Entry.second.is<ast_type_traits::DynTypedNode *>()) { |
| 946 | delete Entry.second.get<ast_type_traits::DynTypedNode *>(); |
| 947 | } else if (Entry.second.is<ParentVector *>()) { |
| 948 | delete Entry.second.get<ParentVector *>(); |
| 949 | } |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 950 | } |
| 951 | } |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 952 | |
| 953 | DynTypedNodeList getParents(const ast_type_traits::DynTypedNode &Node) { |
| 954 | if (Node.getNodeKind().hasPointerIdentity()) |
| 955 | return getDynNodeFromMap(Node.getMemoizationData(), PointerParents); |
| 956 | return getDynNodeFromMap(Node, OtherParents); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 957 | } |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 958 | }; |
| 959 | |
| 960 | void ASTContext::setTraversalScope(const std::vector<Decl *> &TopLevelDecls) { |
| 961 | TraversalScope = TopLevelDecls; |
| 962 | Parents.reset(); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Gauthier Harnisch | dea9d57 | 2019-06-21 08:26:21 +0000 | [diff] [blame] | 965 | void ASTContext::AddDeallocation(void (*Callback)(void *), void *Data) const { |
Chandler Carruth | ff5a01a | 2015-12-30 03:00:23 +0000 | [diff] [blame] | 966 | Deallocations.push_back({Callback, Data}); |
Douglas Gregor | 1a80933 | 2010-05-23 18:26:36 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | void |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 970 | ASTContext::setExternalSource(IntrusiveRefCntPtr<ExternalASTSource> Source) { |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 971 | ExternalSource = std::move(Source); |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 974 | void ASTContext::PrintStats() const { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 975 | llvm::errs() << "\n*** AST Context Stats:\n"; |
| 976 | llvm::errs() << " " << Types.size() << " types total.\n"; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 977 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 978 | unsigned counts[] = { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | #define TYPE(Name, Parent) 0, |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 980 | #define ABSTRACT_TYPE(Name, Parent) |
John McCall | 36b12a8 | 2019-10-02 06:35:23 +0000 | [diff] [blame] | 981 | #include "clang/AST/TypeNodes.inc" |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 982 | 0 // Extra |
| 983 | }; |
Douglas Gregor | b1fe2c9 | 2009-04-07 17:20:56 +0000 | [diff] [blame] | 984 | |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 985 | for (unsigned i = 0, e = Types.size(); i != e; ++i) { |
| 986 | Type *T = Types[i]; |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 987 | counts[(unsigned)T->getTypeClass()]++; |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 990 | unsigned Idx = 0; |
| 991 | unsigned TotalBytes = 0; |
| 992 | #define TYPE(Name, Parent) \ |
| 993 | if (counts[Idx]) \ |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 994 | llvm::errs() << " " << counts[Idx] << " " << #Name \ |
Bruno Ricci | 58e0322 | 2018-08-06 15:17:32 +0000 | [diff] [blame] | 995 | << " types, " << sizeof(Name##Type) << " each " \ |
| 996 | << "(" << counts[Idx] * sizeof(Name##Type) \ |
| 997 | << " bytes)\n"; \ |
Douglas Gregor | a30d046 | 2009-05-26 14:40:08 +0000 | [diff] [blame] | 998 | TotalBytes += counts[Idx] * sizeof(Name##Type); \ |
| 999 | ++Idx; |
| 1000 | #define ABSTRACT_TYPE(Name, Parent) |
John McCall | 36b12a8 | 2019-10-02 06:35:23 +0000 | [diff] [blame] | 1001 | #include "clang/AST/TypeNodes.inc" |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 1003 | llvm::errs() << "Total bytes = " << TotalBytes << "\n"; |
| 1004 | |
Douglas Gregor | 7454c56 | 2010-07-02 20:37:36 +0000 | [diff] [blame] | 1005 | // Implicit special member functions. |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 1006 | llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/" |
| 1007 | << NumImplicitDefaultConstructors |
| 1008 | << " implicit default constructors created\n"; |
| 1009 | llvm::errs() << NumImplicitCopyConstructorsDeclared << "/" |
| 1010 | << NumImplicitCopyConstructors |
| 1011 | << " implicit copy constructors created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1012 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 1013 | llvm::errs() << NumImplicitMoveConstructorsDeclared << "/" |
| 1014 | << NumImplicitMoveConstructors |
| 1015 | << " implicit move constructors created\n"; |
| 1016 | llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/" |
| 1017 | << NumImplicitCopyAssignmentOperators |
| 1018 | << " implicit copy assignment operators created\n"; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1019 | if (getLangOpts().CPlusPlus) |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 1020 | llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/" |
| 1021 | << NumImplicitMoveAssignmentOperators |
| 1022 | << " implicit move assignment operators created\n"; |
| 1023 | llvm::errs() << NumImplicitDestructorsDeclared << "/" |
| 1024 | << NumImplicitDestructors |
| 1025 | << " implicit destructors created\n"; |
| 1026 | |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 1027 | if (ExternalSource) { |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 1028 | llvm::errs() << "\n"; |
Douglas Gregor | ef84c4b | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1029 | ExternalSource->PrintStats(); |
| 1030 | } |
Chandler Carruth | 3c147a7 | 2011-07-04 05:32:14 +0000 | [diff] [blame] | 1031 | |
Douglas Gregor | 5b11d49 | 2010-07-25 17:53:33 +0000 | [diff] [blame] | 1032 | BumpAlloc.PrintStats(); |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 1035 | void ASTContext::mergeDefinitionIntoModule(NamedDecl *ND, Module *M, |
| 1036 | bool NotifyListeners) { |
| 1037 | if (NotifyListeners) |
| 1038 | if (auto *Listener = getASTMutationListener()) |
| 1039 | Listener->RedefinedHiddenDefinition(ND, M); |
| 1040 | |
Richard Smith | 13897eb | 2018-09-12 23:37:00 +0000 | [diff] [blame] | 1041 | MergedDefModules[cast<NamedDecl>(ND->getCanonicalDecl())].push_back(M); |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | void ASTContext::deduplicateMergedDefinitonsFor(NamedDecl *ND) { |
Richard Smith | 20fbdb3 | 2018-09-12 02:13:46 +0000 | [diff] [blame] | 1045 | auto It = MergedDefModules.find(cast<NamedDecl>(ND->getCanonicalDecl())); |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 1046 | if (It == MergedDefModules.end()) |
| 1047 | return; |
| 1048 | |
Benjamin Kramer | a72a70a | 2016-10-17 13:00:44 +0000 | [diff] [blame] | 1049 | auto &Merged = It->second; |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 1050 | llvm::DenseSet<Module*> Found; |
| 1051 | for (Module *&M : Merged) |
| 1052 | if (!Found.insert(M).second) |
| 1053 | M = nullptr; |
| 1054 | Merged.erase(std::remove(Merged.begin(), Merged.end(), nullptr), Merged.end()); |
| 1055 | } |
| 1056 | |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 1057 | void ASTContext::PerModuleInitializers::resolve(ASTContext &Ctx) { |
| 1058 | if (LazyInitializers.empty()) |
| 1059 | return; |
| 1060 | |
| 1061 | auto *Source = Ctx.getExternalSource(); |
| 1062 | assert(Source && "lazy initializers but no external source"); |
| 1063 | |
| 1064 | auto LazyInits = std::move(LazyInitializers); |
| 1065 | LazyInitializers.clear(); |
| 1066 | |
| 1067 | for (auto ID : LazyInits) |
| 1068 | Initializers.push_back(Source->GetExternalDecl(ID)); |
| 1069 | |
| 1070 | assert(LazyInitializers.empty() && |
| 1071 | "GetExternalDecl for lazy module initializer added more inits"); |
| 1072 | } |
| 1073 | |
| 1074 | void ASTContext::addModuleInitializer(Module *M, Decl *D) { |
| 1075 | // One special case: if we add a module initializer that imports another |
| 1076 | // module, and that module's only initializer is an ImportDecl, simplify. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1077 | if (const auto *ID = dyn_cast<ImportDecl>(D)) { |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 1078 | auto It = ModuleInitializers.find(ID->getImportedModule()); |
| 1079 | |
| 1080 | // Maybe the ImportDecl does nothing at all. (Common case.) |
| 1081 | if (It == ModuleInitializers.end()) |
| 1082 | return; |
| 1083 | |
| 1084 | // Maybe the ImportDecl only imports another ImportDecl. |
| 1085 | auto &Imported = *It->second; |
| 1086 | if (Imported.Initializers.size() + Imported.LazyInitializers.size() == 1) { |
| 1087 | Imported.resolve(*this); |
| 1088 | auto *OnlyDecl = Imported.Initializers.front(); |
| 1089 | if (isa<ImportDecl>(OnlyDecl)) |
| 1090 | D = OnlyDecl; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | auto *&Inits = ModuleInitializers[M]; |
| 1095 | if (!Inits) |
| 1096 | Inits = new (*this) PerModuleInitializers; |
| 1097 | Inits->Initializers.push_back(D); |
| 1098 | } |
| 1099 | |
| 1100 | void ASTContext::addLazyModuleInitializers(Module *M, ArrayRef<uint32_t> IDs) { |
| 1101 | auto *&Inits = ModuleInitializers[M]; |
| 1102 | if (!Inits) |
| 1103 | Inits = new (*this) PerModuleInitializers; |
| 1104 | Inits->LazyInitializers.insert(Inits->LazyInitializers.end(), |
| 1105 | IDs.begin(), IDs.end()); |
| 1106 | } |
| 1107 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1108 | ArrayRef<Decl *> ASTContext::getModuleInitializers(Module *M) { |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 1109 | auto It = ModuleInitializers.find(M); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1110 | if (It == ModuleInitializers.end()) |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 1111 | return None; |
| 1112 | |
| 1113 | auto *Inits = It->second; |
| 1114 | Inits->resolve(*this); |
| 1115 | return Inits->Initializers; |
| 1116 | } |
| 1117 | |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 1118 | ExternCContextDecl *ASTContext::getExternCContextDecl() const { |
| 1119 | if (!ExternCContext) |
| 1120 | ExternCContext = ExternCContextDecl::Create(*this, getTranslationUnitDecl()); |
| 1121 | |
| 1122 | return ExternCContext; |
| 1123 | } |
| 1124 | |
David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1125 | BuiltinTemplateDecl * |
| 1126 | ASTContext::buildBuiltinTemplateDecl(BuiltinTemplateKind BTK, |
| 1127 | const IdentifierInfo *II) const { |
| 1128 | auto *BuiltinTemplate = BuiltinTemplateDecl::Create(*this, TUDecl, II, BTK); |
| 1129 | BuiltinTemplate->setImplicit(); |
| 1130 | TUDecl->addDecl(BuiltinTemplate); |
| 1131 | |
| 1132 | return BuiltinTemplate; |
| 1133 | } |
| 1134 | |
| 1135 | BuiltinTemplateDecl * |
| 1136 | ASTContext::getMakeIntegerSeqDecl() const { |
| 1137 | if (!MakeIntegerSeqDecl) |
| 1138 | MakeIntegerSeqDecl = buildBuiltinTemplateDecl(BTK__make_integer_seq, |
| 1139 | getMakeIntegerSeqName()); |
| 1140 | return MakeIntegerSeqDecl; |
| 1141 | } |
| 1142 | |
Eric Fiselier | 6ad6855 | 2016-07-01 01:24:09 +0000 | [diff] [blame] | 1143 | BuiltinTemplateDecl * |
| 1144 | ASTContext::getTypePackElementDecl() const { |
| 1145 | if (!TypePackElementDecl) |
| 1146 | TypePackElementDecl = buildBuiltinTemplateDecl(BTK__type_pack_element, |
| 1147 | getTypePackElementName()); |
| 1148 | return TypePackElementDecl; |
| 1149 | } |
| 1150 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 1151 | RecordDecl *ASTContext::buildImplicitRecord(StringRef Name, |
| 1152 | RecordDecl::TagKind TK) const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1153 | SourceLocation Loc; |
| 1154 | RecordDecl *NewDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 1155 | if (getLangOpts().CPlusPlus) |
| 1156 | NewDecl = CXXRecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, |
| 1157 | Loc, &Idents.get(Name)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1158 | else |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 1159 | NewDecl = RecordDecl::Create(*this, TK, getTranslationUnitDecl(), Loc, Loc, |
| 1160 | &Idents.get(Name)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1161 | NewDecl->setImplicit(); |
David Majnemer | f863736 | 2015-01-15 08:41:25 +0000 | [diff] [blame] | 1162 | NewDecl->addAttr(TypeVisibilityAttr::CreateImplicit( |
| 1163 | const_cast<ASTContext &>(*this), TypeVisibilityAttr::Default)); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1164 | return NewDecl; |
| 1165 | } |
| 1166 | |
| 1167 | TypedefDecl *ASTContext::buildImplicitTypedef(QualType T, |
| 1168 | StringRef Name) const { |
| 1169 | TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T); |
| 1170 | TypedefDecl *NewDecl = TypedefDecl::Create( |
| 1171 | const_cast<ASTContext &>(*this), getTranslationUnitDecl(), |
| 1172 | SourceLocation(), SourceLocation(), &Idents.get(Name), TInfo); |
| 1173 | NewDecl->setImplicit(); |
| 1174 | return NewDecl; |
| 1175 | } |
| 1176 | |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 1177 | TypedefDecl *ASTContext::getInt128Decl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1178 | if (!Int128Decl) |
| 1179 | Int128Decl = buildImplicitTypedef(Int128Ty, "__int128_t"); |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 1180 | return Int128Decl; |
| 1181 | } |
| 1182 | |
| 1183 | TypedefDecl *ASTContext::getUInt128Decl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 1184 | if (!UInt128Decl) |
| 1185 | UInt128Decl = buildImplicitTypedef(UnsignedInt128Ty, "__uint128_t"); |
Douglas Gregor | 801c99d | 2011-08-12 06:49:56 +0000 | [diff] [blame] | 1186 | return UInt128Decl; |
| 1187 | } |
Chris Lattner | 4eb445d | 2007-01-26 01:27:23 +0000 | [diff] [blame] | 1188 | |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 1189 | void ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1190 | auto *Ty = new (*this, TypeAlignment) BuiltinType(K); |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 1191 | R = CanQualType::CreateUnsafe(QualType(Ty, 0)); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 1192 | Types.push_back(Ty); |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 1195 | void ASTContext::InitBuiltinTypes(const TargetInfo &Target, |
| 1196 | const TargetInfo *AuxTarget) { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1197 | assert((!this->Target || this->Target == &Target) && |
| 1198 | "Incorrect target reinitialization"); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1199 | assert(VoidTy.isNull() && "Context reinitialized?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1201 | this->Target = &Target; |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 1202 | this->AuxTarget = AuxTarget; |
| 1203 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1204 | ABI.reset(createCXXABI(Target)); |
| 1205 | AddrSpaceMap = getAddressSpaceMap(Target, LangOpts); |
David Tweed | 31d09b0 | 2013-09-13 12:04:22 +0000 | [diff] [blame] | 1206 | AddrSpaceMapMangling = isAddrSpaceMapManglingEnabled(Target, LangOpts); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1207 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1208 | // C99 6.2.5p19. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1209 | InitBuiltinType(VoidTy, BuiltinType::Void); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1210 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1211 | // C99 6.2.5p2. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1212 | InitBuiltinType(BoolTy, BuiltinType::Bool); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1213 | // C99 6.2.5p3. |
Eli Friedman | 9ffd4a9 | 2009-06-05 07:05:05 +0000 | [diff] [blame] | 1214 | if (LangOpts.CharIsSigned) |
Chris Lattner | b16f455 | 2007-06-03 07:25:34 +0000 | [diff] [blame] | 1215 | InitBuiltinType(CharTy, BuiltinType::Char_S); |
| 1216 | else |
| 1217 | InitBuiltinType(CharTy, BuiltinType::Char_U); |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1218 | // C99 6.2.5p4. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1219 | InitBuiltinType(SignedCharTy, BuiltinType::SChar); |
| 1220 | InitBuiltinType(ShortTy, BuiltinType::Short); |
| 1221 | InitBuiltinType(IntTy, BuiltinType::Int); |
| 1222 | InitBuiltinType(LongTy, BuiltinType::Long); |
| 1223 | InitBuiltinType(LongLongTy, BuiltinType::LongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1225 | // C99 6.2.5p6. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1226 | InitBuiltinType(UnsignedCharTy, BuiltinType::UChar); |
| 1227 | InitBuiltinType(UnsignedShortTy, BuiltinType::UShort); |
| 1228 | InitBuiltinType(UnsignedIntTy, BuiltinType::UInt); |
| 1229 | InitBuiltinType(UnsignedLongTy, BuiltinType::ULong); |
| 1230 | InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1231 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1232 | // C99 6.2.5p10. |
Chris Lattner | 726f97b | 2006-12-03 02:57:32 +0000 | [diff] [blame] | 1233 | InitBuiltinType(FloatTy, BuiltinType::Float); |
| 1234 | InitBuiltinType(DoubleTy, BuiltinType::Double); |
| 1235 | InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1236 | |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1237 | // GNU extension, __float128 for IEEE quadruple precision |
| 1238 | InitBuiltinType(Float128Ty, BuiltinType::Float128); |
| 1239 | |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 1240 | // C11 extension ISO/IEC TS 18661-3 |
| 1241 | InitBuiltinType(Float16Ty, BuiltinType::Float16); |
| 1242 | |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1243 | // ISO/IEC JTC1 SC22 WG14 N1169 Extension |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1244 | InitBuiltinType(ShortAccumTy, BuiltinType::ShortAccum); |
| 1245 | InitBuiltinType(AccumTy, BuiltinType::Accum); |
| 1246 | InitBuiltinType(LongAccumTy, BuiltinType::LongAccum); |
| 1247 | InitBuiltinType(UnsignedShortAccumTy, BuiltinType::UShortAccum); |
| 1248 | InitBuiltinType(UnsignedAccumTy, BuiltinType::UAccum); |
| 1249 | InitBuiltinType(UnsignedLongAccumTy, BuiltinType::ULongAccum); |
| 1250 | InitBuiltinType(ShortFractTy, BuiltinType::ShortFract); |
| 1251 | InitBuiltinType(FractTy, BuiltinType::Fract); |
| 1252 | InitBuiltinType(LongFractTy, BuiltinType::LongFract); |
| 1253 | InitBuiltinType(UnsignedShortFractTy, BuiltinType::UShortFract); |
| 1254 | InitBuiltinType(UnsignedFractTy, BuiltinType::UFract); |
| 1255 | InitBuiltinType(UnsignedLongFractTy, BuiltinType::ULongFract); |
| 1256 | InitBuiltinType(SatShortAccumTy, BuiltinType::SatShortAccum); |
| 1257 | InitBuiltinType(SatAccumTy, BuiltinType::SatAccum); |
| 1258 | InitBuiltinType(SatLongAccumTy, BuiltinType::SatLongAccum); |
| 1259 | InitBuiltinType(SatUnsignedShortAccumTy, BuiltinType::SatUShortAccum); |
| 1260 | InitBuiltinType(SatUnsignedAccumTy, BuiltinType::SatUAccum); |
| 1261 | InitBuiltinType(SatUnsignedLongAccumTy, BuiltinType::SatULongAccum); |
| 1262 | InitBuiltinType(SatShortFractTy, BuiltinType::SatShortFract); |
| 1263 | InitBuiltinType(SatFractTy, BuiltinType::SatFract); |
| 1264 | InitBuiltinType(SatLongFractTy, BuiltinType::SatLongFract); |
| 1265 | InitBuiltinType(SatUnsignedShortFractTy, BuiltinType::SatUShortFract); |
| 1266 | InitBuiltinType(SatUnsignedFractTy, BuiltinType::SatUFract); |
| 1267 | InitBuiltinType(SatUnsignedLongFractTy, BuiltinType::SatULongFract); |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1268 | |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 1269 | // GNU extension, 128-bit integers. |
| 1270 | InitBuiltinType(Int128Ty, BuiltinType::Int128); |
| 1271 | InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128); |
| 1272 | |
Hans Wennborg | 0d81e01 | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 1273 | // C++ 3.9.1p5 |
| 1274 | if (TargetInfo::isTypeSigned(Target.getWCharType())) |
| 1275 | InitBuiltinType(WCharTy, BuiltinType::WChar_S); |
| 1276 | else // -fshort-wchar makes wchar_t be unsigned. |
| 1277 | InitBuiltinType(WCharTy, BuiltinType::WChar_U); |
| 1278 | if (LangOpts.CPlusPlus && LangOpts.WChar) |
| 1279 | WideCharTy = WCharTy; |
| 1280 | else { |
| 1281 | // C99 (or C++ using -fno-wchar). |
| 1282 | WideCharTy = getFromTargetType(Target.getWCharType()); |
| 1283 | } |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1284 | |
James Molloy | 3636554 | 2012-05-04 10:55:22 +0000 | [diff] [blame] | 1285 | WIntTy = getFromTargetType(Target.getWIntType()); |
| 1286 | |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 1287 | // C++20 (proposed) |
| 1288 | InitBuiltinType(Char8Ty, BuiltinType::Char8); |
| 1289 | |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1290 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 1291 | InitBuiltinType(Char16Ty, BuiltinType::Char16); |
| 1292 | else // C99 |
| 1293 | Char16Ty = getFromTargetType(Target.getChar16Type()); |
| 1294 | |
| 1295 | if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++ |
| 1296 | InitBuiltinType(Char32Ty, BuiltinType::Char32); |
| 1297 | else // C99 |
| 1298 | Char32Ty = getFromTargetType(Target.getChar32Type()); |
| 1299 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1300 | // Placeholder type for type-dependent expressions whose type is |
| 1301 | // completely unknown. No code should ever check a type against |
| 1302 | // DependentTy and users should never see it; however, it is here to |
| 1303 | // help diagnose failures to properly check for type-dependent |
| 1304 | // expressions. |
| 1305 | InitBuiltinType(DependentTy, BuiltinType::Dependent); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1306 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 1307 | // Placeholder type for functions. |
| 1308 | InitBuiltinType(OverloadTy, BuiltinType::Overload); |
| 1309 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1310 | // Placeholder type for bound members. |
| 1311 | InitBuiltinType(BoundMemberTy, BuiltinType::BoundMember); |
| 1312 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1313 | // Placeholder type for pseudo-objects. |
| 1314 | InitBuiltinType(PseudoObjectTy, BuiltinType::PseudoObject); |
| 1315 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 1316 | // "any" type; useful for debugger-like clients. |
| 1317 | InitBuiltinType(UnknownAnyTy, BuiltinType::UnknownAny); |
| 1318 | |
John McCall | 8a6b59a | 2011-10-17 18:09:15 +0000 | [diff] [blame] | 1319 | // Placeholder type for unbridged ARC casts. |
| 1320 | InitBuiltinType(ARCUnbridgedCastTy, BuiltinType::ARCUnbridgedCast); |
| 1321 | |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 1322 | // Placeholder type for builtin functions. |
| 1323 | InitBuiltinType(BuiltinFnTy, BuiltinType::BuiltinFn); |
| 1324 | |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 1325 | // Placeholder type for OMP array sections. |
| 1326 | if (LangOpts.OpenMP) |
| 1327 | InitBuiltinType(OMPArraySectionTy, BuiltinType::OMPArraySection); |
| 1328 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1329 | // C99 6.2.5p11. |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 1330 | FloatComplexTy = getComplexType(FloatTy); |
| 1331 | DoubleComplexTy = getComplexType(DoubleTy); |
| 1332 | LongDoubleComplexTy = getComplexType(LongDoubleTy); |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1333 | Float128ComplexTy = getComplexType(Float128Ty); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1334 | |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1335 | // Builtin types for 'id', 'Class', and 'SEL'. |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 1336 | InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId); |
| 1337 | InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass); |
Fariborz Jahanian | 252ba5f | 2009-11-21 19:53:08 +0000 | [diff] [blame] | 1338 | InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel); |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1339 | |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 1340 | if (LangOpts.OpenCL) { |
| 1341 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 1342 | InitBuiltinType(SingletonId, BuiltinType::Id); |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 1343 | #include "clang/Basic/OpenCLImageTypes.def" |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1344 | |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 1345 | InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler); |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 1346 | InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1347 | InitBuiltinType(OCLClkEventTy, BuiltinType::OCLClkEvent); |
| 1348 | InitBuiltinType(OCLQueueTy, BuiltinType::OCLQueue); |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 1349 | InitBuiltinType(OCLReserveIDTy, BuiltinType::OCLReserveID); |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 1350 | |
| 1351 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
| 1352 | InitBuiltinType(Id##Ty, BuiltinType::Id); |
| 1353 | #include "clang/Basic/OpenCLExtensionTypes.def" |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 1354 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1355 | |
Richard Sandiford | eb485fb | 2019-08-09 08:52:54 +0000 | [diff] [blame] | 1356 | if (Target.hasAArch64SVETypes()) { |
| 1357 | #define SVE_TYPE(Name, Id, SingletonId) \ |
| 1358 | InitBuiltinType(SingletonId, BuiltinType::Id); |
| 1359 | #include "clang/Basic/AArch64SVEACLETypes.def" |
| 1360 | } |
| 1361 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1362 | // Builtin type for __objc_yes and __objc_no |
Fariborz Jahanian | 29898f4 | 2012-04-16 21:03:30 +0000 | [diff] [blame] | 1363 | ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ? |
| 1364 | SignedCharTy : BoolTy); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1365 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1366 | ObjCConstantStringType = QualType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1367 | |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 1368 | ObjCSuperType = QualType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 1370 | // void * type |
Yaxun Liu | 3919506 | 2017-08-04 18:16:31 +0000 | [diff] [blame] | 1371 | if (LangOpts.OpenCLVersion >= 200) { |
| 1372 | auto Q = VoidTy.getQualifiers(); |
| 1373 | Q.setAddressSpace(LangAS::opencl_generic); |
| 1374 | VoidPtrTy = getPointerType(getCanonicalType( |
| 1375 | getQualifiedType(VoidTy.getUnqualifiedType(), Q))); |
| 1376 | } else { |
| 1377 | VoidPtrTy = getPointerType(VoidTy); |
| 1378 | } |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1379 | |
| 1380 | // nullptr type (C++0x 2.14.7) |
| 1381 | InitBuiltinType(NullPtrTy, BuiltinType::NullPtr); |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1382 | |
| 1383 | // half type (OpenCL 6.1.1.1) / ARM NEON __fp16 |
| 1384 | InitBuiltinType(HalfTy, BuiltinType::Half); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 1385 | |
| 1386 | // Builtin type used to help define __builtin_va_list. |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 1387 | VaListTagDecl = nullptr; |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1390 | DiagnosticsEngine &ASTContext::getDiagnostics() const { |
Argyrios Kyrtzidis | ca0d0cd | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 1391 | return SourceMgr.getDiagnostics(); |
| 1392 | } |
| 1393 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 1394 | AttrVec& ASTContext::getDeclAttrs(const Decl *D) { |
| 1395 | AttrVec *&Result = DeclAttrs[D]; |
| 1396 | if (!Result) { |
| 1397 | void *Mem = Allocate(sizeof(AttrVec)); |
| 1398 | Result = new (Mem) AttrVec; |
| 1399 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1400 | |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 1401 | return *Result; |
| 1402 | } |
| 1403 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1404 | /// Erase the attributes corresponding to the given declaration. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 1405 | void ASTContext::eraseDeclAttrs(const Decl *D) { |
Douglas Gregor | 561eceb | 2010-08-30 16:49:28 +0000 | [diff] [blame] | 1406 | llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D); |
| 1407 | if (Pos != DeclAttrs.end()) { |
| 1408 | Pos->second->~AttrVec(); |
| 1409 | DeclAttrs.erase(Pos); |
| 1410 | } |
| 1411 | } |
| 1412 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1413 | // FIXME: Remove ? |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1414 | MemberSpecializationInfo * |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1415 | ASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1416 | assert(Var->isStaticDataMember() && "Not a static data member"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1417 | return getTemplateOrSpecializationInfo(Var) |
| 1418 | .dyn_cast<MemberSpecializationInfo *>(); |
| 1419 | } |
| 1420 | |
| 1421 | ASTContext::TemplateOrSpecializationInfo |
| 1422 | ASTContext::getTemplateOrSpecializationInfo(const VarDecl *Var) { |
| 1423 | llvm::DenseMap<const VarDecl *, TemplateOrSpecializationInfo>::iterator Pos = |
| 1424 | TemplateOrInstantiation.find(Var); |
| 1425 | if (Pos == TemplateOrInstantiation.end()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1426 | return {}; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1427 | |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1428 | return Pos->second; |
| 1429 | } |
| 1430 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1431 | void |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1432 | ASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, |
Argyrios Kyrtzidis | cdb8b3f | 2010-07-04 21:44:00 +0000 | [diff] [blame] | 1433 | TemplateSpecializationKind TSK, |
| 1434 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1435 | assert(Inst->isStaticDataMember() && "Not a static data member"); |
| 1436 | assert(Tmpl->isStaticDataMember() && "Not a static data member"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1437 | setTemplateOrSpecializationInfo(Inst, new (*this) MemberSpecializationInfo( |
| 1438 | Tmpl, TSK, PointOfInstantiation)); |
| 1439 | } |
| 1440 | |
| 1441 | void |
| 1442 | ASTContext::setTemplateOrSpecializationInfo(VarDecl *Inst, |
| 1443 | TemplateOrSpecializationInfo TSI) { |
| 1444 | assert(!TemplateOrInstantiation[Inst] && |
| 1445 | "Already noted what the variable was instantiated from"); |
| 1446 | TemplateOrInstantiation[Inst] = TSI; |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1449 | NamedDecl * |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1450 | ASTContext::getInstantiatedFromUsingDecl(NamedDecl *UUD) { |
| 1451 | auto Pos = InstantiatedFromUsingDecl.find(UUD); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1452 | if (Pos == InstantiatedFromUsingDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1453 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1454 | |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1455 | return Pos->second; |
| 1456 | } |
| 1457 | |
| 1458 | void |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1459 | ASTContext::setInstantiatedFromUsingDecl(NamedDecl *Inst, NamedDecl *Pattern) { |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1460 | assert((isa<UsingDecl>(Pattern) || |
| 1461 | isa<UnresolvedUsingValueDecl>(Pattern) || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1462 | isa<UnresolvedUsingTypenameDecl>(Pattern)) && |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1463 | "pattern decl is not a using decl"); |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1464 | assert((isa<UsingDecl>(Inst) || |
| 1465 | isa<UnresolvedUsingValueDecl>(Inst) || |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1466 | isa<UnresolvedUsingTypenameDecl>(Inst)) && |
Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 1467 | "instantiation did not produce a using decl"); |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1468 | assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists"); |
| 1469 | InstantiatedFromUsingDecl[Inst] = Pattern; |
| 1470 | } |
| 1471 | |
| 1472 | UsingShadowDecl * |
| 1473 | ASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) { |
| 1474 | llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos |
| 1475 | = InstantiatedFromUsingShadowDecl.find(Inst); |
| 1476 | if (Pos == InstantiatedFromUsingShadowDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1477 | return nullptr; |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1478 | |
| 1479 | return Pos->second; |
| 1480 | } |
| 1481 | |
| 1482 | void |
| 1483 | ASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, |
| 1484 | UsingShadowDecl *Pattern) { |
| 1485 | assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists"); |
| 1486 | InstantiatedFromUsingShadowDecl[Inst] = Pattern; |
Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1489 | FieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) { |
| 1490 | llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos |
| 1491 | = InstantiatedFromUnnamedFieldDecl.find(Field); |
| 1492 | if (Pos == InstantiatedFromUnnamedFieldDecl.end()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1493 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1494 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1495 | return Pos->second; |
| 1496 | } |
| 1497 | |
| 1498 | void ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, |
| 1499 | FieldDecl *Tmpl) { |
| 1500 | assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed"); |
| 1501 | assert(!Tmpl->getDeclName() && "Template field decl is not unnamed"); |
| 1502 | assert(!InstantiatedFromUnnamedFieldDecl[Inst] && |
| 1503 | "Already noted what unnamed field was instantiated from"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | |
Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1505 | InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl; |
| 1506 | } |
| 1507 | |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1508 | ASTContext::overridden_cxx_method_iterator |
| 1509 | ASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const { |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 1510 | return overridden_methods(Method).begin(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | ASTContext::overridden_cxx_method_iterator |
| 1514 | ASTContext::overridden_methods_end(const CXXMethodDecl *Method) const { |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 1515 | return overridden_methods(Method).end(); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1518 | unsigned |
| 1519 | ASTContext::overridden_methods_size(const CXXMethodDecl *Method) const { |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 1520 | auto Range = overridden_methods(Method); |
| 1521 | return Range.end() - Range.begin(); |
Clement Courbet | 8251ebf | 2016-06-10 11:54:43 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Clement Courbet | 6ecaec8 | 2016-07-05 07:49:31 +0000 | [diff] [blame] | 1524 | ASTContext::overridden_method_range |
| 1525 | ASTContext::overridden_methods(const CXXMethodDecl *Method) const { |
Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 1526 | llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos = |
| 1527 | OverriddenMethods.find(Method->getCanonicalDecl()); |
| 1528 | if (Pos == OverriddenMethods.end()) |
| 1529 | return overridden_method_range(nullptr, nullptr); |
| 1530 | return overridden_method_range(Pos->second.begin(), Pos->second.end()); |
Clement Courbet | 6ecaec8 | 2016-07-05 07:49:31 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1533 | void ASTContext::addOverriddenMethod(const CXXMethodDecl *Method, |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1534 | const CXXMethodDecl *Overridden) { |
Argyrios Kyrtzidis | cc4ca0a | 2012-10-09 01:23:45 +0000 | [diff] [blame] | 1535 | assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl()); |
Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1536 | OverriddenMethods[Method].push_back(Overridden); |
| 1537 | } |
| 1538 | |
Dmitri Gribenko | 941ab0f | 2012-11-03 14:24:57 +0000 | [diff] [blame] | 1539 | void ASTContext::getOverriddenMethods( |
| 1540 | const NamedDecl *D, |
| 1541 | SmallVectorImpl<const NamedDecl *> &Overridden) const { |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1542 | assert(D); |
| 1543 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1544 | if (const auto *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { |
Argyrios Kyrtzidis | c8e7008 | 2013-04-17 00:09:03 +0000 | [diff] [blame] | 1545 | Overridden.append(overridden_methods_begin(CXXMethod), |
| 1546 | overridden_methods_end(CXXMethod)); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1547 | return; |
| 1548 | } |
| 1549 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1550 | const auto *Method = dyn_cast<ObjCMethodDecl>(D); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1551 | if (!Method) |
| 1552 | return; |
| 1553 | |
Argyrios Kyrtzidis | 353f6a4 | 2012-10-09 18:19:01 +0000 | [diff] [blame] | 1554 | SmallVector<const ObjCMethodDecl *, 8> OverDecls; |
| 1555 | Method->getOverriddenMethods(OverDecls); |
Argyrios Kyrtzidis | a7a1081 | 2012-10-09 20:08:43 +0000 | [diff] [blame] | 1556 | Overridden.append(OverDecls.begin(), OverDecls.end()); |
Argyrios Kyrtzidis | b9556e6 | 2012-10-09 01:23:50 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 1559 | void ASTContext::addedLocalImportDecl(ImportDecl *Import) { |
| 1560 | assert(!Import->NextLocalImport && "Import declaration already in the chain"); |
| 1561 | assert(!Import->isFromASTFile() && "Non-local import declaration"); |
| 1562 | if (!FirstLocalImport) { |
| 1563 | FirstLocalImport = Import; |
| 1564 | LastLocalImport = Import; |
| 1565 | return; |
| 1566 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1567 | |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 1568 | LastLocalImport->NextLocalImport = Import; |
| 1569 | LastLocalImport = Import; |
| 1570 | } |
| 1571 | |
Chris Lattner | 53cfe80 | 2007-07-18 17:52:12 +0000 | [diff] [blame] | 1572 | //===----------------------------------------------------------------------===// |
| 1573 | // Type Sizing and Analysis |
| 1574 | //===----------------------------------------------------------------------===// |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1575 | |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1576 | /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified |
| 1577 | /// scalar floating point type. |
| 1578 | const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const { |
Simon Pilgrim | 9588ae7 | 2019-10-03 21:47:42 +0000 | [diff] [blame] | 1579 | switch (T->castAs<BuiltinType>()->getKind()) { |
| 1580 | default: |
| 1581 | llvm_unreachable("Not a floating point type!"); |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 1582 | case BuiltinType::Float16: |
| 1583 | case BuiltinType::Half: |
| 1584 | return Target->getHalfFormat(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1585 | case BuiltinType::Float: return Target->getFloatFormat(); |
| 1586 | case BuiltinType::Double: return Target->getDoubleFormat(); |
Alexey Bataev | e509af3 | 2019-07-09 14:09:53 +0000 | [diff] [blame] | 1587 | case BuiltinType::LongDouble: |
Fangrui Song | 3fbd8fd | 2019-07-09 19:36:22 +0000 | [diff] [blame] | 1588 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice) |
Alexey Bataev | e509af3 | 2019-07-09 14:09:53 +0000 | [diff] [blame] | 1589 | return AuxTarget->getLongDoubleFormat(); |
| 1590 | return Target->getLongDoubleFormat(); |
| 1591 | case BuiltinType::Float128: |
Fangrui Song | 3fbd8fd | 2019-07-09 19:36:22 +0000 | [diff] [blame] | 1592 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice) |
Alexey Bataev | e509af3 | 2019-07-09 14:09:53 +0000 | [diff] [blame] | 1593 | return AuxTarget->getFloat128Format(); |
| 1594 | return Target->getFloat128Format(); |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1598 | CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1599 | unsigned Align = Target->getCharWidth(); |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1600 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1601 | bool UseAlignAttrOnly = false; |
| 1602 | if (unsigned AlignFromAttr = D->getMaxAlignment()) { |
| 1603 | Align = AlignFromAttr; |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1604 | |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1605 | // __attribute__((aligned)) can increase or decrease alignment |
| 1606 | // *except* on a struct or struct member, where it only increases |
| 1607 | // alignment unless 'packed' is also specified. |
| 1608 | // |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1609 | // It is an error for alignas to decrease alignment, so we can |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1610 | // ignore that possibility; Sema should diagnose it. |
| 1611 | if (isa<FieldDecl>(D)) { |
| 1612 | UseAlignAttrOnly = D->hasAttr<PackedAttr>() || |
| 1613 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
| 1614 | } else { |
| 1615 | UseAlignAttrOnly = true; |
| 1616 | } |
| 1617 | } |
Fariborz Jahanian | 9f10718 | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 1618 | else if (isa<FieldDecl>(D)) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1619 | UseAlignAttrOnly = |
Fariborz Jahanian | 9f10718 | 2011-05-05 21:19:14 +0000 | [diff] [blame] | 1620 | D->hasAttr<PackedAttr>() || |
| 1621 | cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>(); |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1622 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1623 | // If we're using the align attribute only, just ignore everything |
| 1624 | // else about the declaration and its type. |
John McCall | 9426870 | 2010-10-08 18:24:19 +0000 | [diff] [blame] | 1625 | if (UseAlignAttrOnly) { |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1626 | // do nothing |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1627 | } else if (const auto *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1628 | QualType T = VD->getType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1629 | if (const auto *RT = T->getAs<ReferenceType>()) { |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1630 | if (ForAlignof) |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1631 | T = RT->getPointeeType(); |
| 1632 | else |
| 1633 | T = getPointerType(RT->getPointeeType()); |
| 1634 | } |
Richard Smith | f6d7030 | 2014-06-10 23:34:28 +0000 | [diff] [blame] | 1635 | QualType BaseT = getBaseElementType(T); |
Akira Hatanaka | d62f2c8 | 2017-01-06 17:56:15 +0000 | [diff] [blame] | 1636 | if (T->isFunctionType()) |
| 1637 | Align = getTypeInfoImpl(T.getTypePtr()).Align; |
| 1638 | else if (!BaseT->isIncompleteType()) { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1639 | // Adjust alignments of declarations with array type by the |
| 1640 | // large-array alignment on the target. |
Rafael Espindola | 8857275 | 2013-08-07 18:08:19 +0000 | [diff] [blame] | 1641 | if (const ArrayType *arrayType = getAsArrayType(T)) { |
Rafael Espindola | 71eccb3 | 2013-08-08 19:53:46 +0000 | [diff] [blame] | 1642 | unsigned MinWidth = Target->getLargeArrayMinWidth(); |
| 1643 | if (!ForAlignof && MinWidth) { |
Rafael Espindola | 8857275 | 2013-08-07 18:08:19 +0000 | [diff] [blame] | 1644 | if (isa<VariableArrayType>(arrayType)) |
| 1645 | Align = std::max(Align, Target->getLargeArrayAlign()); |
| 1646 | else if (isa<ConstantArrayType>(arrayType) && |
| 1647 | MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType))) |
| 1648 | Align = std::max(Align, Target->getLargeArrayAlign()); |
| 1649 | } |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 1650 | } |
Chad Rosier | 99ee782 | 2011-07-26 07:03:04 +0000 | [diff] [blame] | 1651 | Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr())); |
Roger Ferrer Ibanez | 3fa38a1 | 2017-03-08 14:00:44 +0000 | [diff] [blame] | 1652 | if (BaseT.getQualifiers().hasUnaligned()) |
| 1653 | Align = Target->getCharWidth(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1654 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Tom Tan | b7c6d95 | 2019-05-02 00:38:14 +0000 | [diff] [blame] | 1655 | if (VD->hasGlobalStorage() && !ForAlignof) { |
| 1656 | uint64_t TypeSize = getTypeSize(T.getTypePtr()); |
| 1657 | Align = std::max(Align, getTargetInfo().getMinGlobalAlign(TypeSize)); |
| 1658 | } |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 1659 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1660 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1661 | |
| 1662 | // Fields can be subject to extra alignment constraints, like if |
| 1663 | // the field is packed, the struct is packed, or the struct has a |
| 1664 | // a max-field-alignment constraint (#pragma pack). So calculate |
| 1665 | // the actual alignment of the field within the struct, and then |
| 1666 | // (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] | 1667 | if (const auto *Field = dyn_cast<FieldDecl>(VD)) { |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1668 | const RecordDecl *Parent = Field->getParent(); |
| 1669 | // We can only produce a sensible answer if the record is valid. |
| 1670 | if (!Parent->isInvalidDecl()) { |
| 1671 | const ASTRecordLayout &Layout = getASTRecordLayout(Parent); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1672 | |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1673 | // Start with the record's overall alignment. |
| 1674 | unsigned FieldAlign = toBits(Layout.getAlignment()); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1675 | |
Matt Beaumont-Gay | 3577995 | 2013-06-25 22:19:15 +0000 | [diff] [blame] | 1676 | // Use the GCD of that and the offset within the record. |
| 1677 | uint64_t Offset = Layout.getFieldOffset(Field->getFieldIndex()); |
| 1678 | if (Offset > 0) { |
| 1679 | // Alignment is always a power of 2, so the GCD will be a power of 2, |
| 1680 | // which means we get to do this crazy thing instead of Euclid's. |
| 1681 | uint64_t LowBitOfOffset = Offset & (~Offset + 1); |
| 1682 | if (LowBitOfOffset < FieldAlign) |
| 1683 | FieldAlign = static_cast<unsigned>(LowBitOfOffset); |
| 1684 | } |
| 1685 | |
| 1686 | Align = std::min(Align, FieldAlign); |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 1687 | } |
Charles Davis | 3fc5107 | 2010-02-23 04:52:00 +0000 | [diff] [blame] | 1688 | } |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1689 | } |
Eli Friedman | 19a546c | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1690 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 1691 | return toCharUnitsFromBits(Align); |
Chris Lattner | 6806131 | 2009-01-24 21:53:27 +0000 | [diff] [blame] | 1692 | } |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1693 | |
John McCall | f124992 | 2012-08-21 04:10:00 +0000 | [diff] [blame] | 1694 | // getTypeInfoDataSizeInChars - Return the size of a type, in |
| 1695 | // chars. If the type is a record, its data size is returned. This is |
| 1696 | // the size of the memcpy that's performed when assigning this type |
| 1697 | // using a trivial copy/move assignment operator. |
| 1698 | std::pair<CharUnits, CharUnits> |
| 1699 | ASTContext::getTypeInfoDataSizeInChars(QualType T) const { |
| 1700 | std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T); |
| 1701 | |
| 1702 | // In C++, objects can sometimes be allocated into the tail padding |
| 1703 | // of a base-class subobject. We decide whether that's possible |
| 1704 | // during class layout, so here we can just trust the layout results. |
| 1705 | if (getLangOpts().CPlusPlus) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1706 | if (const auto *RT = T->getAs<RecordType>()) { |
John McCall | f124992 | 2012-08-21 04:10:00 +0000 | [diff] [blame] | 1707 | const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl()); |
| 1708 | sizeAndAlign.first = layout.getDataSize(); |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | return sizeAndAlign; |
| 1713 | } |
| 1714 | |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1715 | /// getConstantArrayInfoInChars - Performing the computation in CharUnits |
| 1716 | /// instead of in bits prevents overflowing the uint64_t for some large arrays. |
| 1717 | std::pair<CharUnits, CharUnits> |
| 1718 | static getConstantArrayInfoInChars(const ASTContext &Context, |
| 1719 | const ConstantArrayType *CAT) { |
| 1720 | std::pair<CharUnits, CharUnits> EltInfo = |
| 1721 | Context.getTypeInfoInChars(CAT->getElementType()); |
| 1722 | uint64_t Size = CAT->getSize().getZExtValue(); |
Richard Trieu | c750907 | 2013-05-14 23:41:50 +0000 | [diff] [blame] | 1723 | assert((Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity()) <= |
| 1724 | (uint64_t)(-1)/Size) && |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1725 | "Overflow in array type char size evaluation"); |
| 1726 | uint64_t Width = EltInfo.first.getQuantity() * Size; |
| 1727 | unsigned Align = EltInfo.second.getQuantity(); |
Warren Hunt | 5ae586a | 2013-11-01 23:59:41 +0000 | [diff] [blame] | 1728 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 1729 | Context.getTargetInfo().getPointerWidth(0) == 64) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1730 | Width = llvm::alignTo(Width, Align); |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1731 | return std::make_pair(CharUnits::fromQuantity(Width), |
| 1732 | CharUnits::fromQuantity(Align)); |
| 1733 | } |
| 1734 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1735 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1736 | ASTContext::getTypeInfoInChars(const Type *T) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1737 | if (const auto *CAT = dyn_cast<ConstantArrayType>(T)) |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 1738 | return getConstantArrayInfoInChars(*this, CAT); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1739 | TypeInfo Info = getTypeInfo(T); |
| 1740 | return std::make_pair(toCharUnitsFromBits(Info.Width), |
| 1741 | toCharUnitsFromBits(Info.Align)); |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | std::pair<CharUnits, CharUnits> |
Ken Dyck | d24099d | 2011-02-20 01:55:18 +0000 | [diff] [blame] | 1745 | ASTContext::getTypeInfoInChars(QualType T) const { |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1746 | return getTypeInfoInChars(T.getTypePtr()); |
| 1747 | } |
| 1748 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1749 | bool ASTContext::isAlignmentRequired(const Type *T) const { |
| 1750 | return getTypeInfo(T).AlignIsRequired; |
| 1751 | } |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1752 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1753 | bool ASTContext::isAlignmentRequired(QualType T) const { |
| 1754 | return isAlignmentRequired(T.getTypePtr()); |
| 1755 | } |
| 1756 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1757 | unsigned ASTContext::getTypeAlignIfKnown(QualType T) const { |
| 1758 | // An alignment on a typedef overrides anything else. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1759 | if (const auto *TT = T->getAs<TypedefType>()) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1760 | if (unsigned Align = TT->getDecl()->getMaxAlignment()) |
| 1761 | return Align; |
| 1762 | |
| 1763 | // If we have an (array of) complete type, we're done. |
| 1764 | T = getBaseElementType(T); |
| 1765 | if (!T->isIncompleteType()) |
| 1766 | return getTypeAlign(T); |
| 1767 | |
| 1768 | // If we had an array type, its element type might be a typedef |
| 1769 | // type with an alignment attribute. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1770 | if (const auto *TT = T->getAs<TypedefType>()) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1771 | if (unsigned Align = TT->getDecl()->getMaxAlignment()) |
| 1772 | return Align; |
| 1773 | |
| 1774 | // Otherwise, see if the declaration of the type had an attribute. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1775 | if (const auto *TT = T->getAs<TagType>()) |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 1776 | return TT->getDecl()->getMaxAlignment(); |
| 1777 | |
| 1778 | return 0; |
| 1779 | } |
| 1780 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1781 | TypeInfo ASTContext::getTypeInfo(const Type *T) const { |
David Majnemer | f8d3864 | 2014-07-30 08:42:33 +0000 | [diff] [blame] | 1782 | TypeInfoMap::iterator I = MemoizedTypeInfo.find(T); |
| 1783 | if (I != MemoizedTypeInfo.end()) |
| 1784 | return I->second; |
| 1785 | |
| 1786 | // This call can invalidate MemoizedTypeInfo[T], so we need a second lookup. |
| 1787 | TypeInfo TI = getTypeInfoImpl(T); |
| 1788 | MemoizedTypeInfo[T] = TI; |
Rafael Espindola | eaa88c1 | 2014-07-30 04:40:23 +0000 | [diff] [blame] | 1789 | return TI; |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | /// getTypeInfoImpl - Return the size of the specified type, in bits. This |
| 1793 | /// method does not work on incomplete types. |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1794 | /// |
| 1795 | /// FIXME: Pointers into different addr spaces could have different sizes and |
| 1796 | /// alignment requirements: getPointerInfo should take an AddrSpace, this |
| 1797 | /// should take a QualType, &c. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1798 | TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const { |
| 1799 | uint64_t Width = 0; |
| 1800 | unsigned Align = 8; |
| 1801 | bool AlignIsRequired = false; |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 1802 | unsigned AS = 0; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1803 | switch (T->getTypeClass()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1804 | #define TYPE(Class, Base) |
| 1805 | #define ABSTRACT_TYPE(Class, Base) |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1806 | #define NON_CANONICAL_TYPE(Class, Base) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1807 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
David Blaikie | ab277d6 | 2013-07-13 21:08:03 +0000 | [diff] [blame] | 1808 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) \ |
| 1809 | case Type::Class: \ |
| 1810 | assert(!T->isDependentType() && "should not see dependent types here"); \ |
| 1811 | return getTypeInfo(cast<Class##Type>(T)->desugar().getTypePtr()); |
John McCall | 36b12a8 | 2019-10-02 06:35:23 +0000 | [diff] [blame] | 1812 | #include "clang/AST/TypeNodes.inc" |
John McCall | 15547bb | 2011-06-28 16:49:23 +0000 | [diff] [blame] | 1813 | llvm_unreachable("Should not see dependent types"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1814 | |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1815 | case Type::FunctionNoProto: |
| 1816 | case Type::FunctionProto: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1817 | // GCC extension: alignof(function) = 32 bits |
| 1818 | Width = 0; |
| 1819 | Align = 32; |
| 1820 | break; |
| 1821 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1822 | case Type::IncompleteArray: |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1823 | case Type::VariableArray: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1824 | Width = 0; |
| 1825 | Align = getTypeAlign(cast<ArrayType>(T)->getElementType()); |
| 1826 | break; |
| 1827 | |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 1828 | case Type::ConstantArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1829 | const auto *CAT = cast<ConstantArrayType>(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1830 | |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1831 | TypeInfo EltInfo = getTypeInfo(CAT->getElementType()); |
Abramo Bagnara | d541a4c | 2011-12-13 11:23:52 +0000 | [diff] [blame] | 1832 | uint64_t Size = CAT->getSize().getZExtValue(); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1833 | assert((Size == 0 || EltInfo.Width <= (uint64_t)(-1) / Size) && |
Daniel Dunbar | c647587 | 2012-03-09 04:12:54 +0000 | [diff] [blame] | 1834 | "Overflow in array type bit size evaluation"); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1835 | Width = EltInfo.Width * Size; |
| 1836 | Align = EltInfo.Align; |
Warren Hunt | 5ae586a | 2013-11-01 23:59:41 +0000 | [diff] [blame] | 1837 | if (!getTargetInfo().getCXXABI().isMicrosoft() || |
| 1838 | getTargetInfo().getPointerWidth(0) == 64) |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1839 | Width = llvm::alignTo(Width, Align); |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1840 | break; |
Christopher Lamb | c5fafa2 | 2007-12-29 05:10:55 +0000 | [diff] [blame] | 1841 | } |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1842 | case Type::ExtVector: |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1843 | case Type::Vector: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1844 | const auto *VT = cast<VectorType>(T); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 1845 | TypeInfo EltInfo = getTypeInfo(VT->getElementType()); |
| 1846 | Width = EltInfo.Width * VT->getNumElements(); |
Eli Friedman | 3df5efe | 2008-05-30 09:31:38 +0000 | [diff] [blame] | 1847 | Align = Width; |
Nate Begeman | b699c9b | 2009-01-18 06:42:49 +0000 | [diff] [blame] | 1848 | // If the alignment is not a power of 2, round up to the next power of 2. |
| 1849 | // This happens for non-power-of-2 length vectors. |
Dan Gohman | ef78c8e | 2010-04-21 23:32:43 +0000 | [diff] [blame] | 1850 | if (Align & (Align-1)) { |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1851 | Align = llvm::NextPowerOf2(Align); |
Rui Ueyama | 83aa979 | 2016-01-14 21:00:27 +0000 | [diff] [blame] | 1852 | Width = llvm::alignTo(Width, Align); |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 1853 | } |
Chad Rosier | cc40ea7 | 2012-07-13 23:57:43 +0000 | [diff] [blame] | 1854 | // Adjust the alignment based on the target max. |
| 1855 | uint64_t TargetVectorAlign = Target->getMaxVectorAlign(); |
| 1856 | if (TargetVectorAlign && TargetVectorAlign < Align) |
| 1857 | Align = TargetVectorAlign; |
Chris Lattner | f2e101f | 2007-07-19 22:06:24 +0000 | [diff] [blame] | 1858 | break; |
| 1859 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 1860 | |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1861 | case Type::Builtin: |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 1862 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1863 | default: llvm_unreachable("Unknown builtin type!"); |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 1864 | case BuiltinType::Void: |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 1865 | // GCC extension: alignof(void) = 8 bits. |
| 1866 | Width = 0; |
| 1867 | Align = 8; |
| 1868 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1869 | case BuiltinType::Bool: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1870 | Width = Target->getBoolWidth(); |
| 1871 | Align = Target->getBoolAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1872 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1873 | case BuiltinType::Char_S: |
| 1874 | case BuiltinType::Char_U: |
| 1875 | case BuiltinType::UChar: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1876 | case BuiltinType::SChar: |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 1877 | case BuiltinType::Char8: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1878 | Width = Target->getCharWidth(); |
| 1879 | Align = Target->getCharAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1880 | break; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 1881 | case BuiltinType::WChar_S: |
| 1882 | case BuiltinType::WChar_U: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1883 | Width = Target->getWCharWidth(); |
| 1884 | Align = Target->getWCharAlign(); |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 1885 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1886 | case BuiltinType::Char16: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1887 | Width = Target->getChar16Width(); |
| 1888 | Align = Target->getChar16Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1889 | break; |
| 1890 | case BuiltinType::Char32: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1891 | Width = Target->getChar32Width(); |
| 1892 | Align = Target->getChar32Align(); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 1893 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1894 | case BuiltinType::UShort: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1895 | case BuiltinType::Short: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1896 | Width = Target->getShortWidth(); |
| 1897 | Align = Target->getShortAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1898 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1899 | case BuiltinType::UInt: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1900 | case BuiltinType::Int: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1901 | Width = Target->getIntWidth(); |
| 1902 | Align = Target->getIntAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1903 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1904 | case BuiltinType::ULong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1905 | case BuiltinType::Long: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1906 | Width = Target->getLongWidth(); |
| 1907 | Align = Target->getLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1908 | break; |
Chris Lattner | 355332d | 2007-07-13 22:27:08 +0000 | [diff] [blame] | 1909 | case BuiltinType::ULongLong: |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1910 | case BuiltinType::LongLong: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1911 | Width = Target->getLongLongWidth(); |
| 1912 | Align = Target->getLongLongAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1913 | break; |
Chris Lattner | 0a415ec | 2009-04-30 02:55:13 +0000 | [diff] [blame] | 1914 | case BuiltinType::Int128: |
| 1915 | case BuiltinType::UInt128: |
| 1916 | Width = 128; |
| 1917 | Align = 128; // int128_t is 128-bit aligned on all targets. |
| 1918 | break; |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1919 | case BuiltinType::ShortAccum: |
| 1920 | case BuiltinType::UShortAccum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1921 | case BuiltinType::SatShortAccum: |
| 1922 | case BuiltinType::SatUShortAccum: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1923 | Width = Target->getShortAccumWidth(); |
| 1924 | Align = Target->getShortAccumAlign(); |
| 1925 | break; |
| 1926 | case BuiltinType::Accum: |
| 1927 | case BuiltinType::UAccum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1928 | case BuiltinType::SatAccum: |
| 1929 | case BuiltinType::SatUAccum: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1930 | Width = Target->getAccumWidth(); |
| 1931 | Align = Target->getAccumAlign(); |
| 1932 | break; |
| 1933 | case BuiltinType::LongAccum: |
| 1934 | case BuiltinType::ULongAccum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1935 | case BuiltinType::SatLongAccum: |
| 1936 | case BuiltinType::SatULongAccum: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 1937 | Width = Target->getLongAccumWidth(); |
| 1938 | Align = Target->getLongAccumAlign(); |
| 1939 | break; |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 1940 | case BuiltinType::ShortFract: |
| 1941 | case BuiltinType::UShortFract: |
| 1942 | case BuiltinType::SatShortFract: |
| 1943 | case BuiltinType::SatUShortFract: |
| 1944 | Width = Target->getShortFractWidth(); |
| 1945 | Align = Target->getShortFractAlign(); |
| 1946 | break; |
| 1947 | case BuiltinType::Fract: |
| 1948 | case BuiltinType::UFract: |
| 1949 | case BuiltinType::SatFract: |
| 1950 | case BuiltinType::SatUFract: |
| 1951 | Width = Target->getFractWidth(); |
| 1952 | Align = Target->getFractAlign(); |
| 1953 | break; |
| 1954 | case BuiltinType::LongFract: |
| 1955 | case BuiltinType::ULongFract: |
| 1956 | case BuiltinType::SatLongFract: |
| 1957 | case BuiltinType::SatULongFract: |
| 1958 | Width = Target->getLongFractWidth(); |
| 1959 | Align = Target->getLongFractAlign(); |
| 1960 | break; |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 1961 | case BuiltinType::Float16: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1962 | case BuiltinType::Half: |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 1963 | if (Target->hasFloat16Type() || !getLangOpts().OpenMP || |
| 1964 | !getLangOpts().OpenMPIsDevice) { |
| 1965 | Width = Target->getHalfWidth(); |
| 1966 | Align = Target->getHalfAlign(); |
| 1967 | } else { |
| 1968 | assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && |
| 1969 | "Expected OpenMP device compilation."); |
| 1970 | Width = AuxTarget->getHalfWidth(); |
| 1971 | Align = AuxTarget->getHalfAlign(); |
| 1972 | } |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 1973 | break; |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1974 | case BuiltinType::Float: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1975 | Width = Target->getFloatWidth(); |
| 1976 | Align = Target->getFloatAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1977 | break; |
| 1978 | case BuiltinType::Double: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1979 | Width = Target->getDoubleWidth(); |
| 1980 | Align = Target->getDoubleAlign(); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1981 | break; |
| 1982 | case BuiltinType::LongDouble: |
Alexey Bataev | 8557d1a | 2019-06-18 18:39:26 +0000 | [diff] [blame] | 1983 | if (getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && |
| 1984 | (Target->getLongDoubleWidth() != AuxTarget->getLongDoubleWidth() || |
| 1985 | Target->getLongDoubleAlign() != AuxTarget->getLongDoubleAlign())) { |
| 1986 | Width = AuxTarget->getLongDoubleWidth(); |
| 1987 | Align = AuxTarget->getLongDoubleAlign(); |
| 1988 | } else { |
| 1989 | Width = Target->getLongDoubleWidth(); |
| 1990 | Align = Target->getLongDoubleAlign(); |
| 1991 | } |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 1992 | break; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 1993 | case BuiltinType::Float128: |
Alexey Bataev | 123ad19 | 2019-02-27 20:29:45 +0000 | [diff] [blame] | 1994 | if (Target->hasFloat128Type() || !getLangOpts().OpenMP || |
| 1995 | !getLangOpts().OpenMPIsDevice) { |
| 1996 | Width = Target->getFloat128Width(); |
| 1997 | Align = Target->getFloat128Align(); |
| 1998 | } else { |
| 1999 | assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice && |
| 2000 | "Expected OpenMP device compilation."); |
| 2001 | Width = AuxTarget->getFloat128Width(); |
| 2002 | Align = AuxTarget->getFloat128Align(); |
| 2003 | } |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 2004 | break; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2005 | case BuiltinType::NullPtr: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2006 | Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t) |
| 2007 | Align = Target->getPointerAlign(0); // == sizeof(void*) |
Sebastian Redl | a81b0b7 | 2009-05-27 19:34:06 +0000 | [diff] [blame] | 2008 | break; |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 2009 | case BuiltinType::ObjCId: |
| 2010 | case BuiltinType::ObjCClass: |
| 2011 | case BuiltinType::ObjCSel: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2012 | Width = Target->getPointerWidth(0); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2013 | Align = Target->getPointerAlign(0); |
Fariborz Jahanian | 9c6a39e | 2010-08-02 18:03:20 +0000 | [diff] [blame] | 2014 | break; |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 2015 | case BuiltinType::OCLSampler: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2016 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 2017 | case BuiltinType::OCLClkEvent: |
| 2018 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 2019 | case BuiltinType::OCLReserveID: |
Yaxun Liu | 99444cb | 2016-08-03 20:38:06 +0000 | [diff] [blame] | 2020 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 2021 | case BuiltinType::Id: |
| 2022 | #include "clang/Basic/OpenCLImageTypes.def" |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 2023 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
| 2024 | case BuiltinType::Id: |
| 2025 | #include "clang/Basic/OpenCLExtensionTypes.def" |
Sven van Haastregt | 3bb7eaf | 2017-12-06 10:11:28 +0000 | [diff] [blame] | 2026 | AS = getTargetAddressSpace( |
| 2027 | Target->getOpenCLTypeAddrSpace(getOpenCLTypeKind(T))); |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 2028 | Width = Target->getPointerWidth(AS); |
| 2029 | Align = Target->getPointerAlign(AS); |
| 2030 | break; |
Richard Sandiford | eb485fb | 2019-08-09 08:52:54 +0000 | [diff] [blame] | 2031 | // The SVE types are effectively target-specific. The length of an |
| 2032 | // SVE_VECTOR_TYPE is only known at runtime, but it is always a multiple |
| 2033 | // of 128 bits. There is one predicate bit for each vector byte, so the |
| 2034 | // length of an SVE_PREDICATE_TYPE is always a multiple of 16 bits. |
| 2035 | // |
| 2036 | // Because the length is only known at runtime, we use a dummy value |
| 2037 | // of 0 for the static length. The alignment values are those defined |
| 2038 | // by the Procedure Call Standard for the Arm Architecture. |
| 2039 | #define SVE_VECTOR_TYPE(Name, Id, SingletonId, ElKind, ElBits, IsSigned, IsFP)\ |
| 2040 | case BuiltinType::Id: \ |
| 2041 | Width = 0; \ |
| 2042 | Align = 128; \ |
| 2043 | break; |
| 2044 | #define SVE_PREDICATE_TYPE(Name, Id, SingletonId, ElKind) \ |
| 2045 | case BuiltinType::Id: \ |
| 2046 | Width = 0; \ |
| 2047 | Align = 16; \ |
| 2048 | break; |
| 2049 | #include "clang/Basic/AArch64SVEACLETypes.def" |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2050 | } |
Chris Lattner | 48f84b8 | 2007-07-15 23:46:53 +0000 | [diff] [blame] | 2051 | break; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2052 | case Type::ObjCObjectPointer: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2053 | Width = Target->getPointerWidth(0); |
| 2054 | Align = Target->getPointerAlign(0); |
Chris Lattner | 6a4f745 | 2007-12-19 19:23:28 +0000 | [diff] [blame] | 2055 | break; |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2056 | case Type::BlockPointer: |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 2057 | AS = getTargetAddressSpace(cast<BlockPointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2058 | Width = Target->getPointerWidth(AS); |
| 2059 | Align = Target->getPointerAlign(AS); |
Steve Naroff | 921a45c | 2008-09-24 15:05:44 +0000 | [diff] [blame] | 2060 | break; |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2061 | case Type::LValueReference: |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2062 | case Type::RValueReference: |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2063 | // alignof and sizeof should never enter this code path here, so we go |
| 2064 | // the pointer route. |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 2065 | AS = getTargetAddressSpace(cast<ReferenceType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2066 | Width = Target->getPointerWidth(AS); |
| 2067 | Align = Target->getPointerAlign(AS); |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2068 | break; |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2069 | case Type::Pointer: |
Sven van Haastregt | efb4d4c | 2017-08-15 09:38:18 +0000 | [diff] [blame] | 2070 | AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2071 | Width = Target->getPointerWidth(AS); |
| 2072 | Align = Target->getPointerAlign(AS); |
Chris Lattner | 2dca6ff | 2008-03-08 08:34:58 +0000 | [diff] [blame] | 2073 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2074 | case Type::MemberPointer: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2075 | const auto *MPT = cast<MemberPointerType>(T); |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2076 | CXXABI::MemberPointerInfo MPI = ABI->getMemberPointerInfo(MPT); |
| 2077 | Width = MPI.Width; |
| 2078 | Align = MPI.Align; |
Anders Carlsson | 32440a0 | 2009-05-17 02:06:04 +0000 | [diff] [blame] | 2079 | break; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 2080 | } |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 2081 | case Type::Complex: { |
| 2082 | // Complex types have the same alignment as their elements, but twice the |
| 2083 | // size. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2084 | TypeInfo EltInfo = getTypeInfo(cast<ComplexType>(T)->getElementType()); |
| 2085 | Width = EltInfo.Width * 2; |
| 2086 | Align = EltInfo.Align; |
Chris Lattner | 647fb22 | 2007-07-18 18:26:58 +0000 | [diff] [blame] | 2087 | break; |
| 2088 | } |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2089 | case Type::ObjCObject: |
| 2090 | return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr()); |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2091 | case Type::Adjusted: |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2092 | case Type::Decayed: |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2093 | return getTypeInfo(cast<AdjustedType>(T)->getAdjustedType().getTypePtr()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 2094 | case Type::ObjCInterface: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2095 | const auto *ObjCI = cast<ObjCInterfaceType>(T); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 2096 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 2097 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 2098 | Align = toBits(Layout.getAlignment()); |
Devang Patel | dbb7263 | 2008-06-04 21:54:36 +0000 | [diff] [blame] | 2099 | break; |
| 2100 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2101 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2102 | case Type::Enum: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2103 | const auto *TT = cast<TagType>(T); |
Daniel Dunbar | bbc0af7 | 2008-11-08 05:48:37 +0000 | [diff] [blame] | 2104 | |
| 2105 | if (TT->getDecl()->isInvalidDecl()) { |
Douglas Gregor | 7f97189 | 2011-04-20 17:29:44 +0000 | [diff] [blame] | 2106 | Width = 8; |
| 2107 | Align = 8; |
Chris Lattner | 572100b | 2008-08-09 21:35:13 +0000 | [diff] [blame] | 2108 | break; |
| 2109 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2110 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2111 | if (const auto *ET = dyn_cast<EnumType>(TT)) { |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 2112 | const EnumDecl *ED = ET->getDecl(); |
| 2113 | TypeInfo Info = |
| 2114 | getTypeInfo(ED->getIntegerType()->getUnqualifiedDesugaredType()); |
| 2115 | if (unsigned AttrAlign = ED->getMaxAlignment()) { |
| 2116 | Info.Align = AttrAlign; |
| 2117 | Info.AlignIsRequired = true; |
| 2118 | } |
| 2119 | return Info; |
| 2120 | } |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 2121 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2122 | const auto *RT = cast<RecordType>(TT); |
David Majnemer | 5821ff7 | 2015-02-03 08:49:29 +0000 | [diff] [blame] | 2123 | const RecordDecl *RD = RT->getDecl(); |
| 2124 | const ASTRecordLayout &Layout = getASTRecordLayout(RD); |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 2125 | Width = toBits(Layout.getSize()); |
Ken Dyck | 7ad11e7 | 2011-02-15 02:32:40 +0000 | [diff] [blame] | 2126 | Align = toBits(Layout.getAlignment()); |
David Majnemer | 5821ff7 | 2015-02-03 08:49:29 +0000 | [diff] [blame] | 2127 | AlignIsRequired = RD->hasAttr<AlignedAttr>(); |
Chris Lattner | 49a953a | 2007-07-23 22:46:22 +0000 | [diff] [blame] | 2128 | break; |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2129 | } |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2130 | |
Chris Lattner | 63d2b36 | 2009-10-22 05:17:15 +0000 | [diff] [blame] | 2131 | case Type::SubstTemplateTypeParm: |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2132 | return getTypeInfo(cast<SubstTemplateTypeParmType>(T)-> |
| 2133 | getReplacementType().getTypePtr()); |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 2134 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 2135 | case Type::Auto: |
| 2136 | case Type::DeducedTemplateSpecialization: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2137 | const auto *A = cast<DeducedType>(T); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 2138 | assert(!A->getDeducedType().isNull() && |
| 2139 | "cannot request the size of an undeduced or dependent auto type"); |
Matt Beaumont-Gay | 7a24210e | 2011-02-22 20:00:16 +0000 | [diff] [blame] | 2140 | return getTypeInfo(A->getDeducedType().getTypePtr()); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 2143 | case Type::Paren: |
| 2144 | return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr()); |
| 2145 | |
Leonard Chan | c72aaf6 | 2019-05-07 03:20:17 +0000 | [diff] [blame] | 2146 | case Type::MacroQualified: |
| 2147 | return getTypeInfo( |
| 2148 | cast<MacroQualifiedType>(T)->getUnderlyingType().getTypePtr()); |
| 2149 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 2150 | case Type::ObjCTypeParam: |
| 2151 | return getTypeInfo(cast<ObjCTypeParamType>(T)->desugar().getTypePtr()); |
| 2152 | |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 2153 | case Type::Typedef: { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2154 | const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl(); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2155 | TypeInfo Info = getTypeInfo(Typedef->getUnderlyingType().getTypePtr()); |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 2156 | // If the typedef has an aligned attribute on it, it overrides any computed |
| 2157 | // alignment we have. This violates the GCC documentation (which says that |
| 2158 | // attribute(aligned) can only round up) but matches its implementation. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2159 | if (unsigned AttrAlign = Typedef->getMaxAlignment()) { |
Chris Lattner | 29eb47b | 2011-02-19 22:55:41 +0000 | [diff] [blame] | 2160 | Align = AttrAlign; |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2161 | AlignIsRequired = true; |
David Majnemer | 37bffb6 | 2014-08-04 05:11:01 +0000 | [diff] [blame] | 2162 | } else { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2163 | Align = Info.Align; |
David Majnemer | 37bffb6 | 2014-08-04 05:11:01 +0000 | [diff] [blame] | 2164 | AlignIsRequired = Info.AlignIsRequired; |
| 2165 | } |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2166 | Width = Info.Width; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 2167 | break; |
Chris Lattner | 8b23c25 | 2008-04-06 22:05:18 +0000 | [diff] [blame] | 2168 | } |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 2169 | |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 2170 | case Type::Elaborated: |
| 2171 | return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 2173 | case Type::Attributed: |
| 2174 | return getTypeInfo( |
| 2175 | cast<AttributedType>(T)->getEquivalentType().getTypePtr()); |
| 2176 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2177 | case Type::Atomic: { |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 2178 | // Start with the base type information. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2179 | TypeInfo Info = getTypeInfo(cast<AtomicType>(T)->getValueType()); |
| 2180 | Width = Info.Width; |
| 2181 | Align = Info.Align; |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 2182 | |
JF Bastien | 801fca2 | 2018-05-09 03:51:12 +0000 | [diff] [blame] | 2183 | if (!Width) { |
| 2184 | // An otherwise zero-sized type should still generate an |
| 2185 | // atomic operation. |
| 2186 | Width = Target->getCharWidth(); |
| 2187 | assert(Align); |
| 2188 | } else if (Width <= Target->getMaxAtomicPromoteWidth()) { |
| 2189 | // If the size of the type doesn't exceed the platform's max |
| 2190 | // atomic promotion width, make the size and alignment more |
| 2191 | // favorable to atomic operations: |
| 2192 | |
John McCall | a8ec7eb | 2013-03-07 21:37:17 +0000 | [diff] [blame] | 2193 | // Round the size up to a power of 2. |
| 2194 | if (!llvm::isPowerOf2_64(Width)) |
| 2195 | Width = llvm::NextPowerOf2(Width); |
| 2196 | |
| 2197 | // Set the alignment equal to the size. |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 2198 | Align = static_cast<unsigned>(Width); |
| 2199 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2200 | } |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 2201 | break; |
| 2202 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2203 | case Type::Pipe: |
Anastasia Stulova | b339893 | 2017-06-05 11:27:03 +0000 | [diff] [blame] | 2204 | Width = Target->getPointerWidth(getTargetAddressSpace(LangAS::opencl_global)); |
| 2205 | Align = Target->getPointerAlign(getTargetAddressSpace(LangAS::opencl_global)); |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2206 | break; |
Douglas Gregor | ef462e6 | 2009-04-30 17:32:17 +0000 | [diff] [blame] | 2207 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2208 | |
Eli Friedman | 4b72fdd | 2011-10-14 20:59:01 +0000 | [diff] [blame] | 2209 | assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2"); |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2210 | return TypeInfo(Width, Align, AlignIsRequired); |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 2213 | unsigned ASTContext::getTypeUnadjustedAlign(const Type *T) const { |
| 2214 | UnadjustedAlignMap::iterator I = MemoizedUnadjustedAlign.find(T); |
| 2215 | if (I != MemoizedUnadjustedAlign.end()) |
| 2216 | return I->second; |
| 2217 | |
| 2218 | unsigned UnadjustedAlign; |
| 2219 | if (const auto *RT = T->getAs<RecordType>()) { |
| 2220 | const RecordDecl *RD = RT->getDecl(); |
| 2221 | const ASTRecordLayout &Layout = getASTRecordLayout(RD); |
| 2222 | UnadjustedAlign = toBits(Layout.getUnadjustedAlignment()); |
| 2223 | } else if (const auto *ObjCI = T->getAs<ObjCInterfaceType>()) { |
| 2224 | const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); |
| 2225 | UnadjustedAlign = toBits(Layout.getUnadjustedAlignment()); |
| 2226 | } else { |
John Brawn | 6c49f58 | 2019-05-22 11:42:54 +0000 | [diff] [blame] | 2227 | UnadjustedAlign = getTypeAlign(T->getUnqualifiedDesugaredType()); |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | MemoizedUnadjustedAlign[T] = UnadjustedAlign; |
| 2231 | return UnadjustedAlign; |
| 2232 | } |
| 2233 | |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 2234 | unsigned ASTContext::getOpenMPDefaultSimdAlign(QualType T) const { |
| 2235 | unsigned SimdAlign = getTargetInfo().getSimdDefaultAlign(); |
| 2236 | // Target ppc64 with QPX: simd default alignment for pointer to double is 32. |
| 2237 | if ((getTargetInfo().getTriple().getArch() == llvm::Triple::ppc64 || |
| 2238 | getTargetInfo().getTriple().getArch() == llvm::Triple::ppc64le) && |
| 2239 | getTargetInfo().getABI() == "elfv1-qpx" && |
| 2240 | T->isSpecificBuiltinType(BuiltinType::Double)) |
| 2241 | SimdAlign = 256; |
| 2242 | return SimdAlign; |
| 2243 | } |
| 2244 | |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 2245 | /// toCharUnitsFromBits - Convert a size in bits to a size in characters. |
| 2246 | CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { |
| 2247 | return CharUnits::fromQuantity(BitSize / getCharWidth()); |
| 2248 | } |
| 2249 | |
Ken Dyck | b0fcc59 | 2011-02-11 01:54:29 +0000 | [diff] [blame] | 2250 | /// toBits - Convert a size in characters to a size in characters. |
| 2251 | int64_t ASTContext::toBits(CharUnits CharSize) const { |
| 2252 | return CharSize.getQuantity() * getCharWidth(); |
| 2253 | } |
| 2254 | |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 2255 | /// getTypeSizeInChars - Return the size of the specified type, in characters. |
| 2256 | /// This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2257 | CharUnits ASTContext::getTypeSizeInChars(QualType T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 2258 | return getTypeInfoInChars(T).first; |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 2259 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2260 | CharUnits ASTContext::getTypeSizeInChars(const Type *T) const { |
Richard Trieu | 04d2d94 | 2013-05-14 21:59:17 +0000 | [diff] [blame] | 2261 | return getTypeInfoInChars(T).first; |
Ken Dyck | 8c89d59 | 2009-12-22 14:23:30 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2264 | /// getTypeAlignInChars - Return the ABI-specified alignment of a type, in |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 2265 | /// characters. This method does not work on incomplete types. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2266 | CharUnits ASTContext::getTypeAlignInChars(QualType T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 2267 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 2268 | } |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2269 | CharUnits ASTContext::getTypeAlignInChars(const Type *T) const { |
Ken Dyck | cc56c54 | 2011-01-15 18:38:59 +0000 | [diff] [blame] | 2270 | return toCharUnitsFromBits(getTypeAlign(T)); |
Ken Dyck | 24d28d6 | 2010-01-26 17:22:55 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Momchil Velikov | 20208cc | 2018-07-30 17:48:23 +0000 | [diff] [blame] | 2273 | /// getTypeUnadjustedAlignInChars - Return the ABI-specified alignment of a |
| 2274 | /// type, in characters, before alignment adustments. This method does |
| 2275 | /// not work on incomplete types. |
| 2276 | CharUnits ASTContext::getTypeUnadjustedAlignInChars(QualType T) const { |
| 2277 | return toCharUnitsFromBits(getTypeUnadjustedAlign(T)); |
| 2278 | } |
| 2279 | CharUnits ASTContext::getTypeUnadjustedAlignInChars(const Type *T) const { |
| 2280 | return toCharUnitsFromBits(getTypeUnadjustedAlign(T)); |
| 2281 | } |
| 2282 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 2283 | /// getPreferredTypeAlign - Return the "preferred" alignment of the specified |
| 2284 | /// type for the current target in bits. This can be different than the ABI |
| 2285 | /// alignment in cases where it is beneficial for performance to overalign |
| 2286 | /// a data type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2287 | unsigned ASTContext::getPreferredTypeAlign(const Type *T) const { |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2288 | TypeInfo TI = getTypeInfo(T); |
| 2289 | unsigned ABIAlign = TI.Align; |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2290 | |
David Majnemer | e154456 | 2015-04-24 01:25:05 +0000 | [diff] [blame] | 2291 | T = T->getBaseElementTypeUnsafe(); |
| 2292 | |
| 2293 | // The preferred alignment of member pointers is that of a pointer. |
| 2294 | if (T->isMemberPointerType()) |
| 2295 | return getPreferredTypeAlign(getPointerDiffType().getTypePtr()); |
| 2296 | |
Andrey Turetskiy | db6655f | 2016-02-10 11:58:46 +0000 | [diff] [blame] | 2297 | if (!Target->allowsLargerPreferedTypeAlignment()) |
| 2298 | return ABIAlign; |
Robert Lytton | eaf6f36 | 2013-11-12 10:09:34 +0000 | [diff] [blame] | 2299 | |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2300 | // Double and long long should be naturally aligned if possible. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2301 | if (const auto *CT = T->getAs<ComplexType>()) |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2302 | T = CT->getElementType().getTypePtr(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2303 | if (const auto *ET = T->getAs<EnumType>()) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 2304 | T = ET->getDecl()->getIntegerType().getTypePtr(); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2305 | if (T->isSpecificBuiltinType(BuiltinType::Double) || |
Chad Rosier | b57321a | 2012-03-21 20:20:47 +0000 | [diff] [blame] | 2306 | T->isSpecificBuiltinType(BuiltinType::LongLong) || |
| 2307 | T->isSpecificBuiltinType(BuiltinType::ULongLong)) |
David Majnemer | 8b6bd57 | 2014-02-24 23:34:17 +0000 | [diff] [blame] | 2308 | // Don't increase the alignment if an alignment attribute was specified on a |
| 2309 | // typedef declaration. |
David Majnemer | 34b5749 | 2014-07-30 01:30:47 +0000 | [diff] [blame] | 2310 | if (!TI.AlignIsRequired) |
David Majnemer | 8b6bd57 | 2014-02-24 23:34:17 +0000 | [diff] [blame] | 2311 | return std::max(ABIAlign, (unsigned)getTypeSize(T)); |
Eli Friedman | 7ab0957 | 2009-05-25 21:27:19 +0000 | [diff] [blame] | 2312 | |
Chris Lattner | a3402cd | 2009-01-27 18:08:34 +0000 | [diff] [blame] | 2313 | return ABIAlign; |
| 2314 | } |
| 2315 | |
Ulrich Weigand | ca3cb7f | 2015-04-21 17:29:35 +0000 | [diff] [blame] | 2316 | /// getTargetDefaultAlignForAttributeAligned - Return the default alignment |
| 2317 | /// for __attribute__((aligned)) on this target, to be used if no alignment |
| 2318 | /// value is specified. |
Eugene Zelenko | d4304d2 | 2015-11-04 21:37:17 +0000 | [diff] [blame] | 2319 | unsigned ASTContext::getTargetDefaultAlignForAttributeAligned() const { |
Ulrich Weigand | ca3cb7f | 2015-04-21 17:29:35 +0000 | [diff] [blame] | 2320 | return getTargetInfo().getDefaultAlignForAttributeAligned(); |
| 2321 | } |
| 2322 | |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 2323 | /// getAlignOfGlobalVar - Return the alignment in bits that should be given |
| 2324 | /// to a global variable of the specified type. |
| 2325 | unsigned ASTContext::getAlignOfGlobalVar(QualType T) const { |
Tom Tan | b7c6d95 | 2019-05-02 00:38:14 +0000 | [diff] [blame] | 2326 | uint64_t TypeSize = getTypeSize(T.getTypePtr()); |
| 2327 | return std::max(getTypeAlign(T), getTargetInfo().getMinGlobalAlign(TypeSize)); |
Ulrich Weigand | fa80642 | 2013-05-06 16:23:57 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | /// getAlignOfGlobalVarInChars - Return the alignment in characters that |
| 2331 | /// should be given to a global variable of the specified type. |
| 2332 | CharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T) const { |
| 2333 | return toCharUnitsFromBits(getAlignOfGlobalVar(T)); |
| 2334 | } |
| 2335 | |
David Majnemer | 08ef2ba | 2015-06-23 20:34:18 +0000 | [diff] [blame] | 2336 | CharUnits ASTContext::getOffsetOfBaseWithVBPtr(const CXXRecordDecl *RD) const { |
| 2337 | CharUnits Offset = CharUnits::Zero(); |
| 2338 | const ASTRecordLayout *Layout = &getASTRecordLayout(RD); |
| 2339 | while (const CXXRecordDecl *Base = Layout->getBaseSharingVBPtr()) { |
| 2340 | Offset += Layout->getBaseClassOffset(Base); |
| 2341 | Layout = &getASTRecordLayout(Base); |
| 2342 | } |
| 2343 | return Offset; |
| 2344 | } |
| 2345 | |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2346 | /// DeepCollectObjCIvars - |
| 2347 | /// This routine first collects all declared, but not synthesized, ivars in |
| 2348 | /// super class and then collects all ivars, including those synthesized for |
| 2349 | /// current class. This routine is used for implementation of current class |
| 2350 | /// when all ivars, declared and synthesized are known. |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2351 | void ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, |
| 2352 | bool leafClass, |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2353 | SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const { |
Fariborz Jahanian | a50b3a2 | 2010-08-20 21:21:08 +0000 | [diff] [blame] | 2354 | if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass()) |
| 2355 | DeepCollectObjCIvars(SuperClass, false, Ivars); |
| 2356 | if (!leafClass) { |
Aaron Ballman | 59abbd4 | 2014-03-13 21:09:43 +0000 | [diff] [blame] | 2357 | for (const auto *I : OI->ivars()) |
| 2358 | Ivars.push_back(I); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2359 | } else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2360 | auto *IDecl = const_cast<ObjCInterfaceDecl *>(OI); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2361 | for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv; |
Fariborz Jahanian | b26d578 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 2362 | Iv= Iv->getNextIvar()) |
| 2363 | Ivars.push_back(Iv); |
| 2364 | } |
Fariborz Jahanian | 0f44d81 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2367 | /// CollectInheritedProtocols - Collect all protocols in current class and |
| 2368 | /// those inherited by it. |
| 2369 | void ASTContext::CollectInheritedProtocols(const Decl *CDecl, |
Fariborz Jahanian | dc68f95 | 2010-02-12 19:27:33 +0000 | [diff] [blame] | 2370 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2371 | if (const auto *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
Ted Kremenek | 0ef508d | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2372 | // We can use protocol_iterator here instead of |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2373 | // all_referenced_protocol_iterator since we are walking all categories. |
Aaron Ballman | a9f49e3 | 2014-03-13 20:55:22 +0000 | [diff] [blame] | 2374 | for (auto *Proto : OI->all_referenced_protocols()) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 2375 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 8e3b9db | 2010-02-25 18:24:33 +0000 | [diff] [blame] | 2376 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2377 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2378 | // Categories of this Interface. |
Aaron Ballman | 3fe486a | 2014-03-13 21:23:55 +0000 | [diff] [blame] | 2379 | for (const auto *Cat : OI->visible_categories()) |
| 2380 | CollectInheritedProtocols(Cat, Protocols); |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2381 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2382 | if (ObjCInterfaceDecl *SD = OI->getSuperClass()) |
| 2383 | while (SD) { |
| 2384 | CollectInheritedProtocols(SD, Protocols); |
| 2385 | SD = SD->getSuperClass(); |
| 2386 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2387 | } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Aaron Ballman | 19a4176 | 2014-03-14 12:55:57 +0000 | [diff] [blame] | 2388 | for (auto *Proto : OC->protocols()) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 2389 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2390 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2391 | } else if (const auto *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 2392 | // Insert the protocol. |
| 2393 | if (!Protocols.insert( |
| 2394 | const_cast<ObjCProtocolDecl *>(OP->getCanonicalDecl())).second) |
| 2395 | return; |
| 2396 | |
| 2397 | for (auto *Proto : OP->protocols()) |
| 2398 | CollectInheritedProtocols(Proto, Protocols); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 2399 | } |
| 2400 | } |
| 2401 | |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2402 | static bool unionHasUniqueObjectRepresentations(const ASTContext &Context, |
| 2403 | const RecordDecl *RD) { |
| 2404 | assert(RD->isUnion() && "Must be union type"); |
| 2405 | CharUnits UnionSize = Context.getTypeSizeInChars(RD->getTypeForDecl()); |
| 2406 | |
| 2407 | for (const auto *Field : RD->fields()) { |
| 2408 | if (!Context.hasUniqueObjectRepresentations(Field->getType())) |
| 2409 | return false; |
| 2410 | CharUnits FieldSize = Context.getTypeSizeInChars(Field->getType()); |
| 2411 | if (FieldSize != UnionSize) |
| 2412 | return false; |
| 2413 | } |
Eric Fiselier | 12a9f34 | 2018-02-02 20:30:39 +0000 | [diff] [blame] | 2414 | return !RD->field_empty(); |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Benjamin Kramer | 802e625 | 2017-12-24 12:46:22 +0000 | [diff] [blame] | 2417 | static bool isStructEmpty(QualType Ty) { |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2418 | const RecordDecl *RD = Ty->castAs<RecordType>()->getDecl(); |
| 2419 | |
| 2420 | if (!RD->field_empty()) |
| 2421 | return false; |
| 2422 | |
| 2423 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RD)) |
| 2424 | return ClassDecl->isEmpty(); |
| 2425 | |
| 2426 | return true; |
| 2427 | } |
| 2428 | |
| 2429 | static llvm::Optional<int64_t> |
| 2430 | structHasUniqueObjectRepresentations(const ASTContext &Context, |
| 2431 | const RecordDecl *RD) { |
| 2432 | assert(!RD->isUnion() && "Must be struct/class type"); |
| 2433 | const auto &Layout = Context.getASTRecordLayout(RD); |
| 2434 | |
| 2435 | int64_t CurOffsetInBits = 0; |
| 2436 | if (const auto *ClassDecl = dyn_cast<CXXRecordDecl>(RD)) { |
| 2437 | if (ClassDecl->isDynamicClass()) |
| 2438 | return llvm::None; |
| 2439 | |
| 2440 | SmallVector<std::pair<QualType, int64_t>, 4> Bases; |
| 2441 | for (const auto Base : ClassDecl->bases()) { |
| 2442 | // Empty types can be inherited from, and non-empty types can potentially |
| 2443 | // have tail padding, so just make sure there isn't an error. |
| 2444 | if (!isStructEmpty(Base.getType())) { |
| 2445 | llvm::Optional<int64_t> Size = structHasUniqueObjectRepresentations( |
Simon Pilgrim | eed4b12 | 2019-10-02 11:48:06 +0000 | [diff] [blame] | 2446 | Context, Base.getType()->castAs<RecordType>()->getDecl()); |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2447 | if (!Size) |
| 2448 | return llvm::None; |
| 2449 | Bases.emplace_back(Base.getType(), Size.getValue()); |
| 2450 | } |
| 2451 | } |
| 2452 | |
Fangrui Song | 1d38c13 | 2018-09-30 21:41:11 +0000 | [diff] [blame] | 2453 | llvm::sort(Bases, [&](const std::pair<QualType, int64_t> &L, |
| 2454 | const std::pair<QualType, int64_t> &R) { |
| 2455 | return Layout.getBaseClassOffset(L.first->getAsCXXRecordDecl()) < |
| 2456 | Layout.getBaseClassOffset(R.first->getAsCXXRecordDecl()); |
| 2457 | }); |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2458 | |
| 2459 | for (const auto Base : Bases) { |
| 2460 | int64_t BaseOffset = Context.toBits( |
| 2461 | Layout.getBaseClassOffset(Base.first->getAsCXXRecordDecl())); |
| 2462 | int64_t BaseSize = Base.second; |
| 2463 | if (BaseOffset != CurOffsetInBits) |
| 2464 | return llvm::None; |
| 2465 | CurOffsetInBits = BaseOffset + BaseSize; |
| 2466 | } |
| 2467 | } |
| 2468 | |
| 2469 | for (const auto *Field : RD->fields()) { |
| 2470 | if (!Field->getType()->isReferenceType() && |
| 2471 | !Context.hasUniqueObjectRepresentations(Field->getType())) |
| 2472 | return llvm::None; |
| 2473 | |
| 2474 | int64_t FieldSizeInBits = |
| 2475 | Context.toBits(Context.getTypeSizeInChars(Field->getType())); |
| 2476 | if (Field->isBitField()) { |
| 2477 | int64_t BitfieldSize = Field->getBitWidthValue(Context); |
| 2478 | |
| 2479 | if (BitfieldSize > FieldSizeInBits) |
| 2480 | return llvm::None; |
| 2481 | FieldSizeInBits = BitfieldSize; |
| 2482 | } |
| 2483 | |
| 2484 | int64_t FieldOffsetInBits = Context.getFieldOffset(Field); |
| 2485 | |
| 2486 | if (FieldOffsetInBits != CurOffsetInBits) |
| 2487 | return llvm::None; |
| 2488 | |
| 2489 | CurOffsetInBits = FieldSizeInBits + FieldOffsetInBits; |
| 2490 | } |
| 2491 | |
| 2492 | return CurOffsetInBits; |
| 2493 | } |
| 2494 | |
| 2495 | bool ASTContext::hasUniqueObjectRepresentations(QualType Ty) const { |
| 2496 | // C++17 [meta.unary.prop]: |
| 2497 | // The predicate condition for a template specialization |
| 2498 | // has_unique_object_representations<T> shall be |
| 2499 | // satisfied if and only if: |
| 2500 | // (9.1) - T is trivially copyable, and |
| 2501 | // (9.2) - any two objects of type T with the same value have the same |
| 2502 | // object representation, where two objects |
| 2503 | // of array or non-union class type are considered to have the same value |
| 2504 | // if their respective sequences of |
| 2505 | // direct subobjects have the same values, and two objects of union type |
| 2506 | // are considered to have the same |
| 2507 | // value if they have the same active member and the corresponding members |
| 2508 | // have the same value. |
| 2509 | // The set of scalar types for which this condition holds is |
| 2510 | // implementation-defined. [ Note: If a type has padding |
| 2511 | // bits, the condition does not hold; otherwise, the condition holds true |
| 2512 | // for unsigned integral types. -- end note ] |
| 2513 | assert(!Ty.isNull() && "Null QualType sent to unique object rep check"); |
| 2514 | |
| 2515 | // Arrays are unique only if their element type is unique. |
| 2516 | if (Ty->isArrayType()) |
| 2517 | return hasUniqueObjectRepresentations(getBaseElementType(Ty)); |
| 2518 | |
| 2519 | // (9.1) - T is trivially copyable... |
| 2520 | if (!Ty.isTriviallyCopyableType(*this)) |
| 2521 | return false; |
| 2522 | |
| 2523 | // All integrals and enums are unique. |
| 2524 | if (Ty->isIntegralOrEnumerationType()) |
| 2525 | return true; |
| 2526 | |
| 2527 | // All other pointers are unique. |
| 2528 | if (Ty->isPointerType()) |
| 2529 | return true; |
| 2530 | |
| 2531 | if (Ty->isMemberPointerType()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2532 | const auto *MPT = Ty->getAs<MemberPointerType>(); |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2533 | return !ABI->getMemberPointerInfo(MPT).HasPadding; |
| 2534 | } |
| 2535 | |
| 2536 | if (Ty->isRecordType()) { |
Simon Pilgrim | eed4b12 | 2019-10-02 11:48:06 +0000 | [diff] [blame] | 2537 | const RecordDecl *Record = Ty->castAs<RecordType>()->getDecl(); |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2538 | |
Erich Keane | bd2197c | 2017-12-12 16:02:06 +0000 | [diff] [blame] | 2539 | if (Record->isInvalidDecl()) |
| 2540 | return false; |
| 2541 | |
Erich Keane | 8a6b740 | 2017-11-30 16:37:02 +0000 | [diff] [blame] | 2542 | if (Record->isUnion()) |
| 2543 | return unionHasUniqueObjectRepresentations(*this, Record); |
| 2544 | |
| 2545 | Optional<int64_t> StructSize = |
| 2546 | structHasUniqueObjectRepresentations(*this, Record); |
| 2547 | |
| 2548 | return StructSize && |
| 2549 | StructSize.getValue() == static_cast<int64_t>(getTypeSize(Ty)); |
| 2550 | } |
| 2551 | |
| 2552 | // FIXME: More cases to handle here (list by rsmith): |
| 2553 | // vectors (careful about, eg, vector of 3 foo) |
| 2554 | // _Complex int and friends |
| 2555 | // _Atomic T |
| 2556 | // Obj-C block pointers |
| 2557 | // Obj-C object pointers |
| 2558 | // and perhaps OpenCL's various builtin types (pipe, sampler_t, event_t, |
| 2559 | // clk_event_t, queue_t, reserve_id_t) |
| 2560 | // There're also Obj-C class types and the Obj-C selector type, but I think it |
| 2561 | // makes sense for those to return false here. |
| 2562 | |
| 2563 | return false; |
| 2564 | } |
| 2565 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2566 | unsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2567 | unsigned count = 0; |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 2568 | // Count ivars declared in class extension. |
Aaron Ballman | b4a5345 | 2014-03-13 21:57:01 +0000 | [diff] [blame] | 2569 | for (const auto *Ext : OI->known_extensions()) |
Douglas Gregor | 048fbfa | 2013-01-16 23:00:23 +0000 | [diff] [blame] | 2570 | count += Ext->ivar_size(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2571 | |
Fariborz Jahanian | d2ae2d0 | 2010-03-22 18:25:57 +0000 | [diff] [blame] | 2572 | // Count ivar defined in this class's implementation. This |
| 2573 | // includes synthesized ivars. |
| 2574 | if (ObjCImplementationDecl *ImplDecl = OI->getImplementation()) |
Benjamin Kramer | 0a3fe04 | 2010-04-27 17:47:25 +0000 | [diff] [blame] | 2575 | count += ImplDecl->ivar_size(); |
| 2576 | |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2577 | return count; |
| 2578 | } |
| 2579 | |
Argyrios Kyrtzidis | 2e809ce | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 2580 | bool ASTContext::isSentinelNullExpr(const Expr *E) { |
| 2581 | if (!E) |
| 2582 | return false; |
| 2583 | |
| 2584 | // nullptr_t is always treated as null. |
| 2585 | if (E->getType()->isNullPtrType()) return true; |
| 2586 | |
| 2587 | if (E->getType()->isAnyPointerType() && |
| 2588 | E->IgnoreParenCasts()->isNullPointerConstant(*this, |
| 2589 | Expr::NPC_ValueDependentIsNull)) |
| 2590 | return true; |
| 2591 | |
| 2592 | // Unfortunately, __null has type 'int'. |
| 2593 | if (isa<GNUNullExpr>(E)) return true; |
| 2594 | |
| 2595 | return false; |
| 2596 | } |
| 2597 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2598 | /// Get the implementation of ObjCInterfaceDecl, or nullptr if none |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2599 | /// exists. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2600 | ObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) { |
| 2601 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 2602 | I = ObjCImpls.find(D); |
| 2603 | if (I != ObjCImpls.end()) |
| 2604 | return cast<ObjCImplementationDecl>(I->second); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2605 | return nullptr; |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2606 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2607 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2608 | /// Get the implementation of ObjCCategoryDecl, or nullptr if none |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2609 | /// exists. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2610 | ObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) { |
| 2611 | llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator |
| 2612 | I = ObjCImpls.find(D); |
| 2613 | if (I != ObjCImpls.end()) |
| 2614 | return cast<ObjCCategoryImplDecl>(I->second); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2615 | return nullptr; |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2618 | /// Set the implementation of ObjCInterfaceDecl. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2619 | void ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD, |
| 2620 | ObjCImplementationDecl *ImplD) { |
| 2621 | assert(IFaceD && ImplD && "Passed null params"); |
| 2622 | ObjCImpls[IFaceD] = ImplD; |
| 2623 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 2624 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2625 | /// Set the implementation of ObjCCategoryDecl. |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 2626 | void ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD, |
| 2627 | ObjCCategoryImplDecl *ImplD) { |
| 2628 | assert(CatD && ImplD && "Passed null params"); |
| 2629 | ObjCImpls[CatD] = ImplD; |
| 2630 | } |
| 2631 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 2632 | const ObjCMethodDecl * |
| 2633 | ASTContext::getObjCMethodRedeclaration(const ObjCMethodDecl *MD) const { |
| 2634 | return ObjCMethodRedecls.lookup(MD); |
| 2635 | } |
| 2636 | |
| 2637 | void ASTContext::setObjCMethodRedeclaration(const ObjCMethodDecl *MD, |
| 2638 | const ObjCMethodDecl *Redecl) { |
| 2639 | assert(!getObjCMethodRedeclaration(MD) && "MD already has a redeclaration"); |
| 2640 | ObjCMethodRedecls[MD] = Redecl; |
| 2641 | } |
| 2642 | |
Dmitri Gribenko | 37527c2 | 2013-02-03 13:23:21 +0000 | [diff] [blame] | 2643 | const ObjCInterfaceDecl *ASTContext::getObjContainingInterface( |
| 2644 | const NamedDecl *ND) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2645 | if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2646 | return ID; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2647 | if (const auto *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2648 | return CD->getClassInterface(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2649 | if (const auto *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext())) |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2650 | return IMD->getClassInterface(); |
| 2651 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2652 | return nullptr; |
Argyrios Kyrtzidis | b9689bb | 2011-11-01 17:14:12 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2655 | /// Get the copy initialization expression of VarDecl, or nullptr if |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2656 | /// none exists. |
Akira Hatanaka | 9978da3 | 2018-08-10 15:09:24 +0000 | [diff] [blame] | 2657 | ASTContext::BlockVarCopyInit |
| 2658 | ASTContext::getBlockVarCopyInit(const VarDecl*VD) const { |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2659 | assert(VD && "Passed null params"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2660 | assert(VD->hasAttr<BlocksAttr>() && |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2661 | "getBlockVarCopyInits - not __block var"); |
Akira Hatanaka | 9978da3 | 2018-08-10 15:09:24 +0000 | [diff] [blame] | 2662 | auto I = BlockVarCopyInits.find(VD); |
| 2663 | if (I != BlockVarCopyInits.end()) |
| 2664 | return I->second; |
| 2665 | return {nullptr, false}; |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
JF Bastien | 0d702a7 | 2019-04-30 00:11:53 +0000 | [diff] [blame] | 2668 | /// Set the copy initialization expression of a block var decl. |
Akira Hatanaka | 9978da3 | 2018-08-10 15:09:24 +0000 | [diff] [blame] | 2669 | void ASTContext::setBlockVarCopyInit(const VarDecl*VD, Expr *CopyExpr, |
| 2670 | bool CanThrow) { |
| 2671 | assert(VD && CopyExpr && "Passed null params"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2672 | assert(VD->hasAttr<BlocksAttr>() && |
Fariborz Jahanian | 1321cbb | 2010-12-06 17:28:17 +0000 | [diff] [blame] | 2673 | "setBlockVarCopyInits - not __block var"); |
Akira Hatanaka | 9978da3 | 2018-08-10 15:09:24 +0000 | [diff] [blame] | 2674 | BlockVarCopyInits[VD].setExprAndFlag(CopyExpr, CanThrow); |
Fariborz Jahanian | 7cfe767 | 2010-12-01 22:29:46 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2677 | TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2678 | unsigned DataSize) const { |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 2679 | if (!DataSize) |
| 2680 | DataSize = TypeLoc::getFullDataSizeForType(T); |
| 2681 | else |
| 2682 | assert(DataSize == TypeLoc::getFullDataSizeForType(T) && |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2683 | "incorrect data size provided to CreateTypeSourceInfo!"); |
John McCall | 26fe7e0 | 2009-10-21 00:23:54 +0000 | [diff] [blame] | 2684 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2685 | auto *TInfo = |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2686 | (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8); |
| 2687 | new (TInfo) TypeSourceInfo(T); |
| 2688 | return TInfo; |
Argyrios Kyrtzidis | 3f79ad7 | 2009-08-19 01:27:32 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2691 | TypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T, |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2692 | SourceLocation L) const { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2693 | TypeSourceInfo *DI = CreateTypeSourceInfo(T); |
Douglas Gregor | 2d525f0 | 2011-01-25 19:13:18 +0000 | [diff] [blame] | 2694 | DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L); |
John McCall | 3665e00 | 2009-10-23 21:14:09 +0000 | [diff] [blame] | 2695 | return DI; |
| 2696 | } |
| 2697 | |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2698 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2699 | ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2700 | return getObjCLayout(D, nullptr); |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | const ASTRecordLayout & |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2704 | ASTContext::getASTObjCImplementationLayout( |
| 2705 | const ObjCImplementationDecl *D) const { |
Daniel Dunbar | 02f7f5f | 2009-05-03 10:38:35 +0000 | [diff] [blame] | 2706 | return getObjCLayout(D->getClassInterface(), D); |
| 2707 | } |
| 2708 | |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2709 | //===----------------------------------------------------------------------===// |
| 2710 | // Type creation/memoization methods |
| 2711 | //===----------------------------------------------------------------------===// |
| 2712 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2713 | QualType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2714 | ASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const { |
| 2715 | unsigned fastQuals = quals.getFastQualifiers(); |
| 2716 | quals.removeFastQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2717 | |
| 2718 | // Check if we've already instantiated this type. |
| 2719 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2720 | ExtQuals::Profile(ID, baseType, quals); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2721 | void *insertPos = nullptr; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2722 | if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) { |
| 2723 | assert(eq->getQualifiers() == quals); |
| 2724 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2727 | // If the base type is not canonical, make the appropriate canonical type. |
| 2728 | QualType canon; |
| 2729 | if (!baseType->isCanonicalUnqualified()) { |
| 2730 | SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 2731 | canonSplit.Quals.addConsistentQualifiers(quals); |
| 2732 | canon = getExtQualType(canonSplit.Ty, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2733 | |
| 2734 | // Re-find the insert position. |
| 2735 | (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos); |
| 2736 | } |
| 2737 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2738 | auto *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 2739 | ExtQualNodes.InsertNode(eq, insertPos); |
| 2740 | return QualType(eq, fastQuals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 2743 | QualType ASTContext::getAddrSpaceQualType(QualType T, |
| 2744 | LangAS AddressSpace) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2745 | QualType CanT = getCanonicalType(T); |
| 2746 | if (CanT.getAddressSpace() == AddressSpace) |
Chris Lattner | 445fcab | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 2747 | return T; |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2748 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2749 | // If we are composing extended qualifiers together, merge together |
| 2750 | // into one ExtQuals node. |
| 2751 | QualifierCollector Quals; |
| 2752 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2753 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2754 | // If this type already has an address space specified, it cannot get |
| 2755 | // another one. |
| 2756 | assert(!Quals.hasAddressSpace() && |
| 2757 | "Type cannot be in multiple addr spaces!"); |
| 2758 | Quals.addAddressSpace(AddressSpace); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2759 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2760 | return getExtQualType(TypeNode, Quals); |
Christopher Lamb | 025b5fb | 2008-02-04 02:31:56 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 2763 | QualType ASTContext::removeAddrSpaceQualType(QualType T) const { |
| 2764 | // If we are composing extended qualifiers together, merge together |
| 2765 | // into one ExtQuals node. |
| 2766 | QualifierCollector Quals; |
| 2767 | const Type *TypeNode = Quals.strip(T); |
| 2768 | |
| 2769 | // If the qualifier doesn't have an address space just return it. |
| 2770 | if (!Quals.hasAddressSpace()) |
| 2771 | return T; |
| 2772 | |
| 2773 | Quals.removeAddressSpace(); |
| 2774 | |
| 2775 | // Removal of the address space can mean there are no longer any |
| 2776 | // non-fast qualifiers, so creating an ExtQualType isn't possible (asserts) |
| 2777 | // or required. |
| 2778 | if (Quals.hasNonFastQualifiers()) |
| 2779 | return getExtQualType(TypeNode, Quals); |
| 2780 | else |
| 2781 | return QualType(TypeNode, Quals.getFastQualifiers()); |
| 2782 | } |
| 2783 | |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2784 | QualType ASTContext::getObjCGCQualType(QualType T, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2785 | Qualifiers::GC GCAttr) const { |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2786 | QualType CanT = getCanonicalType(T); |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 2787 | if (CanT.getObjCGCAttr() == GCAttr) |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2788 | return T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2789 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2790 | if (const auto *ptr = T->getAs<PointerType>()) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2791 | QualType Pointee = ptr->getPointeeType(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 2792 | if (Pointee->isAnyPointerType()) { |
Fariborz Jahanian | b68215c | 2009-06-03 17:15:17 +0000 | [diff] [blame] | 2793 | QualType ResultType = getObjCGCQualType(Pointee, GCAttr); |
| 2794 | return getPointerType(ResultType); |
| 2795 | } |
| 2796 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2797 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2798 | // If we are composing extended qualifiers together, merge together |
| 2799 | // into one ExtQuals node. |
| 2800 | QualifierCollector Quals; |
| 2801 | const Type *TypeNode = Quals.strip(T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2802 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2803 | // If this type already has an ObjCGC specified, it cannot get |
| 2804 | // another one. |
| 2805 | assert(!Quals.hasObjCGCAttr() && |
| 2806 | "Type cannot have multiple ObjCGCs!"); |
| 2807 | Quals.addObjCGCAttr(GCAttr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2808 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2809 | return getExtQualType(TypeNode, Quals); |
Fariborz Jahanian | e27e934 | 2009-02-18 05:09:49 +0000 | [diff] [blame] | 2810 | } |
Chris Lattner | 983a8bb | 2007-07-13 22:13:22 +0000 | [diff] [blame] | 2811 | |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2812 | const FunctionType *ASTContext::adjustFunctionType(const FunctionType *T, |
| 2813 | FunctionType::ExtInfo Info) { |
| 2814 | if (T->getExtInfo() == Info) |
| 2815 | return T; |
| 2816 | |
| 2817 | QualType Result; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2818 | if (const auto *FNPT = dyn_cast<FunctionNoProtoType>(T)) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2819 | Result = getFunctionNoProtoType(FNPT->getReturnType(), Info); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2820 | } else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2821 | const auto *FPT = cast<FunctionProtoType>(T); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2822 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 2823 | EPI.ExtInfo = Info; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2824 | Result = getFunctionType(FPT->getReturnType(), FPT->getParamTypes(), EPI); |
John McCall | 4f5019e | 2010-12-19 02:44:49 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | return cast<FunctionType>(Result.getTypePtr()); |
| 2828 | } |
| 2829 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2830 | void ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD, |
| 2831 | QualType ResultType) { |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2832 | FD = FD->getMostRecentDecl(); |
| 2833 | while (true) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2834 | const auto *FPT = FD->getType()->castAs<FunctionProtoType>(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2835 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2836 | FD->setType(getFunctionType(ResultType, FPT->getParamTypes(), EPI)); |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2837 | if (FunctionDecl *Next = FD->getPreviousDecl()) |
| 2838 | FD = Next; |
| 2839 | else |
| 2840 | break; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2841 | } |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 2842 | if (ASTMutationListener *L = getASTMutationListener()) |
| 2843 | L->DeducedReturnType(FD, ResultType); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2846 | /// Get a function type and produce the equivalent function type with the |
| 2847 | /// specified exception specification. Type sugar that can be present on a |
| 2848 | /// declaration of a function with an exception specification is permitted |
| 2849 | /// and preserved. Other type sugar (for instance, typedefs) is not. |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2850 | QualType ASTContext::getFunctionTypeWithExceptionSpec( |
| 2851 | QualType Orig, const FunctionProtoType::ExceptionSpecInfo &ESI) { |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2852 | // Might have some parens. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2853 | if (const auto *PT = dyn_cast<ParenType>(Orig)) |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2854 | return getParenType( |
| 2855 | getFunctionTypeWithExceptionSpec(PT->getInnerType(), ESI)); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2856 | |
Leonard Chan | dc5d975 | 2019-05-10 18:05:15 +0000 | [diff] [blame] | 2857 | // Might be wrapped in a macro qualified type. |
| 2858 | if (const auto *MQT = dyn_cast<MacroQualifiedType>(Orig)) |
| 2859 | return getMacroQualifiedType( |
| 2860 | getFunctionTypeWithExceptionSpec(MQT->getUnderlyingType(), ESI), |
| 2861 | MQT->getMacroIdentifier()); |
| 2862 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2863 | // Might have a calling-convention attribute. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2864 | if (const auto *AT = dyn_cast<AttributedType>(Orig)) |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2865 | return getAttributedType( |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2866 | AT->getAttrKind(), |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2867 | getFunctionTypeWithExceptionSpec(AT->getModifiedType(), ESI), |
| 2868 | getFunctionTypeWithExceptionSpec(AT->getEquivalentType(), ESI)); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2869 | |
| 2870 | // Anything else must be a function type. Rebuild it with the new exception |
| 2871 | // specification. |
Simon Pilgrim | 9588ae7 | 2019-10-03 21:47:42 +0000 | [diff] [blame] | 2872 | const auto *Proto = Orig->castAs<FunctionProtoType>(); |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2873 | return getFunctionType( |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2874 | Proto->getReturnType(), Proto->getParamTypes(), |
| 2875 | Proto->getExtProtoInfo().withExceptionSpec(ESI)); |
| 2876 | } |
| 2877 | |
Richard Smith | dfe85e2 | 2016-12-15 02:35:39 +0000 | [diff] [blame] | 2878 | bool ASTContext::hasSameFunctionTypeIgnoringExceptionSpec(QualType T, |
| 2879 | QualType U) { |
| 2880 | return hasSameType(T, U) || |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2881 | (getLangOpts().CPlusPlus17 && |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2882 | hasSameType(getFunctionTypeWithExceptionSpec(T, EST_None), |
| 2883 | getFunctionTypeWithExceptionSpec(U, EST_None))); |
Richard Smith | dfe85e2 | 2016-12-15 02:35:39 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2886 | void ASTContext::adjustExceptionSpec( |
| 2887 | FunctionDecl *FD, const FunctionProtoType::ExceptionSpecInfo &ESI, |
| 2888 | bool AsWritten) { |
| 2889 | // Update the type. |
| 2890 | QualType Updated = |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2891 | getFunctionTypeWithExceptionSpec(FD->getType(), ESI); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2892 | FD->setType(Updated); |
| 2893 | |
| 2894 | if (!AsWritten) |
| 2895 | return; |
| 2896 | |
| 2897 | // Update the type in the type source information too. |
| 2898 | if (TypeSourceInfo *TSInfo = FD->getTypeSourceInfo()) { |
| 2899 | // If the type and the type-as-written differ, we may need to update |
| 2900 | // the type-as-written too. |
| 2901 | if (TSInfo->getType() != FD->getType()) |
Stephan Bergmann | 8c85bca | 2018-01-05 07:57:12 +0000 | [diff] [blame] | 2902 | Updated = getFunctionTypeWithExceptionSpec(TSInfo->getType(), ESI); |
Richard Smith | 0b3a462 | 2014-11-13 20:01:57 +0000 | [diff] [blame] | 2903 | |
| 2904 | // FIXME: When we get proper type location information for exceptions, |
| 2905 | // we'll also have to rebuild the TypeSourceInfo. For now, we just patch |
| 2906 | // up the TypeSourceInfo; |
| 2907 | assert(TypeLoc::getFullDataSizeForType(Updated) == |
| 2908 | TypeLoc::getFullDataSizeForType(TSInfo->getType()) && |
| 2909 | "TypeLoc size mismatch from updating exception specification"); |
| 2910 | TSInfo->overrideType(Updated); |
| 2911 | } |
| 2912 | } |
| 2913 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2914 | /// getComplexType - Return the uniqued reference to the type for a complex |
| 2915 | /// number with the specified element type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2916 | QualType ASTContext::getComplexType(QualType T) const { |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2917 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2918 | // structure. |
| 2919 | llvm::FoldingSetNodeID ID; |
| 2920 | ComplexType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2921 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2922 | void *InsertPos = nullptr; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2923 | if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 2924 | return QualType(CT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2925 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2926 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2927 | // so fill in the canonical type field. |
| 2928 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 2929 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2930 | Canonical = getComplexType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2931 | |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2932 | // Get the new insert position for the node we care about. |
| 2933 | ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2934 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2935 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2936 | auto *New = new (*this, TypeAlignment) ComplexType(T, Canonical); |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 2937 | Types.push_back(New); |
| 2938 | ComplexTypes.InsertNode(New, InsertPos); |
| 2939 | return QualType(New, 0); |
| 2940 | } |
| 2941 | |
Chris Lattner | 970e54e | 2006-11-12 00:37:36 +0000 | [diff] [blame] | 2942 | /// getPointerType - Return the uniqued reference to the type for a pointer to |
| 2943 | /// the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2944 | QualType ASTContext::getPointerType(QualType T) const { |
Chris Lattner | d5973eb | 2006-11-12 00:53:46 +0000 | [diff] [blame] | 2945 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 2946 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2947 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2948 | PointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2949 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2950 | void *InsertPos = nullptr; |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2951 | if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2952 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2953 | |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 2954 | // If the pointee type isn't canonical, this won't be a canonical type either, |
| 2955 | // so fill in the canonical type field. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2956 | QualType Canonical; |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2957 | if (!T.isCanonical()) { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 2958 | Canonical = getPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2959 | |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2960 | // Get the new insert position for the node we care about. |
| 2961 | PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2962 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Bob Wilson | c8541f2 | 2013-03-15 17:12:43 +0000 | [diff] [blame] | 2963 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2964 | auto *New = new (*this, TypeAlignment) PointerType(T, Canonical); |
Chris Lattner | 67521df | 2007-01-27 01:29:36 +0000 | [diff] [blame] | 2965 | Types.push_back(New); |
| 2966 | PointerTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 2967 | return QualType(New, 0); |
Chris Lattner | ddc135e | 2006-11-10 06:34:16 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2970 | QualType ASTContext::getAdjustedType(QualType Orig, QualType New) const { |
| 2971 | llvm::FoldingSetNodeID ID; |
| 2972 | AdjustedType::Profile(ID, Orig, New); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2973 | void *InsertPos = nullptr; |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2974 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 2975 | if (AT) |
| 2976 | return QualType(AT, 0); |
| 2977 | |
| 2978 | QualType Canonical = getCanonicalType(New); |
| 2979 | |
| 2980 | // Get the new insert position for the node we care about. |
| 2981 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2982 | assert(!AT && "Shouldn't be in the map!"); |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 2983 | |
| 2984 | AT = new (*this, TypeAlignment) |
| 2985 | AdjustedType(Type::Adjusted, Orig, New, Canonical); |
| 2986 | Types.push_back(AT); |
| 2987 | AdjustedTypes.InsertNode(AT, InsertPos); |
| 2988 | return QualType(AT, 0); |
| 2989 | } |
| 2990 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2991 | QualType ASTContext::getDecayedType(QualType T) const { |
| 2992 | assert((T->isArrayType() || T->isFunctionType()) && "T does not decay"); |
| 2993 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2994 | QualType Decayed; |
| 2995 | |
| 2996 | // C99 6.7.5.3p7: |
| 2997 | // A declaration of a parameter as "array of type" shall be |
| 2998 | // adjusted to "qualified pointer to type", where the type |
| 2999 | // qualifiers (if any) are those specified within the [ and ] of |
| 3000 | // the array type derivation. |
| 3001 | if (T->isArrayType()) |
| 3002 | Decayed = getArrayDecayedType(T); |
| 3003 | |
| 3004 | // C99 6.7.5.3p8: |
| 3005 | // A declaration of a parameter as "function returning type" |
| 3006 | // shall be adjusted to "pointer to function returning type", as |
| 3007 | // in 6.3.2.1. |
| 3008 | if (T->isFunctionType()) |
| 3009 | Decayed = getPointerType(T); |
| 3010 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 3011 | llvm::FoldingSetNodeID ID; |
| 3012 | AdjustedType::Profile(ID, T, Decayed); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3013 | void *InsertPos = nullptr; |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 3014 | AdjustedType *AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3015 | if (AT) |
| 3016 | return QualType(AT, 0); |
| 3017 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 3018 | QualType Canonical = getCanonicalType(Decayed); |
| 3019 | |
| 3020 | // Get the new insert position for the node we care about. |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 3021 | AT = AdjustedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3022 | assert(!AT && "Shouldn't be in the map!"); |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 3023 | |
Reid Kleckner | 0503a87 | 2013-12-05 01:23:43 +0000 | [diff] [blame] | 3024 | AT = new (*this, TypeAlignment) DecayedType(T, Decayed, Canonical); |
| 3025 | Types.push_back(AT); |
| 3026 | AdjustedTypes.InsertNode(AT, InsertPos); |
| 3027 | return QualType(AT, 0); |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3030 | /// getBlockPointerType - Return the uniqued reference to the type for |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3031 | /// a pointer to the specified block. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3032 | QualType ASTContext::getBlockPointerType(QualType T) const { |
Steve Naroff | 0ac01283 | 2008-08-28 19:20:44 +0000 | [diff] [blame] | 3033 | assert(T->isFunctionType() && "block of function types only"); |
| 3034 | // Unique pointers, to guarantee there is only one block of a particular |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3035 | // structure. |
| 3036 | llvm::FoldingSetNodeID ID; |
| 3037 | BlockPointerType::Profile(ID, T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3038 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3039 | void *InsertPos = nullptr; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3040 | if (BlockPointerType *PT = |
| 3041 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3042 | return QualType(PT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3043 | |
| 3044 | // 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] | 3045 | // type either so fill in the canonical type field. |
| 3046 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3047 | if (!T.isCanonical()) { |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3048 | Canonical = getBlockPointerType(getCanonicalType(T)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3049 | |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3050 | // Get the new insert position for the node we care about. |
| 3051 | BlockPointerType *NewIP = |
| 3052 | BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3053 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3054 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3055 | auto *New = new (*this, TypeAlignment) BlockPointerType(T, Canonical); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3056 | Types.push_back(New); |
| 3057 | BlockPointerTypes.InsertNode(New, InsertPos); |
| 3058 | return QualType(New, 0); |
| 3059 | } |
| 3060 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3061 | /// getLValueReferenceType - Return the uniqued reference to the type for an |
| 3062 | /// lvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3063 | QualType |
| 3064 | ASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3065 | assert(getCanonicalType(T) != OverloadTy && |
Douglas Gregor | 291e8ee | 2011-05-21 22:16:50 +0000 | [diff] [blame] | 3066 | "Unresolved overloaded function type"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3067 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 3068 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3069 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 3070 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3071 | ReferenceType::Profile(ID, T, SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 3072 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3073 | void *InsertPos = nullptr; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3074 | if (LValueReferenceType *RT = |
| 3075 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 3076 | return QualType(RT, 0); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3077 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3078 | const auto *InnerRef = T->getAs<ReferenceType>(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3079 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 3080 | // If the referencee type isn't canonical, this won't be a canonical type |
| 3081 | // either, so fill in the canonical type field. |
| 3082 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3083 | if (!SpelledAsLValue || InnerRef || !T.isCanonical()) { |
| 3084 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 3085 | Canonical = getLValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3086 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 3087 | // Get the new insert position for the node we care about. |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3088 | LValueReferenceType *NewIP = |
| 3089 | LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3090 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3093 | auto *New = new (*this, TypeAlignment) LValueReferenceType(T, Canonical, |
| 3094 | SpelledAsLValue); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 3095 | Types.push_back(New); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3096 | LValueReferenceTypes.InsertNode(New, InsertPos); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3097 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3098 | return QualType(New, 0); |
| 3099 | } |
| 3100 | |
| 3101 | /// getRValueReferenceType - Return the uniqued reference to the type for an |
| 3102 | /// rvalue reference to the specified type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3103 | QualType ASTContext::getRValueReferenceType(QualType T) const { |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3104 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3105 | // structure. |
| 3106 | llvm::FoldingSetNodeID ID; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3107 | ReferenceType::Profile(ID, T, false); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3108 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3109 | void *InsertPos = nullptr; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3110 | if (RValueReferenceType *RT = |
| 3111 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3112 | return QualType(RT, 0); |
| 3113 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3114 | const auto *InnerRef = T->getAs<ReferenceType>(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3115 | |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3116 | // If the referencee type isn't canonical, this won't be a canonical type |
| 3117 | // either, so fill in the canonical type field. |
| 3118 | QualType Canonical; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3119 | if (InnerRef || !T.isCanonical()) { |
| 3120 | QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T); |
| 3121 | Canonical = getRValueReferenceType(getCanonicalType(PointeeType)); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3122 | |
| 3123 | // Get the new insert position for the node we care about. |
| 3124 | RValueReferenceType *NewIP = |
| 3125 | RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3126 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3127 | } |
| 3128 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3129 | auto *New = new (*this, TypeAlignment) RValueReferenceType(T, Canonical); |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 3130 | Types.push_back(New); |
| 3131 | RValueReferenceTypes.InsertNode(New, InsertPos); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 3132 | return QualType(New, 0); |
| 3133 | } |
| 3134 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3135 | /// getMemberPointerType - Return the uniqued reference to the type for a |
| 3136 | /// member pointer to the specified type, in the specified class. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3137 | QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3138 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 3139 | // structure. |
| 3140 | llvm::FoldingSetNodeID ID; |
| 3141 | MemberPointerType::Profile(ID, T, Cls); |
| 3142 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3143 | void *InsertPos = nullptr; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3144 | if (MemberPointerType *PT = |
| 3145 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3146 | return QualType(PT, 0); |
| 3147 | |
| 3148 | // If the pointee or class type isn't canonical, this won't be a canonical |
| 3149 | // type either, so fill in the canonical type field. |
| 3150 | QualType Canonical; |
Douglas Gregor | 615ac67 | 2009-11-04 16:49:01 +0000 | [diff] [blame] | 3151 | if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3152 | Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls)); |
| 3153 | |
| 3154 | // Get the new insert position for the node we care about. |
| 3155 | MemberPointerType *NewIP = |
| 3156 | MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3157 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3158 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3159 | auto *New = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3160 | Types.push_back(New); |
| 3161 | MemberPointerTypes.InsertNode(New, InsertPos); |
| 3162 | return QualType(New, 0); |
| 3163 | } |
| 3164 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3165 | /// getConstantArrayType - Return the unique reference to the type for an |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 3166 | /// array of the specified element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3167 | QualType ASTContext::getConstantArrayType(QualType EltTy, |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 3168 | const llvm::APInt &ArySizeIn, |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 3169 | const Expr *SizeExpr, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 3170 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3171 | unsigned IndexTypeQuals) const { |
Sebastian Redl | 2dfdb82 | 2009-11-05 15:52:31 +0000 | [diff] [blame] | 3172 | assert((EltTy->isDependentType() || |
| 3173 | EltTy->isIncompleteType() || EltTy->isConstantSizeType()) && |
Eli Friedman | be7e42b | 2009-05-29 20:17:55 +0000 | [diff] [blame] | 3174 | "Constant array of VLAs is illegal!"); |
| 3175 | |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 3176 | // We only need the size as part of the type if it's instantiation-dependent. |
| 3177 | if (SizeExpr && !SizeExpr->isInstantiationDependent()) |
| 3178 | SizeExpr = nullptr; |
| 3179 | |
Chris Lattner | e2df3f9 | 2009-05-13 04:12:56 +0000 | [diff] [blame] | 3180 | // Convert the array size into a canonical width matching the pointer size for |
| 3181 | // the target. |
| 3182 | llvm::APInt ArySize(ArySizeIn); |
Konstantin Zhuravlyov | 9c1e310 | 2017-03-21 18:55:39 +0000 | [diff] [blame] | 3183 | ArySize = ArySize.zextOrTrunc(Target->getMaxPointerWidth()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3184 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 3185 | llvm::FoldingSetNodeID ID; |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 3186 | ConstantArrayType::Profile(ID, *this, EltTy, ArySize, SizeExpr, ASM, |
| 3187 | IndexTypeQuals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3188 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3189 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3190 | if (ConstantArrayType *ATP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 3191 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3192 | return QualType(ATP, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3193 | |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 3194 | // If the element type isn't canonical or has qualifiers, or the array bound |
| 3195 | // is instantiation-dependent, this won't be a canonical type either, so fill |
| 3196 | // in the canonical type field. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3197 | QualType Canon; |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 3198 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers() || SizeExpr) { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3199 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 3200 | Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize, nullptr, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3201 | ASM, IndexTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3202 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3203 | |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 3204 | // Get the new insert position for the node we care about. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3205 | ConstantArrayType *NewIP = |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 3206 | ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3207 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 3208 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3209 | |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 3210 | void *Mem = Allocate( |
| 3211 | ConstantArrayType::totalSizeToAlloc<const Expr *>(SizeExpr ? 1 : 0), |
| 3212 | TypeAlignment); |
| 3213 | auto *New = new (Mem) |
| 3214 | ConstantArrayType(EltTy, Canon, ArySize, SizeExpr, ASM, IndexTypeQuals); |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 3215 | ConstantArrayTypes.InsertNode(New, InsertPos); |
Chris Lattner | 36f8e65 | 2007-01-27 08:31:04 +0000 | [diff] [blame] | 3216 | Types.push_back(New); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3217 | return QualType(New, 0); |
Chris Lattner | 7ccecb9 | 2006-11-12 08:50:50 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3220 | /// getVariableArrayDecayedType - Turns the given type, which may be |
| 3221 | /// variably-modified, into the corresponding type with all the known |
| 3222 | /// sizes replaced with [*]. |
| 3223 | QualType ASTContext::getVariableArrayDecayedType(QualType type) const { |
| 3224 | // Vastly most common case. |
| 3225 | if (!type->isVariablyModifiedType()) return type; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3226 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3227 | QualType result; |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3228 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3229 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3230 | const Type *ty = split.Ty; |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3231 | switch (ty->getTypeClass()) { |
| 3232 | #define TYPE(Class, Base) |
| 3233 | #define ABSTRACT_TYPE(Class, Base) |
| 3234 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
John McCall | 36b12a8 | 2019-10-02 06:35:23 +0000 | [diff] [blame] | 3235 | #include "clang/AST/TypeNodes.inc" |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3236 | llvm_unreachable("didn't desugar past all non-canonical types?"); |
| 3237 | |
| 3238 | // These types should never be variably-modified. |
| 3239 | case Type::Builtin: |
| 3240 | case Type::Complex: |
| 3241 | case Type::Vector: |
Erich Keane | f702b02 | 2018-07-13 19:46:04 +0000 | [diff] [blame] | 3242 | case Type::DependentVector: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3243 | case Type::ExtVector: |
| 3244 | case Type::DependentSizedExtVector: |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3245 | case Type::DependentAddressSpace: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3246 | case Type::ObjCObject: |
| 3247 | case Type::ObjCInterface: |
| 3248 | case Type::ObjCObjectPointer: |
| 3249 | case Type::Record: |
| 3250 | case Type::Enum: |
| 3251 | case Type::UnresolvedUsing: |
| 3252 | case Type::TypeOfExpr: |
| 3253 | case Type::TypeOf: |
| 3254 | case Type::Decltype: |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 3255 | case Type::UnaryTransform: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3256 | case Type::DependentName: |
| 3257 | case Type::InjectedClassName: |
| 3258 | case Type::TemplateSpecialization: |
| 3259 | case Type::DependentTemplateSpecialization: |
| 3260 | case Type::TemplateTypeParm: |
| 3261 | case Type::SubstTemplateTypeParmPack: |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3262 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 3263 | case Type::DeducedTemplateSpecialization: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3264 | case Type::PackExpansion: |
| 3265 | llvm_unreachable("type should never be variably-modified"); |
| 3266 | |
| 3267 | // These types can be variably-modified but should never need to |
| 3268 | // further decay. |
| 3269 | case Type::FunctionNoProto: |
| 3270 | case Type::FunctionProto: |
| 3271 | case Type::BlockPointer: |
| 3272 | case Type::MemberPointer: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3273 | case Type::Pipe: |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3274 | return type; |
| 3275 | |
| 3276 | // These types can be variably-modified. All these modifications |
| 3277 | // preserve structure except as noted by comments. |
| 3278 | // TODO: if we ever care about optimizing VLAs, there are no-op |
| 3279 | // optimizations available here. |
| 3280 | case Type::Pointer: |
| 3281 | result = getPointerType(getVariableArrayDecayedType( |
| 3282 | cast<PointerType>(ty)->getPointeeType())); |
| 3283 | break; |
| 3284 | |
| 3285 | case Type::LValueReference: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3286 | const auto *lv = cast<LValueReferenceType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3287 | result = getLValueReferenceType( |
| 3288 | getVariableArrayDecayedType(lv->getPointeeType()), |
| 3289 | lv->isSpelledAsLValue()); |
| 3290 | break; |
| 3291 | } |
| 3292 | |
| 3293 | case Type::RValueReference: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3294 | const auto *lv = cast<RValueReferenceType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3295 | result = getRValueReferenceType( |
| 3296 | getVariableArrayDecayedType(lv->getPointeeType())); |
| 3297 | break; |
| 3298 | } |
| 3299 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3300 | case Type::Atomic: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3301 | const auto *at = cast<AtomicType>(ty); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3302 | result = getAtomicType(getVariableArrayDecayedType(at->getValueType())); |
| 3303 | break; |
| 3304 | } |
| 3305 | |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3306 | case Type::ConstantArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3307 | const auto *cat = cast<ConstantArrayType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3308 | result = getConstantArrayType( |
| 3309 | getVariableArrayDecayedType(cat->getElementType()), |
| 3310 | cat->getSize(), |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 3311 | cat->getSizeExpr(), |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3312 | cat->getSizeModifier(), |
| 3313 | cat->getIndexTypeCVRQualifiers()); |
| 3314 | break; |
| 3315 | } |
| 3316 | |
| 3317 | case Type::DependentSizedArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3318 | const auto *dat = cast<DependentSizedArrayType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3319 | result = getDependentSizedArrayType( |
| 3320 | getVariableArrayDecayedType(dat->getElementType()), |
| 3321 | dat->getSizeExpr(), |
| 3322 | dat->getSizeModifier(), |
| 3323 | dat->getIndexTypeCVRQualifiers(), |
| 3324 | dat->getBracketsRange()); |
| 3325 | break; |
| 3326 | } |
| 3327 | |
| 3328 | // Turn incomplete types into [*] types. |
| 3329 | case Type::IncompleteArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3330 | const auto *iat = cast<IncompleteArrayType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3331 | result = getVariableArrayType( |
| 3332 | getVariableArrayDecayedType(iat->getElementType()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3333 | /*size*/ nullptr, |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3334 | ArrayType::Normal, |
| 3335 | iat->getIndexTypeCVRQualifiers(), |
| 3336 | SourceRange()); |
| 3337 | break; |
| 3338 | } |
| 3339 | |
| 3340 | // Turn VLA types into [*] types. |
| 3341 | case Type::VariableArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3342 | const auto *vat = cast<VariableArrayType>(ty); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3343 | result = getVariableArrayType( |
| 3344 | getVariableArrayDecayedType(vat->getElementType()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3345 | /*size*/ nullptr, |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3346 | ArrayType::Star, |
| 3347 | vat->getIndexTypeCVRQualifiers(), |
| 3348 | vat->getBracketsRange()); |
| 3349 | break; |
| 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | // Apply the top-level qualifiers from the original. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3354 | return getQualifiedType(result, split.Quals); |
John McCall | 0654946 | 2011-01-18 08:40:38 +0000 | [diff] [blame] | 3355 | } |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3356 | |
Steve Naroff | cadebd0 | 2007-08-30 18:14:25 +0000 | [diff] [blame] | 3357 | /// getVariableArrayType - Returns a non-unique reference to the type for a |
| 3358 | /// variable array of the specified element type. |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 3359 | QualType ASTContext::getVariableArrayType(QualType EltTy, |
| 3360 | Expr *NumElts, |
Steve Naroff | 90dfdd5 | 2007-08-30 18:10:14 +0000 | [diff] [blame] | 3361 | ArrayType::ArraySizeModifier ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3362 | unsigned IndexTypeQuals, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3363 | SourceRange Brackets) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3364 | // Since we don't unique expressions, it isn't possible to unique VLA's |
| 3365 | // that have an expression provided for their size. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3366 | QualType Canon; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3367 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3368 | // Be sure to pull qualifiers off the element type. |
| 3369 | if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) { |
| 3370 | SplitQualType canonSplit = getCanonicalType(EltTy).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3371 | Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM, |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3372 | IndexTypeQuals, Brackets); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3373 | Canon = getQualifiedType(Canon, canonSplit.Quals); |
Douglas Gregor | 5e8c8c0 | 2010-05-23 16:10:32 +0000 | [diff] [blame] | 3374 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3375 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3376 | auto *New = new (*this, TypeAlignment) |
Abramo Bagnara | 92141d2 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 3377 | VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3378 | |
| 3379 | VariableArrayTypes.push_back(New); |
| 3380 | Types.push_back(New); |
| 3381 | return QualType(New, 0); |
| 3382 | } |
| 3383 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3384 | /// getDependentSizedArrayType - Returns a non-unique reference to |
| 3385 | /// the type for a dependently-sized array of the specified element |
Douglas Gregor | f3f9552 | 2009-07-31 00:23:35 +0000 | [diff] [blame] | 3386 | /// type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3387 | QualType ASTContext::getDependentSizedArrayType(QualType elementType, |
| 3388 | Expr *numElements, |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3389 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3390 | unsigned elementTypeQuals, |
| 3391 | SourceRange brackets) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3392 | assert((!numElements || numElements->isTypeDependent() || |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3393 | numElements->isValueDependent()) && |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3394 | "Size must be type- or value-dependent!"); |
| 3395 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3396 | // Dependently-sized array types that do not have a specified number |
| 3397 | // of elements will have their sizes deduced from a dependent |
| 3398 | // initializer. We do no canonicalization here at all, which is okay |
| 3399 | // because they can't be used in most locations. |
| 3400 | if (!numElements) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3401 | auto *newType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3402 | = new (*this, TypeAlignment) |
| 3403 | DependentSizedArrayType(*this, elementType, QualType(), |
| 3404 | numElements, ASM, elementTypeQuals, |
| 3405 | brackets); |
| 3406 | Types.push_back(newType); |
| 3407 | return QualType(newType, 0); |
| 3408 | } |
| 3409 | |
| 3410 | // Otherwise, we actually build a new type every time, but we |
| 3411 | // also build a canonical type. |
| 3412 | |
| 3413 | SplitQualType canonElementType = getCanonicalType(elementType).split(); |
| 3414 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3415 | void *insertPos = nullptr; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3416 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3417 | DependentSizedArrayType::Profile(ID, *this, |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3418 | QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3419 | ASM, elementTypeQuals, numElements); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 3420 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3421 | // Look for an existing type with these properties. |
| 3422 | DependentSizedArrayType *canonTy = |
| 3423 | DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 3424 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3425 | // If we don't have one, build one. |
| 3426 | if (!canonTy) { |
| 3427 | canonTy = new (*this, TypeAlignment) |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3428 | DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3429 | QualType(), numElements, ASM, elementTypeQuals, |
| 3430 | brackets); |
| 3431 | DependentSizedArrayTypes.InsertNode(canonTy, insertPos); |
| 3432 | Types.push_back(canonTy); |
Douglas Gregor | ad2956c | 2009-11-19 18:03:26 +0000 | [diff] [blame] | 3433 | } |
| 3434 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3435 | // Apply qualifiers from the element type to the array. |
| 3436 | QualType canon = getQualifiedType(QualType(canonTy,0), |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3437 | canonElementType.Quals); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3438 | |
David Majnemer | 16a7470 | 2015-07-24 05:54:19 +0000 | [diff] [blame] | 3439 | // If we didn't need extra canonicalization for the element type or the size |
| 3440 | // expression, then just use that as our result. |
| 3441 | if (QualType(canonElementType.Ty, 0) == elementType && |
| 3442 | canonTy->getSizeExpr() == numElements) |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3443 | return canon; |
| 3444 | |
| 3445 | // Otherwise, we need to build a type which follows the spelling |
| 3446 | // of the element type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3447 | auto *sugaredType |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3448 | = new (*this, TypeAlignment) |
| 3449 | DependentSizedArrayType(*this, elementType, canon, numElements, |
| 3450 | ASM, elementTypeQuals, brackets); |
| 3451 | Types.push_back(sugaredType); |
| 3452 | return QualType(sugaredType, 0); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3455 | QualType ASTContext::getIncompleteArrayType(QualType elementType, |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3456 | ArrayType::ArraySizeModifier ASM, |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3457 | unsigned elementTypeQuals) const { |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3458 | llvm::FoldingSetNodeID ID; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3459 | IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3460 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3461 | void *insertPos = nullptr; |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3462 | if (IncompleteArrayType *iat = |
| 3463 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos)) |
| 3464 | return QualType(iat, 0); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3465 | |
| 3466 | // 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] | 3467 | // either, so fill in the canonical type field. We also have to pull |
| 3468 | // qualifiers off the element type. |
| 3469 | QualType canon; |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3470 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3471 | if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) { |
| 3472 | SplitQualType canonSplit = getCanonicalType(elementType).split(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3473 | canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0), |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3474 | ASM, elementTypeQuals); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 3475 | canon = getQualifiedType(canon, canonSplit.Quals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3476 | |
| 3477 | // Get the new insert position for the node we care about. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3478 | IncompleteArrayType *existing = |
| 3479 | IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos); |
| 3480 | assert(!existing && "Shouldn't be in the map!"); (void) existing; |
Ted Kremenek | 843ebedd | 2007-10-29 23:37:31 +0000 | [diff] [blame] | 3481 | } |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3482 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3483 | auto *newType = new (*this, TypeAlignment) |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3484 | IncompleteArrayType(elementType, canon, ASM, elementTypeQuals); |
Eli Friedman | bd25828 | 2008-02-15 18:16:39 +0000 | [diff] [blame] | 3485 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3486 | IncompleteArrayTypes.InsertNode(newType, insertPos); |
| 3487 | Types.push_back(newType); |
| 3488 | return QualType(newType, 0); |
Steve Naroff | 5c13180 | 2007-08-30 01:06:46 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3491 | /// getVectorType - Return the unique reference to a vector type of |
| 3492 | /// 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] | 3493 | QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3494 | VectorType::VectorKind VecKind) const { |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 3495 | assert(vecType->isBuiltinType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3496 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3497 | // Check if we've already instantiated a vector of this type. |
| 3498 | llvm::FoldingSetNodeID ID; |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 3499 | VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind); |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 3500 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3501 | void *InsertPos = nullptr; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3502 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3503 | return QualType(VTP, 0); |
| 3504 | |
| 3505 | // If the element type isn't canonical, this won't be a canonical type either, |
| 3506 | // so fill in the canonical type field. |
| 3507 | QualType Canonical; |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 3508 | if (!vecType.isCanonical()) { |
Bob Wilson | 7795480 | 2010-11-16 00:32:20 +0000 | [diff] [blame] | 3509 | Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3510 | |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3511 | // Get the new insert position for the node we care about. |
| 3512 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3513 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3514 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3515 | auto *New = new (*this, TypeAlignment) |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 3516 | VectorType(vecType, NumElts, Canonical, VecKind); |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 3517 | VectorTypes.InsertNode(New, InsertPos); |
| 3518 | Types.push_back(New); |
| 3519 | return QualType(New, 0); |
| 3520 | } |
| 3521 | |
Erich Keane | f702b02 | 2018-07-13 19:46:04 +0000 | [diff] [blame] | 3522 | QualType |
| 3523 | ASTContext::getDependentVectorType(QualType VecType, Expr *SizeExpr, |
| 3524 | SourceLocation AttrLoc, |
| 3525 | VectorType::VectorKind VecKind) const { |
| 3526 | llvm::FoldingSetNodeID ID; |
| 3527 | DependentVectorType::Profile(ID, *this, getCanonicalType(VecType), SizeExpr, |
| 3528 | VecKind); |
| 3529 | void *InsertPos = nullptr; |
| 3530 | DependentVectorType *Canon = |
| 3531 | DependentVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3532 | DependentVectorType *New; |
| 3533 | |
| 3534 | if (Canon) { |
| 3535 | New = new (*this, TypeAlignment) DependentVectorType( |
| 3536 | *this, VecType, QualType(Canon, 0), SizeExpr, AttrLoc, VecKind); |
| 3537 | } else { |
| 3538 | QualType CanonVecTy = getCanonicalType(VecType); |
| 3539 | if (CanonVecTy == VecType) { |
| 3540 | New = new (*this, TypeAlignment) DependentVectorType( |
| 3541 | *this, VecType, QualType(), SizeExpr, AttrLoc, VecKind); |
| 3542 | |
| 3543 | DependentVectorType *CanonCheck = |
| 3544 | DependentVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3545 | assert(!CanonCheck && |
| 3546 | "Dependent-sized vector_size canonical type broken"); |
| 3547 | (void)CanonCheck; |
| 3548 | DependentVectorTypes.InsertNode(New, InsertPos); |
| 3549 | } else { |
| 3550 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 3551 | SourceLocation()); |
| 3552 | New = new (*this, TypeAlignment) DependentVectorType( |
| 3553 | *this, VecType, Canon, SizeExpr, AttrLoc, VecKind); |
| 3554 | } |
| 3555 | } |
| 3556 | |
| 3557 | Types.push_back(New); |
| 3558 | return QualType(New, 0); |
| 3559 | } |
| 3560 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3561 | /// getExtVectorType - Return the unique reference to an extended vector type of |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3562 | /// 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] | 3563 | QualType |
| 3564 | ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 3565 | assert(vecType->isBuiltinType() || vecType->isDependentType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3566 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3567 | // Check if we've already instantiated a vector of this type. |
| 3568 | llvm::FoldingSetNodeID ID; |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 3569 | VectorType::Profile(ID, vecType, NumElts, Type::ExtVector, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 3570 | VectorType::GenericVector); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3571 | void *InsertPos = nullptr; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3572 | if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 3573 | return QualType(VTP, 0); |
| 3574 | |
| 3575 | // If the element type isn't canonical, this won't be a canonical type either, |
| 3576 | // so fill in the canonical type field. |
| 3577 | QualType Canonical; |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 3578 | if (!vecType.isCanonical()) { |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3579 | Canonical = getExtVectorType(getCanonicalType(vecType), NumElts); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3580 | |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3581 | // Get the new insert position for the node we care about. |
| 3582 | VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3583 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3584 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3585 | auto *New = new (*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3586 | ExtVectorType(vecType, NumElts, Canonical); |
Steve Naroff | 91fcddb | 2007-07-18 18:00:27 +0000 | [diff] [blame] | 3587 | VectorTypes.InsertNode(New, InsertPos); |
| 3588 | Types.push_back(New); |
| 3589 | return QualType(New, 0); |
| 3590 | } |
| 3591 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3592 | QualType |
| 3593 | ASTContext::getDependentSizedExtVectorType(QualType vecType, |
| 3594 | Expr *SizeExpr, |
| 3595 | SourceLocation AttrLoc) const { |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3596 | llvm::FoldingSetNodeID ID; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3597 | DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType), |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3598 | SizeExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3599 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3600 | void *InsertPos = nullptr; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3601 | DependentSizedExtVectorType *Canon |
| 3602 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3603 | DependentSizedExtVectorType *New; |
| 3604 | if (Canon) { |
| 3605 | // We already have a canonical version of this array type; use it as |
| 3606 | // the canonical type for a newly-built type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3607 | New = new (*this, TypeAlignment) |
| 3608 | DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0), |
| 3609 | SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3610 | } else { |
| 3611 | QualType CanonVecTy = getCanonicalType(vecType); |
| 3612 | if (CanonVecTy == vecType) { |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3613 | New = new (*this, TypeAlignment) |
| 3614 | DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr, |
| 3615 | AttrLoc); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 3616 | |
| 3617 | DependentSizedExtVectorType *CanonCheck |
| 3618 | = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 3619 | assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken"); |
| 3620 | (void)CanonCheck; |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3621 | DependentSizedExtVectorTypes.InsertNode(New, InsertPos); |
| 3622 | } else { |
| 3623 | QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr, |
| 3624 | SourceLocation()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3625 | New = new (*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 3626 | DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc); |
Douglas Gregor | 352169a | 2009-07-31 03:54:25 +0000 | [diff] [blame] | 3627 | } |
| 3628 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3629 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 3630 | Types.push_back(New); |
| 3631 | return QualType(New, 0); |
| 3632 | } |
| 3633 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3634 | QualType ASTContext::getDependentAddressSpaceType(QualType PointeeType, |
| 3635 | Expr *AddrSpaceExpr, |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3636 | SourceLocation AttrLoc) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3637 | assert(AddrSpaceExpr->isInstantiationDependent()); |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3638 | |
| 3639 | QualType canonPointeeType = getCanonicalType(PointeeType); |
| 3640 | |
| 3641 | void *insertPos = nullptr; |
| 3642 | llvm::FoldingSetNodeID ID; |
| 3643 | DependentAddressSpaceType::Profile(ID, *this, canonPointeeType, |
| 3644 | AddrSpaceExpr); |
| 3645 | |
| 3646 | DependentAddressSpaceType *canonTy = |
| 3647 | DependentAddressSpaceTypes.FindNodeOrInsertPos(ID, insertPos); |
| 3648 | |
| 3649 | if (!canonTy) { |
| 3650 | canonTy = new (*this, TypeAlignment) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3651 | DependentAddressSpaceType(*this, canonPointeeType, |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3652 | QualType(), AddrSpaceExpr, AttrLoc); |
| 3653 | DependentAddressSpaceTypes.InsertNode(canonTy, insertPos); |
| 3654 | Types.push_back(canonTy); |
| 3655 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3656 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3657 | if (canonPointeeType == PointeeType && |
| 3658 | canonTy->getAddrSpaceExpr() == AddrSpaceExpr) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3659 | return QualType(canonTy, 0); |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3660 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3661 | auto *sugaredType |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3662 | = new (*this, TypeAlignment) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3663 | DependentAddressSpaceType(*this, PointeeType, QualType(canonTy, 0), |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3664 | AddrSpaceExpr, AttrLoc); |
| 3665 | Types.push_back(sugaredType); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3666 | return QualType(sugaredType, 0); |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 3667 | } |
| 3668 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3669 | /// 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] | 3670 | static bool isCanonicalResultType(QualType T) { |
| 3671 | return T.isCanonical() && |
| 3672 | (T.getObjCLifetime() == Qualifiers::OCL_None || |
| 3673 | T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone); |
| 3674 | } |
| 3675 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3676 | /// getFunctionNoProtoType - Return a K&R style C function type like 'int()'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3677 | QualType |
| 3678 | ASTContext::getFunctionNoProtoType(QualType ResultTy, |
| 3679 | const FunctionType::ExtInfo &Info) const { |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3680 | // Unique functions, to guarantee there is only one function of a particular |
| 3681 | // structure. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 3682 | llvm::FoldingSetNodeID ID; |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 3683 | FunctionNoProtoType::Profile(ID, ResultTy, Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3685 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3686 | if (FunctionNoProtoType *FT = |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3687 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3688 | return QualType(FT, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3689 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3690 | QualType Canonical; |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3691 | if (!isCanonicalResultType(ResultTy)) { |
| 3692 | Canonical = |
| 3693 | getFunctionNoProtoType(getCanonicalFunctionResultType(ResultTy), Info); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3694 | |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 3695 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3696 | FunctionNoProtoType *NewIP = |
| 3697 | FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3698 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 3699 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3700 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3701 | auto *New = new (*this, TypeAlignment) |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3702 | FunctionNoProtoType(ResultTy, Canonical, Info); |
Chris Lattner | 47955de | 2007-01-27 08:37:20 +0000 | [diff] [blame] | 3703 | Types.push_back(New); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3704 | FunctionNoProtoTypes.InsertNode(New, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3705 | return QualType(New, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
Douglas Gregor | a602a15 | 2015-10-01 20:20:47 +0000 | [diff] [blame] | 3708 | CanQualType |
| 3709 | ASTContext::getCanonicalFunctionResultType(QualType ResultType) const { |
| 3710 | CanQualType CanResultType = getCanonicalType(ResultType); |
| 3711 | |
| 3712 | // Canonical result types do not have ARC lifetime qualifiers. |
| 3713 | if (CanResultType.getQualifiers().hasObjCLifetime()) { |
| 3714 | Qualifiers Qs = CanResultType.getQualifiers(); |
| 3715 | Qs.removeObjCLifetime(); |
| 3716 | return CanQualType::CreateUnsafe( |
| 3717 | getQualifiedType(CanResultType.getUnqualifiedType(), Qs)); |
| 3718 | } |
| 3719 | |
| 3720 | return CanResultType; |
| 3721 | } |
| 3722 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3723 | static bool isCanonicalExceptionSpecification( |
| 3724 | const FunctionProtoType::ExceptionSpecInfo &ESI, bool NoexceptInType) { |
| 3725 | if (ESI.Type == EST_None) |
| 3726 | return true; |
| 3727 | if (!NoexceptInType) |
| 3728 | return false; |
| 3729 | |
| 3730 | // C++17 onwards: exception specification is part of the type, as a simple |
| 3731 | // boolean "can this function type throw". |
| 3732 | if (ESI.Type == EST_BasicNoexcept) |
| 3733 | return true; |
| 3734 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3735 | // A noexcept(expr) specification is (possibly) canonical if expr is |
| 3736 | // value-dependent. |
| 3737 | if (ESI.Type == EST_DependentNoexcept) |
| 3738 | return true; |
| 3739 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3740 | // A dynamic exception specification is canonical if it only contains pack |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3741 | // expansions (so we can't tell whether it's non-throwing) and all its |
| 3742 | // contained types are canonical. |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3743 | if (ESI.Type == EST_Dynamic) { |
Richard Smith | fda59e5 | 2016-10-26 01:05:54 +0000 | [diff] [blame] | 3744 | bool AnyPackExpansions = false; |
| 3745 | for (QualType ET : ESI.Exceptions) { |
| 3746 | if (!ET.isCanonical()) |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3747 | return false; |
Richard Smith | fda59e5 | 2016-10-26 01:05:54 +0000 | [diff] [blame] | 3748 | if (ET->getAs<PackExpansionType>()) |
| 3749 | AnyPackExpansions = true; |
| 3750 | } |
| 3751 | return AnyPackExpansions; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3754 | return false; |
| 3755 | } |
| 3756 | |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3757 | QualType ASTContext::getFunctionTypeInternal( |
| 3758 | QualType ResultTy, ArrayRef<QualType> ArgArray, |
| 3759 | const FunctionProtoType::ExtProtoInfo &EPI, bool OnlyWantCanonical) const { |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3760 | size_t NumArgs = ArgArray.size(); |
| 3761 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3762 | // Unique functions, to guarantee there is only one function of a particular |
| 3763 | // structure. |
| 3764 | llvm::FoldingSetNodeID ID; |
| 3765 | FunctionProtoType::Profile(ID, ResultTy, ArgArray.begin(), NumArgs, EPI, |
| 3766 | *this, true); |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3767 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3768 | QualType Canonical; |
| 3769 | bool Unique = false; |
| 3770 | |
| 3771 | void *InsertPos = nullptr; |
| 3772 | if (FunctionProtoType *FPT = |
| 3773 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos)) { |
| 3774 | QualType Existing = QualType(FPT, 0); |
| 3775 | |
| 3776 | // If we find a pre-existing equivalent FunctionProtoType, we can just reuse |
| 3777 | // it so long as our exception specification doesn't contain a dependent |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3778 | // noexcept expression, or we're just looking for a canonical type. |
| 3779 | // Otherwise, we're going to need to create a type |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3780 | // sugar node to hold the concrete expression. |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3781 | if (OnlyWantCanonical || !isComputedNoexcept(EPI.ExceptionSpec.Type) || |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3782 | EPI.ExceptionSpec.NoexceptExpr == FPT->getNoexceptExpr()) |
| 3783 | return Existing; |
| 3784 | |
| 3785 | // We need a new type sugar node for this one, to hold the new noexcept |
| 3786 | // expression. We do no canonicalization here, but that's OK since we don't |
| 3787 | // expect to see the same noexcept expression much more than once. |
| 3788 | Canonical = getCanonicalType(Existing); |
| 3789 | Unique = true; |
| 3790 | } |
| 3791 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3792 | bool NoexceptInType = getLangOpts().CPlusPlus17; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3793 | bool IsCanonicalExceptionSpec = |
| 3794 | isCanonicalExceptionSpecification(EPI.ExceptionSpec, NoexceptInType); |
| 3795 | |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3796 | // Determine whether the type being created is already canonical or not. |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3797 | bool isCanonical = !Unique && IsCanonicalExceptionSpec && |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3798 | isCanonicalResultType(ResultTy) && !EPI.HasTrailingReturn; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3799 | for (unsigned i = 0; i != NumArgs && isCanonical; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3800 | if (!ArgArray[i].isCanonicalAsParam()) |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3801 | isCanonical = false; |
| 3802 | |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3803 | if (OnlyWantCanonical) |
| 3804 | assert(isCanonical && |
| 3805 | "given non-canonical parameters constructing canonical type"); |
| 3806 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3807 | // If this type isn't canonical, get the canonical version of it if we don't |
| 3808 | // already have it. The exception spec is only partially part of the |
| 3809 | // canonical type, and only in C++17 onwards. |
| 3810 | if (!isCanonical && Canonical.isNull()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3811 | SmallVector<QualType, 16> CanonicalArgs; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3812 | CanonicalArgs.reserve(NumArgs); |
| 3813 | for (unsigned i = 0; i != NumArgs; ++i) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 3814 | CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i])); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3815 | |
Benjamin Kramer | 3f515cd | 2016-10-26 12:51:45 +0000 | [diff] [blame] | 3816 | llvm::SmallVector<QualType, 8> ExceptionTypeStorage; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3817 | FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI; |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 3818 | CanonicalEPI.HasTrailingReturn = false; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3819 | |
| 3820 | if (IsCanonicalExceptionSpec) { |
| 3821 | // Exception spec is already OK. |
| 3822 | } else if (NoexceptInType) { |
| 3823 | switch (EPI.ExceptionSpec.Type) { |
| 3824 | case EST_Unparsed: case EST_Unevaluated: case EST_Uninstantiated: |
| 3825 | // We don't know yet. It shouldn't matter what we pick here; no-one |
| 3826 | // should ever look at this. |
| 3827 | LLVM_FALLTHROUGH; |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3828 | case EST_None: case EST_MSAny: case EST_NoexceptFalse: |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3829 | CanonicalEPI.ExceptionSpec.Type = EST_None; |
| 3830 | break; |
| 3831 | |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3832 | // A dynamic exception specification is almost always "not noexcept", |
| 3833 | // with the exception that a pack expansion might expand to no types. |
| 3834 | case EST_Dynamic: { |
| 3835 | bool AnyPacks = false; |
| 3836 | for (QualType ET : EPI.ExceptionSpec.Exceptions) { |
| 3837 | if (ET->getAs<PackExpansionType>()) |
| 3838 | AnyPacks = true; |
| 3839 | ExceptionTypeStorage.push_back(getCanonicalType(ET)); |
| 3840 | } |
| 3841 | if (!AnyPacks) |
| 3842 | CanonicalEPI.ExceptionSpec.Type = EST_None; |
| 3843 | else { |
| 3844 | CanonicalEPI.ExceptionSpec.Type = EST_Dynamic; |
| 3845 | CanonicalEPI.ExceptionSpec.Exceptions = ExceptionTypeStorage; |
| 3846 | } |
| 3847 | break; |
| 3848 | } |
| 3849 | |
Erich Keane | d02f4a1 | 2019-05-30 17:31:54 +0000 | [diff] [blame] | 3850 | case EST_DynamicNone: |
| 3851 | case EST_BasicNoexcept: |
| 3852 | case EST_NoexceptTrue: |
| 3853 | case EST_NoThrow: |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3854 | CanonicalEPI.ExceptionSpec.Type = EST_BasicNoexcept; |
| 3855 | break; |
| 3856 | |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 3857 | case EST_DependentNoexcept: |
| 3858 | llvm_unreachable("dependent noexcept is already canonical"); |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3859 | } |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3860 | } else { |
| 3861 | CanonicalEPI.ExceptionSpec = FunctionProtoType::ExceptionSpecInfo(); |
| 3862 | } |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3863 | |
Douglas Gregor | a602a15 | 2015-10-01 20:20:47 +0000 | [diff] [blame] | 3864 | // Adjust the canonical function result type. |
| 3865 | CanQualType CanResultTy = getCanonicalFunctionResultType(ResultTy); |
Richard Smith | 304b124 | 2016-10-18 20:13:25 +0000 | [diff] [blame] | 3866 | Canonical = |
| 3867 | getFunctionTypeInternal(CanResultTy, CanonicalArgs, CanonicalEPI, true); |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3868 | |
Chris Lattner | fd4de79 | 2007-01-27 01:15:32 +0000 | [diff] [blame] | 3869 | // Get the new insert position for the node we care about. |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3870 | FunctionProtoType *NewIP = |
| 3871 | FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3872 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3873 | } |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 3874 | |
Bruno Ricci | 26a2536 | 2018-10-02 11:46:38 +0000 | [diff] [blame] | 3875 | // Compute the needed size to hold this FunctionProtoType and the |
| 3876 | // various trailing objects. |
| 3877 | auto ESH = FunctionProtoType::getExceptionSpecSize( |
| 3878 | EPI.ExceptionSpec.Type, EPI.ExceptionSpec.Exceptions.size()); |
| 3879 | size_t Size = FunctionProtoType::totalSizeToAlloc< |
Nicolas Manichon | cc3c935 | 2019-12-03 08:21:55 -0500 | [diff] [blame] | 3880 | QualType, SourceLocation, FunctionType::FunctionTypeExtraBitfields, |
Bruno Ricci | 26a2536 | 2018-10-02 11:46:38 +0000 | [diff] [blame] | 3881 | FunctionType::ExceptionType, Expr *, FunctionDecl *, |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 3882 | FunctionProtoType::ExtParameterInfo, Qualifiers>( |
Nicolas Manichon | cc3c935 | 2019-12-03 08:21:55 -0500 | [diff] [blame] | 3883 | NumArgs, EPI.Variadic, |
| 3884 | FunctionProtoType::hasExtraBitfields(EPI.ExceptionSpec.Type), |
Bruno Ricci | 26a2536 | 2018-10-02 11:46:38 +0000 | [diff] [blame] | 3885 | ESH.NumExceptionType, ESH.NumExprPtr, ESH.NumFunctionDeclPtr, |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 3886 | EPI.ExtParameterInfos ? NumArgs : 0, |
| 3887 | EPI.TypeQuals.hasNonFastQualifiers() ? 1 : 0); |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 3888 | |
Bruno Ricci | 26a2536 | 2018-10-02 11:46:38 +0000 | [diff] [blame] | 3889 | auto *FTP = (FunctionProtoType *)Allocate(Size, TypeAlignment); |
Roman Divacky | 65b88cd | 2011-03-01 17:40:53 +0000 | [diff] [blame] | 3890 | FunctionProtoType::ExtProtoInfo newEPI = EPI; |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3891 | new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3892 | Types.push_back(FTP); |
Richard Smith | 2a2cda5 | 2016-10-18 19:29:18 +0000 | [diff] [blame] | 3893 | if (!Unique) |
| 3894 | FunctionProtoTypes.InsertNode(FTP, InsertPos); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 3895 | return QualType(FTP, 0); |
Chris Lattner | c6ad813 | 2006-12-02 07:52:18 +0000 | [diff] [blame] | 3896 | } |
Chris Lattner | ef51c20 | 2006-11-10 07:17:23 +0000 | [diff] [blame] | 3897 | |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3898 | QualType ASTContext::getPipeType(QualType T, bool ReadOnly) const { |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3899 | llvm::FoldingSetNodeID ID; |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3900 | PipeType::Profile(ID, T, ReadOnly); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3901 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 3902 | void *InsertPos = nullptr; |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3903 | if (PipeType *PT = PipeTypes.FindNodeOrInsertPos(ID, InsertPos)) |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3904 | return QualType(PT, 0); |
| 3905 | |
| 3906 | // If the pipe element type isn't canonical, this won't be a canonical type |
| 3907 | // either, so fill in the canonical type field. |
| 3908 | QualType Canonical; |
| 3909 | if (!T.isCanonical()) { |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3910 | Canonical = getPipeType(getCanonicalType(T), ReadOnly); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3911 | |
| 3912 | // Get the new insert position for the node we care about. |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3913 | PipeType *NewIP = PipeTypes.FindNodeOrInsertPos(ID, InsertPos); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3914 | assert(!NewIP && "Shouldn't be in the map!"); |
| 3915 | (void)NewIP; |
| 3916 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3917 | auto *New = new (*this, TypeAlignment) PipeType(T, Canonical, ReadOnly); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3918 | Types.push_back(New); |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3919 | PipeTypes.InsertNode(New, InsertPos); |
Joey Gouly | 5788b78 | 2016-11-18 14:10:54 +0000 | [diff] [blame] | 3920 | return QualType(New, 0); |
| 3921 | } |
| 3922 | |
Anastasia Stulova | 59055b9 | 2018-05-09 13:23:26 +0000 | [diff] [blame] | 3923 | QualType ASTContext::adjustStringLiteralBaseType(QualType Ty) const { |
| 3924 | // OpenCL v1.1 s6.5.3: a string literal is in the constant address space. |
| 3925 | return LangOpts.OpenCL ? getAddrSpaceQualType(Ty, LangAS::opencl_constant) |
| 3926 | : Ty; |
| 3927 | } |
| 3928 | |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3929 | QualType ASTContext::getReadPipeType(QualType T) const { |
| 3930 | return getPipeType(T, true); |
| 3931 | } |
| 3932 | |
Joey Gouly | 5788b78 | 2016-11-18 14:10:54 +0000 | [diff] [blame] | 3933 | QualType ASTContext::getWritePipeType(QualType T) const { |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 3934 | return getPipeType(T, false); |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3937 | #ifndef NDEBUG |
| 3938 | static bool NeedsInjectedClassNameType(const RecordDecl *D) { |
| 3939 | if (!isa<CXXRecordDecl>(D)) return false; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3940 | const auto *RD = cast<CXXRecordDecl>(D); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3941 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) |
| 3942 | return true; |
| 3943 | if (RD->getDescribedClassTemplate() && |
| 3944 | !isa<ClassTemplateSpecializationDecl>(RD)) |
| 3945 | return true; |
| 3946 | return false; |
| 3947 | } |
| 3948 | #endif |
| 3949 | |
| 3950 | /// getInjectedClassNameType - Return the unique reference to the |
| 3951 | /// injected class name type for the specified templated declaration. |
| 3952 | QualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3953 | QualType TST) const { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3954 | assert(NeedsInjectedClassNameType(Decl)); |
| 3955 | if (Decl->TypeForDecl) { |
| 3956 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 3957 | } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) { |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3958 | assert(PrevDecl->TypeForDecl && "previous declaration has no type"); |
| 3959 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 3960 | assert(isa<InjectedClassNameType>(Decl->TypeForDecl)); |
| 3961 | } else { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3962 | Type *newType = |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3963 | new (*this, TypeAlignment) InjectedClassNameType(Decl, TST); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3964 | Decl->TypeForDecl = newType; |
| 3965 | Types.push_back(newType); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3966 | } |
| 3967 | return QualType(Decl->TypeForDecl, 0); |
| 3968 | } |
| 3969 | |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3970 | /// getTypeDeclType - Return the unique reference to the type for the |
| 3971 | /// specified type declaration. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3972 | QualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const { |
Argyrios Kyrtzidis | 89656d2 | 2008-10-16 16:50:47 +0000 | [diff] [blame] | 3973 | assert(Decl && "Passed null for Decl param"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3974 | assert(!Decl->TypeForDecl && "TypeForDecl present in slow case"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3975 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3976 | if (const auto *Typedef = dyn_cast<TypedefNameDecl>(Decl)) |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3977 | return getTypedefType(Typedef); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3978 | |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3979 | assert(!isa<TemplateTypeParmDecl>(Decl) && |
| 3980 | "Template type parameter types are always available."); |
| 3981 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3982 | if (const auto *Record = dyn_cast<RecordDecl>(Decl)) { |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 3983 | assert(Record->isFirstDecl() && "struct/union has previous declaration"); |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3984 | assert(!NeedsInjectedClassNameType(Record)); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3985 | return getRecordType(Record); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3986 | } else if (const auto *Enum = dyn_cast<EnumDecl>(Decl)) { |
Rafael Espindola | 3f9e444 | 2013-10-19 02:13:21 +0000 | [diff] [blame] | 3987 | assert(Enum->isFirstDecl() && "enum has previous declaration"); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 3988 | return getEnumType(Enum); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 3989 | } else if (const auto *Using = dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3990 | Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using); |
| 3991 | Decl->TypeForDecl = newType; |
| 3992 | Types.push_back(newType); |
Mike Stump | e9c6ffc | 2009-07-31 02:02:20 +0000 | [diff] [blame] | 3993 | } else |
John McCall | 96f0b5f | 2010-03-10 06:48:02 +0000 | [diff] [blame] | 3994 | llvm_unreachable("TypeDecl without a type?"); |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 3995 | |
Argyrios Kyrtzidis | faf0876 | 2008-08-07 20:55:28 +0000 | [diff] [blame] | 3996 | return QualType(Decl->TypeForDecl, 0); |
Douglas Gregor | 83a586e | 2008-04-13 21:07:44 +0000 | [diff] [blame] | 3997 | } |
| 3998 | |
Chris Lattner | 32d920b | 2007-01-26 02:01:53 +0000 | [diff] [blame] | 3999 | /// getTypedefType - Return the unique reference to the type for the |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4000 | /// specified typedef name decl. |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4001 | QualType |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4002 | ASTContext::getTypedefType(const TypedefNameDecl *Decl, |
| 4003 | QualType Canonical) const { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 4004 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4005 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4006 | if (Canonical.isNull()) |
| 4007 | Canonical = getCanonicalType(Decl->getUnderlyingType()); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4008 | auto *newType = new (*this, TypeAlignment) |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4009 | TypedefType(Type::Typedef, Decl, Canonical); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4010 | Decl->TypeForDecl = newType; |
| 4011 | Types.push_back(newType); |
| 4012 | return QualType(newType, 0); |
Chris Lattner | d0342e5 | 2006-11-20 04:02:15 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4015 | QualType ASTContext::getRecordType(const RecordDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 4016 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 4017 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4018 | if (const RecordDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 4019 | if (PrevDecl->TypeForDecl) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4020 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 4021 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4022 | auto *newType = new (*this, TypeAlignment) RecordType(Decl); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4023 | Decl->TypeForDecl = newType; |
| 4024 | Types.push_back(newType); |
| 4025 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4028 | QualType ASTContext::getEnumType(const EnumDecl *Decl) const { |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 4029 | if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); |
| 4030 | |
Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 4031 | if (const EnumDecl *PrevDecl = Decl->getPreviousDecl()) |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 4032 | if (PrevDecl->TypeForDecl) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4033 | return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 4034 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4035 | auto *newType = new (*this, TypeAlignment) EnumType(Decl); |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 4036 | Decl->TypeForDecl = newType; |
| 4037 | Types.push_back(newType); |
| 4038 | return QualType(newType, 0); |
Argyrios Kyrtzidis | b5fcdc2 | 2010-07-04 21:44:47 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
Richard Smith | e43e2b3 | 2018-08-20 21:47:29 +0000 | [diff] [blame] | 4041 | QualType ASTContext::getAttributedType(attr::Kind attrKind, |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 4042 | QualType modifiedType, |
| 4043 | QualType equivalentType) { |
| 4044 | llvm::FoldingSetNodeID id; |
| 4045 | AttributedType::Profile(id, attrKind, modifiedType, equivalentType); |
| 4046 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4047 | void *insertPos = nullptr; |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 4048 | AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos); |
| 4049 | if (type) return QualType(type, 0); |
| 4050 | |
| 4051 | QualType canon = getCanonicalType(equivalentType); |
| 4052 | type = new (*this, TypeAlignment) |
Leonard Chan | c72aaf6 | 2019-05-07 03:20:17 +0000 | [diff] [blame] | 4053 | AttributedType(canon, attrKind, modifiedType, equivalentType); |
John McCall | 8190451 | 2011-01-06 01:58:22 +0000 | [diff] [blame] | 4054 | |
| 4055 | Types.push_back(type); |
| 4056 | AttributedTypes.InsertNode(type, insertPos); |
| 4057 | |
| 4058 | return QualType(type, 0); |
| 4059 | } |
| 4060 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4061 | /// Retrieve a substitution-result type. |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 4062 | QualType |
| 4063 | ASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4064 | QualType Replacement) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 4065 | assert(Replacement.isCanonical() |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 4066 | && "replacement types must always be canonical"); |
| 4067 | |
| 4068 | llvm::FoldingSetNodeID ID; |
| 4069 | SubstTemplateTypeParmType::Profile(ID, Parm, Replacement); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4070 | void *InsertPos = nullptr; |
John McCall | cebee16 | 2009-10-18 09:09:24 +0000 | [diff] [blame] | 4071 | SubstTemplateTypeParmType *SubstParm |
| 4072 | = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4073 | |
| 4074 | if (!SubstParm) { |
| 4075 | SubstParm = new (*this, TypeAlignment) |
| 4076 | SubstTemplateTypeParmType(Parm, Replacement); |
| 4077 | Types.push_back(SubstParm); |
| 4078 | SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos); |
| 4079 | } |
| 4080 | |
| 4081 | return QualType(SubstParm, 0); |
| 4082 | } |
| 4083 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4084 | /// Retrieve a |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4085 | QualType ASTContext::getSubstTemplateTypeParmPackType( |
| 4086 | const TemplateTypeParmType *Parm, |
| 4087 | const TemplateArgument &ArgPack) { |
| 4088 | #ifndef NDEBUG |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 4089 | for (const auto &P : ArgPack.pack_elements()) { |
| 4090 | assert(P.getKind() == TemplateArgument::Type &&"Pack contains a non-type"); |
| 4091 | assert(P.getAsType().isCanonical() && "Pack contains non-canonical type"); |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4092 | } |
| 4093 | #endif |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4094 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4095 | llvm::FoldingSetNodeID ID; |
| 4096 | SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4097 | void *InsertPos = nullptr; |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4098 | if (SubstTemplateTypeParmPackType *SubstParm |
| 4099 | = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4100 | return QualType(SubstParm, 0); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4101 | |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4102 | QualType Canon; |
| 4103 | if (!Parm->isCanonicalUnqualified()) { |
| 4104 | Canon = getCanonicalType(QualType(Parm, 0)); |
| 4105 | Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon), |
| 4106 | ArgPack); |
| 4107 | SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4108 | } |
| 4109 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4110 | auto *SubstParm |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4111 | = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon, |
| 4112 | ArgPack); |
| 4113 | Types.push_back(SubstParm); |
George Burgess IV | b5fe855 | 2017-06-12 17:44:30 +0000 | [diff] [blame] | 4114 | SubstTemplateTypeParmPackTypes.InsertNode(SubstParm, InsertPos); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4115 | return QualType(SubstParm, 0); |
Douglas Gregor | ada4b79 | 2011-01-14 02:55:32 +0000 | [diff] [blame] | 4116 | } |
| 4117 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4118 | /// Retrieve the template type parameter type for a template |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4119 | /// parameter or parameter pack with the given depth, index, and (optionally) |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 4120 | /// name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4121 | QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index, |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 4122 | bool ParameterPack, |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4123 | TemplateTypeParmDecl *TTPDecl) const { |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 4124 | llvm::FoldingSetNodeID ID; |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4125 | TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4126 | void *InsertPos = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4127 | TemplateTypeParmType *TypeParm |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 4128 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4129 | |
| 4130 | if (TypeParm) |
| 4131 | return QualType(TypeParm, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4132 | |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4133 | if (TTPDecl) { |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 4134 | QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4135 | TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4136 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4137 | TemplateTypeParmType *TypeCheck |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4138 | = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4139 | assert(!TypeCheck && "Template type parameter canonical type broken"); |
| 4140 | (void)TypeCheck; |
Anders Carlsson | 90036dc | 2009-06-16 00:30:48 +0000 | [diff] [blame] | 4141 | } else |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4142 | TypeParm = new (*this, TypeAlignment) |
| 4143 | TemplateTypeParmType(Depth, Index, ParameterPack); |
Douglas Gregor | eff93e0 | 2009-02-05 23:33:38 +0000 | [diff] [blame] | 4144 | |
| 4145 | Types.push_back(TypeParm); |
| 4146 | TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos); |
| 4147 | |
| 4148 | return QualType(TypeParm, 0); |
| 4149 | } |
| 4150 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4151 | TypeSourceInfo * |
| 4152 | ASTContext::getTemplateSpecializationTypeInfo(TemplateName Name, |
| 4153 | SourceLocation NameLoc, |
| 4154 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4155 | QualType Underlying) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4156 | assert(!Name.getAsDependentTemplateName() && |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 4157 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4158 | QualType TST = getTemplateSpecializationType(Name, Args, Underlying); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4159 | |
| 4160 | TypeSourceInfo *DI = CreateTypeSourceInfo(TST); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 4161 | TemplateSpecializationTypeLoc TL = |
| 4162 | DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>(); |
Abramo Bagnara | 48c05be | 2012-02-06 14:41:24 +0000 | [diff] [blame] | 4163 | TL.setTemplateKeywordLoc(SourceLocation()); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 4164 | TL.setTemplateNameLoc(NameLoc); |
| 4165 | TL.setLAngleLoc(Args.getLAngleLoc()); |
| 4166 | TL.setRAngleLoc(Args.getRAngleLoc()); |
| 4167 | for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) |
| 4168 | TL.setArgLocInfo(i, Args[i].getLocInfo()); |
| 4169 | return DI; |
| 4170 | } |
| 4171 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4172 | QualType |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 4173 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4174 | const TemplateArgumentListInfo &Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4175 | QualType Underlying) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4176 | assert(!Template.getAsDependentTemplateName() && |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 4177 | "No dependent template names here!"); |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4178 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4179 | SmallVector<TemplateArgument, 4> ArgVec; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4180 | ArgVec.reserve(Args.size()); |
| 4181 | for (const TemplateArgumentLoc &Arg : Args.arguments()) |
| 4182 | ArgVec.push_back(Arg.getArgument()); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4183 | |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4184 | return getTemplateSpecializationType(Template, ArgVec, Underlying); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 4187 | #ifndef NDEBUG |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4188 | static bool hasAnyPackExpansions(ArrayRef<TemplateArgument> Args) { |
| 4189 | for (const TemplateArgument &Arg : Args) |
| 4190 | if (Arg.isPackExpansion()) |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 4191 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4192 | |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 4193 | return true; |
| 4194 | } |
| 4195 | #endif |
| 4196 | |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 4197 | QualType |
| 4198 | ASTContext::getTemplateSpecializationType(TemplateName Template, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4199 | ArrayRef<TemplateArgument> Args, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4200 | QualType Underlying) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4201 | assert(!Template.getAsDependentTemplateName() && |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 4202 | "No dependent template names here!"); |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 4203 | // Look through qualified template names. |
| 4204 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 4205 | Template = TemplateName(QTN->getTemplateDecl()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4206 | |
| 4207 | bool IsTypeAlias = |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4208 | Template.getAsTemplateDecl() && |
| 4209 | isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl()); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4210 | QualType CanonType; |
| 4211 | if (!Underlying.isNull()) |
| 4212 | CanonType = getCanonicalType(Underlying); |
| 4213 | else { |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 4214 | // We can get here with an alias template when the specialization contains |
| 4215 | // a pack expansion that does not match up with a parameter pack. |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4216 | assert((!IsTypeAlias || hasAnyPackExpansions(Args)) && |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 4217 | "Caller must compute aliased type"); |
| 4218 | IsTypeAlias = false; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4219 | CanonType = getCanonicalTemplateSpecializationType(Template, Args); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4220 | } |
Douglas Gregor | d56a91e | 2009-02-26 22:19:44 +0000 | [diff] [blame] | 4221 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 4222 | // Allocate the (non-canonical) template specialization type, but don't |
| 4223 | // try to unique it: these types typically have location information that |
| 4224 | // we don't unique and don't want to lose. |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4225 | void *Mem = Allocate(sizeof(TemplateSpecializationType) + |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4226 | sizeof(TemplateArgument) * Args.size() + |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 4227 | (IsTypeAlias? sizeof(QualType) : 0), |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4228 | TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4229 | auto *Spec |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4230 | = new (Mem) TemplateSpecializationType(Template, Args, CanonType, |
Douglas Gregor | 1f79ca8 | 2012-02-03 17:16:23 +0000 | [diff] [blame] | 4231 | IsTypeAlias ? Underlying : QualType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4232 | |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 4233 | Types.push_back(Spec); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4234 | return QualType(Spec, 0); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 4235 | } |
| 4236 | |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4237 | QualType ASTContext::getCanonicalTemplateSpecializationType( |
| 4238 | TemplateName Template, ArrayRef<TemplateArgument> Args) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4239 | assert(!Template.getAsDependentTemplateName() && |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 4240 | "No dependent template names here!"); |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4241 | |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 4242 | // Look through qualified template names. |
| 4243 | if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName()) |
| 4244 | Template = TemplateName(QTN->getTemplateDecl()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4245 | |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4246 | // Build the canonical template specialization type. |
| 4247 | TemplateName CanonTemplate = getCanonicalTemplateName(Template); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4248 | SmallVector<TemplateArgument, 4> CanonArgs; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4249 | unsigned NumArgs = Args.size(); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4250 | CanonArgs.reserve(NumArgs); |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4251 | for (const TemplateArgument &Arg : Args) |
| 4252 | CanonArgs.push_back(getCanonicalTemplateArgument(Arg)); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4253 | |
| 4254 | // Determine whether this canonical template specialization type already |
| 4255 | // exists. |
| 4256 | llvm::FoldingSetNodeID ID; |
| 4257 | TemplateSpecializationType::Profile(ID, CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4258 | CanonArgs, *this); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4259 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4260 | void *InsertPos = nullptr; |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4261 | TemplateSpecializationType *Spec |
| 4262 | = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4263 | |
| 4264 | if (!Spec) { |
| 4265 | // Allocate a new canonical template specialization type. |
| 4266 | void *Mem = Allocate((sizeof(TemplateSpecializationType) + |
| 4267 | sizeof(TemplateArgument) * NumArgs), |
| 4268 | TypeAlignment); |
| 4269 | Spec = new (Mem) TemplateSpecializationType(CanonTemplate, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4270 | CanonArgs, |
Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 4271 | QualType(), QualType()); |
Argyrios Kyrtzidis | 45a83f9 | 2010-07-02 11:55:11 +0000 | [diff] [blame] | 4272 | Types.push_back(Spec); |
| 4273 | TemplateSpecializationTypes.InsertNode(Spec, InsertPos); |
| 4274 | } |
| 4275 | |
| 4276 | assert(Spec->isDependentType() && |
| 4277 | "Non-dependent template-id type must have a canonical type"); |
| 4278 | return QualType(Spec, 0); |
| 4279 | } |
| 4280 | |
Joel E. Denny | 7509a2f | 2018-05-14 19:36:45 +0000 | [diff] [blame] | 4281 | QualType ASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword, |
| 4282 | NestedNameSpecifier *NNS, |
| 4283 | QualType NamedType, |
| 4284 | TagDecl *OwnedTagDecl) const { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4285 | llvm::FoldingSetNodeID ID; |
Joel E. Denny | 7509a2f | 2018-05-14 19:36:45 +0000 | [diff] [blame] | 4286 | ElaboratedType::Profile(ID, Keyword, NNS, NamedType, OwnedTagDecl); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4287 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4288 | void *InsertPos = nullptr; |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4289 | ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4290 | if (T) |
| 4291 | return QualType(T, 0); |
| 4292 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4293 | QualType Canon = NamedType; |
| 4294 | if (!Canon.isCanonical()) { |
| 4295 | Canon = getCanonicalType(NamedType); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4296 | ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4297 | assert(!CheckT && "Elaborated canonical type broken"); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4298 | (void)CheckT; |
| 4299 | } |
| 4300 | |
Bruno Ricci | d6bd598 | 2018-08-16 10:48:16 +0000 | [diff] [blame] | 4301 | void *Mem = Allocate(ElaboratedType::totalSizeToAlloc<TagDecl *>(!!OwnedTagDecl), |
| 4302 | TypeAlignment); |
| 4303 | T = new (Mem) ElaboratedType(Keyword, NNS, NamedType, Canon, OwnedTagDecl); |
| 4304 | |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4305 | Types.push_back(T); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4306 | ElaboratedTypes.InsertNode(T, InsertPos); |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 4307 | return QualType(T, 0); |
| 4308 | } |
| 4309 | |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4310 | QualType |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4311 | ASTContext::getParenType(QualType InnerType) const { |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4312 | llvm::FoldingSetNodeID ID; |
| 4313 | ParenType::Profile(ID, InnerType); |
| 4314 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4315 | void *InsertPos = nullptr; |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4316 | ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4317 | if (T) |
| 4318 | return QualType(T, 0); |
| 4319 | |
| 4320 | QualType Canon = InnerType; |
| 4321 | if (!Canon.isCanonical()) { |
| 4322 | Canon = getCanonicalType(InnerType); |
| 4323 | ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4324 | assert(!CheckT && "Paren canonical type broken"); |
| 4325 | (void)CheckT; |
| 4326 | } |
| 4327 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 4328 | T = new (*this, TypeAlignment) ParenType(InnerType, Canon); |
Abramo Bagnara | 924a8f3 | 2010-12-10 16:29:40 +0000 | [diff] [blame] | 4329 | Types.push_back(T); |
| 4330 | ParenTypes.InsertNode(T, InsertPos); |
| 4331 | return QualType(T, 0); |
| 4332 | } |
| 4333 | |
Leonard Chan | c72aaf6 | 2019-05-07 03:20:17 +0000 | [diff] [blame] | 4334 | QualType |
| 4335 | ASTContext::getMacroQualifiedType(QualType UnderlyingTy, |
| 4336 | const IdentifierInfo *MacroII) const { |
| 4337 | QualType Canon = UnderlyingTy; |
| 4338 | if (!Canon.isCanonical()) |
| 4339 | Canon = getCanonicalType(UnderlyingTy); |
| 4340 | |
| 4341 | auto *newType = new (*this, TypeAlignment) |
| 4342 | MacroQualifiedType(UnderlyingTy, Canon, MacroII); |
| 4343 | Types.push_back(newType); |
| 4344 | return QualType(newType, 0); |
| 4345 | } |
| 4346 | |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 4347 | QualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword, |
| 4348 | NestedNameSpecifier *NNS, |
| 4349 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4350 | QualType Canon) const { |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4351 | if (Canon.isNull()) { |
| 4352 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Richard Smith | 74f0234 | 2017-01-19 21:00:13 +0000 | [diff] [blame] | 4353 | if (CanonNNS != NNS) |
| 4354 | Canon = getDependentNameType(Keyword, CanonNNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4355 | } |
| 4356 | |
| 4357 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 4358 | DependentNameType::Profile(ID, Keyword, NNS, Name); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4359 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4360 | void *InsertPos = nullptr; |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4361 | DependentNameType *T |
| 4362 | = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4363 | if (T) |
| 4364 | return QualType(T, 0); |
| 4365 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 4366 | T = new (*this, TypeAlignment) DependentNameType(Keyword, NNS, Name, Canon); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4367 | Types.push_back(T); |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4368 | DependentNameTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4369 | return QualType(T, 0); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4372 | QualType |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4373 | ASTContext::getDependentTemplateSpecializationType( |
| 4374 | ElaboratedTypeKeyword Keyword, |
Douglas Gregor | 0208535 | 2010-03-31 20:19:30 +0000 | [diff] [blame] | 4375 | NestedNameSpecifier *NNS, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4376 | const IdentifierInfo *Name, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4377 | const TemplateArgumentListInfo &Args) const { |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4378 | // TODO: avoid this copy |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4379 | SmallVector<TemplateArgument, 16> ArgCopy; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4380 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
| 4381 | ArgCopy.push_back(Args[I].getArgument()); |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4382 | return getDependentTemplateSpecializationType(Keyword, NNS, Name, ArgCopy); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | QualType |
| 4386 | ASTContext::getDependentTemplateSpecializationType( |
| 4387 | ElaboratedTypeKeyword Keyword, |
| 4388 | NestedNameSpecifier *NNS, |
| 4389 | const IdentifierInfo *Name, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4390 | ArrayRef<TemplateArgument> Args) const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4391 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 6e06801 | 2011-02-28 00:04:36 +0000 | [diff] [blame] | 4392 | "nested-name-specifier must be dependent"); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4393 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4394 | llvm::FoldingSetNodeID ID; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4395 | DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4396 | Name, Args); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4397 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4398 | void *InsertPos = nullptr; |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4399 | DependentTemplateSpecializationType *T |
| 4400 | = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4401 | if (T) |
| 4402 | return QualType(T, 0); |
| 4403 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4404 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 4405 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4406 | ElaboratedTypeKeyword CanonKeyword = Keyword; |
| 4407 | if (Keyword == ETK_None) CanonKeyword = ETK_Typename; |
| 4408 | |
| 4409 | bool AnyNonCanonArgs = false; |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4410 | unsigned NumArgs = Args.size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4411 | SmallVector<TemplateArgument, 16> CanonArgs(NumArgs); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4412 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 4413 | CanonArgs[I] = getCanonicalTemplateArgument(Args[I]); |
| 4414 | if (!CanonArgs[I].structurallyEquals(Args[I])) |
| 4415 | AnyNonCanonArgs = true; |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4416 | } |
| 4417 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4418 | QualType Canon; |
| 4419 | if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) { |
| 4420 | Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4421 | Name, |
| 4422 | CanonArgs); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4423 | |
| 4424 | // Find the insert position again. |
| 4425 | DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4426 | } |
| 4427 | |
| 4428 | void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) + |
| 4429 | sizeof(TemplateArgument) * NumArgs), |
| 4430 | TypeAlignment); |
John McCall | 773cc98 | 2010-06-11 11:07:21 +0000 | [diff] [blame] | 4431 | T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS, |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4432 | Name, Args, Canon); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4433 | Types.push_back(T); |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4434 | DependentTemplateSpecializationTypes.InsertNode(T, InsertPos); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4435 | return QualType(T, 0); |
Douglas Gregor | dce2b62 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 4438 | TemplateArgument ASTContext::getInjectedTemplateArg(NamedDecl *Param) { |
| 4439 | TemplateArgument Arg; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4440 | if (const auto *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 4441 | QualType ArgType = getTypeDeclType(TTP); |
| 4442 | if (TTP->isParameterPack()) |
| 4443 | ArgType = getPackExpansionType(ArgType, None); |
| 4444 | |
| 4445 | Arg = TemplateArgument(ArgType); |
| 4446 | } else if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4447 | Expr *E = new (*this) DeclRefExpr( |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 4448 | *this, NTTP, /*enclosing*/ false, |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 4449 | NTTP->getType().getNonLValueExprType(*this), |
| 4450 | Expr::getValueKindForType(NTTP->getType()), NTTP->getLocation()); |
| 4451 | |
| 4452 | if (NTTP->isParameterPack()) |
| 4453 | E = new (*this) PackExpansionExpr(DependentTy, E, NTTP->getLocation(), |
| 4454 | None); |
| 4455 | Arg = TemplateArgument(E); |
| 4456 | } else { |
| 4457 | auto *TTP = cast<TemplateTemplateParmDecl>(Param); |
| 4458 | if (TTP->isParameterPack()) |
| 4459 | Arg = TemplateArgument(TemplateName(TTP), Optional<unsigned>()); |
| 4460 | else |
| 4461 | Arg = TemplateArgument(TemplateName(TTP)); |
| 4462 | } |
| 4463 | |
| 4464 | if (Param->isTemplateParameterPack()) |
| 4465 | Arg = TemplateArgument::CreatePackCopy(*this, Arg); |
| 4466 | |
| 4467 | return Arg; |
| 4468 | } |
| 4469 | |
Richard Smith | 43e14d2 | 2016-12-23 02:10:11 +0000 | [diff] [blame] | 4470 | void |
| 4471 | ASTContext::getInjectedTemplateArgs(const TemplateParameterList *Params, |
| 4472 | SmallVectorImpl<TemplateArgument> &Args) { |
| 4473 | Args.reserve(Args.size() + Params->size()); |
| 4474 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 4475 | for (NamedDecl *Param : *Params) |
| 4476 | Args.push_back(getInjectedTemplateArg(Param)); |
Richard Smith | 43e14d2 | 2016-12-23 02:10:11 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 4479 | QualType ASTContext::getPackExpansionType(QualType Pattern, |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4480 | Optional<unsigned> NumExpansions) { |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4481 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 4482 | PackExpansionType::Profile(ID, Pattern, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4483 | |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 4484 | // A deduced type can deduce to a pack, eg |
| 4485 | // auto ...x = some_pack; |
| 4486 | // That declaration isn't (yet) valid, but is created as part of building an |
| 4487 | // init-capture pack: |
| 4488 | // [...x = some_pack] {} |
| 4489 | assert((Pattern->containsUnexpandedParameterPack() || |
| 4490 | Pattern->getContainedDeducedType()) && |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4491 | "Pack expansions must expand one or more parameter packs"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4492 | void *InsertPos = nullptr; |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4493 | PackExpansionType *T |
| 4494 | = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4495 | if (T) |
| 4496 | return QualType(T, 0); |
| 4497 | |
| 4498 | QualType Canon; |
| 4499 | if (!Pattern.isCanonical()) { |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 4500 | Canon = getCanonicalType(Pattern); |
| 4501 | // The canonical type might not contain an unexpanded parameter pack, if it |
| 4502 | // contains an alias template specialization which ignores one of its |
| 4503 | // parameters. |
| 4504 | if (Canon->containsUnexpandedParameterPack()) { |
Richard Smith | 8b4e1e2 | 2014-07-10 01:20:17 +0000 | [diff] [blame] | 4505 | Canon = getPackExpansionType(Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4506 | |
Richard Smith | 68eea50 | 2012-07-16 00:20:35 +0000 | [diff] [blame] | 4507 | // Find the insert position again, in case we inserted an element into |
| 4508 | // PackExpansionTypes and invalidated our insert position. |
| 4509 | PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4510 | } |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4511 | } |
| 4512 | |
Benjamin Kramer | 8adeef9 | 2015-04-02 16:19:54 +0000 | [diff] [blame] | 4513 | T = new (*this, TypeAlignment) |
| 4514 | PackExpansionType(Pattern, Canon, NumExpansions); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4515 | Types.push_back(T); |
| 4516 | PackExpansionTypes.InsertNode(T, InsertPos); |
Richard Smith | 8b4e1e2 | 2014-07-10 01:20:17 +0000 | [diff] [blame] | 4517 | return QualType(T, 0); |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4520 | /// CmpProtocolNames - Comparison predicate for sorting protocols |
| 4521 | /// alphabetically. |
Benjamin Kramer | 0eb262f | 2015-03-14 13:32:49 +0000 | [diff] [blame] | 4522 | static int CmpProtocolNames(ObjCProtocolDecl *const *LHS, |
| 4523 | ObjCProtocolDecl *const *RHS) { |
| 4524 | return DeclarationName::compare((*LHS)->getDeclName(), (*RHS)->getDeclName()); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Craig Topper | 1f26d5b | 2016-01-03 19:43:23 +0000 | [diff] [blame] | 4527 | static bool areSortedAndUniqued(ArrayRef<ObjCProtocolDecl *> Protocols) { |
| 4528 | if (Protocols.empty()) return true; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4529 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 4530 | if (Protocols[0]->getCanonicalDecl() != Protocols[0]) |
| 4531 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4532 | |
Craig Topper | 1f26d5b | 2016-01-03 19:43:23 +0000 | [diff] [blame] | 4533 | for (unsigned i = 1; i != Protocols.size(); ++i) |
Benjamin Kramer | 0eb262f | 2015-03-14 13:32:49 +0000 | [diff] [blame] | 4534 | if (CmpProtocolNames(&Protocols[i - 1], &Protocols[i]) >= 0 || |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 4535 | Protocols[i]->getCanonicalDecl() != Protocols[i]) |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4536 | return false; |
| 4537 | return true; |
| 4538 | } |
| 4539 | |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4540 | static void |
| 4541 | SortAndUniqueProtocols(SmallVectorImpl<ObjCProtocolDecl *> &Protocols) { |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4542 | // Sort protocols, keyed by name. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4543 | llvm::array_pod_sort(Protocols.begin(), Protocols.end(), CmpProtocolNames); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4544 | |
Douglas Gregor | cf9f3ea | 2012-01-02 02:00:30 +0000 | [diff] [blame] | 4545 | // Canonicalize. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4546 | for (ObjCProtocolDecl *&P : Protocols) |
| 4547 | P = P->getCanonicalDecl(); |
| 4548 | |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4549 | // Remove duplicates. |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4550 | auto ProtocolsEnd = std::unique(Protocols.begin(), Protocols.end()); |
| 4551 | Protocols.erase(ProtocolsEnd, Protocols.end()); |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4552 | } |
| 4553 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4554 | QualType ASTContext::getObjCObjectType(QualType BaseType, |
| 4555 | ObjCProtocolDecl * const *Protocols, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4556 | unsigned NumProtocols) const { |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 4557 | return getObjCObjectType(BaseType, {}, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4558 | llvm::makeArrayRef(Protocols, NumProtocols), |
| 4559 | /*isKindOf=*/false); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4560 | } |
| 4561 | |
| 4562 | QualType ASTContext::getObjCObjectType( |
| 4563 | QualType baseType, |
| 4564 | ArrayRef<QualType> typeArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4565 | ArrayRef<ObjCProtocolDecl *> protocols, |
| 4566 | bool isKindOf) const { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4567 | // If the base type is an interface and there aren't any protocols or |
| 4568 | // type arguments to add, then the interface type will do just fine. |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4569 | if (typeArgs.empty() && protocols.empty() && !isKindOf && |
| 4570 | isa<ObjCInterfaceType>(baseType)) |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4571 | return baseType; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4572 | |
| 4573 | // Look in the folding set for an existing type. |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4574 | llvm::FoldingSetNodeID ID; |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4575 | ObjCObjectTypeImpl::Profile(ID, baseType, typeArgs, protocols, isKindOf); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4576 | void *InsertPos = nullptr; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4577 | if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4578 | return QualType(QT, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4579 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4580 | // Determine the type arguments to be used for canonicalization, |
| 4581 | // which may be explicitly specified here or written on the base |
| 4582 | // type. |
| 4583 | ArrayRef<QualType> effectiveTypeArgs = typeArgs; |
| 4584 | if (effectiveTypeArgs.empty()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4585 | if (const auto *baseObject = baseType->getAs<ObjCObjectType>()) |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4586 | effectiveTypeArgs = baseObject->getTypeArgs(); |
| 4587 | } |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4588 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4589 | // Build the canonical type, which has the canonical base type and a |
| 4590 | // sorted-and-uniqued list of protocols and the type arguments |
| 4591 | // canonicalized. |
| 4592 | QualType canonical; |
| 4593 | bool typeArgsAreCanonical = std::all_of(effectiveTypeArgs.begin(), |
| 4594 | effectiveTypeArgs.end(), |
| 4595 | [&](QualType type) { |
| 4596 | return type.isCanonical(); |
| 4597 | }); |
Craig Topper | 1f26d5b | 2016-01-03 19:43:23 +0000 | [diff] [blame] | 4598 | bool protocolsSorted = areSortedAndUniqued(protocols); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4599 | if (!typeArgsAreCanonical || !protocolsSorted || !baseType.isCanonical()) { |
| 4600 | // Determine the canonical type arguments. |
| 4601 | ArrayRef<QualType> canonTypeArgs; |
| 4602 | SmallVector<QualType, 4> canonTypeArgsVec; |
| 4603 | if (!typeArgsAreCanonical) { |
| 4604 | canonTypeArgsVec.reserve(effectiveTypeArgs.size()); |
| 4605 | for (auto typeArg : effectiveTypeArgs) |
| 4606 | canonTypeArgsVec.push_back(getCanonicalType(typeArg)); |
| 4607 | canonTypeArgs = canonTypeArgsVec; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4608 | } else { |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4609 | canonTypeArgs = effectiveTypeArgs; |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4610 | } |
| 4611 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4612 | ArrayRef<ObjCProtocolDecl *> canonProtocols; |
| 4613 | SmallVector<ObjCProtocolDecl*, 8> canonProtocolsVec; |
| 4614 | if (!protocolsSorted) { |
Craig Topper | 6a8c151 | 2015-10-22 01:56:18 +0000 | [diff] [blame] | 4615 | canonProtocolsVec.append(protocols.begin(), protocols.end()); |
| 4616 | SortAndUniqueProtocols(canonProtocolsVec); |
| 4617 | canonProtocols = canonProtocolsVec; |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4618 | } else { |
| 4619 | canonProtocols = protocols; |
| 4620 | } |
| 4621 | |
| 4622 | canonical = getObjCObjectType(getCanonicalType(baseType), canonTypeArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4623 | canonProtocols, isKindOf); |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4624 | |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4625 | // Regenerate InsertPos. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4626 | ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4627 | } |
| 4628 | |
Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 4629 | unsigned size = sizeof(ObjCObjectTypeImpl); |
| 4630 | size += typeArgs.size() * sizeof(QualType); |
| 4631 | size += protocols.size() * sizeof(ObjCProtocolDecl *); |
| 4632 | void *mem = Allocate(size, TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4633 | auto *T = |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 4634 | new (mem) ObjCObjectTypeImpl(canonical, baseType, typeArgs, protocols, |
| 4635 | isKindOf); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4636 | |
| 4637 | Types.push_back(T); |
| 4638 | ObjCObjectTypes.InsertNode(T, InsertPos); |
| 4639 | return QualType(T, 0); |
| 4640 | } |
| 4641 | |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4642 | /// Apply Objective-C protocol qualifiers to the given type. |
| 4643 | /// If this is for the canonical type of a type parameter, we can apply |
| 4644 | /// protocol qualifiers on the ObjCObjectPointerType. |
| 4645 | QualType |
| 4646 | ASTContext::applyObjCProtocolQualifiers(QualType type, |
| 4647 | ArrayRef<ObjCProtocolDecl *> protocols, bool &hasError, |
| 4648 | bool allowOnPointerType) const { |
| 4649 | hasError = false; |
| 4650 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4651 | if (const auto *objT = dyn_cast<ObjCTypeParamType>(type.getTypePtr())) { |
Manman Ren | c5705ba | 2016-09-13 17:41:05 +0000 | [diff] [blame] | 4652 | return getObjCTypeParamType(objT->getDecl(), protocols); |
| 4653 | } |
| 4654 | |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4655 | // Apply protocol qualifiers to ObjCObjectPointerType. |
| 4656 | if (allowOnPointerType) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4657 | if (const auto *objPtr = |
| 4658 | dyn_cast<ObjCObjectPointerType>(type.getTypePtr())) { |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4659 | const ObjCObjectType *objT = objPtr->getObjectType(); |
| 4660 | // Merge protocol lists and construct ObjCObjectType. |
| 4661 | SmallVector<ObjCProtocolDecl*, 8> protocolsVec; |
| 4662 | protocolsVec.append(objT->qual_begin(), |
| 4663 | objT->qual_end()); |
| 4664 | protocolsVec.append(protocols.begin(), protocols.end()); |
| 4665 | ArrayRef<ObjCProtocolDecl *> protocols = protocolsVec; |
| 4666 | type = getObjCObjectType( |
| 4667 | objT->getBaseType(), |
| 4668 | objT->getTypeArgsAsWritten(), |
| 4669 | protocols, |
| 4670 | objT->isKindOfTypeAsWritten()); |
| 4671 | return getObjCObjectPointerType(type); |
| 4672 | } |
| 4673 | } |
| 4674 | |
| 4675 | // Apply protocol qualifiers to ObjCObjectType. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4676 | if (const auto *objT = dyn_cast<ObjCObjectType>(type.getTypePtr())){ |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4677 | // FIXME: Check for protocols to which the class type is already |
| 4678 | // known to conform. |
| 4679 | |
| 4680 | return getObjCObjectType(objT->getBaseType(), |
| 4681 | objT->getTypeArgsAsWritten(), |
| 4682 | protocols, |
| 4683 | objT->isKindOfTypeAsWritten()); |
| 4684 | } |
| 4685 | |
| 4686 | // If the canonical type is ObjCObjectType, ... |
| 4687 | if (type->isObjCObjectType()) { |
| 4688 | // Silently overwrite any existing protocol qualifiers. |
| 4689 | // TODO: determine whether that's the right thing to do. |
| 4690 | |
| 4691 | // FIXME: Check for protocols to which the class type is already |
| 4692 | // known to conform. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 4693 | return getObjCObjectType(type, {}, protocols, false); |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | // id<protocol-list> |
| 4697 | if (type->isObjCIdType()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4698 | const auto *objPtr = type->castAs<ObjCObjectPointerType>(); |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 4699 | type = getObjCObjectType(ObjCBuiltinIdTy, {}, protocols, |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4700 | objPtr->isKindOfType()); |
| 4701 | return getObjCObjectPointerType(type); |
| 4702 | } |
| 4703 | |
| 4704 | // Class<protocol-list> |
| 4705 | if (type->isObjCClassType()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4706 | const auto *objPtr = type->castAs<ObjCObjectPointerType>(); |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 4707 | type = getObjCObjectType(ObjCBuiltinClassTy, {}, protocols, |
Manman Ren | 3569eb5 | 2016-09-13 17:03:12 +0000 | [diff] [blame] | 4708 | objPtr->isKindOfType()); |
| 4709 | return getObjCObjectPointerType(type); |
| 4710 | } |
| 4711 | |
| 4712 | hasError = true; |
| 4713 | return type; |
| 4714 | } |
| 4715 | |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 4716 | QualType |
| 4717 | ASTContext::getObjCTypeParamType(const ObjCTypeParamDecl *Decl, |
Volodymyr Sapsai | b95d4c3 | 2019-10-11 21:21:02 +0000 | [diff] [blame] | 4718 | ArrayRef<ObjCProtocolDecl *> protocols) const { |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 4719 | // Look in the folding set for an existing type. |
| 4720 | llvm::FoldingSetNodeID ID; |
| 4721 | ObjCTypeParamType::Profile(ID, Decl, protocols); |
| 4722 | void *InsertPos = nullptr; |
| 4723 | if (ObjCTypeParamType *TypeParam = |
| 4724 | ObjCTypeParamTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4725 | return QualType(TypeParam, 0); |
| 4726 | |
Volodymyr Sapsai | b95d4c3 | 2019-10-11 21:21:02 +0000 | [diff] [blame] | 4727 | // We canonicalize to the underlying type. |
| 4728 | QualType Canonical = getCanonicalType(Decl->getUnderlyingType()); |
| 4729 | if (!protocols.empty()) { |
| 4730 | // Apply the protocol qualifers. |
| 4731 | bool hasError; |
| 4732 | Canonical = getCanonicalType(applyObjCProtocolQualifiers( |
| 4733 | Canonical, protocols, hasError, true /*allowOnPointerType*/)); |
| 4734 | assert(!hasError && "Error when apply protocol qualifier to bound type"); |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | unsigned size = sizeof(ObjCTypeParamType); |
| 4738 | size += protocols.size() * sizeof(ObjCProtocolDecl *); |
| 4739 | void *mem = Allocate(size, TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4740 | auto *newType = new (mem) ObjCTypeParamType(Decl, Canonical, protocols); |
Manman Ren | e6be26c | 2016-09-13 17:25:08 +0000 | [diff] [blame] | 4741 | |
| 4742 | Types.push_back(newType); |
| 4743 | ObjCTypeParamTypes.InsertNode(newType, InsertPos); |
| 4744 | return QualType(newType, 0); |
| 4745 | } |
| 4746 | |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4747 | /// ObjCObjectAdoptsQTypeProtocols - Checks that protocols in IC's |
| 4748 | /// protocol list adopt all protocols in QT's qualified-id protocol |
| 4749 | /// list. |
| 4750 | bool ASTContext::ObjCObjectAdoptsQTypeProtocols(QualType QT, |
| 4751 | ObjCInterfaceDecl *IC) { |
| 4752 | if (!QT->isObjCQualifiedIdType()) |
| 4753 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4754 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4755 | if (const auto *OPT = QT->getAs<ObjCObjectPointerType>()) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4756 | // If both the right and left sides have qualifiers. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4757 | for (auto *Proto : OPT->quals()) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4758 | if (!IC->ClassImplementsProtocol(Proto, false)) |
| 4759 | return false; |
| 4760 | } |
| 4761 | return true; |
| 4762 | } |
| 4763 | return false; |
| 4764 | } |
| 4765 | |
| 4766 | /// QIdProtocolsAdoptObjCObjectProtocols - Checks that protocols in |
| 4767 | /// QT's qualified-id protocol list adopt all protocols in IDecl's list |
| 4768 | /// of protocols. |
| 4769 | bool ASTContext::QIdProtocolsAdoptObjCObjectProtocols(QualType QT, |
| 4770 | ObjCInterfaceDecl *IDecl) { |
| 4771 | if (!QT->isObjCQualifiedIdType()) |
| 4772 | return false; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4773 | const auto *OPT = QT->getAs<ObjCObjectPointerType>(); |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4774 | if (!OPT) |
| 4775 | return false; |
| 4776 | if (!IDecl->hasDefinition()) |
| 4777 | return false; |
Fariborz Jahanian | 91fb0be | 2013-11-20 19:01:50 +0000 | [diff] [blame] | 4778 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocols; |
| 4779 | CollectInheritedProtocols(IDecl, InheritedProtocols); |
| 4780 | if (InheritedProtocols.empty()) |
| 4781 | return false; |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4782 | // 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] | 4783 | // of IDecl's, then bridge casting is ok. |
| 4784 | bool Conforms = false; |
| 4785 | for (auto *Proto : OPT->quals()) { |
| 4786 | Conforms = false; |
| 4787 | for (auto *PI : InheritedProtocols) { |
| 4788 | if (ProtocolCompatibleWithProtocol(Proto, PI)) { |
| 4789 | Conforms = true; |
| 4790 | break; |
| 4791 | } |
| 4792 | } |
| 4793 | if (!Conforms) |
| 4794 | break; |
| 4795 | } |
| 4796 | if (Conforms) |
| 4797 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4798 | |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4799 | for (auto *PI : InheritedProtocols) { |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4800 | // If both the right and left sides have qualifiers. |
| 4801 | bool Adopts = false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4802 | for (auto *Proto : OPT->quals()) { |
Fariborz Jahanian | 48a01cb | 2014-04-04 23:53:45 +0000 | [diff] [blame] | 4803 | // return 'true' if 'PI' is in the inheritance hierarchy of Proto |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 4804 | if ((Adopts = ProtocolCompatibleWithProtocol(PI, Proto))) |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4805 | break; |
| 4806 | } |
| 4807 | if (!Adopts) |
Fariborz Jahanian | 91fb0be | 2013-11-20 19:01:50 +0000 | [diff] [blame] | 4808 | return false; |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 4809 | } |
| 4810 | return true; |
| 4811 | } |
| 4812 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4813 | /// getObjCObjectPointerType - Return a ObjCObjectPointerType type for |
| 4814 | /// the given object type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4815 | QualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4816 | llvm::FoldingSetNodeID ID; |
| 4817 | ObjCObjectPointerType::Profile(ID, ObjectT); |
| 4818 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4819 | void *InsertPos = nullptr; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4820 | if (ObjCObjectPointerType *QT = |
| 4821 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4822 | return QualType(QT, 0); |
| 4823 | |
| 4824 | // Find the canonical object type. |
| 4825 | QualType Canonical; |
| 4826 | if (!ObjectT.isCanonical()) { |
| 4827 | Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT)); |
| 4828 | |
| 4829 | // Regenerate InsertPos. |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 4830 | ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4831 | } |
| 4832 | |
Douglas Gregor | f85bee6 | 2010-02-08 22:59:26 +0000 | [diff] [blame] | 4833 | // No match. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4834 | void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4835 | auto *QType = |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4836 | new (Mem) ObjCObjectPointerType(Canonical, ObjectT); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4837 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4838 | Types.push_back(QType); |
| 4839 | ObjCObjectPointerTypes.InsertNode(QType, InsertPos); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4840 | return QualType(QType, 0); |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4841 | } |
Chris Lattner | e0ea37a | 2008-04-07 04:56:42 +0000 | [diff] [blame] | 4842 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4843 | /// getObjCInterfaceType - Return the unique reference to the type for the |
| 4844 | /// specified ObjC interface decl. The list of protocols is optional. |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 4845 | QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl, |
| 4846 | ObjCInterfaceDecl *PrevDecl) const { |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4847 | if (Decl->TypeForDecl) |
| 4848 | return QualType(Decl->TypeForDecl, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4849 | |
Douglas Gregor | ab1ec82e | 2011-12-16 03:12:41 +0000 | [diff] [blame] | 4850 | if (PrevDecl) { |
| 4851 | assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); |
| 4852 | Decl->TypeForDecl = PrevDecl->TypeForDecl; |
| 4853 | return QualType(PrevDecl->TypeForDecl, 0); |
| 4854 | } |
| 4855 | |
Douglas Gregor | 7671e53 | 2011-12-16 16:34:57 +0000 | [diff] [blame] | 4856 | // Prefer the definition, if there is one. |
| 4857 | if (const ObjCInterfaceDecl *Def = Decl->getDefinition()) |
| 4858 | Decl = Def; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4859 | |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4860 | void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4861 | auto *T = new (Mem) ObjCInterfaceType(Decl); |
Douglas Gregor | 1c28331 | 2010-08-11 12:19:30 +0000 | [diff] [blame] | 4862 | Decl->TypeForDecl = T; |
| 4863 | Types.push_back(T); |
| 4864 | return QualType(T, 0); |
Fariborz Jahanian | 70e8f10 | 2007-10-11 00:55:41 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4867 | /// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique |
| 4868 | /// TypeOfExprType AST's (since expression's are never shared). For example, |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4869 | /// multiple declarations that refer to "typeof(x)" all contain different |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4870 | /// DeclRefExpr's. This doesn't effect the type checker, since it operates |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4871 | /// on canonical type's (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4872 | QualType ASTContext::getTypeOfExprType(Expr *tofExpr) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4873 | TypeOfExprType *toe; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4874 | if (tofExpr->isTypeDependent()) { |
| 4875 | llvm::FoldingSetNodeID ID; |
| 4876 | DependentTypeOfExprType::Profile(ID, *this, tofExpr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4877 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4878 | void *InsertPos = nullptr; |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4879 | DependentTypeOfExprType *Canon |
| 4880 | = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4881 | if (Canon) { |
| 4882 | // We already have a "canonical" version of an identical, dependent |
| 4883 | // typeof(expr) type. Use that as our canonical type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4884 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4885 | QualType((TypeOfExprType*)Canon, 0)); |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 4886 | } else { |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4887 | // Build a new, canonical typeof(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4888 | Canon |
| 4889 | = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr); |
Douglas Gregor | a5dd9f8 | 2009-07-30 23:18:24 +0000 | [diff] [blame] | 4890 | DependentTypeOfExprTypes.InsertNode(Canon, InsertPos); |
| 4891 | toe = Canon; |
| 4892 | } |
| 4893 | } else { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4894 | QualType Canonical = getCanonicalType(tofExpr->getType()); |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4895 | toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4896 | } |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4897 | Types.push_back(toe); |
| 4898 | return QualType(toe, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4899 | } |
| 4900 | |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4901 | /// getTypeOfType - Unlike many "get<Type>" functions, we don't unique |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4902 | /// TypeOfType nodes. The only motivation to unique these nodes would be |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4903 | /// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4904 | /// an issue. This doesn't affect the type checker, since it operates |
| 4905 | /// on canonical types (which are always unique). |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4906 | QualType ASTContext::getTypeOfType(QualType tofType) const { |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 4907 | QualType Canonical = getCanonicalType(tofType); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4908 | auto *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical); |
Steve Naroff | a773cd5 | 2007-08-01 18:02:17 +0000 | [diff] [blame] | 4909 | Types.push_back(tot); |
| 4910 | return QualType(tot, 0); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4911 | } |
| 4912 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4913 | /// Unlike many "get<Type>" functions, we don't unique DecltypeType |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4914 | /// nodes. This would never be helpful, since each such type has its own |
| 4915 | /// expression, and would not give a significant memory saving, since there |
| 4916 | /// is an Expr tree under each such type. |
Douglas Gregor | 81495f3 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 4917 | QualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const { |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4918 | DecltypeType *dt; |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4919 | |
| 4920 | // C++11 [temp.type]p2: |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4921 | // If an expression e involves a template parameter, decltype(e) denotes a |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4922 | // unique dependent type. Two such decltype-specifiers refer to the same |
| 4923 | // type only if their expressions are equivalent (14.5.6.1). |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 4924 | if (e->isInstantiationDependent()) { |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4925 | llvm::FoldingSetNodeID ID; |
| 4926 | DependentDecltypeType::Profile(ID, *this, e); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4927 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4928 | void *InsertPos = nullptr; |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4929 | DependentDecltypeType *Canon |
| 4930 | = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos); |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4931 | if (!Canon) { |
Yaron Keren | 633e14a | 2016-11-29 10:08:20 +0000 | [diff] [blame] | 4932 | // Build a new, canonical decltype(expr) type. |
John McCall | 90d1c2d | 2009-09-24 23:30:46 +0000 | [diff] [blame] | 4933 | Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4934 | DependentDecltypeTypes.InsertNode(Canon, InsertPos); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4935 | } |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4936 | dt = new (*this, TypeAlignment) |
| 4937 | DecltypeType(e, UnderlyingType, QualType((DecltypeType *)Canon, 0)); |
Douglas Gregor | a21f6c3 | 2009-07-30 23:36:40 +0000 | [diff] [blame] | 4938 | } else { |
Richard Smith | 8eb1d32 | 2014-06-05 20:13:13 +0000 | [diff] [blame] | 4939 | dt = new (*this, TypeAlignment) |
| 4940 | DecltypeType(e, UnderlyingType, getCanonicalType(UnderlyingType)); |
Douglas Gregor | abd6813 | 2009-07-08 00:03:05 +0000 | [diff] [blame] | 4941 | } |
Anders Carlsson | 81df7b8 | 2009-06-24 19:06:50 +0000 | [diff] [blame] | 4942 | Types.push_back(dt); |
| 4943 | return QualType(dt, 0); |
| 4944 | } |
| 4945 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4946 | /// getUnaryTransformationType - We don't unique these, since the memory |
| 4947 | /// savings are minimal and these are rare. |
| 4948 | QualType ASTContext::getUnaryTransformType(QualType BaseType, |
| 4949 | QualType UnderlyingType, |
| 4950 | UnaryTransformType::UTTKind Kind) |
| 4951 | const { |
Vassil Vassilev | bab6f96 | 2016-03-30 22:18:29 +0000 | [diff] [blame] | 4952 | UnaryTransformType *ut = nullptr; |
| 4953 | |
| 4954 | if (BaseType->isDependentType()) { |
| 4955 | // Look in the folding set for an existing type. |
| 4956 | llvm::FoldingSetNodeID ID; |
| 4957 | DependentUnaryTransformType::Profile(ID, getCanonicalType(BaseType), Kind); |
| 4958 | |
| 4959 | void *InsertPos = nullptr; |
| 4960 | DependentUnaryTransformType *Canon |
| 4961 | = DependentUnaryTransformTypes.FindNodeOrInsertPos(ID, InsertPos); |
| 4962 | |
| 4963 | if (!Canon) { |
| 4964 | // Build a new, canonical __underlying_type(type) type. |
| 4965 | Canon = new (*this, TypeAlignment) |
| 4966 | DependentUnaryTransformType(*this, getCanonicalType(BaseType), |
| 4967 | Kind); |
| 4968 | DependentUnaryTransformTypes.InsertNode(Canon, InsertPos); |
| 4969 | } |
| 4970 | ut = new (*this, TypeAlignment) UnaryTransformType (BaseType, |
| 4971 | QualType(), Kind, |
| 4972 | QualType(Canon, 0)); |
| 4973 | } else { |
| 4974 | QualType CanonType = getCanonicalType(UnderlyingType); |
| 4975 | ut = new (*this, TypeAlignment) UnaryTransformType (BaseType, |
| 4976 | UnderlyingType, Kind, |
| 4977 | CanonType); |
| 4978 | } |
| 4979 | Types.push_back(ut); |
| 4980 | return QualType(ut, 0); |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4983 | /// getAutoType - Return the uniqued reference to the 'auto' type which has been |
| 4984 | /// deduced to the given type, or to the canonical undeduced 'auto' type, or the |
| 4985 | /// canonical deduced-but-dependent 'auto' type. |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4986 | QualType ASTContext::getAutoType(QualType DeducedType, AutoTypeKeyword Keyword, |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 4987 | bool IsDependent, bool IsPack) const { |
| 4988 | assert((!IsPack || IsDependent) && "only use IsPack for a dependent pack"); |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4989 | if (DeducedType.isNull() && Keyword == AutoTypeKeyword::Auto && !IsDependent) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4990 | return getAutoDeductType(); |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4991 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4992 | // Look in the folding set for an existing type. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4993 | void *InsertPos = nullptr; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4994 | llvm::FoldingSetNodeID ID; |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 4995 | AutoType::Profile(ID, DeducedType, Keyword, IsDependent, IsPack); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4996 | if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 4997 | return QualType(AT, 0); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4998 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 4999 | auto *AT = new (*this, TypeAlignment) |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 5000 | AutoType(DeducedType, Keyword, IsDependent, IsPack); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 5001 | Types.push_back(AT); |
| 5002 | if (InsertPos) |
| 5003 | AutoTypes.InsertNode(AT, InsertPos); |
| 5004 | return QualType(AT, 0); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5005 | } |
| 5006 | |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 5007 | /// Return the uniqued reference to the deduced template specialization type |
| 5008 | /// which has been deduced to the given type, or to the canonical undeduced |
| 5009 | /// such type, or the canonical deduced-but-dependent such type. |
| 5010 | QualType ASTContext::getDeducedTemplateSpecializationType( |
| 5011 | TemplateName Template, QualType DeducedType, bool IsDependent) const { |
| 5012 | // Look in the folding set for an existing type. |
| 5013 | void *InsertPos = nullptr; |
| 5014 | llvm::FoldingSetNodeID ID; |
| 5015 | DeducedTemplateSpecializationType::Profile(ID, Template, DeducedType, |
| 5016 | IsDependent); |
| 5017 | if (DeducedTemplateSpecializationType *DTST = |
| 5018 | DeducedTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 5019 | return QualType(DTST, 0); |
| 5020 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5021 | auto *DTST = new (*this, TypeAlignment) |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 5022 | DeducedTemplateSpecializationType(Template, DeducedType, IsDependent); |
| 5023 | Types.push_back(DTST); |
| 5024 | if (InsertPos) |
| 5025 | DeducedTemplateSpecializationTypes.InsertNode(DTST, InsertPos); |
| 5026 | return QualType(DTST, 0); |
| 5027 | } |
| 5028 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5029 | /// getAtomicType - Return the uniqued reference to the atomic type for |
| 5030 | /// the given value type. |
| 5031 | QualType ASTContext::getAtomicType(QualType T) const { |
| 5032 | // Unique pointers, to guarantee there is only one pointer of a particular |
| 5033 | // structure. |
| 5034 | llvm::FoldingSetNodeID ID; |
| 5035 | AtomicType::Profile(ID, T); |
| 5036 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5037 | void *InsertPos = nullptr; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5038 | if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos)) |
| 5039 | return QualType(AT, 0); |
| 5040 | |
| 5041 | // If the atomic value type isn't canonical, this won't be a canonical type |
| 5042 | // either, so fill in the canonical type field. |
| 5043 | QualType Canonical; |
| 5044 | if (!T.isCanonical()) { |
| 5045 | Canonical = getAtomicType(getCanonicalType(T)); |
| 5046 | |
| 5047 | // Get the new insert position for the node we care about. |
| 5048 | AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5049 | assert(!NewIP && "Shouldn't be in the map!"); (void)NewIP; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5050 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5051 | auto *New = new (*this, TypeAlignment) AtomicType(T, Canonical); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5052 | Types.push_back(New); |
| 5053 | AtomicTypes.InsertNode(New, InsertPos); |
| 5054 | return QualType(New, 0); |
| 5055 | } |
| 5056 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 5057 | /// getAutoDeductType - Get type pattern for deducing against 'auto'. |
| 5058 | QualType ASTContext::getAutoDeductType() const { |
| 5059 | if (AutoDeductTy.isNull()) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 5060 | AutoDeductTy = QualType( |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 5061 | new (*this, TypeAlignment) AutoType(QualType(), AutoTypeKeyword::Auto, |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 5062 | /*dependent*/false, /*pack*/false), |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 5063 | 0); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 5064 | return AutoDeductTy; |
| 5065 | } |
| 5066 | |
| 5067 | /// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'. |
| 5068 | QualType ASTContext::getAutoRRefDeductType() const { |
| 5069 | if (AutoRRefDeductTy.isNull()) |
| 5070 | AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType()); |
| 5071 | assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern"); |
| 5072 | return AutoRRefDeductTy; |
| 5073 | } |
| 5074 | |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 5075 | /// getTagDeclType - Return the unique reference to the type for the |
| 5076 | /// specified TagDecl (struct/union/class/enum) decl. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5077 | QualType ASTContext::getTagDeclType(const TagDecl *Decl) const { |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 5078 | assert(Decl); |
Mike Stump | b93185d | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 5079 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 5080 | // away const? mutable? |
| 5081 | return getTypeDeclType(const_cast<TagDecl*>(Decl)); |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 5082 | } |
| 5083 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5084 | /// getSizeType - Return the unique type for "size_t" (C99 7.17), the result |
| 5085 | /// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and |
| 5086 | /// needs to agree with the definition in <stddef.h>. |
Anders Carlsson | 22f443f | 2009-12-12 00:26:23 +0000 | [diff] [blame] | 5087 | CanQualType ASTContext::getSizeType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 5088 | return getFromTargetType(Target->getSizeType()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 5089 | } |
Chris Lattner | fb07246 | 2007-01-23 05:45:31 +0000 | [diff] [blame] | 5090 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5091 | /// Return the unique signed counterpart of the integer type |
Alexander Shaposhnikov | 1e898d9 | 2017-07-14 17:30:14 +0000 | [diff] [blame] | 5092 | /// corresponding to size_t. |
| 5093 | CanQualType ASTContext::getSignedSizeType() const { |
| 5094 | return getFromTargetType(Target->getSignedSizeType()); |
| 5095 | } |
| 5096 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 5097 | /// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5). |
| 5098 | CanQualType ASTContext::getIntMaxType() const { |
| 5099 | return getFromTargetType(Target->getIntMaxType()); |
| 5100 | } |
| 5101 | |
| 5102 | /// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5). |
| 5103 | CanQualType ASTContext::getUIntMaxType() const { |
| 5104 | return getFromTargetType(Target->getUIntMaxType()); |
| 5105 | } |
| 5106 | |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 5107 | /// getSignedWCharType - Return the type of "signed wchar_t". |
| 5108 | /// Used when in C++, as a GCC extension. |
| 5109 | QualType ASTContext::getSignedWCharType() const { |
| 5110 | // FIXME: derive from "Target" ? |
| 5111 | return WCharTy; |
| 5112 | } |
| 5113 | |
| 5114 | /// getUnsignedWCharType - Return the type of "unsigned wchar_t". |
| 5115 | /// Used when in C++, as a GCC extension. |
| 5116 | QualType ASTContext::getUnsignedWCharType() const { |
| 5117 | // FIXME: derive from "Target" ? |
| 5118 | return UnsignedIntTy; |
| 5119 | } |
| 5120 | |
Enea Zaffanella | f11ceb6 | 2013-01-26 17:08:37 +0000 | [diff] [blame] | 5121 | QualType ASTContext::getIntPtrType() const { |
| 5122 | return getFromTargetType(Target->getIntPtrType()); |
| 5123 | } |
| 5124 | |
| 5125 | QualType ASTContext::getUIntPtrType() const { |
| 5126 | return getCorrespondingUnsignedType(getIntPtrType()); |
| 5127 | } |
| 5128 | |
Hans Wennborg | 27541db | 2011-10-27 08:29:09 +0000 | [diff] [blame] | 5129 | /// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17) |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 5130 | /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). |
| 5131 | QualType ASTContext::getPointerDiffType() const { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 5132 | return getFromTargetType(Target->getPtrDiffType(0)); |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5135 | /// Return the unique unsigned counterpart of "ptrdiff_t" |
Alexander Shaposhnikov | 195b25c | 2017-09-28 23:11:31 +0000 | [diff] [blame] | 5136 | /// integer type. The standard (C11 7.21.6.1p7) refers to this type |
| 5137 | /// in the definition of %tu format specifier. |
| 5138 | QualType ASTContext::getUnsignedPointerDiffType() const { |
| 5139 | return getFromTargetType(Target->getUnsignedPtrDiffType(0)); |
| 5140 | } |
| 5141 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5142 | /// Return the unique type for "pid_t" defined in |
Eli Friedman | 4e91899e | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 5143 | /// <sys/types.h>. We need this to compute the correct type for vfork(). |
| 5144 | QualType ASTContext::getProcessIDType() const { |
| 5145 | return getFromTargetType(Target->getProcessIDType()); |
| 5146 | } |
| 5147 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 5148 | //===----------------------------------------------------------------------===// |
| 5149 | // Type Operators |
| 5150 | //===----------------------------------------------------------------------===// |
| 5151 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5152 | CanQualType ASTContext::getCanonicalParamType(QualType T) const { |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 5153 | // Push qualifiers into arrays, and then discard any remaining |
| 5154 | // qualifiers. |
| 5155 | T = getCanonicalType(T); |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 5156 | T = getVariableArrayDecayedType(T); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 5157 | const Type *Ty = T.getTypePtr(); |
John McCall | fc93cf9 | 2009-10-22 22:37:11 +0000 | [diff] [blame] | 5158 | QualType Result; |
| 5159 | if (isa<ArrayType>(Ty)) { |
| 5160 | Result = getArrayDecayedType(QualType(Ty,0)); |
| 5161 | } else if (isa<FunctionType>(Ty)) { |
| 5162 | Result = getPointerType(QualType(Ty, 0)); |
| 5163 | } else { |
| 5164 | Result = QualType(Ty, 0); |
| 5165 | } |
| 5166 | |
| 5167 | return CanQualType::CreateUnsafe(Result); |
| 5168 | } |
| 5169 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5170 | QualType ASTContext::getUnqualifiedArrayType(QualType type, |
| 5171 | Qualifiers &quals) { |
| 5172 | SplitQualType splitType = type.getSplitUnqualifiedType(); |
| 5173 | |
| 5174 | // FIXME: getSplitUnqualifiedType() actually walks all the way to |
| 5175 | // the unqualified desugared type and then drops it on the floor. |
| 5176 | // We then have to strip that sugar back off with |
| 5177 | // getUnqualifiedDesugaredType(), which is silly. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5178 | const auto *AT = |
| 5179 | dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType()); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5180 | |
| 5181 | // 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] | 5182 | if (!AT) { |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5183 | quals = splitType.Quals; |
| 5184 | return QualType(splitType.Ty, 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 5185 | } |
| 5186 | |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5187 | // Otherwise, recurse on the array's element type. |
| 5188 | QualType elementType = AT->getElementType(); |
| 5189 | QualType unqualElementType = getUnqualifiedArrayType(elementType, quals); |
| 5190 | |
| 5191 | // If that didn't change the element type, AT has no qualifiers, so we |
| 5192 | // can just use the results in splitType. |
| 5193 | if (elementType == unqualElementType) { |
| 5194 | assert(quals.empty()); // from the recursive call |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5195 | quals = splitType.Quals; |
| 5196 | return QualType(splitType.Ty, 0); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
| 5199 | // Otherwise, add in the qualifiers from the outermost type, then |
| 5200 | // build the type back up. |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5201 | quals.addConsistentQualifiers(splitType.Quals); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 5202 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5203 | if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5204 | return getConstantArrayType(unqualElementType, CAT->getSize(), |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 5205 | CAT->getSizeExpr(), CAT->getSizeModifier(), 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 5206 | } |
| 5207 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5208 | if (const auto *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5209 | return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0); |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 5210 | } |
| 5211 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5212 | if (const auto *VAT = dyn_cast<VariableArrayType>(AT)) { |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5213 | return getVariableArrayType(unqualElementType, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5214 | VAT->getSizeExpr(), |
Douglas Gregor | 3b05bdb | 2010-05-17 18:45:21 +0000 | [diff] [blame] | 5215 | VAT->getSizeModifier(), |
| 5216 | VAT->getIndexTypeCVRQualifiers(), |
| 5217 | VAT->getBracketsRange()); |
| 5218 | } |
| 5219 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5220 | const auto *DSAT = cast<DependentSizedArrayType>(AT); |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 5221 | return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(), |
Chandler Carruth | 607f38e | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 5222 | DSAT->getSizeModifier(), 0, |
| 5223 | SourceRange()); |
| 5224 | } |
| 5225 | |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5226 | /// Attempt to unwrap two types that may both be array types with the same bound |
| 5227 | /// (or both be array types of unknown bound) for the purpose of comparing the |
| 5228 | /// cv-decomposition of two types per C++ [conv.qual]. |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5229 | bool ASTContext::UnwrapSimilarArrayTypes(QualType &T1, QualType &T2) { |
| 5230 | bool UnwrappedAny = false; |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5231 | while (true) { |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5232 | auto *AT1 = getAsArrayType(T1); |
| 5233 | if (!AT1) return UnwrappedAny; |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5234 | |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5235 | auto *AT2 = getAsArrayType(T2); |
| 5236 | if (!AT2) return UnwrappedAny; |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5237 | |
| 5238 | // If we don't have two array types with the same constant bound nor two |
| 5239 | // incomplete array types, we've unwrapped everything we can. |
| 5240 | if (auto *CAT1 = dyn_cast<ConstantArrayType>(AT1)) { |
| 5241 | auto *CAT2 = dyn_cast<ConstantArrayType>(AT2); |
| 5242 | if (!CAT2 || CAT1->getSize() != CAT2->getSize()) |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5243 | return UnwrappedAny; |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5244 | } else if (!isa<IncompleteArrayType>(AT1) || |
| 5245 | !isa<IncompleteArrayType>(AT2)) { |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5246 | return UnwrappedAny; |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5247 | } |
| 5248 | |
| 5249 | T1 = AT1->getElementType(); |
| 5250 | T2 = AT2->getElementType(); |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5251 | UnwrappedAny = true; |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5252 | } |
| 5253 | } |
| 5254 | |
| 5255 | /// Attempt to unwrap two types that may be similar (C++ [conv.qual]). |
| 5256 | /// |
| 5257 | /// If T1 and T2 are both pointer types of the same kind, or both array types |
| 5258 | /// with the same bound, unwraps layers from T1 and T2 until a pointer type is |
| 5259 | /// unwrapped. Top-level qualifiers on T1 and T2 are ignored. |
| 5260 | /// |
| 5261 | /// This function will typically be called in a loop that successively |
| 5262 | /// "unwraps" pointer and pointer-to-member types to compare them at each |
| 5263 | /// level. |
| 5264 | /// |
| 5265 | /// \return \c true if a pointer type was unwrapped, \c false if we reached a |
| 5266 | /// pair of types that can't be unwrapped further. |
| 5267 | bool ASTContext::UnwrapSimilarTypes(QualType &T1, QualType &T2) { |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5268 | UnwrapSimilarArrayTypes(T1, T2); |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5269 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5270 | const auto *T1PtrType = T1->getAs<PointerType>(); |
| 5271 | const auto *T2PtrType = T2->getAs<PointerType>(); |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 5272 | if (T1PtrType && T2PtrType) { |
| 5273 | T1 = T1PtrType->getPointeeType(); |
| 5274 | T2 = T2PtrType->getPointeeType(); |
| 5275 | return true; |
| 5276 | } |
Richard Smith | 5407d4f | 2018-07-18 20:13:36 +0000 | [diff] [blame] | 5277 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5278 | const auto *T1MPType = T1->getAs<MemberPointerType>(); |
| 5279 | const auto *T2MPType = T2->getAs<MemberPointerType>(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5280 | if (T1MPType && T2MPType && |
| 5281 | hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0), |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 5282 | QualType(T2MPType->getClass(), 0))) { |
| 5283 | T1 = T1MPType->getPointeeType(); |
| 5284 | T2 = T2MPType->getPointeeType(); |
| 5285 | return true; |
| 5286 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5287 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 5288 | if (getLangOpts().ObjC) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5289 | const auto *T1OPType = T1->getAs<ObjCObjectPointerType>(); |
| 5290 | const auto *T2OPType = T2->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 5291 | if (T1OPType && T2OPType) { |
| 5292 | T1 = T1OPType->getPointeeType(); |
| 5293 | T2 = T2OPType->getPointeeType(); |
| 5294 | return true; |
| 5295 | } |
| 5296 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5297 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 5298 | // FIXME: Block pointers, too? |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5299 | |
Douglas Gregor | 1fc3d66 | 2010-06-09 03:53:18 +0000 | [diff] [blame] | 5300 | return false; |
| 5301 | } |
| 5302 | |
Richard Smith | a3405ff | 2018-07-11 00:19:19 +0000 | [diff] [blame] | 5303 | bool ASTContext::hasSimilarType(QualType T1, QualType T2) { |
| 5304 | while (true) { |
| 5305 | Qualifiers Quals; |
| 5306 | T1 = getUnqualifiedArrayType(T1, Quals); |
| 5307 | T2 = getUnqualifiedArrayType(T2, Quals); |
| 5308 | if (hasSameType(T1, T2)) |
| 5309 | return true; |
| 5310 | if (!UnwrapSimilarTypes(T1, T2)) |
| 5311 | return false; |
| 5312 | } |
| 5313 | } |
| 5314 | |
| 5315 | bool ASTContext::hasCvrSimilarType(QualType T1, QualType T2) { |
| 5316 | while (true) { |
| 5317 | Qualifiers Quals1, Quals2; |
| 5318 | T1 = getUnqualifiedArrayType(T1, Quals1); |
| 5319 | T2 = getUnqualifiedArrayType(T2, Quals2); |
| 5320 | |
| 5321 | Quals1.removeCVRQualifiers(); |
| 5322 | Quals2.removeCVRQualifiers(); |
| 5323 | if (Quals1 != Quals2) |
| 5324 | return false; |
| 5325 | |
| 5326 | if (hasSameType(T1, T2)) |
| 5327 | return true; |
| 5328 | |
| 5329 | if (!UnwrapSimilarTypes(T1, T2)) |
| 5330 | return false; |
| 5331 | } |
| 5332 | } |
| 5333 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5334 | DeclarationNameInfo |
| 5335 | ASTContext::getNameForTemplate(TemplateName Name, |
| 5336 | SourceLocation NameLoc) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5337 | switch (Name.getKind()) { |
| 5338 | case TemplateName::QualifiedTemplate: |
| 5339 | case TemplateName::Template: |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5340 | // DNInfo work in progress: CHECKME: what about DNLoc? |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5341 | return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(), |
| 5342 | NameLoc); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5343 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5344 | case TemplateName::OverloadedTemplate: { |
| 5345 | OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate(); |
| 5346 | // DNInfo work in progress: CHECKME: what about DNLoc? |
| 5347 | return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc); |
| 5348 | } |
| 5349 | |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 5350 | case TemplateName::AssumedTemplate: { |
| 5351 | AssumedTemplateStorage *Storage = Name.getAsAssumedTemplateName(); |
| 5352 | return DeclarationNameInfo(Storage->getDeclName(), NameLoc); |
| 5353 | } |
| 5354 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5355 | case TemplateName::DependentTemplate: { |
| 5356 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5357 | DeclarationName DName; |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 5358 | if (DTN->isIdentifier()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5359 | DName = DeclarationNames.getIdentifier(DTN->getIdentifier()); |
| 5360 | return DeclarationNameInfo(DName, NameLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 5361 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 5362 | DName = DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 5363 | // DNInfo work in progress: FIXME: source locations? |
| 5364 | DeclarationNameLoc DNLoc; |
| 5365 | DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding(); |
| 5366 | DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding(); |
| 5367 | return DeclarationNameInfo(DName, NameLoc, DNLoc); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 5368 | } |
| 5369 | } |
| 5370 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5371 | case TemplateName::SubstTemplateTemplateParm: { |
| 5372 | SubstTemplateTemplateParmStorage *subst |
| 5373 | = Name.getAsSubstTemplateTemplateParm(); |
| 5374 | return DeclarationNameInfo(subst->getParameter()->getDeclName(), |
| 5375 | NameLoc); |
| 5376 | } |
| 5377 | |
| 5378 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 5379 | SubstTemplateTemplateParmPackStorage *subst |
| 5380 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 5381 | return DeclarationNameInfo(subst->getParameterPack()->getDeclName(), |
| 5382 | NameLoc); |
| 5383 | } |
| 5384 | } |
| 5385 | |
| 5386 | llvm_unreachable("bad template name kind!"); |
John McCall | 847e2a1 | 2009-11-24 18:42:40 +0000 | [diff] [blame] | 5387 | } |
| 5388 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5389 | TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const { |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5390 | switch (Name.getKind()) { |
| 5391 | case TemplateName::QualifiedTemplate: |
| 5392 | case TemplateName::Template: { |
| 5393 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5394 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Template)) |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 5395 | Template = getCanonicalTemplateTemplateParmDecl(TTP); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5396 | |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 5397 | // The canonical template name is the canonical template declaration. |
Argyrios Kyrtzidis | 6b7e376 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 5398 | return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl())); |
Douglas Gregor | 7dbfb46 | 2010-06-16 21:09:37 +0000 | [diff] [blame] | 5399 | } |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 5400 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5401 | case TemplateName::OverloadedTemplate: |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 5402 | case TemplateName::AssumedTemplate: |
| 5403 | llvm_unreachable("cannot canonicalize unresolved template"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5404 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 5405 | case TemplateName::DependentTemplate: { |
| 5406 | DependentTemplateName *DTN = Name.getAsDependentTemplateName(); |
| 5407 | assert(DTN && "Non-dependent template names must refer to template decls."); |
| 5408 | return DTN->CanonicalTemplateName; |
| 5409 | } |
| 5410 | |
| 5411 | case TemplateName::SubstTemplateTemplateParm: { |
| 5412 | SubstTemplateTemplateParmStorage *subst |
| 5413 | = Name.getAsSubstTemplateTemplateParm(); |
| 5414 | return getCanonicalTemplateName(subst->getReplacement()); |
| 5415 | } |
| 5416 | |
| 5417 | case TemplateName::SubstTemplateTemplateParmPack: { |
| 5418 | SubstTemplateTemplateParmPackStorage *subst |
| 5419 | = Name.getAsSubstTemplateTemplateParmPack(); |
| 5420 | TemplateTemplateParmDecl *canonParameter |
| 5421 | = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack()); |
| 5422 | TemplateArgument canonArgPack |
| 5423 | = getCanonicalTemplateArgument(subst->getArgumentPack()); |
| 5424 | return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack); |
| 5425 | } |
| 5426 | } |
| 5427 | |
| 5428 | llvm_unreachable("bad template name!"); |
Douglas Gregor | 6bc5058 | 2009-05-07 06:41:52 +0000 | [diff] [blame] | 5429 | } |
| 5430 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 5431 | bool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) { |
| 5432 | X = getCanonicalTemplateName(X); |
| 5433 | Y = getCanonicalTemplateName(Y); |
| 5434 | return X.getAsVoidPointer() == Y.getAsVoidPointer(); |
| 5435 | } |
| 5436 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5437 | TemplateArgument |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5438 | ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const { |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5439 | switch (Arg.getKind()) { |
| 5440 | case TemplateArgument::Null: |
| 5441 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5442 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5443 | case TemplateArgument::Expression: |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5444 | return Arg; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5445 | |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5446 | case TemplateArgument::Declaration: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5447 | auto *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl()); |
David Blaikie | 952a9b1 | 2014-10-17 18:00:12 +0000 | [diff] [blame] | 5448 | return TemplateArgument(D, Arg.getParamTypeForDecl()); |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5449 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5450 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5451 | case TemplateArgument::NullPtr: |
| 5452 | return TemplateArgument(getCanonicalType(Arg.getNullPtrType()), |
| 5453 | /*isNullPtr*/true); |
| 5454 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5455 | case TemplateArgument::Template: |
| 5456 | return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate())); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5457 | |
| 5458 | case TemplateArgument::TemplateExpansion: |
| 5459 | return TemplateArgument(getCanonicalTemplateName( |
| 5460 | Arg.getAsTemplateOrTemplatePattern()), |
Douglas Gregor | e1d60df | 2011-01-14 23:41:42 +0000 | [diff] [blame] | 5461 | Arg.getNumTemplateExpansions()); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5462 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5463 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 5464 | return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5465 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5466 | case TemplateArgument::Type: |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 5467 | return TemplateArgument(getCanonicalType(Arg.getAsType())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5468 | |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5469 | case TemplateArgument::Pack: { |
Douglas Gregor | 0192c23 | 2010-12-20 16:52:59 +0000 | [diff] [blame] | 5470 | if (Arg.pack_size() == 0) |
| 5471 | return Arg; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5472 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5473 | auto *CanonArgs = new (*this) TemplateArgument[Arg.pack_size()]; |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5474 | unsigned Idx = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5475 | for (TemplateArgument::pack_iterator A = Arg.pack_begin(), |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5476 | AEnd = Arg.pack_end(); |
| 5477 | A != AEnd; (void)++A, ++Idx) |
| 5478 | CanonArgs[Idx] = getCanonicalTemplateArgument(*A); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5479 | |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 5480 | return TemplateArgument(llvm::makeArrayRef(CanonArgs, Arg.pack_size())); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5481 | } |
| 5482 | } |
| 5483 | |
| 5484 | // Silence GCC warning |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5485 | llvm_unreachable("Unhandled template argument kind"); |
Douglas Gregor | a8e02e7 | 2009-07-28 23:00:59 +0000 | [diff] [blame] | 5486 | } |
| 5487 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5488 | NestedNameSpecifier * |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5489 | ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5490 | if (!NNS) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5491 | return nullptr; |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5492 | |
| 5493 | switch (NNS->getKind()) { |
| 5494 | case NestedNameSpecifier::Identifier: |
| 5495 | // Canonicalize the prefix but keep the identifier the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5496 | return NestedNameSpecifier::Create(*this, |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5497 | getCanonicalNestedNameSpecifier(NNS->getPrefix()), |
| 5498 | NNS->getAsIdentifier()); |
| 5499 | |
| 5500 | case NestedNameSpecifier::Namespace: |
| 5501 | // A namespace is canonical; build a nested-name-specifier with |
| 5502 | // this namespace and no prefix. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5503 | return NestedNameSpecifier::Create(*this, nullptr, |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 5504 | NNS->getAsNamespace()->getOriginalNamespace()); |
| 5505 | |
| 5506 | case NestedNameSpecifier::NamespaceAlias: |
| 5507 | // A namespace is canonical; build a nested-name-specifier with |
| 5508 | // this namespace and no prefix. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5509 | return NestedNameSpecifier::Create(*this, nullptr, |
Douglas Gregor | 7b26ff9 | 2011-02-24 02:36:08 +0000 | [diff] [blame] | 5510 | NNS->getAsNamespaceAlias()->getNamespace() |
| 5511 | ->getOriginalNamespace()); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5512 | |
| 5513 | case NestedNameSpecifier::TypeSpec: |
| 5514 | case NestedNameSpecifier::TypeSpecWithTemplate: { |
| 5515 | QualType T = getCanonicalType(QualType(NNS->getAsType(), 0)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5516 | |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 5517 | // If we have some kind of dependent-named type (e.g., "typename T::type"), |
| 5518 | // break it apart into its prefix and identifier, then reconsititute those |
| 5519 | // as the canonical nested-name-specifier. This is required to canonicalize |
| 5520 | // a dependent nested-name-specifier involving typedefs of dependent-name |
| 5521 | // types, e.g., |
| 5522 | // typedef typename T::type T1; |
| 5523 | // typedef typename T1::type T2; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5524 | if (const auto *DNT = T->getAs<DependentNameType>()) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5525 | return NestedNameSpecifier::Create(*this, DNT->getQualifier(), |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 5526 | const_cast<IdentifierInfo *>(DNT->getIdentifier())); |
Douglas Gregor | 3ade570 | 2010-11-04 00:09:33 +0000 | [diff] [blame] | 5527 | |
Eli Friedman | 5358a0a | 2012-03-03 04:09:56 +0000 | [diff] [blame] | 5528 | // Otherwise, just canonicalize the type, and force it to be a TypeSpec. |
| 5529 | // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the |
| 5530 | // first place? |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5531 | return NestedNameSpecifier::Create(*this, nullptr, false, |
| 5532 | const_cast<Type *>(T.getTypePtr())); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5533 | } |
| 5534 | |
| 5535 | case NestedNameSpecifier::Global: |
Nikola Smiljanic | 6786024 | 2014-09-26 00:28:20 +0000 | [diff] [blame] | 5536 | case NestedNameSpecifier::Super: |
| 5537 | // The global specifier and __super specifer are canonical and unique. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5538 | return NNS; |
| 5539 | } |
| 5540 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 5541 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 5542 | } |
| 5543 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5544 | const ArrayType *ASTContext::getAsArrayType(QualType T) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5545 | // Handle the non-qualified case efficiently. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5546 | if (!T.hasLocalQualifiers()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5547 | // Handle the common positive case fast. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5548 | if (const auto *AT = dyn_cast<ArrayType>(T)) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5549 | return AT; |
| 5550 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5551 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5552 | // Handle the common negative case fast. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5553 | if (!isa<ArrayType>(T.getCanonicalType())) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5554 | return nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5555 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5556 | // Apply any qualifiers from the array type to the element type. This |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5557 | // implements C99 6.7.3p8: "If the specification of an array type includes |
| 5558 | // 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] | 5559 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5560 | // If we get here, we either have type qualifiers on the type, or we have |
| 5561 | // 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] | 5562 | // we must propagate them down into the element type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5563 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5564 | SplitQualType split = T.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5565 | Qualifiers qs = split.Quals; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5566 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5567 | // If we have a simple case, just return now. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5568 | const auto *ATy = dyn_cast<ArrayType>(split.Ty); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5569 | if (!ATy || qs.empty()) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5570 | return ATy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5571 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5572 | // Otherwise, we have an array and we have qualifiers on it. Push the |
| 5573 | // qualifiers into the array element type and return a new array type. |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5574 | QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5575 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5576 | if (const auto *CAT = dyn_cast<ConstantArrayType>(ATy)) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5577 | return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(), |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 5578 | CAT->getSizeExpr(), |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5579 | CAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5580 | CAT->getIndexTypeCVRQualifiers())); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5581 | if (const auto *IAT = dyn_cast<IncompleteArrayType>(ATy)) |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5582 | return cast<ArrayType>(getIncompleteArrayType(NewEltTy, |
| 5583 | IAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5584 | IAT->getIndexTypeCVRQualifiers())); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5585 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5586 | if (const auto *DSAT = dyn_cast<DependentSizedArrayType>(ATy)) |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5587 | return cast<ArrayType>( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5588 | getDependentSizedArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5589 | DSAT->getSizeExpr(), |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5590 | DSAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5591 | DSAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5592 | DSAT->getBracketsRange())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5593 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5594 | const auto *VAT = cast<VariableArrayType>(ATy); |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5595 | return cast<ArrayType>(getVariableArrayType(NewEltTy, |
John McCall | c3007a2 | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 5596 | VAT->getSizeExpr(), |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5597 | VAT->getSizeModifier(), |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5598 | VAT->getIndexTypeCVRQualifiers(), |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5599 | VAT->getBracketsRange())); |
Chris Lattner | ed0d079 | 2008-04-06 22:41:35 +0000 | [diff] [blame] | 5600 | } |
| 5601 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 5602 | QualType ASTContext::getAdjustedParameterType(QualType T) const { |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 5603 | if (T->isArrayType() || T->isFunctionType()) |
| 5604 | return getDecayedType(T); |
| 5605 | return T; |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 5606 | } |
| 5607 | |
Abramo Bagnara | e1decdf | 2012-05-17 12:44:05 +0000 | [diff] [blame] | 5608 | QualType ASTContext::getSignatureParameterType(QualType T) const { |
Douglas Gregor | 8428064 | 2011-07-12 04:42:08 +0000 | [diff] [blame] | 5609 | T = getVariableArrayDecayedType(T); |
| 5610 | T = getAdjustedParameterType(T); |
| 5611 | return T.getUnqualifiedType(); |
| 5612 | } |
| 5613 | |
David Majnemer | d09a51c | 2015-03-03 01:50:05 +0000 | [diff] [blame] | 5614 | QualType ASTContext::getExceptionObjectType(QualType T) const { |
| 5615 | // C++ [except.throw]p3: |
| 5616 | // A throw-expression initializes a temporary object, called the exception |
| 5617 | // object, the type of which is determined by removing any top-level |
| 5618 | // cv-qualifiers from the static type of the operand of throw and adjusting |
| 5619 | // the type from "array of T" or "function returning T" to "pointer to T" |
| 5620 | // or "pointer to function returning T", [...] |
| 5621 | T = getVariableArrayDecayedType(T); |
| 5622 | if (T->isArrayType() || T->isFunctionType()) |
| 5623 | T = getDecayedType(T); |
| 5624 | return T.getUnqualifiedType(); |
| 5625 | } |
| 5626 | |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 5627 | /// getArrayDecayedType - Return the properly qualified result of decaying the |
| 5628 | /// specified array type to a pointer. This operation is non-trivial when |
| 5629 | /// handling typedefs etc. The canonical type of "T" must be an array type, |
| 5630 | /// this returns a pointer to a properly qualified element of the array. |
| 5631 | /// |
| 5632 | /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5633 | QualType ASTContext::getArrayDecayedType(QualType Ty) const { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5634 | // Get the element type with 'getAsArrayType' so that we don't lose any |
| 5635 | // typedefs in the element type of the array. This also handles propagation |
| 5636 | // of type qualifiers from the array type into the element type if present |
| 5637 | // (C99 6.7.3p8). |
| 5638 | const ArrayType *PrettyArrayType = getAsArrayType(Ty); |
| 5639 | assert(PrettyArrayType && "Not an array type!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5640 | |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5641 | QualType PtrTy = getPointerType(PrettyArrayType->getElementType()); |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 5642 | |
| 5643 | // int x[restrict 4] -> int *restrict |
Jordan Rose | 303e2f1 | 2016-11-10 23:28:17 +0000 | [diff] [blame] | 5644 | QualType Result = getQualifiedType(PtrTy, |
| 5645 | PrettyArrayType->getIndexTypeQualifiers()); |
| 5646 | |
| 5647 | // int x[_Nullable] -> int * _Nullable |
| 5648 | if (auto Nullability = Ty->getNullability(*this)) { |
| 5649 | Result = const_cast<ASTContext *>(this)->getAttributedType( |
| 5650 | AttributedType::getNullabilityAttrKind(*Nullability), Result, Result); |
| 5651 | } |
| 5652 | return Result; |
Chris Lattner | a21ad80 | 2008-04-02 05:18:44 +0000 | [diff] [blame] | 5653 | } |
| 5654 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5655 | QualType ASTContext::getBaseElementType(const ArrayType *array) const { |
| 5656 | return getBaseElementType(array->getElementType()); |
Douglas Gregor | 79f83ed | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 5657 | } |
| 5658 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5659 | QualType ASTContext::getBaseElementType(QualType type) const { |
| 5660 | Qualifiers qs; |
| 5661 | while (true) { |
| 5662 | SplitQualType split = type.getSplitDesugaredType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5663 | const ArrayType *array = split.Ty->getAsArrayTypeUnsafe(); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5664 | if (!array) break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5665 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5666 | type = array->getElementType(); |
John McCall | 18ce25e | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5667 | qs.addConsistentQualifiers(split.Quals); |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5668 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5669 | |
John McCall | 33ddac0 | 2011-01-19 10:06:00 +0000 | [diff] [blame] | 5670 | return getQualifiedType(type, qs); |
Anders Carlsson | e0808df | 2008-12-21 03:44:36 +0000 | [diff] [blame] | 5671 | } |
| 5672 | |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 5673 | /// getConstantArrayElementCount - Returns number of constant array elements. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5674 | uint64_t |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 5675 | ASTContext::getConstantArrayElementCount(const ConstantArrayType *CA) const { |
| 5676 | uint64_t ElementCount = 1; |
| 5677 | do { |
| 5678 | ElementCount *= CA->getSize().getZExtValue(); |
Richard Smith | 7808c6a | 2012-12-06 03:04:50 +0000 | [diff] [blame] | 5679 | CA = dyn_cast_or_null<ConstantArrayType>( |
| 5680 | CA->getElementType()->getAsArrayTypeUnsafe()); |
Fariborz Jahanian | 6c9e5a2 | 2009-08-21 16:31:06 +0000 | [diff] [blame] | 5681 | } while (CA); |
| 5682 | return ElementCount; |
| 5683 | } |
| 5684 | |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 5685 | /// getFloatingRank - Return a relative rank for floating point types. |
| 5686 | /// 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] | 5687 | static FloatingRank getFloatingRank(QualType T) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5688 | if (const auto *CT = T->getAs<ComplexType>()) |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 5689 | return getFloatingRank(CT->getElementType()); |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5690 | |
Simon Pilgrim | eed4b12 | 2019-10-02 11:48:06 +0000 | [diff] [blame] | 5691 | switch (T->castAs<BuiltinType>()->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5692 | default: llvm_unreachable("getFloatingRank(): not a floating type"); |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 5693 | case BuiltinType::Float16: return Float16Rank; |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 5694 | case BuiltinType::Half: return HalfRank; |
Chris Lattner | c639593 | 2007-06-22 20:56:16 +0000 | [diff] [blame] | 5695 | case BuiltinType::Float: return FloatRank; |
| 5696 | case BuiltinType::Double: return DoubleRank; |
| 5697 | case BuiltinType::LongDouble: return LongDoubleRank; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5698 | case BuiltinType::Float128: return Float128Rank; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5699 | } |
| 5700 | } |
| 5701 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5702 | /// getFloatingTypeOfSizeWithinDomain - Returns a real floating |
| 5703 | /// point or a complex type (based on typeDomain/typeSize). |
Steve Naroff | fc6ffa2 | 2007-08-27 01:41:48 +0000 | [diff] [blame] | 5704 | /// 'typeDomain' is a real floating point or complex type. |
| 5705 | /// 'typeSize' is a real floating point or complex type. |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 5706 | QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size, |
| 5707 | QualType Domain) const { |
| 5708 | FloatingRank EltRank = getFloatingRank(Size); |
| 5709 | if (Domain->isComplexType()) { |
| 5710 | switch (EltRank) { |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 5711 | case Float16Rank: |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 5712 | case HalfRank: llvm_unreachable("Complex half is not supported"); |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 5713 | case FloatRank: return FloatComplexTy; |
| 5714 | case DoubleRank: return DoubleComplexTy; |
| 5715 | case LongDoubleRank: return LongDoubleComplexTy; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5716 | case Float128Rank: return Float128ComplexTy; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 5717 | } |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 5718 | } |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 5719 | |
| 5720 | assert(Domain->isRealFloatingType() && "Unknown domain!"); |
| 5721 | switch (EltRank) { |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 5722 | case Float16Rank: return HalfTy; |
Joey Gouly | dd7f456 | 2013-01-23 11:56:20 +0000 | [diff] [blame] | 5723 | case HalfRank: return HalfTy; |
Chris Lattner | b9dfb03 | 2008-04-06 23:58:54 +0000 | [diff] [blame] | 5724 | case FloatRank: return FloatTy; |
| 5725 | case DoubleRank: return DoubleTy; |
| 5726 | case LongDoubleRank: return LongDoubleTy; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 5727 | case Float128Rank: return Float128Ty; |
Steve Naroff | 9091ef7 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 5728 | } |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 5729 | llvm_unreachable("getFloatingRank(): illegal value for rank"); |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5730 | } |
| 5731 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5732 | /// getFloatingTypeOrder - Compare the rank of the two specified floating |
| 5733 | /// point types, ignoring the domain of the type (i.e. 'double' == |
| 5734 | /// '_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] | 5735 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5736 | int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const { |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5737 | FloatingRank LHSR = getFloatingRank(LHS); |
| 5738 | FloatingRank RHSR = getFloatingRank(RHS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5739 | |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5740 | if (LHSR == RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 5741 | return 0; |
Chris Lattner | b90739d | 2008-04-06 23:38:49 +0000 | [diff] [blame] | 5742 | if (LHSR > RHSR) |
Steve Naroff | 7af82d4 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 5743 | return 1; |
| 5744 | return -1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5745 | } |
| 5746 | |
Erik Pilkington | eac7c3f | 2019-02-16 01:11:47 +0000 | [diff] [blame] | 5747 | int ASTContext::getFloatingTypeSemanticOrder(QualType LHS, QualType RHS) const { |
| 5748 | if (&getFloatTypeSemantics(LHS) == &getFloatTypeSemantics(RHS)) |
| 5749 | return 0; |
| 5750 | return getFloatingTypeOrder(LHS, RHS); |
| 5751 | } |
| 5752 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5753 | /// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This |
| 5754 | /// routine will assert if passed a built-in type that isn't an integer or enum, |
| 5755 | /// or if it is not canonicalized. |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5756 | unsigned ASTContext::getIntegerRank(const Type *T) const { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 5757 | assert(T->isCanonicalUnqualified() && "T should be canonicalized"); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 5758 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5759 | switch (cast<BuiltinType>(T)->getKind()) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5760 | default: llvm_unreachable("getIntegerRank(): not a built-in integer"); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5761 | case BuiltinType::Bool: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5762 | return 1 + (getIntWidth(BoolTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5763 | case BuiltinType::Char_S: |
| 5764 | case BuiltinType::Char_U: |
| 5765 | case BuiltinType::SChar: |
| 5766 | case BuiltinType::UChar: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5767 | return 2 + (getIntWidth(CharTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5768 | case BuiltinType::Short: |
| 5769 | case BuiltinType::UShort: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5770 | return 3 + (getIntWidth(ShortTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5771 | case BuiltinType::Int: |
| 5772 | case BuiltinType::UInt: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5773 | return 4 + (getIntWidth(IntTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5774 | case BuiltinType::Long: |
| 5775 | case BuiltinType::ULong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5776 | return 5 + (getIntWidth(LongTy) << 3); |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5777 | case BuiltinType::LongLong: |
| 5778 | case BuiltinType::ULongLong: |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 5779 | return 6 + (getIntWidth(LongLongTy) << 3); |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 5780 | case BuiltinType::Int128: |
| 5781 | case BuiltinType::UInt128: |
| 5782 | return 7 + (getIntWidth(Int128Ty) << 3); |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5783 | } |
| 5784 | } |
| 5785 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5786 | /// Whether this is a promotable bitfield reference according |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5787 | /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). |
| 5788 | /// |
| 5789 | /// \returns the type this bit-field will promote to, or NULL if no |
| 5790 | /// promotion occurs. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5791 | QualType ASTContext::isPromotableBitField(Expr *E) const { |
Douglas Gregor | e05d3cb | 2010-05-24 20:13:53 +0000 | [diff] [blame] | 5792 | if (E->isTypeDependent() || E->isValueDependent()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5793 | return {}; |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 5794 | |
Richard Smith | aadb254 | 2018-06-28 21:17:55 +0000 | [diff] [blame] | 5795 | // C++ [conv.prom]p5: |
| 5796 | // If the bit-field has an enumerated type, it is treated as any other |
| 5797 | // value of that type for promotion purposes. |
| 5798 | if (getLangOpts().CPlusPlus && E->getType()->isEnumeralType()) |
| 5799 | return {}; |
| 5800 | |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 5801 | // FIXME: We should not do this unless E->refersToBitField() is true. This |
| 5802 | // matters in C where getSourceBitField() will find bit-fields for various |
| 5803 | // cases where the source expression is not a bit-field designator. |
| 5804 | |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5805 | FieldDecl *Field = E->getSourceBitField(); // FIXME: conditional bit-fields? |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5806 | if (!Field) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5807 | return {}; |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5808 | |
| 5809 | QualType FT = Field->getType(); |
| 5810 | |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 5811 | uint64_t BitWidth = Field->getBitWidthValue(*this); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5812 | uint64_t IntSize = getTypeSize(IntTy); |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 5813 | // C++ [conv.prom]p5: |
| 5814 | // A prvalue for an integral bit-field can be converted to a prvalue of type |
| 5815 | // int if int can represent all the values of the bit-field; otherwise, it |
| 5816 | // can be converted to unsigned int if unsigned int can represent all the |
| 5817 | // values of the bit-field. If the bit-field is larger yet, no integral |
| 5818 | // promotion applies to it. |
| 5819 | // C11 6.3.1.1/2: |
| 5820 | // [For a bit-field of type _Bool, int, signed int, or unsigned int:] |
| 5821 | // If an int can represent all values of the original type (as restricted by |
| 5822 | // the width, for a bit-field), the value is converted to an int; otherwise, |
| 5823 | // it is converted to an unsigned int. |
| 5824 | // |
| 5825 | // FIXME: C does not permit promotion of a 'long : 3' bitfield to int. |
| 5826 | // We perform that promotion here to match GCC and C++. |
Richard Smith | aadb254 | 2018-06-28 21:17:55 +0000 | [diff] [blame] | 5827 | // FIXME: C does not permit promotion of an enum bit-field whose rank is |
| 5828 | // greater than that of 'int'. We perform that promotion to match GCC. |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5829 | if (BitWidth < IntSize) |
| 5830 | return IntTy; |
| 5831 | |
| 5832 | if (BitWidth == IntSize) |
| 5833 | return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy; |
| 5834 | |
Richard Smith | aadb254 | 2018-06-28 21:17:55 +0000 | [diff] [blame] | 5835 | // 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] | 5836 | // like the base type. GCC has some weird bugs in this area that we |
| 5837 | // deliberately do not follow (GCC follows a pre-standard resolution to |
| 5838 | // C's DR315 which treats bit-width as being part of the type, and this leaks |
| 5839 | // into their semantics in some cases). |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5840 | return {}; |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5841 | } |
| 5842 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5843 | /// getPromotedIntegerType - Returns the type that Promotable will |
| 5844 | /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable |
| 5845 | /// integer type. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5846 | QualType ASTContext::getPromotedIntegerType(QualType Promotable) const { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5847 | assert(!Promotable.isNull()); |
| 5848 | assert(Promotable->isPromotableIntegerType()); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5849 | if (const auto *ET = Promotable->getAs<EnumType>()) |
John McCall | 5677499 | 2009-12-09 09:09:27 +0000 | [diff] [blame] | 5850 | return ET->getDecl()->getPromotionType(); |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 5851 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5852 | if (const auto *BT = Promotable->getAs<BuiltinType>()) { |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 5853 | // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t |
| 5854 | // (3.9.1) can be converted to a prvalue of the first of the following |
| 5855 | // types that can represent all the values of its underlying type: |
| 5856 | // int, unsigned int, long int, unsigned long int, long long int, or |
| 5857 | // unsigned long long int [...] |
| 5858 | // FIXME: Is there some better way to compute this? |
| 5859 | if (BT->getKind() == BuiltinType::WChar_S || |
| 5860 | BT->getKind() == BuiltinType::WChar_U || |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 5861 | BT->getKind() == BuiltinType::Char8 || |
Eli Friedman | 3f37c1c | 2011-10-26 07:22:48 +0000 | [diff] [blame] | 5862 | BT->getKind() == BuiltinType::Char16 || |
| 5863 | BT->getKind() == BuiltinType::Char32) { |
| 5864 | bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S; |
| 5865 | uint64_t FromSize = getTypeSize(BT); |
| 5866 | QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy, |
| 5867 | LongLongTy, UnsignedLongLongTy }; |
| 5868 | for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) { |
| 5869 | uint64_t ToSize = getTypeSize(PromoteTypes[Idx]); |
| 5870 | if (FromSize < ToSize || |
| 5871 | (FromSize == ToSize && |
| 5872 | FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType())) |
| 5873 | return PromoteTypes[Idx]; |
| 5874 | } |
| 5875 | llvm_unreachable("char type should fit into long long"); |
| 5876 | } |
| 5877 | } |
| 5878 | |
| 5879 | // At this point, we should have a signed or unsigned integer type. |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5880 | if (Promotable->isSignedIntegerType()) |
| 5881 | return IntTy; |
Eli Friedman | 6745c3b | 2012-11-15 01:21:59 +0000 | [diff] [blame] | 5882 | uint64_t PromotableSize = getIntWidth(Promotable); |
| 5883 | uint64_t IntSize = getIntWidth(IntTy); |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 5884 | assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize); |
| 5885 | return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy; |
| 5886 | } |
| 5887 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5888 | /// Recurses in pointer/array types until it finds an objc retainable |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 5889 | /// type and returns its ownership. |
| 5890 | Qualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const { |
| 5891 | while (!T.isNull()) { |
| 5892 | if (T.getObjCLifetime() != Qualifiers::OCL_None) |
| 5893 | return T.getObjCLifetime(); |
| 5894 | if (T->isArrayType()) |
| 5895 | T = getBaseElementType(T); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5896 | else if (const auto *PT = T->getAs<PointerType>()) |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 5897 | T = PT->getPointeeType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5898 | else if (const auto *RT = T->getAs<ReferenceType>()) |
Argyrios Kyrtzidis | 8e25253 | 2011-07-01 23:01:46 +0000 | [diff] [blame] | 5899 | T = RT->getPointeeType(); |
Argyrios Kyrtzidis | 7451d1c | 2011-07-01 22:22:50 +0000 | [diff] [blame] | 5900 | else |
| 5901 | break; |
| 5902 | } |
| 5903 | |
| 5904 | return Qualifiers::OCL_None; |
| 5905 | } |
| 5906 | |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5907 | static const Type *getIntegerTypeForEnum(const EnumType *ET) { |
| 5908 | // Incomplete enum types are not treated as integer types. |
| 5909 | // FIXME: In C++, enum types are never integer types. |
| 5910 | if (ET->getDecl()->isComplete() && !ET->getDecl()->isScoped()) |
| 5911 | return ET->getDecl()->getIntegerType().getTypePtr(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 5912 | return nullptr; |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5913 | } |
| 5914 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5915 | /// getIntegerTypeOrder - Returns the highest ranked integer type: |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5916 | /// 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] | 5917 | /// LHS < RHS, return -1. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 5918 | int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 5919 | const Type *LHSC = getCanonicalType(LHS).getTypePtr(); |
| 5920 | const Type *RHSC = getCanonicalType(RHS).getTypePtr(); |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5921 | |
| 5922 | // Unwrap enums to their underlying type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5923 | if (const auto *ET = dyn_cast<EnumType>(LHSC)) |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5924 | LHSC = getIntegerTypeForEnum(ET); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 5925 | if (const auto *ET = dyn_cast<EnumType>(RHSC)) |
Ted Kremenek | e65ab9e | 2013-10-10 00:54:01 +0000 | [diff] [blame] | 5926 | RHSC = getIntegerTypeForEnum(ET); |
| 5927 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5928 | if (LHSC == RHSC) return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5929 | |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5930 | bool LHSUnsigned = LHSC->isUnsignedIntegerType(); |
| 5931 | bool RHSUnsigned = RHSC->isUnsignedIntegerType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5932 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5933 | unsigned LHSRank = getIntegerRank(LHSC); |
| 5934 | unsigned RHSRank = getIntegerRank(RHSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5935 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5936 | if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned. |
| 5937 | if (LHSRank == RHSRank) return 0; |
| 5938 | return LHSRank > RHSRank ? 1 : -1; |
| 5939 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5940 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5941 | // Otherwise, the LHS is signed and the RHS is unsigned or visa versa. |
| 5942 | if (LHSUnsigned) { |
| 5943 | // If the unsigned [LHS] type is larger, return it. |
| 5944 | if (LHSRank >= RHSRank) |
| 5945 | return 1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5946 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5947 | // If the signed type can represent all values of the unsigned type, it |
| 5948 | // 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] | 5949 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5950 | return -1; |
| 5951 | } |
Chris Lattner | 76a00cf | 2008-04-06 22:59:24 +0000 | [diff] [blame] | 5952 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5953 | // If the unsigned [RHS] type is larger, return it. |
| 5954 | if (RHSRank >= LHSRank) |
| 5955 | return -1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5956 | |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5957 | // If the signed type can represent all values of the unsigned type, it |
| 5958 | // 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] | 5959 | // powers of two larger than each other, this is always safe. |
Chris Lattner | d4bacd6 | 2008-04-06 23:55:33 +0000 | [diff] [blame] | 5960 | return 1; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5961 | } |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 5962 | |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 5963 | TypedefDecl *ASTContext::getCFConstantStringDecl() const { |
Saleem Abdulrasool | ef9b88a | 2018-10-24 16:38:16 +0000 | [diff] [blame] | 5964 | if (CFConstantStringTypeDecl) |
| 5965 | return CFConstantStringTypeDecl; |
Anders Carlsson | 6d41727 | 2009-11-14 21:45:58 +0000 | [diff] [blame] | 5966 | |
Saleem Abdulrasool | ef9b88a | 2018-10-24 16:38:16 +0000 | [diff] [blame] | 5967 | assert(!CFConstantStringTagDecl && |
| 5968 | "tag and typedef should be initialized together"); |
| 5969 | CFConstantStringTagDecl = buildImplicitRecord("__NSConstantString_tag"); |
| 5970 | CFConstantStringTagDecl->startDefinition(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5971 | |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 5972 | struct { |
| 5973 | QualType Type; |
| 5974 | const char *Name; |
| 5975 | } Fields[5]; |
| 5976 | unsigned Count = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5977 | |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 5978 | /// Objective-C ABI |
| 5979 | /// |
| 5980 | /// typedef struct __NSConstantString_tag { |
Saleem Abdulrasool | 1f6c41f | 2018-10-27 06:12:52 +0000 | [diff] [blame] | 5981 | /// const int *isa; |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 5982 | /// int flags; |
| 5983 | /// const char *str; |
| 5984 | /// long length; |
| 5985 | /// } __NSConstantString; |
| 5986 | /// |
| 5987 | /// Swift ABI (4.1, 4.2) |
| 5988 | /// |
| 5989 | /// typedef struct __NSConstantString_tag { |
| 5990 | /// uintptr_t _cfisa; |
| 5991 | /// uintptr_t _swift_rc; |
| 5992 | /// _Atomic(uint64_t) _cfinfoa; |
| 5993 | /// const char *_ptr; |
| 5994 | /// uint32_t _length; |
| 5995 | /// } __NSConstantString; |
| 5996 | /// |
| 5997 | /// Swift ABI (5.0) |
| 5998 | /// |
| 5999 | /// typedef struct __NSConstantString_tag { |
| 6000 | /// uintptr_t _cfisa; |
| 6001 | /// uintptr_t _swift_rc; |
| 6002 | /// _Atomic(uint64_t) _cfinfoa; |
| 6003 | /// const char *_ptr; |
| 6004 | /// uintptr_t _length; |
| 6005 | /// } __NSConstantString; |
| 6006 | |
| 6007 | const auto CFRuntime = getLangOpts().CFRuntime; |
| 6008 | if (static_cast<unsigned>(CFRuntime) < |
| 6009 | static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift)) { |
| 6010 | Fields[Count++] = { getPointerType(IntTy.withConst()), "isa" }; |
| 6011 | Fields[Count++] = { IntTy, "flags" }; |
| 6012 | Fields[Count++] = { getPointerType(CharTy.withConst()), "str" }; |
| 6013 | Fields[Count++] = { LongTy, "length" }; |
| 6014 | } else { |
| 6015 | Fields[Count++] = { getUIntPtrType(), "_cfisa" }; |
| 6016 | Fields[Count++] = { getUIntPtrType(), "_swift_rc" }; |
| 6017 | Fields[Count++] = { getFromTargetType(Target->getUInt64Type()), "_swift_rc" }; |
| 6018 | Fields[Count++] = { getPointerType(CharTy.withConst()), "_ptr" }; |
| 6019 | if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 || |
| 6020 | CFRuntime == LangOptions::CoreFoundationABI::Swift4_2) |
| 6021 | Fields[Count++] = { IntTy, "_ptr" }; |
| 6022 | else |
| 6023 | Fields[Count++] = { getUIntPtrType(), "_ptr" }; |
| 6024 | } |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6025 | |
Saleem Abdulrasool | ef9b88a | 2018-10-24 16:38:16 +0000 | [diff] [blame] | 6026 | // Create fields |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 6027 | for (unsigned i = 0; i < Count; ++i) { |
Saleem Abdulrasool | ef9b88a | 2018-10-24 16:38:16 +0000 | [diff] [blame] | 6028 | FieldDecl *Field = |
| 6029 | FieldDecl::Create(*this, CFConstantStringTagDecl, SourceLocation(), |
Saleem Abdulrasool | 81a650e | 2018-10-24 23:28:28 +0000 | [diff] [blame] | 6030 | SourceLocation(), &Idents.get(Fields[i].Name), |
| 6031 | Fields[i].Type, /*TInfo=*/nullptr, |
Saleem Abdulrasool | ef9b88a | 2018-10-24 16:38:16 +0000 | [diff] [blame] | 6032 | /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit); |
| 6033 | Field->setAccess(AS_public); |
| 6034 | CFConstantStringTagDecl->addDecl(Field); |
Anders Carlsson | 98f0790 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 6035 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6036 | |
Saleem Abdulrasool | ef9b88a | 2018-10-24 16:38:16 +0000 | [diff] [blame] | 6037 | CFConstantStringTagDecl->completeDefinition(); |
| 6038 | // This type is designed to be compatible with NSConstantString, but cannot |
| 6039 | // use the same name, since NSConstantString is an interface. |
| 6040 | auto tagType = getTagDeclType(CFConstantStringTagDecl); |
| 6041 | CFConstantStringTypeDecl = |
| 6042 | buildImplicitTypedef(tagType, "__NSConstantString"); |
| 6043 | |
Quentin Colombet | 043406b | 2016-02-03 22:41:00 +0000 | [diff] [blame] | 6044 | return CFConstantStringTypeDecl; |
| 6045 | } |
| 6046 | |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 6047 | RecordDecl *ASTContext::getCFConstantStringTagDecl() const { |
| 6048 | if (!CFConstantStringTagDecl) |
| 6049 | getCFConstantStringDecl(); // Build the tag and the typedef. |
| 6050 | return CFConstantStringTagDecl; |
| 6051 | } |
| 6052 | |
Quentin Colombet | 043406b | 2016-02-03 22:41:00 +0000 | [diff] [blame] | 6053 | // getCFConstantStringType - Return the type used for constant CFStrings. |
| 6054 | QualType ASTContext::getCFConstantStringType() const { |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 6055 | return getTypedefType(getCFConstantStringDecl()); |
Gabor Greif | 412af03 | 2007-09-11 15:32:40 +0000 | [diff] [blame] | 6056 | } |
Anders Carlsson | 87c149b | 2007-10-11 01:00:40 +0000 | [diff] [blame] | 6057 | |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 6058 | QualType ASTContext::getObjCSuperType() const { |
| 6059 | if (ObjCSuperType.isNull()) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6060 | RecordDecl *ObjCSuperTypeDecl = buildImplicitRecord("objc_super"); |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 6061 | TUDecl->addDecl(ObjCSuperTypeDecl); |
| 6062 | ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl); |
| 6063 | } |
| 6064 | return ObjCSuperType; |
| 6065 | } |
| 6066 | |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 6067 | void ASTContext::setCFConstantStringType(QualType T) { |
Simon Pilgrim | 9588ae7 | 2019-10-03 21:47:42 +0000 | [diff] [blame] | 6068 | const auto *TD = T->castAs<TypedefType>(); |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 6069 | CFConstantStringTypeDecl = cast<TypedefDecl>(TD->getDecl()); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6070 | const auto *TagType = |
Simon Pilgrim | 9588ae7 | 2019-10-03 21:47:42 +0000 | [diff] [blame] | 6071 | CFConstantStringTypeDecl->getUnderlyingType()->castAs<RecordType>(); |
Ben Langmuir | f541674 | 2016-02-04 00:55:24 +0000 | [diff] [blame] | 6072 | CFConstantStringTagDecl = TagType->getDecl(); |
Douglas Gregor | 512b077 | 2009-04-23 22:29:11 +0000 | [diff] [blame] | 6073 | } |
| 6074 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6075 | QualType ASTContext::getBlockDescriptorType() const { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 6076 | if (BlockDescriptorType) |
| 6077 | return getTagDeclType(BlockDescriptorType); |
| 6078 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6079 | RecordDecl *RD; |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 6080 | // FIXME: Needs the FlagAppleBlock bit. |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6081 | RD = buildImplicitRecord("__block_descriptor"); |
| 6082 | RD->startDefinition(); |
| 6083 | |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 6084 | QualType FieldTypes[] = { |
| 6085 | UnsignedLongTy, |
| 6086 | UnsignedLongTy, |
| 6087 | }; |
| 6088 | |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 6089 | static const char *const FieldNames[] = { |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 6090 | "reserved", |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 6091 | "Size" |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 6092 | }; |
| 6093 | |
| 6094 | for (size_t i = 0; i < 2; ++i) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6095 | FieldDecl *Field = FieldDecl::Create( |
| 6096 | *this, RD, SourceLocation(), SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6097 | &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
| 6098 | /*BitWidth=*/nullptr, /*Mutable=*/false, ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 6099 | Field->setAccess(AS_public); |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6100 | RD->addDecl(Field); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 6101 | } |
| 6102 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6103 | RD->completeDefinition(); |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 6104 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6105 | BlockDescriptorType = RD; |
Mike Stump | d015328 | 2009-10-20 02:12:22 +0000 | [diff] [blame] | 6106 | |
| 6107 | return getTagDeclType(BlockDescriptorType); |
| 6108 | } |
| 6109 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6110 | QualType ASTContext::getBlockDescriptorExtendedType() const { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 6111 | if (BlockDescriptorExtendedType) |
| 6112 | return getTagDeclType(BlockDescriptorExtendedType); |
| 6113 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6114 | RecordDecl *RD; |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 6115 | // FIXME: Needs the FlagAppleBlock bit. |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6116 | RD = buildImplicitRecord("__block_descriptor_withcopydispose"); |
| 6117 | RD->startDefinition(); |
| 6118 | |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 6119 | QualType FieldTypes[] = { |
| 6120 | UnsignedLongTy, |
| 6121 | UnsignedLongTy, |
| 6122 | getPointerType(VoidPtrTy), |
| 6123 | getPointerType(VoidPtrTy) |
| 6124 | }; |
| 6125 | |
Craig Topper | d6d31ac | 2013-07-15 08:24:27 +0000 | [diff] [blame] | 6126 | static const char *const FieldNames[] = { |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 6127 | "reserved", |
| 6128 | "Size", |
| 6129 | "CopyFuncPtr", |
| 6130 | "DestroyFuncPtr" |
| 6131 | }; |
| 6132 | |
| 6133 | for (size_t i = 0; i < 4; ++i) { |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6134 | FieldDecl *Field = FieldDecl::Create( |
| 6135 | *this, RD, SourceLocation(), SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6136 | &Idents.get(FieldNames[i]), FieldTypes[i], /*TInfo=*/nullptr, |
| 6137 | /*BitWidth=*/nullptr, |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6138 | /*Mutable=*/false, ICIS_NoInit); |
John McCall | 4d4dcc8 | 2010-04-30 21:35:41 +0000 | [diff] [blame] | 6139 | Field->setAccess(AS_public); |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6140 | RD->addDecl(Field); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 6141 | } |
| 6142 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6143 | RD->completeDefinition(); |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 6144 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 6145 | BlockDescriptorExtendedType = RD; |
Mike Stump | e1b19ba | 2009-10-22 00:49:09 +0000 | [diff] [blame] | 6146 | return getTagDeclType(BlockDescriptorExtendedType); |
| 6147 | } |
| 6148 | |
Sven van Haastregt | 3bb7eaf | 2017-12-06 10:11:28 +0000 | [diff] [blame] | 6149 | TargetInfo::OpenCLTypeKind ASTContext::getOpenCLTypeKind(const Type *T) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6150 | const auto *BT = dyn_cast<BuiltinType>(T); |
Sven van Haastregt | 3bb7eaf | 2017-12-06 10:11:28 +0000 | [diff] [blame] | 6151 | |
| 6152 | if (!BT) { |
| 6153 | if (isa<PipeType>(T)) |
| 6154 | return TargetInfo::OCLTK_Pipe; |
| 6155 | |
| 6156 | return TargetInfo::OCLTK_Default; |
| 6157 | } |
| 6158 | |
| 6159 | switch (BT->getKind()) { |
| 6160 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 6161 | case BuiltinType::Id: \ |
| 6162 | return TargetInfo::OCLTK_Image; |
| 6163 | #include "clang/Basic/OpenCLImageTypes.def" |
| 6164 | |
| 6165 | case BuiltinType::OCLClkEvent: |
| 6166 | return TargetInfo::OCLTK_ClkEvent; |
| 6167 | |
| 6168 | case BuiltinType::OCLEvent: |
| 6169 | return TargetInfo::OCLTK_Event; |
| 6170 | |
| 6171 | case BuiltinType::OCLQueue: |
| 6172 | return TargetInfo::OCLTK_Queue; |
| 6173 | |
| 6174 | case BuiltinType::OCLReserveID: |
| 6175 | return TargetInfo::OCLTK_ReserveID; |
| 6176 | |
| 6177 | case BuiltinType::OCLSampler: |
| 6178 | return TargetInfo::OCLTK_Sampler; |
| 6179 | |
| 6180 | default: |
| 6181 | return TargetInfo::OCLTK_Default; |
| 6182 | } |
| 6183 | } |
| 6184 | |
| 6185 | LangAS ASTContext::getOpenCLTypeAddrSpace(const Type *T) const { |
| 6186 | return Target->getOpenCLTypeAddrSpace(getOpenCLTypeKind(T)); |
| 6187 | } |
| 6188 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6189 | /// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty" |
| 6190 | /// requires copy/dispose. Note that this must match the logic |
| 6191 | /// in buildByrefHelpers. |
| 6192 | bool ASTContext::BlockRequiresCopying(QualType Ty, |
| 6193 | const VarDecl *D) { |
| 6194 | if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) { |
Akira Hatanaka | 9978da3 | 2018-08-10 15:09:24 +0000 | [diff] [blame] | 6195 | const Expr *copyExpr = getBlockVarCopyInit(D).getCopyExpr(); |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6196 | if (!copyExpr && record->hasTrivialDestructor()) return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6197 | |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 6198 | return true; |
Fariborz Jahanian | a00076c | 2010-11-17 00:21:28 +0000 | [diff] [blame] | 6199 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6200 | |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 6201 | // The block needs copy/destroy helpers if Ty is non-trivial to destructively |
| 6202 | // move or destroy. |
| 6203 | if (Ty.isNonTrivialToPrimitiveDestructiveMove() || Ty.isDestructedType()) |
| 6204 | return true; |
| 6205 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6206 | if (!Ty->isObjCRetainableType()) return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6207 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6208 | Qualifiers qs = Ty.getQualifiers(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6209 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6210 | // If we have lifetime, that dominates. |
| 6211 | if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) { |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6212 | switch (lifetime) { |
| 6213 | case Qualifiers::OCL_None: llvm_unreachable("impossible"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6214 | |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6215 | // These are just bits as far as the runtime is concerned. |
| 6216 | case Qualifiers::OCL_ExplicitNone: |
| 6217 | case Qualifiers::OCL_Autoreleasing: |
| 6218 | return false; |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 6219 | |
| 6220 | // These cases should have been taken care of when checking the type's |
| 6221 | // non-triviality. |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6222 | case Qualifiers::OCL_Weak: |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6223 | case Qualifiers::OCL_Strong: |
Akira Hatanaka | 7275da0 | 2018-02-28 07:15:55 +0000 | [diff] [blame] | 6224 | llvm_unreachable("impossible"); |
Fariborz Jahanian | 998f0a3 | 2012-11-28 23:12:17 +0000 | [diff] [blame] | 6225 | } |
| 6226 | llvm_unreachable("fell out of lifetime switch!"); |
| 6227 | } |
| 6228 | return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) || |
| 6229 | Ty->isObjCObjectPointerType()); |
Mike Stump | 9496790 | 2009-10-21 18:16:27 +0000 | [diff] [blame] | 6230 | } |
| 6231 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 6232 | bool ASTContext::getByrefLifetime(QualType Ty, |
| 6233 | Qualifiers::ObjCLifetime &LifeTime, |
| 6234 | bool &HasByrefExtendedLayout) const { |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 6235 | if (!getLangOpts().ObjC || |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 6236 | getLangOpts().getGC() != LangOptions::NonGC) |
| 6237 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6238 | |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 6239 | HasByrefExtendedLayout = false; |
Fariborz Jahanian | f762b72 | 2012-12-11 19:58:01 +0000 | [diff] [blame] | 6240 | if (Ty->isRecordType()) { |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 6241 | HasByrefExtendedLayout = true; |
| 6242 | LifeTime = Qualifiers::OCL_None; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 6243 | } else if ((LifeTime = Ty.getObjCLifetime())) { |
| 6244 | // Honor the ARC qualifiers. |
| 6245 | } else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) { |
| 6246 | // The MRR rule. |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 6247 | LifeTime = Qualifiers::OCL_ExplicitNone; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 6248 | } else { |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 6249 | LifeTime = Qualifiers::OCL_None; |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 6250 | } |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 6251 | return true; |
| 6252 | } |
| 6253 | |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 6254 | TypedefDecl *ASTContext::getObjCInstanceTypeDecl() { |
| 6255 | if (!ObjCInstanceTypeDecl) |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 6256 | ObjCInstanceTypeDecl = |
| 6257 | buildImplicitTypedef(getObjCIdType(), "instancetype"); |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 6258 | return ObjCInstanceTypeDecl; |
| 6259 | } |
| 6260 | |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 6261 | // This returns true if a type has been typedefed to BOOL: |
| 6262 | // typedef <type> BOOL; |
Chris Lattner | e021899 | 2007-10-30 20:27:44 +0000 | [diff] [blame] | 6263 | static bool isTypeTypedefedAsBOOL(QualType T) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6264 | if (const auto *TT = dyn_cast<TypedefType>(T)) |
Chris Lattner | 9b1f279 | 2008-11-24 03:52:59 +0000 | [diff] [blame] | 6265 | if (IdentifierInfo *II = TT->getDecl()->getIdentifier()) |
| 6266 | return II->isStr("BOOL"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6267 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6268 | return false; |
| 6269 | } |
| 6270 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6271 | /// getObjCEncodingTypeSize returns size of type for objective-c encoding |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6272 | /// purpose. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6273 | CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const { |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6274 | if (!type->isIncompleteArrayType() && type->isIncompleteType()) |
| 6275 | return CharUnits::Zero(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6276 | |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6277 | CharUnits sz = getTypeSizeInChars(type); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6278 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6279 | // 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] | 6280 | if (sz.isPositive() && type->isIntegralOrEnumerationType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6281 | sz = std::max(sz, getTypeSizeInChars(IntTy)); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6282 | // Treat arrays as pointers, since that's how they're passed in. |
| 6283 | else if (type->isArrayType()) |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6284 | sz = getTypeSizeInChars(VoidPtrTy); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 6285 | return sz; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6286 | } |
| 6287 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 6288 | bool ASTContext::isMSStaticDataMemberInlineDefinition(const VarDecl *VD) const { |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 6289 | return getTargetInfo().getCXXABI().isMicrosoft() && |
| 6290 | VD->isStaticDataMember() && |
Hans Wennborg | b18da9b | 2018-02-20 12:43:02 +0000 | [diff] [blame] | 6291 | VD->getType()->isIntegralOrEnumerationType() && |
David Majnemer | fac5243 | 2015-10-19 23:22:49 +0000 | [diff] [blame] | 6292 | !VD->getFirstDecl()->isOutOfLine() && VD->getFirstDecl()->hasInit(); |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 6293 | } |
| 6294 | |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 6295 | ASTContext::InlineVariableDefinitionKind |
| 6296 | ASTContext::getInlineVariableDefinitionKind(const VarDecl *VD) const { |
| 6297 | if (!VD->isInline()) |
| 6298 | return InlineVariableDefinitionKind::None; |
| 6299 | |
| 6300 | // In almost all cases, it's a weak definition. |
| 6301 | auto *First = VD->getFirstDecl(); |
Richard Smith | 8910fe6 | 2017-10-23 03:58:34 +0000 | [diff] [blame] | 6302 | if (First->isInlineSpecified() || !First->isStaticDataMember()) |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 6303 | return InlineVariableDefinitionKind::Weak; |
| 6304 | |
| 6305 | // If there's a file-context declaration in this translation unit, it's a |
| 6306 | // non-discardable definition. |
| 6307 | for (auto *D : VD->redecls()) |
Richard Smith | 8910fe6 | 2017-10-23 03:58:34 +0000 | [diff] [blame] | 6308 | if (D->getLexicalDeclContext()->isFileContext() && |
| 6309 | !D->isInlineSpecified() && (D->isConstexpr() || First->isConstexpr())) |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 6310 | return InlineVariableDefinitionKind::Strong; |
| 6311 | |
| 6312 | // If we've not seen one yet, we don't know. |
| 6313 | return InlineVariableDefinitionKind::WeakUnknown; |
| 6314 | } |
| 6315 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6316 | static std::string charUnitsToString(const CharUnits &CU) { |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6317 | return llvm::itostr(CU.getQuantity()); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6318 | } |
| 6319 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 6320 | /// getObjCEncodingForBlock - Return the encoded type for this block |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6321 | /// declaration. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 6322 | std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const { |
| 6323 | std::string S; |
| 6324 | |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6325 | const BlockDecl *Decl = Expr->getBlockDecl(); |
| 6326 | QualType BlockTy = |
Simon Pilgrim | eed4b12 | 2019-10-02 11:48:06 +0000 | [diff] [blame] | 6327 | Expr->getType()->castAs<BlockPointerType>()->getPointeeType(); |
| 6328 | QualType BlockReturnTy = BlockTy->castAs<FunctionType>()->getReturnType(); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6329 | // Encode result type. |
Fariborz Jahanian | 0e3043b | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 6330 | if (getLangOpts().EncodeExtendedBlockSig) |
Simon Pilgrim | eed4b12 | 2019-10-02 11:48:06 +0000 | [diff] [blame] | 6331 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, BlockReturnTy, S, |
| 6332 | true /*Extended*/); |
Fariborz Jahanian | 64223e6 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 6333 | else |
Simon Pilgrim | eed4b12 | 2019-10-02 11:48:06 +0000 | [diff] [blame] | 6334 | getObjCEncodingForType(BlockReturnTy, S); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6335 | // Compute size of all parameters. |
| 6336 | // Start with computing size of a pointer in number of bytes. |
| 6337 | // FIXME: There might(should) be a better way of doing this computation! |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6338 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
| 6339 | CharUnits ParmOffset = PtrSize; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 6340 | for (auto PI : Decl->parameters()) { |
Aaron Ballman | b2b8b1d | 2014-03-07 16:09:59 +0000 | [diff] [blame] | 6341 | QualType PType = PI->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 6342 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | d487941 | 2012-06-30 00:48:59 +0000 | [diff] [blame] | 6343 | if (sz.isZero()) |
| 6344 | continue; |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 6345 | assert(sz.isPositive() && "BlockExpr - Incomplete param type"); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6346 | ParmOffset += sz; |
| 6347 | } |
| 6348 | // Size of the argument frame |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6349 | S += charUnitsToString(ParmOffset); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6350 | // Block pointer and offset. |
| 6351 | S += "@?0"; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6352 | |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6353 | // Argument types. |
| 6354 | ParmOffset = PtrSize; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 6355 | for (auto PVDecl : Decl->parameters()) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6356 | QualType PType = PVDecl->getOriginalType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6357 | if (const auto *AT = |
| 6358 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6359 | // Use array's original type only if it has known number of |
| 6360 | // elements. |
| 6361 | if (!isa<ConstantArrayType>(AT)) |
| 6362 | PType = PVDecl->getType(); |
| 6363 | } else if (PType->isFunctionType()) |
| 6364 | PType = PVDecl->getType(); |
Fariborz Jahanian | 0e3043b | 2012-11-15 19:02:45 +0000 | [diff] [blame] | 6365 | if (getLangOpts().EncodeExtendedBlockSig) |
Fariborz Jahanian | 64223e6 | 2012-11-14 23:11:38 +0000 | [diff] [blame] | 6366 | getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType, |
| 6367 | S, true /*Extended*/); |
| 6368 | else |
| 6369 | getObjCEncodingForType(PType, S); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6370 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 6371 | ParmOffset += getObjCEncodingTypeSize(PType); |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6372 | } |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 6373 | |
| 6374 | return S; |
David Chisnall | 950a951 | 2009-11-17 19:33:30 +0000 | [diff] [blame] | 6375 | } |
| 6376 | |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6377 | std::string |
| 6378 | ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl) const { |
| 6379 | std::string S; |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6380 | // Encode result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6381 | getObjCEncodingForType(Decl->getReturnType(), S); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6382 | CharUnits ParmOffset; |
| 6383 | // Compute size of all parameters. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 6384 | for (auto PI : Decl->parameters()) { |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 6385 | QualType PType = PI->getType(); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6386 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6387 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 6388 | continue; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6389 | |
| 6390 | assert(sz.isPositive() && |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6391 | "getObjCEncodingForFunctionDecl - Incomplete param type"); |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6392 | ParmOffset += sz; |
| 6393 | } |
| 6394 | S += charUnitsToString(ParmOffset); |
| 6395 | ParmOffset = CharUnits::Zero(); |
| 6396 | |
| 6397 | // Argument types. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 6398 | for (auto PVDecl : Decl->parameters()) { |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6399 | QualType PType = PVDecl->getOriginalType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6400 | if (const auto *AT = |
| 6401 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6402 | // Use array's original type only if it has known number of |
| 6403 | // elements. |
| 6404 | if (!isa<ConstantArrayType>(AT)) |
| 6405 | PType = PVDecl->getType(); |
| 6406 | } else if (PType->isFunctionType()) |
| 6407 | PType = PVDecl->getType(); |
| 6408 | getObjCEncodingForType(PType, S); |
| 6409 | S += charUnitsToString(ParmOffset); |
| 6410 | ParmOffset += getObjCEncodingTypeSize(PType); |
| 6411 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6412 | |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6413 | return S; |
David Chisnall | 50e4eba | 2010-12-30 14:05:53 +0000 | [diff] [blame] | 6414 | } |
| 6415 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6416 | /// getObjCEncodingForMethodParameter - Return the encoded type for a single |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6417 | /// method parameter or return type. If Extended, include class names and |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6418 | /// block object types. |
| 6419 | void ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, |
| 6420 | QualType T, std::string& S, |
| 6421 | bool Extended) const { |
| 6422 | // Encode type qualifer, 'in', 'inout', etc. for the parameter. |
| 6423 | getObjCEncodingForTypeQualifier(QT, S); |
| 6424 | // Encode parameter type. |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6425 | ObjCEncOptions Options = ObjCEncOptions() |
| 6426 | .setExpandPointedToStructures() |
| 6427 | .setExpandStructures() |
| 6428 | .setIsOutermostType(); |
| 6429 | if (Extended) |
| 6430 | Options.setEncodeBlockParameters().setEncodeClassNames(); |
| 6431 | getObjCEncodingForTypeImpl(T, S, Options, /*Field=*/nullptr); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6432 | } |
| 6433 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 6434 | /// getObjCEncodingForMethodDecl - Return the encoded type for this method |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6435 | /// declaration. |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6436 | std::string ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, |
| 6437 | bool Extended) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6438 | // FIXME: This is not very efficient. |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6439 | // Encode return type. |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6440 | std::string S; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 6441 | getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(), |
| 6442 | Decl->getReturnType(), S, Extended); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6443 | // Compute size of all parameters. |
| 6444 | // Start with computing size of a pointer in number of bytes. |
| 6445 | // FIXME: There might(should) be a better way of doing this computation! |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6446 | CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6447 | // The first two arguments (self and _cmd) are pointers; account for |
| 6448 | // their size. |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6449 | CharUnits ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 6450 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 6451 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 6452 | QualType PType = (*PI)->getType(); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 6453 | CharUnits sz = getObjCEncodingTypeSize(PType); |
Douglas Gregor | a9d8493 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6454 | if (sz.isZero()) |
Fariborz Jahanian | 271b8d4 | 2012-06-29 22:54:56 +0000 | [diff] [blame] | 6455 | continue; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6456 | |
| 6457 | assert(sz.isPositive() && |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 6458 | "getObjCEncodingForMethodDecl - Incomplete param type"); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6459 | ParmOffset += sz; |
| 6460 | } |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6461 | S += charUnitsToString(ParmOffset); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6462 | S += "@0:"; |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6463 | S += charUnitsToString(PtrSize); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6464 | |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6465 | // Argument types. |
| 6466 | ParmOffset = 2 * PtrSize; |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 6467 | for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(), |
Fariborz Jahanian | d9235db | 2010-04-08 21:29:11 +0000 | [diff] [blame] | 6468 | E = Decl->sel_param_end(); PI != E; ++PI) { |
Argyrios Kyrtzidis | b8c3aaf | 2011-10-03 06:37:04 +0000 | [diff] [blame] | 6469 | const ParmVarDecl *PVDecl = *PI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6470 | QualType PType = PVDecl->getOriginalType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6471 | if (const auto *AT = |
| 6472 | dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) { |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 6473 | // Use array's original type only if it has known number of |
| 6474 | // elements. |
Steve Naroff | 323827e | 2009-04-14 00:40:09 +0000 | [diff] [blame] | 6475 | if (!isa<ConstantArrayType>(AT)) |
Steve Naroff | e4e55d2 | 2009-04-14 00:03:58 +0000 | [diff] [blame] | 6476 | PType = PVDecl->getType(); |
| 6477 | } else if (PType->isFunctionType()) |
| 6478 | PType = PVDecl->getType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6479 | getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(), |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6480 | PType, S, Extended); |
Ken Dyck | 4077500 | 2010-01-11 17:06:35 +0000 | [diff] [blame] | 6481 | S += charUnitsToString(ParmOffset); |
Ken Dyck | de37a67 | 2010-01-11 19:19:56 +0000 | [diff] [blame] | 6482 | ParmOffset += getObjCEncodingTypeSize(PType); |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6483 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6484 | |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6485 | return S; |
Fariborz Jahanian | 797f24c | 2007-10-29 22:57:28 +0000 | [diff] [blame] | 6486 | } |
| 6487 | |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 6488 | ObjCPropertyImplDecl * |
| 6489 | ASTContext::getObjCPropertyImplDeclForPropertyDecl( |
| 6490 | const ObjCPropertyDecl *PD, |
| 6491 | const Decl *Container) const { |
| 6492 | if (!Container) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6493 | return nullptr; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6494 | if (const auto *CID = dyn_cast<ObjCCategoryImplDecl>(Container)) { |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 6495 | for (auto *PID : CID->property_impls()) |
| 6496 | if (PID->getPropertyDecl() == PD) |
| 6497 | return PID; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6498 | } else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6499 | const auto *OID = cast<ObjCImplementationDecl>(Container); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6500 | for (auto *PID : OID->property_impls()) |
| 6501 | if (PID->getPropertyDecl() == PD) |
| 6502 | return PID; |
| 6503 | } |
| 6504 | return nullptr; |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 6505 | } |
| 6506 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6507 | /// getObjCEncodingForPropertyDecl - Return the encoded type for this |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 6508 | /// property declaration. If non-NULL, Container must be either an |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6509 | /// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be |
| 6510 | /// NULL when getting encodings for protocol properties. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6511 | /// Property attributes are stored as a comma-delimited C string. The simple |
| 6512 | /// attributes readonly and bycopy are encoded as single characters. The |
| 6513 | /// parametrized attributes, getter=name, setter=name, and ivar=name, are |
| 6514 | /// encoded as single characters, followed by an identifier. Property types |
| 6515 | /// are also encoded as a parametrized attribute. The characters used to encode |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 6516 | /// these attributes are defined by the following enumeration: |
| 6517 | /// @code |
| 6518 | /// enum PropertyAttributes { |
| 6519 | /// kPropertyReadOnly = 'R', // property is read-only. |
| 6520 | /// kPropertyBycopy = 'C', // property is a copy of the value last assigned |
| 6521 | /// kPropertyByref = '&', // property is a reference to the value last assigned |
| 6522 | /// kPropertyDynamic = 'D', // property is dynamic |
| 6523 | /// kPropertyGetter = 'G', // followed by getter selector name |
| 6524 | /// kPropertySetter = 'S', // followed by setter selector name |
| 6525 | /// kPropertyInstanceVariable = 'V' // followed by instance variable name |
Bob Wilson | 8cf6185 | 2012-03-22 17:48:02 +0000 | [diff] [blame] | 6526 | /// kPropertyType = 'T' // followed by old-style type encoding. |
Fariborz Jahanian | 2f85a64 | 2009-01-20 20:04:12 +0000 | [diff] [blame] | 6527 | /// kPropertyWeak = 'W' // 'weak' property |
| 6528 | /// kPropertyStrong = 'P' // property GC'able |
| 6529 | /// kPropertyNonAtomic = 'N' // property non-atomic |
| 6530 | /// }; |
| 6531 | /// @endcode |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6532 | std::string |
| 6533 | ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, |
| 6534 | const Decl *Container) const { |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6535 | // Collect information from the property implementation decl(s). |
| 6536 | bool Dynamic = false; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 6537 | ObjCPropertyImplDecl *SynthesizePID = nullptr; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6538 | |
Fariborz Jahanian | dad9630 | 2014-01-22 19:02:20 +0000 | [diff] [blame] | 6539 | if (ObjCPropertyImplDecl *PropertyImpDecl = |
| 6540 | getObjCPropertyImplDeclForPropertyDecl(PD, Container)) { |
| 6541 | if (PropertyImpDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 6542 | Dynamic = true; |
| 6543 | else |
| 6544 | SynthesizePID = PropertyImpDecl; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6545 | } |
| 6546 | |
| 6547 | // FIXME: This is not very efficient. |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6548 | std::string S = "T"; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6549 | |
| 6550 | // Encode result type. |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 6551 | // GCC has some special rules regarding encoding of properties which |
| 6552 | // closely resembles encoding of ivars. |
Joe Groff | 98ac7d2 | 2014-07-07 22:25:15 +0000 | [diff] [blame] | 6553 | getObjCEncodingForPropertyType(PD->getType(), S); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6554 | |
| 6555 | if (PD->isReadOnly()) { |
| 6556 | S += ",R"; |
Nico Weber | 4e8626f | 2013-05-08 23:47:40 +0000 | [diff] [blame] | 6557 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy) |
| 6558 | S += ",C"; |
| 6559 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain) |
| 6560 | S += ",&"; |
Fariborz Jahanian | 33079ee | 2014-04-02 22:49:42 +0000 | [diff] [blame] | 6561 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) |
| 6562 | S += ",W"; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6563 | } else { |
| 6564 | switch (PD->getSetterKind()) { |
| 6565 | case ObjCPropertyDecl::Assign: break; |
| 6566 | case ObjCPropertyDecl::Copy: S += ",C"; break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6567 | case ObjCPropertyDecl::Retain: S += ",&"; break; |
Fariborz Jahanian | 70a315c | 2011-08-12 20:47:08 +0000 | [diff] [blame] | 6568 | case ObjCPropertyDecl::Weak: S += ",W"; break; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6569 | } |
| 6570 | } |
| 6571 | |
| 6572 | // It really isn't clear at all what this means, since properties |
| 6573 | // are "dynamic by default". |
| 6574 | if (Dynamic) |
| 6575 | S += ",D"; |
| 6576 | |
Fariborz Jahanian | 218c630 | 2009-01-20 19:14:18 +0000 | [diff] [blame] | 6577 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 6578 | S += ",N"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6579 | |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6580 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) { |
| 6581 | S += ",G"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 6582 | S += PD->getGetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6583 | } |
| 6584 | |
| 6585 | if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) { |
| 6586 | S += ",S"; |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 6587 | S += PD->getSetterName().getAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6588 | } |
| 6589 | |
| 6590 | if (SynthesizePID) { |
| 6591 | const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl(); |
| 6592 | S += ",V"; |
Chris Lattner | 1cbaacc | 2008-11-24 04:00:27 +0000 | [diff] [blame] | 6593 | S += OID->getNameAsString(); |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6594 | } |
| 6595 | |
| 6596 | // FIXME: OBJCGC: weak & strong |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 6597 | return S; |
Daniel Dunbar | 4932b36 | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 6598 | } |
| 6599 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6600 | /// getLegacyIntegralTypeEncoding - |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6601 | /// Another legacy compatibility encoding: 32-bit longs are encoded as |
| 6602 | /// 'l' or 'L' , but not always. For typedefs, we need to use |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6603 | /// 'i' or 'I' instead if encoding a struct field, or a pointer! |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6604 | void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const { |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 6605 | if (isa<TypedefType>(PointeeTy.getTypePtr())) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6606 | if (const auto *BT = PointeeTy->getAs<BuiltinType>()) { |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6607 | if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6608 | PointeeTy = UnsignedIntTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6609 | else |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6610 | if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32) |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6611 | PointeeTy = IntTy; |
| 6612 | } |
| 6613 | } |
| 6614 | } |
| 6615 | |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 6616 | void ASTContext::getObjCEncodingForType(QualType T, std::string& S, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6617 | const FieldDecl *Field, |
| 6618 | QualType *NotEncodedT) const { |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 6619 | // We follow the behavior of gcc, expanding structures which are |
| 6620 | // directly pointed to, and expanding embedded structures. Note that |
| 6621 | // these rules are sufficient to prevent recursive encoding of the |
| 6622 | // same type. |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6623 | getObjCEncodingForTypeImpl(T, S, |
| 6624 | ObjCEncOptions() |
| 6625 | .setExpandPointedToStructures() |
| 6626 | .setExpandStructures() |
| 6627 | .setIsOutermostType(), |
| 6628 | Field, NotEncodedT); |
Daniel Dunbar | 3cd9a29 | 2008-10-17 07:30:50 +0000 | [diff] [blame] | 6629 | } |
| 6630 | |
Joe Groff | 98ac7d2 | 2014-07-07 22:25:15 +0000 | [diff] [blame] | 6631 | void ASTContext::getObjCEncodingForPropertyType(QualType T, |
| 6632 | std::string& S) const { |
| 6633 | // Encode result type. |
| 6634 | // GCC has some special rules regarding encoding of properties which |
| 6635 | // closely resembles encoding of ivars. |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6636 | getObjCEncodingForTypeImpl(T, S, |
| 6637 | ObjCEncOptions() |
| 6638 | .setExpandPointedToStructures() |
| 6639 | .setExpandStructures() |
| 6640 | .setIsOutermostType() |
| 6641 | .setEncodingProperty(), |
| 6642 | /*Field=*/nullptr); |
Joe Groff | 98ac7d2 | 2014-07-07 22:25:15 +0000 | [diff] [blame] | 6643 | } |
| 6644 | |
Richard Sandiford | eb485fb | 2019-08-09 08:52:54 +0000 | [diff] [blame] | 6645 | static char getObjCEncodingForPrimitiveType(const ASTContext *C, |
| 6646 | const BuiltinType *BT) { |
| 6647 | BuiltinType::Kind kind = BT->getKind(); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6648 | switch (kind) { |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6649 | case BuiltinType::Void: return 'v'; |
| 6650 | case BuiltinType::Bool: return 'B'; |
Richard Smith | 3a8244d | 2018-05-01 05:02:45 +0000 | [diff] [blame] | 6651 | case BuiltinType::Char8: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6652 | case BuiltinType::Char_U: |
| 6653 | case BuiltinType::UChar: return 'C'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6654 | case BuiltinType::Char16: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6655 | case BuiltinType::UShort: return 'S'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6656 | case BuiltinType::Char32: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6657 | case BuiltinType::UInt: return 'I'; |
| 6658 | case BuiltinType::ULong: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6659 | return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6660 | case BuiltinType::UInt128: return 'T'; |
| 6661 | case BuiltinType::ULongLong: return 'Q'; |
| 6662 | case BuiltinType::Char_S: |
| 6663 | case BuiltinType::SChar: return 'c'; |
| 6664 | case BuiltinType::Short: return 's'; |
Chris Lattner | ad3467e | 2010-12-25 23:25:43 +0000 | [diff] [blame] | 6665 | case BuiltinType::WChar_S: |
| 6666 | case BuiltinType::WChar_U: |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6667 | case BuiltinType::Int: return 'i'; |
| 6668 | case BuiltinType::Long: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6669 | return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6670 | case BuiltinType::LongLong: return 'q'; |
| 6671 | case BuiltinType::Int128: return 't'; |
| 6672 | case BuiltinType::Float: return 'f'; |
| 6673 | case BuiltinType::Double: return 'd'; |
Daniel Dunbar | 7cba5a7 | 2010-10-11 21:13:48 +0000 | [diff] [blame] | 6674 | case BuiltinType::LongDouble: return 'D'; |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6675 | case BuiltinType::NullPtr: return '*'; // like char* |
| 6676 | |
Sjoerd Meijer | cc623ad | 2017-09-08 15:15:00 +0000 | [diff] [blame] | 6677 | case BuiltinType::Float16: |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 6678 | case BuiltinType::Float128: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6679 | case BuiltinType::Half: |
Leonard Chan | f921d85 | 2018-06-04 16:07:52 +0000 | [diff] [blame] | 6680 | case BuiltinType::ShortAccum: |
| 6681 | case BuiltinType::Accum: |
| 6682 | case BuiltinType::LongAccum: |
| 6683 | case BuiltinType::UShortAccum: |
| 6684 | case BuiltinType::UAccum: |
| 6685 | case BuiltinType::ULongAccum: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 6686 | case BuiltinType::ShortFract: |
| 6687 | case BuiltinType::Fract: |
| 6688 | case BuiltinType::LongFract: |
| 6689 | case BuiltinType::UShortFract: |
| 6690 | case BuiltinType::UFract: |
| 6691 | case BuiltinType::ULongFract: |
| 6692 | case BuiltinType::SatShortAccum: |
| 6693 | case BuiltinType::SatAccum: |
| 6694 | case BuiltinType::SatLongAccum: |
| 6695 | case BuiltinType::SatUShortAccum: |
| 6696 | case BuiltinType::SatUAccum: |
| 6697 | case BuiltinType::SatULongAccum: |
| 6698 | case BuiltinType::SatShortFract: |
| 6699 | case BuiltinType::SatFract: |
| 6700 | case BuiltinType::SatLongFract: |
| 6701 | case BuiltinType::SatUShortFract: |
| 6702 | case BuiltinType::SatUFract: |
| 6703 | case BuiltinType::SatULongFract: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6704 | // FIXME: potentially need @encodes for these! |
| 6705 | return ' '; |
| 6706 | |
Richard Sandiford | eb485fb | 2019-08-09 08:52:54 +0000 | [diff] [blame] | 6707 | #define SVE_TYPE(Name, Id, SingletonId) \ |
| 6708 | case BuiltinType::Id: |
| 6709 | #include "clang/Basic/AArch64SVEACLETypes.def" |
| 6710 | { |
| 6711 | DiagnosticsEngine &Diags = C->getDiagnostics(); |
| 6712 | unsigned DiagID = Diags.getCustomDiagID( |
| 6713 | DiagnosticsEngine::Error, "cannot yet @encode type %0"); |
| 6714 | Diags.Report(DiagID) << BT->getName(C->getPrintingPolicy()); |
| 6715 | return ' '; |
| 6716 | } |
| 6717 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6718 | case BuiltinType::ObjCId: |
| 6719 | case BuiltinType::ObjCClass: |
| 6720 | case BuiltinType::ObjCSel: |
| 6721 | llvm_unreachable("@encoding ObjC primitive type"); |
| 6722 | |
| 6723 | // OpenCL and placeholder types don't need @encodings. |
Alexey Bader | 954ba21 | 2016-04-08 13:40:33 +0000 | [diff] [blame] | 6724 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
| 6725 | case BuiltinType::Id: |
Alexey Bader | b62f144 | 2016-04-13 08:33:41 +0000 | [diff] [blame] | 6726 | #include "clang/Basic/OpenCLImageTypes.def" |
Andrew Savonichev | 3fee351 | 2018-11-08 11:25:41 +0000 | [diff] [blame] | 6727 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
| 6728 | case BuiltinType::Id: |
| 6729 | #include "clang/Basic/OpenCLExtensionTypes.def" |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6730 | case BuiltinType::OCLEvent: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 6731 | case BuiltinType::OCLClkEvent: |
| 6732 | case BuiltinType::OCLQueue: |
Alexey Bader | 9c8453f | 2015-09-15 11:18:52 +0000 | [diff] [blame] | 6733 | case BuiltinType::OCLReserveID: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6734 | case BuiltinType::OCLSampler: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6735 | case BuiltinType::Dependent: |
| 6736 | #define BUILTIN_TYPE(KIND, ID) |
| 6737 | #define PLACEHOLDER_TYPE(KIND, ID) \ |
| 6738 | case BuiltinType::KIND: |
| 6739 | #include "clang/AST/BuiltinTypes.def" |
| 6740 | llvm_unreachable("invalid builtin type for @encode"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6741 | } |
David Blaikie | 5a6a020 | 2013-01-09 17:48:41 +0000 | [diff] [blame] | 6742 | llvm_unreachable("invalid BuiltinType::Kind value"); |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6743 | } |
| 6744 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 6745 | static char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) { |
| 6746 | EnumDecl *Enum = ET->getDecl(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6747 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 6748 | // The encoding of an non-fixed enum type is always 'i', regardless of size. |
| 6749 | if (!Enum->isFixed()) |
| 6750 | return 'i'; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6751 | |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 6752 | // The encoding of a fixed enum type matches its fixed underlying type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6753 | const auto *BT = Enum->getIntegerType()->castAs<BuiltinType>(); |
Richard Sandiford | eb485fb | 2019-08-09 08:52:54 +0000 | [diff] [blame] | 6754 | return getObjCEncodingForPrimitiveType(C, BT); |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 6755 | } |
| 6756 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 6757 | static void EncodeBitField(const ASTContext *Ctx, std::string& S, |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6758 | QualType T, const FieldDecl *FD) { |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6759 | assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl"); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 6760 | S += 'b'; |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6761 | // The NeXT runtime encodes bit fields as b followed by the number of bits. |
| 6762 | // The GNU runtime requires more information; bitfields are encoded as b, |
| 6763 | // then the offset (in bits) of the first element, then the type of the |
| 6764 | // bitfield, then the size in bits. For example, in this structure: |
| 6765 | // |
| 6766 | // struct |
| 6767 | // { |
| 6768 | // int integer; |
| 6769 | // int flags:2; |
| 6770 | // }; |
| 6771 | // On a 32-bit system, the encoding for flags would be b2 for the NeXT |
| 6772 | // runtime, but b32i2 for the GNU runtime. The reason for this extra |
| 6773 | // information is not especially sensible, but we're stuck with it for |
| 6774 | // compatibility with GCC, although providing it breaks anything that |
| 6775 | // actually uses runtime introspection and wants to work on both runtimes... |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 6776 | if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) { |
Akira Hatanaka | 4b1c484 | 2017-06-27 04:34:04 +0000 | [diff] [blame] | 6777 | uint64_t Offset; |
| 6778 | |
| 6779 | if (const auto *IVD = dyn_cast<ObjCIvarDecl>(FD)) { |
| 6780 | Offset = Ctx->lookupFieldBitOffset(IVD->getContainingInterface(), nullptr, |
| 6781 | IVD); |
| 6782 | } else { |
| 6783 | const RecordDecl *RD = FD->getParent(); |
| 6784 | const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD); |
| 6785 | Offset = RL.getFieldOffset(FD->getFieldIndex()); |
| 6786 | } |
| 6787 | |
| 6788 | S += llvm::utostr(Offset); |
| 6789 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6790 | if (const auto *ET = T->getAs<EnumType>()) |
Douglas Gregor | 8b7d403 | 2011-09-08 17:18:35 +0000 | [diff] [blame] | 6791 | S += ObjCEncodingForEnumType(Ctx, ET); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6792 | else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6793 | const auto *BT = T->castAs<BuiltinType>(); |
Richard Sandiford | eb485fb | 2019-08-09 08:52:54 +0000 | [diff] [blame] | 6794 | S += getObjCEncodingForPrimitiveType(Ctx, BT); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6795 | } |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6796 | } |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 6797 | S += llvm::utostr(FD->getBitWidthValue(*Ctx)); |
Fariborz Jahanian | 5c76772 | 2009-01-13 01:18:13 +0000 | [diff] [blame] | 6798 | } |
| 6799 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6800 | // FIXME: Use SmallString for accumulating string. |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6801 | void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string &S, |
| 6802 | const ObjCEncOptions Options, |
Daniel Dunbar | c040ce4 | 2009-04-20 06:37:24 +0000 | [diff] [blame] | 6803 | const FieldDecl *FD, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6804 | QualType *NotEncodedT) const { |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6805 | CanQualType CT = getCanonicalType(T); |
| 6806 | switch (CT->getTypeClass()) { |
| 6807 | case Type::Builtin: |
| 6808 | case Type::Enum: |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6809 | if (FD && FD->isBitField()) |
David Chisnall | b190a2c | 2010-06-04 01:10:52 +0000 | [diff] [blame] | 6810 | return EncodeBitField(this, S, T, FD); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6811 | if (const auto *BT = dyn_cast<BuiltinType>(CT)) |
Richard Sandiford | eb485fb | 2019-08-09 08:52:54 +0000 | [diff] [blame] | 6812 | S += getObjCEncodingForPrimitiveType(this, BT); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6813 | else |
| 6814 | S += ObjCEncodingForEnumType(this, cast<EnumType>(CT)); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6815 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6816 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6817 | case Type::Complex: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6818 | const auto *CT = T->castAs<ComplexType>(); |
Anders Carlsson | 39b2e13 | 2009-04-09 21:55:45 +0000 | [diff] [blame] | 6819 | S += 'j'; |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6820 | getObjCEncodingForTypeImpl(CT->getElementType(), S, ObjCEncOptions(), |
| 6821 | /*Field=*/nullptr); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6822 | return; |
| 6823 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6824 | |
| 6825 | case Type::Atomic: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6826 | const auto *AT = T->castAs<AtomicType>(); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6827 | S += 'A'; |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6828 | getObjCEncodingForTypeImpl(AT->getValueType(), S, ObjCEncOptions(), |
| 6829 | /*Field=*/nullptr); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6830 | return; |
Fariborz Jahanian | 9ffd706 | 2010-04-13 23:45:47 +0000 | [diff] [blame] | 6831 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6832 | |
| 6833 | // encoding for pointer or reference types. |
| 6834 | case Type::Pointer: |
| 6835 | case Type::LValueReference: |
| 6836 | case Type::RValueReference: { |
| 6837 | QualType PointeeTy; |
| 6838 | if (isa<PointerType>(CT)) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6839 | const auto *PT = T->castAs<PointerType>(); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6840 | if (PT->isObjCSelType()) { |
| 6841 | S += ':'; |
| 6842 | return; |
| 6843 | } |
| 6844 | PointeeTy = PT->getPointeeType(); |
| 6845 | } else { |
| 6846 | PointeeTy = T->castAs<ReferenceType>()->getPointeeType(); |
| 6847 | } |
| 6848 | |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6849 | bool isReadOnly = false; |
| 6850 | // For historical/compatibility reasons, the read-only qualifier of the |
| 6851 | // pointee gets emitted _before_ the '^'. The read-only qualifier of |
| 6852 | // the pointer itself gets ignored, _unless_ we are looking at a typedef! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6853 | // Also, do not emit the 'r' for anything but the outermost type! |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 6854 | if (isa<TypedefType>(T.getTypePtr())) { |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6855 | if (Options.IsOutermostType() && T.isConstQualified()) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6856 | isReadOnly = true; |
| 6857 | S += 'r'; |
| 6858 | } |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6859 | } else if (Options.IsOutermostType()) { |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6860 | QualType P = PointeeTy; |
Simon Pilgrim | d834f1f | 2019-10-03 15:08:20 +0000 | [diff] [blame] | 6861 | while (auto PT = P->getAs<PointerType>()) |
| 6862 | P = PT->getPointeeType(); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6863 | if (P.isConstQualified()) { |
| 6864 | isReadOnly = true; |
| 6865 | S += 'r'; |
| 6866 | } |
| 6867 | } |
| 6868 | if (isReadOnly) { |
| 6869 | // Another legacy compatibility encoding. Some ObjC qualifier and type |
| 6870 | // combinations need to be rearranged. |
| 6871 | // Rewrite "in const" from "nr" to "rn" |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6872 | if (StringRef(S).endswith("nr")) |
Benjamin Kramer | 2e3197e | 2010-04-27 17:12:11 +0000 | [diff] [blame] | 6873 | S.replace(S.end()-2, S.end(), "rn"); |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6874 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6875 | |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6876 | if (PointeeTy->isCharType()) { |
| 6877 | // char pointer types should be encoded as '*' unless it is a |
| 6878 | // type that has been typedef'd to 'BOOL'. |
Anders Carlsson | 18acd44 | 2007-10-29 06:33:42 +0000 | [diff] [blame] | 6879 | if (!isTypeTypedefedAsBOOL(PointeeTy)) { |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6880 | S += '*'; |
| 6881 | return; |
| 6882 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6883 | } else if (const auto *RTy = PointeeTy->getAs<RecordType>()) { |
Steve Naroff | 3de6b70 | 2009-07-22 17:14:51 +0000 | [diff] [blame] | 6884 | // GCC binary compat: Need to convert "struct objc_class *" to "#". |
| 6885 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) { |
| 6886 | S += '#'; |
| 6887 | return; |
| 6888 | } |
| 6889 | // GCC binary compat: Need to convert "struct objc_object *" to "@". |
| 6890 | if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) { |
| 6891 | S += '@'; |
| 6892 | return; |
| 6893 | } |
| 6894 | // fall through... |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6895 | } |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 6896 | S += '^'; |
Fariborz Jahanian | 0f66a6c | 2008-12-23 19:56:47 +0000 | [diff] [blame] | 6897 | getLegacyIntegralTypeEncoding(PointeeTy); |
| 6898 | |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6899 | ObjCEncOptions NewOptions; |
| 6900 | if (Options.ExpandPointedToStructures()) |
| 6901 | NewOptions.setExpandStructures(); |
| 6902 | getObjCEncodingForTypeImpl(PointeeTy, S, NewOptions, |
| 6903 | /*Field=*/nullptr, NotEncodedT); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6904 | return; |
| 6905 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6906 | |
| 6907 | case Type::ConstantArray: |
| 6908 | case Type::IncompleteArray: |
| 6909 | case Type::VariableArray: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6910 | const auto *AT = cast<ArrayType>(CT); |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6911 | |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6912 | if (isa<IncompleteArrayType>(AT) && !Options.IsStructField()) { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6913 | // Incomplete arrays are encoded as a pointer to the array element. |
| 6914 | S += '^'; |
| 6915 | |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6916 | getObjCEncodingForTypeImpl( |
| 6917 | AT->getElementType(), S, |
| 6918 | Options.keepingOnly(ObjCEncOptions().setExpandStructures()), FD); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6919 | } else { |
| 6920 | S += '['; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6921 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6922 | if (const auto *CAT = dyn_cast<ConstantArrayType>(AT)) |
Fariborz Jahanian | f0dc11a | 2013-06-04 16:04:37 +0000 | [diff] [blame] | 6923 | S += llvm::utostr(CAT->getSize().getZExtValue()); |
| 6924 | else { |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6925 | //Variable length arrays are encoded as a regular array with 0 elements. |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6926 | assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) && |
| 6927 | "Unknown array type!"); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6928 | S += '0'; |
| 6929 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6930 | |
Nico Weber | df12933 | 2019-05-10 13:56:56 +0000 | [diff] [blame] | 6931 | getObjCEncodingForTypeImpl( |
| 6932 | AT->getElementType(), S, |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6933 | Options.keepingOnly(ObjCEncOptions().setExpandStructures()), FD, |
| 6934 | NotEncodedT); |
Anders Carlsson | d05f44b | 2009-02-22 01:38:57 +0000 | [diff] [blame] | 6935 | S += ']'; |
| 6936 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6937 | return; |
| 6938 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6939 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6940 | case Type::FunctionNoProto: |
| 6941 | case Type::FunctionProto: |
Anders Carlsson | df4cc61 | 2007-10-30 00:06:20 +0000 | [diff] [blame] | 6942 | S += '?'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6943 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6944 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6945 | case Type::Record: { |
| 6946 | RecordDecl *RDecl = cast<RecordType>(CT)->getDecl(); |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 6947 | S += RDecl->isUnion() ? '(' : '{'; |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 6948 | // Anonymous structures print as '?' |
| 6949 | if (const IdentifierInfo *II = RDecl->getIdentifier()) { |
| 6950 | S += II->getName(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6951 | if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) { |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 6952 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 6953 | llvm::raw_string_ostream OS(S); |
Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 6954 | printTemplateArgumentList(OS, TemplateArgs.asArray(), |
| 6955 | getPrintingPolicy()); |
Fariborz Jahanian | c515820 | 2010-05-07 00:28:49 +0000 | [diff] [blame] | 6956 | } |
Daniel Dunbar | 40cac77 | 2008-10-17 06:22:57 +0000 | [diff] [blame] | 6957 | } else { |
| 6958 | S += '?'; |
| 6959 | } |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6960 | if (Options.ExpandStructures()) { |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 6961 | S += '='; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6962 | if (!RDecl->isUnion()) { |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 6963 | getObjCEncodingForStructureImpl(RDecl, S, FD, true, NotEncodedT); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6964 | } else { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 6965 | for (const auto *Field : RDecl->fields()) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6966 | if (FD) { |
| 6967 | S += '"'; |
| 6968 | S += Field->getNameAsString(); |
| 6969 | S += '"'; |
| 6970 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6971 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6972 | // Special case bit-fields. |
| 6973 | if (Field->isBitField()) { |
Nico Weber | df12933 | 2019-05-10 13:56:56 +0000 | [diff] [blame] | 6974 | getObjCEncodingForTypeImpl(Field->getType(), S, |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6975 | ObjCEncOptions().setExpandStructures(), |
| 6976 | Field); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6977 | } else { |
| 6978 | QualType qt = Field->getType(); |
| 6979 | getLegacyIntegralTypeEncoding(qt); |
Nico Weber | df12933 | 2019-05-10 13:56:56 +0000 | [diff] [blame] | 6980 | getObjCEncodingForTypeImpl( |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6981 | qt, S, |
| 6982 | ObjCEncOptions().setExpandStructures().setIsStructField(), FD, |
| 6983 | NotEncodedT); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 6984 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 6985 | } |
Fariborz Jahanian | 0a71ad2 | 2008-01-22 22:44:46 +0000 | [diff] [blame] | 6986 | } |
Fariborz Jahanian | bc92fd7 | 2007-11-13 23:21:38 +0000 | [diff] [blame] | 6987 | } |
Daniel Dunbar | ff3c674 | 2008-10-17 16:17:37 +0000 | [diff] [blame] | 6988 | S += RDecl->isUnion() ? ')' : '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 6989 | return; |
| 6990 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6991 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 6992 | case Type::BlockPointer: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6993 | const auto *BT = T->castAs<BlockPointerType>(); |
Steve Naroff | 49140cb | 2009-02-02 18:24:29 +0000 | [diff] [blame] | 6994 | S += "@?"; // Unlike a pointer-to-function, which is "^?". |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 6995 | if (Options.EncodeBlockParameters()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 6996 | const auto *FT = BT->getPointeeType()->castAs<FunctionType>(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 6997 | |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6998 | S += '<'; |
| 6999 | // Block return type |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 7000 | getObjCEncodingForTypeImpl(FT->getReturnType(), S, |
| 7001 | Options.forComponentType(), FD, NotEncodedT); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 7002 | // Block self |
| 7003 | S += "@?"; |
| 7004 | // Block parameters |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7005 | if (const auto *FPT = dyn_cast<FunctionProtoType>(FT)) { |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 7006 | for (const auto &I : FPT->param_types()) |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 7007 | getObjCEncodingForTypeImpl(I, S, Options.forComponentType(), FD, |
| 7008 | NotEncodedT); |
Bob Wilson | 5f4e3a7 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 7009 | } |
| 7010 | S += '>'; |
| 7011 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 7012 | return; |
| 7013 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7014 | |
Fariborz Jahanian | 33fa962 | 2014-01-28 20:41:15 +0000 | [diff] [blame] | 7015 | case Type::ObjCObject: { |
| 7016 | // hack to match legacy encoding of *id and *Class |
| 7017 | QualType Ty = getObjCObjectPointerType(CT); |
| 7018 | if (Ty->isObjCIdType()) { |
| 7019 | S += "{objc_object=}"; |
| 7020 | return; |
| 7021 | } |
| 7022 | else if (Ty->isObjCClassType()) { |
| 7023 | S += "{objc_class=}"; |
| 7024 | return; |
| 7025 | } |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 7026 | // TODO: Double check to make sure this intentionally falls through. |
Galina Kistanova | f87496d | 2017-06-03 06:31:42 +0000 | [diff] [blame] | 7027 | LLVM_FALLTHROUGH; |
Fariborz Jahanian | 33fa962 | 2014-01-28 20:41:15 +0000 | [diff] [blame] | 7028 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7029 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 7030 | case Type::ObjCInterface: { |
| 7031 | // Ignore protocol qualifiers when mangling at this level. |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 7032 | // @encode(class_name) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 7033 | ObjCInterfaceDecl *OI = T->castAs<ObjCObjectType>()->getInterface(); |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 7034 | S += '{'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 7035 | S += OI->getObjCRuntimeNameAsString(); |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 7036 | if (Options.ExpandStructures()) { |
Akira Hatanaka | fd0fb20 | 2016-08-17 19:42:22 +0000 | [diff] [blame] | 7037 | S += '='; |
| 7038 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
| 7039 | DeepCollectObjCIvars(OI, true, Ivars); |
| 7040 | for (unsigned i = 0, e = Ivars.size(); i != e; ++i) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 7041 | const FieldDecl *Field = Ivars[i]; |
Akira Hatanaka | fd0fb20 | 2016-08-17 19:42:22 +0000 | [diff] [blame] | 7042 | if (Field->isBitField()) |
Nico Weber | df12933 | 2019-05-10 13:56:56 +0000 | [diff] [blame] | 7043 | getObjCEncodingForTypeImpl(Field->getType(), S, |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 7044 | ObjCEncOptions().setExpandStructures(), |
| 7045 | Field); |
Akira Hatanaka | f3c89b1 | 2019-05-29 21:23:30 +0000 | [diff] [blame] | 7046 | else |
| 7047 | getObjCEncodingForTypeImpl(Field->getType(), S, |
| 7048 | ObjCEncOptions().setExpandStructures(), FD, |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 7049 | NotEncodedT); |
Akira Hatanaka | fd0fb20 | 2016-08-17 19:42:22 +0000 | [diff] [blame] | 7050 | } |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 7051 | } |
| 7052 | S += '}'; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 7053 | return; |
Fariborz Jahanian | 1d35f12 | 2008-12-19 23:34:38 +0000 | [diff] [blame] | 7054 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7055 | |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 7056 | case Type::ObjCObjectPointer: { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7057 | const auto *OPT = T->castAs<ObjCObjectPointerType>(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7058 | if (OPT->isObjCIdType()) { |
| 7059 | S += '@'; |
| 7060 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 7061 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7062 | |
Steve Naroff | f0c8611 | 2009-10-28 22:03:49 +0000 | [diff] [blame] | 7063 | if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) { |
| 7064 | // FIXME: Consider if we need to output qualifiers for 'Class<p>'. |
Nico Weber | df12933 | 2019-05-10 13:56:56 +0000 | [diff] [blame] | 7065 | // Since this is a binary compatibility issue, need to consult with |
| 7066 | // runtime folks. Fortunately, this is a *very* obscure construct. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7067 | S += '#'; |
| 7068 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 7069 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7070 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 7071 | if (OPT->isObjCQualifiedIdType()) { |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 7072 | getObjCEncodingForTypeImpl( |
| 7073 | getObjCIdType(), S, |
| 7074 | Options.keepingOnly(ObjCEncOptions() |
| 7075 | .setExpandPointedToStructures() |
| 7076 | .setExpandStructures()), |
| 7077 | FD); |
| 7078 | if (FD || Options.EncodingProperty() || Options.EncodeClassNames()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7079 | // Note that we do extended encoding of protocol qualifer list |
| 7080 | // Only when doing ivar or property encoding. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7081 | S += '"'; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7082 | for (const auto *I : OPT->quals()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7083 | S += '<'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 7084 | S += I->getObjCRuntimeNameAsString(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7085 | S += '>'; |
| 7086 | } |
| 7087 | S += '"'; |
| 7088 | } |
| 7089 | return; |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 7090 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7091 | |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 7092 | S += '@'; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7093 | if (OPT->getInterfaceDecl() && |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 7094 | (FD || Options.EncodingProperty() || Options.EncodeClassNames())) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 7095 | S += '"'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 7096 | S += OPT->getInterfaceDecl()->getObjCRuntimeNameAsString(); |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 7097 | for (const auto *I : OPT->quals()) { |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 7098 | S += '<'; |
Douglas Gregor | b32684e | 2015-06-16 21:04:55 +0000 | [diff] [blame] | 7099 | S += I->getObjCRuntimeNameAsString(); |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 7100 | S += '>'; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7101 | } |
Chris Lattner | e7cabb9 | 2009-07-13 00:10:46 +0000 | [diff] [blame] | 7102 | S += '"'; |
| 7103 | } |
| 7104 | return; |
| 7105 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7106 | |
John McCall | a9e6e8d | 2010-05-17 23:56:34 +0000 | [diff] [blame] | 7107 | // gcc just blithely ignores member pointers. |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 7108 | // FIXME: we should do better than that. 'M' is available. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 7109 | case Type::MemberPointer: |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 7110 | // This matches gcc's encoding, even though technically it is insufficient. |
| 7111 | //FIXME. We should do a better job than gcc. |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 7112 | case Type::Vector: |
| 7113 | case Type::ExtVector: |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 7114 | // Until we have a coherent encoding of these three types, issue warning. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 7115 | if (NotEncodedT) |
| 7116 | *NotEncodedT = T; |
| 7117 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7118 | |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 7119 | // We could see an undeduced auto type here during error recovery. |
| 7120 | // Just ignore it. |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 7121 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 7122 | case Type::DeducedTemplateSpecialization: |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 7123 | return; |
| 7124 | |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 7125 | case Type::Pipe: |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 7126 | #define ABSTRACT_TYPE(KIND, BASE) |
| 7127 | #define TYPE(KIND, BASE) |
| 7128 | #define DEPENDENT_TYPE(KIND, BASE) \ |
| 7129 | case Type::KIND: |
| 7130 | #define NON_CANONICAL_TYPE(KIND, BASE) \ |
| 7131 | case Type::KIND: |
| 7132 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \ |
| 7133 | case Type::KIND: |
John McCall | 36b12a8 | 2019-10-02 06:35:23 +0000 | [diff] [blame] | 7134 | #include "clang/AST/TypeNodes.inc" |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 7135 | llvm_unreachable("@encode for dependent type!"); |
Fariborz Jahanian | e0587be | 2010-10-07 21:25:25 +0000 | [diff] [blame] | 7136 | } |
John McCall | 393a78d | 2012-12-20 02:45:14 +0000 | [diff] [blame] | 7137 | llvm_unreachable("bad type kind!"); |
Anders Carlsson | d849982 | 2007-10-29 05:01:08 +0000 | [diff] [blame] | 7138 | } |
| 7139 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7140 | void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl, |
| 7141 | std::string &S, |
| 7142 | const FieldDecl *FD, |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 7143 | bool includeVBases, |
| 7144 | QualType *NotEncodedT) const { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7145 | assert(RDecl && "Expected non-null RecordDecl"); |
| 7146 | assert(!RDecl->isUnion() && "Should not be called for unions"); |
Argyrios Kyrtzidis | 785705b | 2016-01-16 00:20:02 +0000 | [diff] [blame] | 7147 | if (!RDecl->getDefinition() || RDecl->getDefinition()->isInvalidDecl()) |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7148 | return; |
| 7149 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7150 | const auto *CXXRec = dyn_cast<CXXRecordDecl>(RDecl); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7151 | std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets; |
| 7152 | const ASTRecordLayout &layout = getASTRecordLayout(RDecl); |
| 7153 | |
| 7154 | if (CXXRec) { |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 7155 | for (const auto &BI : CXXRec->bases()) { |
| 7156 | if (!BI.isVirtual()) { |
| 7157 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 7158 | if (base->isEmpty()) |
| 7159 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 7160 | uint64_t offs = toBits(layout.getBaseClassOffset(base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7161 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
| 7162 | std::make_pair(offs, base)); |
| 7163 | } |
| 7164 | } |
| 7165 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7166 | |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7167 | unsigned i = 0; |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7168 | for (auto *Field : RDecl->fields()) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7169 | uint64_t offs = layout.getFieldOffset(i); |
| 7170 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 7171 | std::make_pair(offs, Field)); |
| 7172 | ++i; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7173 | } |
| 7174 | |
| 7175 | if (CXXRec && includeVBases) { |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 7176 | for (const auto &BI : CXXRec->vbases()) { |
| 7177 | CXXRecordDecl *base = BI.getType()->getAsCXXRecordDecl(); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 7178 | if (base->isEmpty()) |
| 7179 | continue; |
Benjamin Kramer | 2ef3031 | 2012-07-04 18:45:14 +0000 | [diff] [blame] | 7180 | uint64_t offs = toBits(layout.getVBaseClassOffset(base)); |
Eli Friedman | 1d24af8 | 2013-09-18 01:59:16 +0000 | [diff] [blame] | 7181 | if (offs >= uint64_t(toBits(layout.getNonVirtualSize())) && |
| 7182 | FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end()) |
Argyrios Kyrtzidis | 81c0b5c | 2011-09-26 18:14:24 +0000 | [diff] [blame] | 7183 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(), |
| 7184 | std::make_pair(offs, base)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7185 | } |
| 7186 | } |
| 7187 | |
| 7188 | CharUnits size; |
| 7189 | if (CXXRec) { |
| 7190 | size = includeVBases ? layout.getSize() : layout.getNonVirtualSize(); |
| 7191 | } else { |
| 7192 | size = layout.getSize(); |
| 7193 | } |
| 7194 | |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7195 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7196 | uint64_t CurOffs = 0; |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7197 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7198 | std::multimap<uint64_t, NamedDecl *>::iterator |
| 7199 | CurLayObj = FieldOrBaseOffsets.begin(); |
| 7200 | |
Douglas Gregor | f5d6c74 | 2012-04-27 22:30:01 +0000 | [diff] [blame] | 7201 | if (CXXRec && CXXRec->isDynamicClass() && |
| 7202 | (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7203 | if (FD) { |
| 7204 | S += "\"_vptr$"; |
| 7205 | std::string recname = CXXRec->getNameAsString(); |
| 7206 | if (recname.empty()) recname = "?"; |
| 7207 | S += recname; |
| 7208 | S += '"'; |
| 7209 | } |
| 7210 | S += "^^?"; |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7211 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7212 | CurOffs += getTypeSize(VoidPtrTy); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7213 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7214 | } |
| 7215 | |
| 7216 | if (!RDecl->hasFlexibleArrayMember()) { |
| 7217 | // Mark the end of the structure. |
| 7218 | uint64_t offs = toBits(size); |
| 7219 | FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7220 | std::make_pair(offs, nullptr)); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7221 | } |
| 7222 | |
| 7223 | for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) { |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7224 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7225 | assert(CurOffs <= CurLayObj->first); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7226 | if (CurOffs < CurLayObj->first) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7227 | uint64_t padding = CurLayObj->first - CurOffs; |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7228 | // FIXME: There doesn't seem to be a way to indicate in the encoding that |
| 7229 | // packing/alignment of members is different that normal, in which case |
| 7230 | // the encoding will be out-of-sync with the real layout. |
| 7231 | // If the runtime switches to just consider the size of types without |
| 7232 | // taking into account alignment, we could make padding explicit in the |
| 7233 | // encoding (e.g. using arrays of chars). The encoding strings would be |
| 7234 | // longer then though. |
| 7235 | CurOffs += padding; |
| 7236 | } |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7237 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7238 | |
| 7239 | NamedDecl *dcl = CurLayObj->second; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7240 | if (!dcl) |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7241 | break; // reached end of structure. |
| 7242 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7243 | if (auto *base = dyn_cast<CXXRecordDecl>(dcl)) { |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7244 | // We expand the bases without their virtual bases since those are going |
| 7245 | // in the initial structure. Note that this differs from gcc which |
| 7246 | // expands virtual bases each time one is encountered in the hierarchy, |
| 7247 | // making the encoding type bigger than it really is. |
Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 7248 | getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false, |
| 7249 | NotEncodedT); |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 7250 | assert(!base->isEmpty()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7251 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 95a76f3 | 2011-06-17 23:19:38 +0000 | [diff] [blame] | 7252 | CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7253 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7254 | } else { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7255 | const auto *field = cast<FieldDecl>(dcl); |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7256 | if (FD) { |
| 7257 | S += '"'; |
| 7258 | S += field->getNameAsString(); |
| 7259 | S += '"'; |
| 7260 | } |
| 7261 | |
| 7262 | if (field->isBitField()) { |
| 7263 | EncodeBitField(this, S, field->getType(), field); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7264 | #ifndef NDEBUG |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 7265 | CurOffs += field->getBitWidthValue(*this); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7266 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7267 | } else { |
| 7268 | QualType qt = field->getType(); |
| 7269 | getLegacyIntegralTypeEncoding(qt); |
Nico Weber | df12933 | 2019-05-10 13:56:56 +0000 | [diff] [blame] | 7270 | getObjCEncodingForTypeImpl( |
Nico Weber | 2e9591c | 2019-05-14 12:32:37 +0000 | [diff] [blame] | 7271 | qt, S, ObjCEncOptions().setExpandStructures().setIsStructField(), |
| 7272 | FD, NotEncodedT); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7273 | #ifndef NDEBUG |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7274 | CurOffs += getTypeSize(field->getType()); |
Fariborz Jahanian | f1a66de | 2014-01-07 01:02:50 +0000 | [diff] [blame] | 7275 | #endif |
Argyrios Kyrtzidis | 49b35de | 2011-05-17 00:46:38 +0000 | [diff] [blame] | 7276 | } |
| 7277 | } |
| 7278 | } |
| 7279 | } |
| 7280 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7281 | void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, |
Fariborz Jahanian | ac73ff8 | 2007-11-01 17:18:37 +0000 | [diff] [blame] | 7282 | std::string& S) const { |
| 7283 | if (QT & Decl::OBJC_TQ_In) |
| 7284 | S += 'n'; |
| 7285 | if (QT & Decl::OBJC_TQ_Inout) |
| 7286 | S += 'N'; |
| 7287 | if (QT & Decl::OBJC_TQ_Out) |
| 7288 | S += 'o'; |
| 7289 | if (QT & Decl::OBJC_TQ_Bycopy) |
| 7290 | S += 'O'; |
| 7291 | if (QT & Decl::OBJC_TQ_Byref) |
| 7292 | S += 'R'; |
| 7293 | if (QT & Decl::OBJC_TQ_Oneway) |
| 7294 | S += 'V'; |
| 7295 | } |
| 7296 | |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 7297 | TypedefDecl *ASTContext::getObjCIdDecl() const { |
| 7298 | if (!ObjCIdDecl) { |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 7299 | QualType T = getObjCObjectType(ObjCBuiltinIdTy, {}, {}); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 7300 | T = getObjCObjectPointerType(T); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7301 | ObjCIdDecl = buildImplicitTypedef(T, "id"); |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 7302 | } |
Douglas Gregor | 3ea7269 | 2011-08-12 05:46:01 +0000 | [diff] [blame] | 7303 | return ObjCIdDecl; |
Steve Naroff | 66e9f33 | 2007-10-15 14:41:52 +0000 | [diff] [blame] | 7304 | } |
| 7305 | |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 7306 | TypedefDecl *ASTContext::getObjCSelDecl() const { |
| 7307 | if (!ObjCSelDecl) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7308 | QualType T = getPointerType(ObjCBuiltinSelTy); |
| 7309 | ObjCSelDecl = buildImplicitTypedef(T, "SEL"); |
Douglas Gregor | 52e0280 | 2011-08-12 06:17:30 +0000 | [diff] [blame] | 7310 | } |
| 7311 | return ObjCSelDecl; |
Fariborz Jahanian | 4bef462 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 7312 | } |
| 7313 | |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 7314 | TypedefDecl *ASTContext::getObjCClassDecl() const { |
| 7315 | if (!ObjCClassDecl) { |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 7316 | QualType T = getObjCObjectType(ObjCBuiltinClassTy, {}, {}); |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 7317 | T = getObjCObjectPointerType(T); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7318 | ObjCClassDecl = buildImplicitTypedef(T, "Class"); |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 7319 | } |
Douglas Gregor | 0a58618 | 2011-08-12 05:59:41 +0000 | [diff] [blame] | 7320 | return ObjCClassDecl; |
Anders Carlsson | f56a7ae | 2007-10-31 02:53:19 +0000 | [diff] [blame] | 7321 | } |
| 7322 | |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 7323 | ObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const { |
| 7324 | if (!ObjCProtocolClassDecl) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7325 | ObjCProtocolClassDecl |
| 7326 | = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(), |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 7327 | SourceLocation(), |
| 7328 | &Idents.get("Protocol"), |
Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 7329 | /*typeParamList=*/nullptr, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7330 | /*PrevDecl=*/nullptr, |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7331 | SourceLocation(), true); |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 7332 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7333 | |
Douglas Gregor | d53ae83 | 2012-01-17 18:09:05 +0000 | [diff] [blame] | 7334 | return ObjCProtocolClassDecl; |
| 7335 | } |
| 7336 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7337 | //===----------------------------------------------------------------------===// |
| 7338 | // __builtin_va_list Construction Functions |
| 7339 | //===----------------------------------------------------------------------===// |
| 7340 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 7341 | static TypedefDecl *CreateCharPtrNamedVaListDecl(const ASTContext *Context, |
| 7342 | StringRef Name) { |
| 7343 | // typedef char* __builtin[_ms]_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7344 | QualType T = Context->getPointerType(Context->CharTy); |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 7345 | return Context->buildImplicitTypedef(T, Name); |
| 7346 | } |
| 7347 | |
| 7348 | static TypedefDecl *CreateMSVaListDecl(const ASTContext *Context) { |
| 7349 | return CreateCharPtrNamedVaListDecl(Context, "__builtin_ms_va_list"); |
| 7350 | } |
| 7351 | |
| 7352 | static TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 7353 | return CreateCharPtrNamedVaListDecl(Context, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7354 | } |
| 7355 | |
| 7356 | static TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) { |
| 7357 | // typedef void* __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7358 | QualType T = Context->getPointerType(Context->VoidTy); |
| 7359 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7360 | } |
| 7361 | |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7362 | static TypedefDecl * |
| 7363 | CreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7364 | // struct __va_list |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7365 | RecordDecl *VaListTagDecl = Context->buildImplicitRecord("__va_list"); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7366 | if (Context->getLangOpts().CPlusPlus) { |
| 7367 | // namespace std { struct __va_list { |
| 7368 | NamespaceDecl *NS; |
| 7369 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 7370 | Context->getTranslationUnitDecl(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7371 | /*Inline*/ false, SourceLocation(), |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7372 | SourceLocation(), &Context->Idents.get("std"), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7373 | /*PrevDecl*/ nullptr); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7374 | NS->setImplicit(); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7375 | VaListTagDecl->setDeclContext(NS); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7376 | } |
| 7377 | |
| 7378 | VaListTagDecl->startDefinition(); |
| 7379 | |
| 7380 | const size_t NumFields = 5; |
| 7381 | QualType FieldTypes[NumFields]; |
| 7382 | const char *FieldNames[NumFields]; |
| 7383 | |
| 7384 | // void *__stack; |
| 7385 | FieldTypes[0] = Context->getPointerType(Context->VoidTy); |
| 7386 | FieldNames[0] = "__stack"; |
| 7387 | |
| 7388 | // void *__gr_top; |
| 7389 | FieldTypes[1] = Context->getPointerType(Context->VoidTy); |
| 7390 | FieldNames[1] = "__gr_top"; |
| 7391 | |
| 7392 | // void *__vr_top; |
| 7393 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 7394 | FieldNames[2] = "__vr_top"; |
| 7395 | |
| 7396 | // int __gr_offs; |
| 7397 | FieldTypes[3] = Context->IntTy; |
| 7398 | FieldNames[3] = "__gr_offs"; |
| 7399 | |
| 7400 | // int __vr_offs; |
| 7401 | FieldTypes[4] = Context->IntTy; |
| 7402 | FieldNames[4] = "__vr_offs"; |
| 7403 | |
| 7404 | // Create fields |
| 7405 | for (unsigned i = 0; i < NumFields; ++i) { |
| 7406 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 7407 | VaListTagDecl, |
| 7408 | SourceLocation(), |
| 7409 | SourceLocation(), |
| 7410 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7411 | FieldTypes[i], /*TInfo=*/nullptr, |
| 7412 | /*BitWidth=*/nullptr, |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7413 | /*Mutable=*/false, |
| 7414 | ICIS_NoInit); |
| 7415 | Field->setAccess(AS_public); |
| 7416 | VaListTagDecl->addDecl(Field); |
| 7417 | } |
| 7418 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7419 | Context->VaListTagDecl = VaListTagDecl; |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7420 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7421 | |
| 7422 | // } __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7423 | return Context->buildImplicitTypedef(VaListTagType, "__builtin_va_list"); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7424 | } |
| 7425 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7426 | static TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) { |
| 7427 | // typedef struct __va_list_tag { |
| 7428 | RecordDecl *VaListTagDecl; |
| 7429 | |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7430 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7431 | VaListTagDecl->startDefinition(); |
| 7432 | |
| 7433 | const size_t NumFields = 5; |
| 7434 | QualType FieldTypes[NumFields]; |
| 7435 | const char *FieldNames[NumFields]; |
| 7436 | |
| 7437 | // unsigned char gpr; |
| 7438 | FieldTypes[0] = Context->UnsignedCharTy; |
| 7439 | FieldNames[0] = "gpr"; |
| 7440 | |
| 7441 | // unsigned char fpr; |
| 7442 | FieldTypes[1] = Context->UnsignedCharTy; |
| 7443 | FieldNames[1] = "fpr"; |
| 7444 | |
| 7445 | // unsigned short reserved; |
| 7446 | FieldTypes[2] = Context->UnsignedShortTy; |
| 7447 | FieldNames[2] = "reserved"; |
| 7448 | |
| 7449 | // void* overflow_arg_area; |
| 7450 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 7451 | FieldNames[3] = "overflow_arg_area"; |
| 7452 | |
| 7453 | // void* reg_save_area; |
| 7454 | FieldTypes[4] = Context->getPointerType(Context->VoidTy); |
| 7455 | FieldNames[4] = "reg_save_area"; |
| 7456 | |
| 7457 | // Create fields |
| 7458 | for (unsigned i = 0; i < NumFields; ++i) { |
| 7459 | FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl, |
| 7460 | SourceLocation(), |
| 7461 | SourceLocation(), |
| 7462 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7463 | FieldTypes[i], /*TInfo=*/nullptr, |
| 7464 | /*BitWidth=*/nullptr, |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7465 | /*Mutable=*/false, |
| 7466 | ICIS_NoInit); |
| 7467 | Field->setAccess(AS_public); |
| 7468 | VaListTagDecl->addDecl(Field); |
| 7469 | } |
| 7470 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7471 | Context->VaListTagDecl = VaListTagDecl; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7472 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 7473 | |
| 7474 | // } __va_list_tag; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7475 | TypedefDecl *VaListTagTypedefDecl = |
| 7476 | Context->buildImplicitTypedef(VaListTagType, "__va_list_tag"); |
| 7477 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7478 | QualType VaListTagTypedefType = |
| 7479 | Context->getTypedefType(VaListTagTypedefDecl); |
| 7480 | |
| 7481 | // typedef __va_list_tag __builtin_va_list[1]; |
| 7482 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
| 7483 | QualType VaListTagArrayType |
| 7484 | = Context->getConstantArrayType(VaListTagTypedefType, |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 7485 | Size, nullptr, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7486 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7487 | } |
| 7488 | |
| 7489 | static TypedefDecl * |
| 7490 | CreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) { |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7491 | // struct __va_list_tag { |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7492 | RecordDecl *VaListTagDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7493 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7494 | VaListTagDecl->startDefinition(); |
| 7495 | |
| 7496 | const size_t NumFields = 4; |
| 7497 | QualType FieldTypes[NumFields]; |
| 7498 | const char *FieldNames[NumFields]; |
| 7499 | |
| 7500 | // unsigned gp_offset; |
| 7501 | FieldTypes[0] = Context->UnsignedIntTy; |
| 7502 | FieldNames[0] = "gp_offset"; |
| 7503 | |
| 7504 | // unsigned fp_offset; |
| 7505 | FieldTypes[1] = Context->UnsignedIntTy; |
| 7506 | FieldNames[1] = "fp_offset"; |
| 7507 | |
| 7508 | // void* overflow_arg_area; |
| 7509 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 7510 | FieldNames[2] = "overflow_arg_area"; |
| 7511 | |
| 7512 | // void* reg_save_area; |
| 7513 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 7514 | FieldNames[3] = "reg_save_area"; |
| 7515 | |
| 7516 | // Create fields |
| 7517 | for (unsigned i = 0; i < NumFields; ++i) { |
| 7518 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 7519 | VaListTagDecl, |
| 7520 | SourceLocation(), |
| 7521 | SourceLocation(), |
| 7522 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7523 | FieldTypes[i], /*TInfo=*/nullptr, |
| 7524 | /*BitWidth=*/nullptr, |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7525 | /*Mutable=*/false, |
| 7526 | ICIS_NoInit); |
| 7527 | Field->setAccess(AS_public); |
| 7528 | VaListTagDecl->addDecl(Field); |
| 7529 | } |
| 7530 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7531 | Context->VaListTagDecl = VaListTagDecl; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7532 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
| 7533 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7534 | // }; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7535 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7536 | // typedef struct __va_list_tag __builtin_va_list[1]; |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7537 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 7538 | QualType VaListTagArrayType = Context->getConstantArrayType( |
| 7539 | VaListTagType, Size, nullptr, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7540 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7541 | } |
| 7542 | |
| 7543 | static TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) { |
| 7544 | // typedef int __builtin_va_list[4]; |
| 7545 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4); |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 7546 | QualType IntArrayType = Context->getConstantArrayType( |
| 7547 | Context->IntTy, Size, nullptr, ArrayType::Normal, 0); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7548 | return Context->buildImplicitTypedef(IntArrayType, "__builtin_va_list"); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7549 | } |
| 7550 | |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7551 | static TypedefDecl * |
| 7552 | CreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7553 | // struct __va_list |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7554 | RecordDecl *VaListDecl = Context->buildImplicitRecord("__va_list"); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7555 | if (Context->getLangOpts().CPlusPlus) { |
| 7556 | // namespace std { struct __va_list { |
| 7557 | NamespaceDecl *NS; |
| 7558 | NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context), |
| 7559 | Context->getTranslationUnitDecl(), |
| 7560 | /*Inline*/false, SourceLocation(), |
| 7561 | SourceLocation(), &Context->Idents.get("std"), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7562 | /*PrevDecl*/ nullptr); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7563 | NS->setImplicit(); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7564 | VaListDecl->setDeclContext(NS); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7565 | } |
| 7566 | |
| 7567 | VaListDecl->startDefinition(); |
| 7568 | |
| 7569 | // void * __ap; |
| 7570 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 7571 | VaListDecl, |
| 7572 | SourceLocation(), |
| 7573 | SourceLocation(), |
| 7574 | &Context->Idents.get("__ap"), |
| 7575 | Context->getPointerType(Context->VoidTy), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7576 | /*TInfo=*/nullptr, |
| 7577 | /*BitWidth=*/nullptr, |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7578 | /*Mutable=*/false, |
| 7579 | ICIS_NoInit); |
| 7580 | Field->setAccess(AS_public); |
| 7581 | VaListDecl->addDecl(Field); |
| 7582 | |
| 7583 | // }; |
| 7584 | VaListDecl->completeDefinition(); |
Oleg Ranevskyy | b88d247 | 2016-03-30 21:30:30 +0000 | [diff] [blame] | 7585 | Context->VaListTagDecl = VaListDecl; |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7586 | |
| 7587 | // typedef struct __va_list __builtin_va_list; |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7588 | QualType T = Context->getRecordType(VaListDecl); |
| 7589 | return Context->buildImplicitTypedef(T, "__builtin_va_list"); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7590 | } |
| 7591 | |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7592 | static TypedefDecl * |
| 7593 | CreateSystemZBuiltinVaListDecl(const ASTContext *Context) { |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7594 | // struct __va_list_tag { |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7595 | RecordDecl *VaListTagDecl; |
Alp Toker | 2dea15b | 2013-12-17 01:22:38 +0000 | [diff] [blame] | 7596 | VaListTagDecl = Context->buildImplicitRecord("__va_list_tag"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7597 | VaListTagDecl->startDefinition(); |
| 7598 | |
| 7599 | const size_t NumFields = 4; |
| 7600 | QualType FieldTypes[NumFields]; |
| 7601 | const char *FieldNames[NumFields]; |
| 7602 | |
| 7603 | // long __gpr; |
| 7604 | FieldTypes[0] = Context->LongTy; |
| 7605 | FieldNames[0] = "__gpr"; |
| 7606 | |
| 7607 | // long __fpr; |
| 7608 | FieldTypes[1] = Context->LongTy; |
| 7609 | FieldNames[1] = "__fpr"; |
| 7610 | |
| 7611 | // void *__overflow_arg_area; |
| 7612 | FieldTypes[2] = Context->getPointerType(Context->VoidTy); |
| 7613 | FieldNames[2] = "__overflow_arg_area"; |
| 7614 | |
| 7615 | // void *__reg_save_area; |
| 7616 | FieldTypes[3] = Context->getPointerType(Context->VoidTy); |
| 7617 | FieldNames[3] = "__reg_save_area"; |
| 7618 | |
| 7619 | // Create fields |
| 7620 | for (unsigned i = 0; i < NumFields; ++i) { |
| 7621 | FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context), |
| 7622 | VaListTagDecl, |
| 7623 | SourceLocation(), |
| 7624 | SourceLocation(), |
| 7625 | &Context->Idents.get(FieldNames[i]), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7626 | FieldTypes[i], /*TInfo=*/nullptr, |
| 7627 | /*BitWidth=*/nullptr, |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7628 | /*Mutable=*/false, |
| 7629 | ICIS_NoInit); |
| 7630 | Field->setAccess(AS_public); |
| 7631 | VaListTagDecl->addDecl(Field); |
| 7632 | } |
| 7633 | VaListTagDecl->completeDefinition(); |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7634 | Context->VaListTagDecl = VaListTagDecl; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7635 | QualType VaListTagType = Context->getRecordType(VaListTagDecl); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7636 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7637 | // }; |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7638 | |
| 7639 | // typedef __va_list_tag __builtin_va_list[1]; |
| 7640 | llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1); |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 7641 | QualType VaListTagArrayType = Context->getConstantArrayType( |
| 7642 | VaListTagType, Size, nullptr, ArrayType::Normal, 0); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7643 | |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7644 | return Context->buildImplicitTypedef(VaListTagArrayType, "__builtin_va_list"); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7645 | } |
| 7646 | |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7647 | static TypedefDecl *CreateVaListDecl(const ASTContext *Context, |
| 7648 | TargetInfo::BuiltinVaListKind Kind) { |
| 7649 | switch (Kind) { |
| 7650 | case TargetInfo::CharPtrBuiltinVaList: |
| 7651 | return CreateCharPtrBuiltinVaListDecl(Context); |
| 7652 | case TargetInfo::VoidPtrBuiltinVaList: |
| 7653 | return CreateVoidPtrBuiltinVaListDecl(Context); |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 7654 | case TargetInfo::AArch64ABIBuiltinVaList: |
| 7655 | return CreateAArch64ABIBuiltinVaListDecl(Context); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7656 | case TargetInfo::PowerABIBuiltinVaList: |
| 7657 | return CreatePowerABIBuiltinVaListDecl(Context); |
| 7658 | case TargetInfo::X86_64ABIBuiltinVaList: |
| 7659 | return CreateX86_64ABIBuiltinVaListDecl(Context); |
| 7660 | case TargetInfo::PNaClABIBuiltinVaList: |
| 7661 | return CreatePNaClABIBuiltinVaListDecl(Context); |
Logan Chien | 57086ce | 2012-10-10 06:56:20 +0000 | [diff] [blame] | 7662 | case TargetInfo::AAPCSABIBuiltinVaList: |
| 7663 | return CreateAAPCSABIBuiltinVaListDecl(Context); |
Ulrich Weigand | 4744507 | 2013-05-06 16:26:41 +0000 | [diff] [blame] | 7664 | case TargetInfo::SystemZBuiltinVaList: |
| 7665 | return CreateSystemZBuiltinVaListDecl(Context); |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7666 | } |
| 7667 | |
| 7668 | llvm_unreachable("Unhandled __builtin_va_list type kind"); |
| 7669 | } |
| 7670 | |
| 7671 | TypedefDecl *ASTContext::getBuiltinVaListDecl() const { |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7672 | if (!BuiltinVaListDecl) { |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7673 | BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind()); |
Alp Toker | 56b5cc9 | 2013-12-15 10:36:26 +0000 | [diff] [blame] | 7674 | assert(BuiltinVaListDecl->isImplicit()); |
| 7675 | } |
Meador Inge | 5d3fb22 | 2012-06-16 03:34:49 +0000 | [diff] [blame] | 7676 | |
| 7677 | return BuiltinVaListDecl; |
| 7678 | } |
| 7679 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7680 | Decl *ASTContext::getVaListTagDecl() const { |
| 7681 | // Force the creation of VaListTagDecl by building the __builtin_va_list |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 7682 | // declaration. |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7683 | if (!VaListTagDecl) |
| 7684 | (void)getBuiltinVaListDecl(); |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 7685 | |
Richard Smith | 9b88a4c | 2015-07-27 05:40:23 +0000 | [diff] [blame] | 7686 | return VaListTagDecl; |
Meador Inge | cfb6090 | 2012-07-01 15:57:25 +0000 | [diff] [blame] | 7687 | } |
| 7688 | |
Charles Davis | c7d5c94 | 2015-09-17 20:55:33 +0000 | [diff] [blame] | 7689 | TypedefDecl *ASTContext::getBuiltinMSVaListDecl() const { |
| 7690 | if (!BuiltinMSVaListDecl) |
| 7691 | BuiltinMSVaListDecl = CreateMSVaListDecl(this); |
| 7692 | |
| 7693 | return BuiltinMSVaListDecl; |
| 7694 | } |
| 7695 | |
Erich Keane | 41af971 | 2018-04-16 21:30:08 +0000 | [diff] [blame] | 7696 | bool ASTContext::canBuiltinBeRedeclared(const FunctionDecl *FD) const { |
| 7697 | return BuiltinInfo.canBeRedeclared(FD->getBuiltinID()); |
| 7698 | } |
| 7699 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 7700 | void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7701 | assert(ObjCConstantStringType.isNull() && |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 7702 | "'NSConstantString' type already set!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7703 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 7704 | ObjCConstantStringType = getObjCInterfaceType(Decl); |
Steve Naroff | f73b784 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 7705 | } |
| 7706 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7707 | /// Retrieve the template name that corresponds to a non-empty |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7708 | /// lookup. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7709 | TemplateName |
| 7710 | ASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin, |
| 7711 | UnresolvedSetIterator End) const { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7712 | unsigned size = End - Begin; |
| 7713 | assert(size > 1 && "set is not overloaded!"); |
| 7714 | |
| 7715 | void *memory = Allocate(sizeof(OverloadedTemplateStorage) + |
| 7716 | size * sizeof(FunctionTemplateDecl*)); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7717 | auto *OT = new (memory) OverloadedTemplateStorage(size); |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7718 | |
| 7719 | NamedDecl **Storage = OT->getStorage(); |
John McCall | ad37125 | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 7720 | for (UnresolvedSetIterator I = Begin; I != End; ++I) { |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7721 | NamedDecl *D = *I; |
| 7722 | assert(isa<FunctionTemplateDecl>(D) || |
Richard Smith | ef53a3e | 2018-06-06 16:36:56 +0000 | [diff] [blame] | 7723 | isa<UnresolvedUsingValueDecl>(D) || |
John McCall | d28ae27 | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 7724 | (isa<UsingShadowDecl>(D) && |
| 7725 | isa<FunctionTemplateDecl>(D->getUnderlyingDecl()))); |
| 7726 | *Storage++ = D; |
| 7727 | } |
| 7728 | |
| 7729 | return TemplateName(OT); |
| 7730 | } |
| 7731 | |
Richard Smith | b23c5e8 | 2019-05-09 03:31:27 +0000 | [diff] [blame] | 7732 | /// Retrieve a template name representing an unqualified-id that has been |
| 7733 | /// assumed to name a template for ADL purposes. |
| 7734 | TemplateName ASTContext::getAssumedTemplateName(DeclarationName Name) const { |
| 7735 | auto *OT = new (*this) AssumedTemplateStorage(Name); |
| 7736 | return TemplateName(OT); |
| 7737 | } |
| 7738 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7739 | /// Retrieve the template name that represents a qualified |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7740 | /// template name such as \c std::vector. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7741 | TemplateName |
| 7742 | ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS, |
| 7743 | bool TemplateKeyword, |
| 7744 | TemplateDecl *Template) const { |
Douglas Gregor | e29139c | 2011-03-03 17:04:51 +0000 | [diff] [blame] | 7745 | assert(NNS && "Missing nested-name-specifier in qualified template name"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7746 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7747 | // FIXME: Canonicalization? |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7748 | llvm::FoldingSetNodeID ID; |
| 7749 | QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template); |
| 7750 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7751 | void *InsertPos = nullptr; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7752 | QualifiedTemplateName *QTN = |
| 7753 | QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7754 | if (!QTN) { |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7755 | QTN = new (*this, alignof(QualifiedTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7756 | QualifiedTemplateName(NNS, TemplateKeyword, Template); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7757 | QualifiedTemplateNames.InsertNode(QTN, InsertPos); |
| 7758 | } |
| 7759 | |
| 7760 | return TemplateName(QTN); |
| 7761 | } |
| 7762 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7763 | /// Retrieve the template name that represents a dependent |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7764 | /// template name such as \c MetaFun::template apply. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7765 | TemplateName |
| 7766 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
| 7767 | const IdentifierInfo *Name) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7768 | assert((!NNS || NNS->isDependent()) && |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 7769 | "Nested name specifier must be dependent"); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7770 | |
| 7771 | llvm::FoldingSetNodeID ID; |
| 7772 | DependentTemplateName::Profile(ID, NNS, Name); |
| 7773 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7774 | void *InsertPos = nullptr; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7775 | DependentTemplateName *QTN = |
| 7776 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7777 | |
| 7778 | if (QTN) |
| 7779 | return TemplateName(QTN); |
| 7780 | |
| 7781 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 7782 | if (CanonNNS == NNS) { |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7783 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7784 | DependentTemplateName(NNS, Name); |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7785 | } else { |
| 7786 | TemplateName Canon = getDependentTemplateName(CanonNNS, Name); |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7787 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7788 | DependentTemplateName(NNS, Name, Canon); |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7789 | DependentTemplateName *CheckQTN = |
| 7790 | DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7791 | assert(!CheckQTN && "Dependent type name canonicalization broken"); |
| 7792 | (void)CheckQTN; |
Douglas Gregor | dc572a3 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 7793 | } |
| 7794 | |
| 7795 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 7796 | return TemplateName(QTN); |
| 7797 | } |
| 7798 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 7799 | /// Retrieve the template name that represents a dependent |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7800 | /// template name such as \c MetaFun::template operator+. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7801 | TemplateName |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7802 | ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS, |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7803 | OverloadedOperatorKind Operator) const { |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7804 | assert((!NNS || NNS->isDependent()) && |
| 7805 | "Nested name specifier must be dependent"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7806 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7807 | llvm::FoldingSetNodeID ID; |
| 7808 | DependentTemplateName::Profile(ID, NNS, Operator); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7809 | |
| 7810 | void *InsertPos = nullptr; |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7811 | DependentTemplateName *QTN |
| 7812 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7813 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7814 | if (QTN) |
| 7815 | return TemplateName(QTN); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7816 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7817 | NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS); |
| 7818 | if (CanonNNS == NNS) { |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7819 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7820 | DependentTemplateName(NNS, Operator); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7821 | } else { |
| 7822 | TemplateName Canon = getDependentTemplateName(CanonNNS, Operator); |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7823 | QTN = new (*this, alignof(DependentTemplateName)) |
Richard Smith | a5e6976 | 2012-08-16 01:19:31 +0000 | [diff] [blame] | 7824 | DependentTemplateName(NNS, Operator, Canon); |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 7825 | |
Douglas Gregor | c42075a | 2010-02-04 18:10:26 +0000 | [diff] [blame] | 7826 | DependentTemplateName *CheckQTN |
| 7827 | = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos); |
| 7828 | assert(!CheckQTN && "Dependent template name canonicalization broken"); |
| 7829 | (void)CheckQTN; |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7830 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7831 | |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 7832 | DependentTemplateNames.InsertNode(QTN, InsertPos); |
| 7833 | return TemplateName(QTN); |
| 7834 | } |
| 7835 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7836 | TemplateName |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 7837 | ASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, |
| 7838 | TemplateName replacement) const { |
| 7839 | llvm::FoldingSetNodeID ID; |
| 7840 | SubstTemplateTemplateParmStorage::Profile(ID, param, replacement); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7841 | |
| 7842 | void *insertPos = nullptr; |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 7843 | SubstTemplateTemplateParmStorage *subst |
| 7844 | = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7845 | |
John McCall | d9dfe3a | 2011-06-30 08:33:18 +0000 | [diff] [blame] | 7846 | if (!subst) { |
| 7847 | subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement); |
| 7848 | SubstTemplateTemplateParms.InsertNode(subst, insertPos); |
| 7849 | } |
| 7850 | |
| 7851 | return TemplateName(subst); |
| 7852 | } |
| 7853 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7854 | TemplateName |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7855 | ASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, |
| 7856 | const TemplateArgument &ArgPack) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7857 | auto &Self = const_cast<ASTContext &>(*this); |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7858 | llvm::FoldingSetNodeID ID; |
| 7859 | SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 7860 | |
| 7861 | void *InsertPos = nullptr; |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7862 | SubstTemplateTemplateParmPackStorage *Subst |
| 7863 | = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7864 | |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7865 | if (!Subst) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 7866 | Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param, |
Douglas Gregor | 5590be0 | 2011-01-15 06:45:20 +0000 | [diff] [blame] | 7867 | ArgPack.pack_size(), |
| 7868 | ArgPack.pack_begin()); |
| 7869 | SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos); |
| 7870 | } |
| 7871 | |
| 7872 | return TemplateName(Subst); |
| 7873 | } |
| 7874 | |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7875 | /// getFromTargetType - Given one of the integer types provided by |
Douglas Gregor | ab13857 | 2008-11-03 15:57:00 +0000 | [diff] [blame] | 7876 | /// TargetInfo, produce the corresponding type. The unsigned @p Type |
| 7877 | /// is actually a value of type @c TargetInfo::IntType. |
John McCall | 48f2d58 | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 7878 | CanQualType ASTContext::getFromTargetType(unsigned Type) const { |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7879 | switch (Type) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7880 | case TargetInfo::NoInt: return {}; |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 7881 | case TargetInfo::SignedChar: return SignedCharTy; |
| 7882 | case TargetInfo::UnsignedChar: return UnsignedCharTy; |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7883 | case TargetInfo::SignedShort: return ShortTy; |
| 7884 | case TargetInfo::UnsignedShort: return UnsignedShortTy; |
| 7885 | case TargetInfo::SignedInt: return IntTy; |
| 7886 | case TargetInfo::UnsignedInt: return UnsignedIntTy; |
| 7887 | case TargetInfo::SignedLong: return LongTy; |
| 7888 | case TargetInfo::UnsignedLong: return UnsignedLongTy; |
| 7889 | case TargetInfo::SignedLongLong: return LongLongTy; |
| 7890 | case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy; |
| 7891 | } |
| 7892 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 7893 | llvm_unreachable("Unhandled TargetInfo::IntType value"); |
Douglas Gregor | 8af6e6d | 2008-11-03 14:12:49 +0000 | [diff] [blame] | 7894 | } |
Ted Kremenek | 77c51b2 | 2008-07-24 23:58:27 +0000 | [diff] [blame] | 7895 | |
| 7896 | //===----------------------------------------------------------------------===// |
| 7897 | // Type Predicates. |
| 7898 | //===----------------------------------------------------------------------===// |
| 7899 | |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 7900 | /// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's |
| 7901 | /// garbage collection attribute. |
| 7902 | /// |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7903 | Qualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7904 | if (getLangOpts().getGC() == LangOptions::NonGC) |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7905 | return Qualifiers::GCNone; |
| 7906 | |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 7907 | assert(getLangOpts().ObjC); |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7908 | Qualifiers::GC GCAttrs = Ty.getObjCGCAttr(); |
| 7909 | |
| 7910 | // Default behaviour under objective-C's gc is for ObjC pointers |
| 7911 | // (or pointers to them) be treated as though they were declared |
| 7912 | // as __strong. |
| 7913 | if (GCAttrs == Qualifiers::GCNone) { |
| 7914 | if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType()) |
| 7915 | return Qualifiers::Strong; |
| 7916 | else if (Ty->isPointerType()) |
Simon Pilgrim | eed4b12 | 2019-10-02 11:48:06 +0000 | [diff] [blame] | 7917 | return getObjCGCAttrKind(Ty->castAs<PointerType>()->getPointeeType()); |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7918 | } else { |
| 7919 | // It's not valid to set GC attributes on anything that isn't a |
| 7920 | // pointer. |
| 7921 | #ifndef NDEBUG |
| 7922 | QualType CT = Ty->getCanonicalTypeInternal(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 7923 | while (const auto *AT = dyn_cast<ArrayType>(CT)) |
John McCall | 553d45a | 2011-01-12 00:34:59 +0000 | [diff] [blame] | 7924 | CT = AT->getElementType(); |
| 7925 | assert(CT->isAnyPointerType() || CT->isBlockPointerType()); |
| 7926 | #endif |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 7927 | } |
Chris Lattner | d60183d | 2009-02-18 22:53:11 +0000 | [diff] [blame] | 7928 | return GCAttrs; |
Fariborz Jahanian | 9620769 | 2009-02-18 21:49:28 +0000 | [diff] [blame] | 7929 | } |
| 7930 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7931 | //===----------------------------------------------------------------------===// |
| 7932 | // Type Compatibility Testing |
| 7933 | //===----------------------------------------------------------------------===// |
Chris Lattner | b338a6b | 2007-11-01 05:03:41 +0000 | [diff] [blame] | 7934 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7935 | /// areCompatVectorTypes - Return true if the two specified vector types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7936 | /// compatible. |
| 7937 | static bool areCompatVectorTypes(const VectorType *LHS, |
| 7938 | const VectorType *RHS) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 7939 | assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified()); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7940 | return LHS->getElementType() == RHS->getElementType() && |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 7941 | LHS->getNumElements() == RHS->getNumElements(); |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 7942 | } |
| 7943 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7944 | bool ASTContext::areCompatibleVectorTypes(QualType FirstVec, |
| 7945 | QualType SecondVec) { |
| 7946 | assert(FirstVec->isVectorType() && "FirstVec should be a vector type"); |
| 7947 | assert(SecondVec->isVectorType() && "SecondVec should be a vector type"); |
| 7948 | |
| 7949 | if (hasSameUnqualifiedType(FirstVec, SecondVec)) |
| 7950 | return true; |
| 7951 | |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 7952 | // Treat Neon vector types and most AltiVec vector types as if they are the |
| 7953 | // equivalent GCC vector types. |
Simon Pilgrim | eed4b12 | 2019-10-02 11:48:06 +0000 | [diff] [blame] | 7954 | const auto *First = FirstVec->castAs<VectorType>(); |
| 7955 | const auto *Second = SecondVec->castAs<VectorType>(); |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 7956 | if (First->getNumElements() == Second->getNumElements() && |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7957 | hasSameType(First->getElementType(), Second->getElementType()) && |
Bob Wilson | e6aeebb | 2010-11-12 17:24:54 +0000 | [diff] [blame] | 7958 | First->getVectorKind() != VectorType::AltiVecPixel && |
| 7959 | First->getVectorKind() != VectorType::AltiVecBool && |
| 7960 | Second->getVectorKind() != VectorType::AltiVecPixel && |
| 7961 | Second->getVectorKind() != VectorType::AltiVecBool) |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7962 | return true; |
| 7963 | |
| 7964 | return false; |
| 7965 | } |
| 7966 | |
Akira Hatanaka | 3f2c991 | 2019-09-07 00:34:47 +0000 | [diff] [blame] | 7967 | bool ASTContext::hasDirectOwnershipQualifier(QualType Ty) const { |
| 7968 | while (true) { |
| 7969 | // __strong id |
| 7970 | if (const AttributedType *Attr = dyn_cast<AttributedType>(Ty)) { |
| 7971 | if (Attr->getAttrKind() == attr::ObjCOwnership) |
| 7972 | return true; |
| 7973 | |
| 7974 | Ty = Attr->getModifiedType(); |
| 7975 | |
| 7976 | // X *__strong (...) |
| 7977 | } else if (const ParenType *Paren = dyn_cast<ParenType>(Ty)) { |
| 7978 | Ty = Paren->getInnerType(); |
| 7979 | |
| 7980 | // We do not want to look through typedefs, typeof(expr), |
| 7981 | // typeof(type), or any other way that the type is somehow |
| 7982 | // abstracted. |
| 7983 | } else { |
| 7984 | return false; |
| 7985 | } |
| 7986 | } |
| 7987 | } |
| 7988 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7989 | //===----------------------------------------------------------------------===// |
| 7990 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 7991 | //===----------------------------------------------------------------------===// |
| 7992 | |
| 7993 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 7994 | /// inheritance hierarchy of 'rProto'. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 7995 | bool |
| 7996 | ASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 7997 | ObjCProtocolDecl *rProto) const { |
Douglas Gregor | 33b2429 | 2012-01-01 18:09:12 +0000 | [diff] [blame] | 7998 | if (declaresSameEntity(lProto, rProto)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 7999 | return true; |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 8000 | for (auto *PI : rProto->protocols()) |
| 8001 | if (ProtocolCompatibleWithProtocol(lProto, PI)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8002 | return true; |
| 8003 | return false; |
| 8004 | } |
| 8005 | |
Dmitri Gribenko | 4364fcf | 2012-08-28 02:49:14 +0000 | [diff] [blame] | 8006 | /// ObjCQualifiedClassTypesAreCompatible - compare Class<pr,...> and |
| 8007 | /// Class<pr1, ...>. |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8008 | bool ASTContext::ObjCQualifiedClassTypesAreCompatible( |
| 8009 | const ObjCObjectPointerType *lhs, const ObjCObjectPointerType *rhs) { |
| 8010 | for (auto *lhsProto : lhs->quals()) { |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 8011 | bool match = false; |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8012 | for (auto *rhsProto : rhs->quals()) { |
Fariborz Jahanian | 3c7ebc3 | 2010-07-19 22:02:22 +0000 | [diff] [blame] | 8013 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) { |
| 8014 | match = true; |
| 8015 | break; |
| 8016 | } |
| 8017 | } |
| 8018 | if (!match) |
| 8019 | return false; |
| 8020 | } |
| 8021 | return true; |
| 8022 | } |
| 8023 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8024 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 8025 | /// ObjCQualifiedIDType. |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8026 | bool ASTContext::ObjCQualifiedIdTypesAreCompatible( |
| 8027 | const ObjCObjectPointerType *lhs, const ObjCObjectPointerType *rhs, |
| 8028 | bool compare) { |
James Y Knight | ccc4d83 | 2019-10-17 15:27:04 +0000 | [diff] [blame] | 8029 | // Allow id<P..> and an 'id' in all cases. |
| 8030 | if (lhs->isObjCIdType() || rhs->isObjCIdType()) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8031 | return true; |
James Y Knight | ccc4d83 | 2019-10-17 15:27:04 +0000 | [diff] [blame] | 8032 | |
| 8033 | // Don't allow id<P..> to convert to Class or Class<P..> in either direction. |
| 8034 | if (lhs->isObjCClassType() || lhs->isObjCQualifiedClassType() || |
| 8035 | rhs->isObjCClassType() || rhs->isObjCQualifiedClassType()) |
| 8036 | return false; |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8037 | |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8038 | if (lhs->isObjCQualifiedIdType()) { |
| 8039 | if (rhs->qual_empty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8040 | // If the RHS is a unqualified interface pointer "NSString*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8041 | // make sure we check the class hierarchy. |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8042 | if (ObjCInterfaceDecl *rhsID = rhs->getInterfaceDecl()) { |
| 8043 | for (auto *I : lhs->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8044 | // when comparing an id<P> on lhs with a static type on rhs, |
| 8045 | // see if static class implements all of id's protocols, directly or |
| 8046 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 8047 | if (!rhsID->ClassImplementsProtocol(I, true)) |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8048 | return false; |
| 8049 | } |
| 8050 | } |
| 8051 | // If there are no qualifiers and no interface, we have an 'id'. |
| 8052 | return true; |
| 8053 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8054 | // Both the right and left sides have qualifiers. |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8055 | for (auto *lhsProto : lhs->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8056 | bool match = false; |
| 8057 | |
| 8058 | // when comparing an id<P> on lhs with a static type on rhs, |
| 8059 | // see if static class implements all of id's protocols, directly or |
| 8060 | // through its super class and categories. |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8061 | for (auto *rhsProto : rhs->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8062 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 8063 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 8064 | match = true; |
| 8065 | break; |
| 8066 | } |
| 8067 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8068 | // If the RHS is a qualified interface pointer "NSString<P>*", |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8069 | // make sure we check the class hierarchy. |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8070 | if (ObjCInterfaceDecl *rhsID = rhs->getInterfaceDecl()) { |
| 8071 | for (auto *I : lhs->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8072 | // when comparing an id<P> on lhs with a static type on rhs, |
| 8073 | // see if static class implements all of id's protocols, directly or |
| 8074 | // through its super class and categories. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 8075 | if (rhsID->ClassImplementsProtocol(I, true)) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8076 | match = true; |
| 8077 | break; |
| 8078 | } |
| 8079 | } |
| 8080 | } |
| 8081 | if (!match) |
| 8082 | return false; |
| 8083 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8084 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8085 | return true; |
| 8086 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8087 | |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8088 | assert(rhs->isObjCQualifiedIdType() && "One of the LHS/RHS should be id<x>"); |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8089 | |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8090 | if (lhs->getInterfaceType()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 8091 | // If both the right and left sides have qualifiers. |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8092 | for (auto *lhsProto : lhs->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8093 | bool match = false; |
| 8094 | |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 8095 | // 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] | 8096 | // see if static class implements all of id's protocols, directly or |
| 8097 | // through its super class and categories. |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 8098 | // First, lhs protocols in the qualifier list must be found, direct |
| 8099 | // or indirect in rhs's qualifier list or it is a mismatch. |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8100 | for (auto *rhsProto : rhs->quals()) { |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8101 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 8102 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 8103 | match = true; |
| 8104 | break; |
| 8105 | } |
| 8106 | } |
| 8107 | if (!match) |
| 8108 | return false; |
| 8109 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8110 | |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 8111 | // Static class's protocols, or its super class or category protocols |
| 8112 | // must be found, direct or indirect in rhs's qualifier list or it is a mismatch. |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8113 | if (ObjCInterfaceDecl *lhsID = lhs->getInterfaceDecl()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 8114 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols; |
| 8115 | CollectInheritedProtocols(lhsID, LHSInheritedProtocols); |
| 8116 | // This is rather dubious but matches gcc's behavior. If lhs has |
| 8117 | // no type qualifier and its class has no static protocol(s) |
| 8118 | // assume that it is mismatch. |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8119 | if (LHSInheritedProtocols.empty() && lhs->qual_empty()) |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 8120 | return false; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 8121 | for (auto *lhsProto : LHSInheritedProtocols) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 8122 | bool match = false; |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8123 | for (auto *rhsProto : rhs->quals()) { |
Fariborz Jahanian | eb7714c | 2010-11-01 20:47:16 +0000 | [diff] [blame] | 8124 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 8125 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
| 8126 | match = true; |
| 8127 | break; |
| 8128 | } |
| 8129 | } |
| 8130 | if (!match) |
| 8131 | return false; |
| 8132 | } |
| 8133 | } |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8134 | return true; |
| 8135 | } |
| 8136 | return false; |
| 8137 | } |
| 8138 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8139 | /// canAssignObjCInterfaces - Return true if the two interface types are |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 8140 | /// compatible for assignment from RHS to LHS. This handles validation of any |
| 8141 | /// protocol qualifiers on the LHS or RHS. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8142 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, |
| 8143 | const ObjCObjectPointerType *RHSOPT) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8144 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 8145 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 8146 | |
James Y Knight | ccc4d83 | 2019-10-17 15:27:04 +0000 | [diff] [blame] | 8147 | // If either type represents the built-in 'id' type, return true. |
| 8148 | if (LHS->isObjCUnqualifiedId() || RHS->isObjCUnqualifiedId()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8149 | return true; |
| 8150 | |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8151 | // Function object that propagates a successful result or handles |
| 8152 | // __kindof types. |
| 8153 | auto finish = [&](bool succeeded) -> bool { |
| 8154 | if (succeeded) |
| 8155 | return true; |
| 8156 | |
| 8157 | if (!RHS->isKindOfType()) |
| 8158 | return false; |
| 8159 | |
| 8160 | // Strip off __kindof and protocol qualifiers, then check whether |
| 8161 | // we can assign the other way. |
| 8162 | return canAssignObjCInterfaces(RHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 8163 | LHSOPT->stripObjCKindOfTypeAndQuals(*this)); |
| 8164 | }; |
| 8165 | |
James Y Knight | ccc4d83 | 2019-10-17 15:27:04 +0000 | [diff] [blame] | 8166 | // Casts from or to id<P> are allowed when the other side has compatible |
| 8167 | // protocols. |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8168 | if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId()) { |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8169 | return finish(ObjCQualifiedIdTypesAreCompatible(LHSOPT, RHSOPT, false)); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8170 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8171 | |
James Y Knight | ccc4d83 | 2019-10-17 15:27:04 +0000 | [diff] [blame] | 8172 | // Verify protocol compatibility for casts from Class<P1> to Class<P2>. |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8173 | if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass()) { |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8174 | return finish(ObjCQualifiedClassTypesAreCompatible(LHSOPT, RHSOPT)); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8175 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8176 | |
James Y Knight | ccc4d83 | 2019-10-17 15:27:04 +0000 | [diff] [blame] | 8177 | // Casts from Class to Class<Foo>, or vice-versa, are allowed. |
| 8178 | if (LHS->isObjCClass() && RHS->isObjCClass()) { |
| 8179 | return true; |
| 8180 | } |
| 8181 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8182 | // If we have 2 user-defined types, fall into that path. |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8183 | if (LHS->getInterface() && RHS->getInterface()) { |
| 8184 | return finish(canAssignObjCInterfaces(LHS, RHS)); |
| 8185 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8186 | |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 8187 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8188 | } |
| 8189 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8190 | /// canAssignObjCInterfacesInBlockPointer - This routine is specifically written |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8191 | /// for providing type-safety for objective-c pointers used to pass/return |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8192 | /// arguments in block literals. When passed as arguments, passing 'A*' where |
| 8193 | /// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is |
| 8194 | /// not OK. For the return type, the opposite is not OK. |
| 8195 | bool ASTContext::canAssignObjCInterfacesInBlockPointer( |
| 8196 | const ObjCObjectPointerType *LHSOPT, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 8197 | const ObjCObjectPointerType *RHSOPT, |
| 8198 | bool BlockReturnType) { |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8199 | |
| 8200 | // Function object that propagates a successful result or handles |
| 8201 | // __kindof types. |
| 8202 | auto finish = [&](bool succeeded) -> bool { |
| 8203 | if (succeeded) |
| 8204 | return true; |
| 8205 | |
| 8206 | const ObjCObjectPointerType *Expected = BlockReturnType ? RHSOPT : LHSOPT; |
| 8207 | if (!Expected->isKindOfType()) |
| 8208 | return false; |
| 8209 | |
| 8210 | // Strip off __kindof and protocol qualifiers, then check whether |
| 8211 | // we can assign the other way. |
| 8212 | return canAssignObjCInterfacesInBlockPointer( |
| 8213 | RHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 8214 | LHSOPT->stripObjCKindOfTypeAndQuals(*this), |
| 8215 | BlockReturnType); |
| 8216 | }; |
| 8217 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 8218 | if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType()) |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8219 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8220 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8221 | if (LHSOPT->isObjCBuiltinType()) { |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8222 | return finish(RHSOPT->isObjCBuiltinType() || |
| 8223 | RHSOPT->isObjCQualifiedIdType()); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8224 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8225 | |
Fariborz Jahanian | 440a683 | 2010-04-06 17:23:39 +0000 | [diff] [blame] | 8226 | if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType()) |
Volodymyr Sapsai | 73152a2 | 2019-08-28 00:25:06 +0000 | [diff] [blame] | 8227 | return finish(ObjCQualifiedIdTypesAreCompatible( |
James Y Knight | c2ca003 | 2019-09-21 22:31:28 +0000 | [diff] [blame] | 8228 | (BlockReturnType ? LHSOPT : RHSOPT), |
| 8229 | (BlockReturnType ? RHSOPT : LHSOPT), false)); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8230 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8231 | const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); |
| 8232 | const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); |
| 8233 | if (LHS && RHS) { // We have 2 user-defined types. |
| 8234 | if (LHS != RHS) { |
| 8235 | if (LHS->getDecl()->isSuperClassOf(RHS->getDecl())) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8236 | return finish(BlockReturnType); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8237 | if (RHS->getDecl()->isSuperClassOf(LHS->getDecl())) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8238 | return finish(!BlockReturnType); |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8239 | } |
| 8240 | else |
| 8241 | return true; |
| 8242 | } |
| 8243 | return false; |
| 8244 | } |
| 8245 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8246 | /// Comparison routine for Objective-C protocols to be used with |
| 8247 | /// llvm::array_pod_sort. |
| 8248 | static int compareObjCProtocolsByName(ObjCProtocolDecl * const *lhs, |
| 8249 | ObjCProtocolDecl * const *rhs) { |
| 8250 | return (*lhs)->getName().compare((*rhs)->getName()); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8251 | } |
| 8252 | |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8253 | /// getIntersectionOfProtocols - This routine finds the intersection of set |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8254 | /// of protocols inherited from two distinct objective-c pointer objects with |
| 8255 | /// the given common base. |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8256 | /// It is used to build composite qualifier list of the composite type of |
| 8257 | /// the conditional expression involving two objective-c pointer objects. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8258 | static |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8259 | void getIntersectionOfProtocols(ASTContext &Context, |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8260 | const ObjCInterfaceDecl *CommonBase, |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8261 | const ObjCObjectPointerType *LHSOPT, |
| 8262 | const ObjCObjectPointerType *RHSOPT, |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8263 | SmallVectorImpl<ObjCProtocolDecl *> &IntersectionSet) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8264 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8265 | const ObjCObjectType* LHS = LHSOPT->getObjectType(); |
| 8266 | const ObjCObjectType* RHS = RHSOPT->getObjectType(); |
| 8267 | assert(LHS->getInterface() && "LHS must have an interface base"); |
| 8268 | assert(RHS->getInterface() && "RHS must have an interface base"); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8269 | |
| 8270 | // Add all of the protocols for the LHS. |
| 8271 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSProtocolSet; |
| 8272 | |
| 8273 | // Start with the protocol qualifiers. |
| 8274 | for (auto proto : LHS->quals()) { |
| 8275 | Context.CollectInheritedProtocols(proto, LHSProtocolSet); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8276 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8277 | |
| 8278 | // Also add the protocols associated with the LHS interface. |
| 8279 | Context.CollectInheritedProtocols(LHS->getInterface(), LHSProtocolSet); |
| 8280 | |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 8281 | // Add all of the protocols for the RHS. |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8282 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSProtocolSet; |
| 8283 | |
| 8284 | // Start with the protocol qualifiers. |
| 8285 | for (auto proto : RHS->quals()) { |
| 8286 | Context.CollectInheritedProtocols(proto, RHSProtocolSet); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8287 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8288 | |
| 8289 | // Also add the protocols associated with the RHS interface. |
| 8290 | Context.CollectInheritedProtocols(RHS->getInterface(), RHSProtocolSet); |
| 8291 | |
| 8292 | // Compute the intersection of the collected protocol sets. |
| 8293 | for (auto proto : LHSProtocolSet) { |
| 8294 | if (RHSProtocolSet.count(proto)) |
| 8295 | IntersectionSet.push_back(proto); |
| 8296 | } |
| 8297 | |
| 8298 | // Compute the set of protocols that is implied by either the common type or |
| 8299 | // the protocols within the intersection. |
| 8300 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> ImpliedProtocols; |
| 8301 | Context.CollectInheritedProtocols(CommonBase, ImpliedProtocols); |
| 8302 | |
| 8303 | // Remove any implied protocols from the list of inherited protocols. |
| 8304 | if (!ImpliedProtocols.empty()) { |
| 8305 | IntersectionSet.erase( |
| 8306 | std::remove_if(IntersectionSet.begin(), |
| 8307 | IntersectionSet.end(), |
| 8308 | [&](ObjCProtocolDecl *proto) -> bool { |
| 8309 | return ImpliedProtocols.count(proto) > 0; |
| 8310 | }), |
| 8311 | IntersectionSet.end()); |
| 8312 | } |
| 8313 | |
| 8314 | // Sort the remaining protocols by name. |
| 8315 | llvm::array_pod_sort(IntersectionSet.begin(), IntersectionSet.end(), |
| 8316 | compareObjCProtocolsByName); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8317 | } |
| 8318 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8319 | /// Determine whether the first type is a subtype of the second. |
| 8320 | static bool canAssignObjCObjectTypes(ASTContext &ctx, QualType lhs, |
| 8321 | QualType rhs) { |
| 8322 | // Common case: two object pointers. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8323 | const auto *lhsOPT = lhs->getAs<ObjCObjectPointerType>(); |
| 8324 | const auto *rhsOPT = rhs->getAs<ObjCObjectPointerType>(); |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8325 | if (lhsOPT && rhsOPT) |
| 8326 | return ctx.canAssignObjCInterfaces(lhsOPT, rhsOPT); |
| 8327 | |
| 8328 | // Two block pointers. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8329 | const auto *lhsBlock = lhs->getAs<BlockPointerType>(); |
| 8330 | const auto *rhsBlock = rhs->getAs<BlockPointerType>(); |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8331 | if (lhsBlock && rhsBlock) |
| 8332 | return ctx.typesAreBlockPointerCompatible(lhs, rhs); |
| 8333 | |
| 8334 | // If either is an unqualified 'id' and the other is a block, it's |
| 8335 | // acceptable. |
| 8336 | if ((lhsOPT && lhsOPT->isObjCIdType() && rhsBlock) || |
| 8337 | (rhsOPT && rhsOPT->isObjCIdType() && lhsBlock)) |
| 8338 | return true; |
| 8339 | |
| 8340 | return false; |
| 8341 | } |
| 8342 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8343 | // Check that the given Objective-C type argument lists are equivalent. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8344 | static bool sameObjCTypeArgs(ASTContext &ctx, |
| 8345 | const ObjCInterfaceDecl *iface, |
| 8346 | ArrayRef<QualType> lhsArgs, |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8347 | ArrayRef<QualType> rhsArgs, |
| 8348 | bool stripKindOf) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8349 | if (lhsArgs.size() != rhsArgs.size()) |
| 8350 | return false; |
| 8351 | |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8352 | ObjCTypeParamList *typeParams = iface->getTypeParamList(); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8353 | for (unsigned i = 0, n = lhsArgs.size(); i != n; ++i) { |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8354 | if (ctx.hasSameType(lhsArgs[i], rhsArgs[i])) |
| 8355 | continue; |
| 8356 | |
| 8357 | switch (typeParams->begin()[i]->getVariance()) { |
| 8358 | case ObjCTypeParamVariance::Invariant: |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8359 | if (!stripKindOf || |
| 8360 | !ctx.hasSameType(lhsArgs[i].stripObjCKindOfType(ctx), |
| 8361 | rhsArgs[i].stripObjCKindOfType(ctx))) { |
| 8362 | return false; |
| 8363 | } |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8364 | break; |
| 8365 | |
| 8366 | case ObjCTypeParamVariance::Covariant: |
| 8367 | if (!canAssignObjCObjectTypes(ctx, lhsArgs[i], rhsArgs[i])) |
| 8368 | return false; |
| 8369 | break; |
| 8370 | |
| 8371 | case ObjCTypeParamVariance::Contravariant: |
| 8372 | if (!canAssignObjCObjectTypes(ctx, rhsArgs[i], lhsArgs[i])) |
| 8373 | return false; |
| 8374 | break; |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8375 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8376 | } |
| 8377 | |
| 8378 | return true; |
| 8379 | } |
| 8380 | |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 8381 | QualType ASTContext::areCommonBaseCompatible( |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8382 | const ObjCObjectPointerType *Lptr, |
| 8383 | const ObjCObjectPointerType *Rptr) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8384 | const ObjCObjectType *LHS = Lptr->getObjectType(); |
| 8385 | const ObjCObjectType *RHS = Rptr->getObjectType(); |
| 8386 | const ObjCInterfaceDecl* LDecl = LHS->getInterface(); |
| 8387 | const ObjCInterfaceDecl* RDecl = RHS->getInterface(); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8388 | |
| 8389 | if (!LDecl || !RDecl) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8390 | return {}; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8391 | |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 8392 | // When either LHS or RHS is a kindof type, we should return a kindof type. |
| 8393 | // For example, for common base of kindof(ASub1) and kindof(ASub2), we return |
| 8394 | // kindof(A). |
| 8395 | bool anyKindOf = LHS->isKindOfType() || RHS->isKindOfType(); |
| 8396 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8397 | // Follow the left-hand side up the class hierarchy until we either hit a |
| 8398 | // root or find the RHS. Record the ancestors in case we don't find it. |
| 8399 | llvm::SmallDenseMap<const ObjCInterfaceDecl *, const ObjCObjectType *, 4> |
| 8400 | LHSAncestors; |
| 8401 | while (true) { |
| 8402 | // Record this ancestor. We'll need this if the common type isn't in the |
| 8403 | // path from the LHS to the root. |
| 8404 | LHSAncestors[LHS->getInterface()->getCanonicalDecl()] = LHS; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8405 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8406 | if (declaresSameEntity(LHS->getInterface(), RDecl)) { |
| 8407 | // Get the type arguments. |
| 8408 | ArrayRef<QualType> LHSTypeArgs = LHS->getTypeArgsAsWritten(); |
| 8409 | bool anyChanges = false; |
| 8410 | if (LHS->isSpecialized() && RHS->isSpecialized()) { |
| 8411 | // Both have type arguments, compare them. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8412 | if (!sameObjCTypeArgs(*this, LHS->getInterface(), |
| 8413 | LHS->getTypeArgs(), RHS->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8414 | /*stripKindOf=*/true)) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8415 | return {}; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8416 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { |
| 8417 | // If only one has type arguments, the result will not have type |
| 8418 | // arguments. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 8419 | LHSTypeArgs = {}; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8420 | anyChanges = true; |
| 8421 | } |
| 8422 | |
| 8423 | // Compute the intersection of protocols. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8424 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8425 | getIntersectionOfProtocols(*this, LHS->getInterface(), Lptr, Rptr, |
| 8426 | Protocols); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8427 | if (!Protocols.empty()) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8428 | anyChanges = true; |
| 8429 | |
| 8430 | // 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] | 8431 | // If we need to return a kindof type but LHS is not a kindof type, we |
| 8432 | // build a new result type. |
| 8433 | if (anyChanges || LHS->isKindOfType() != anyKindOf) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8434 | QualType Result = getObjCInterfaceType(LHS->getInterface()); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8435 | Result = getObjCObjectType(Result, LHSTypeArgs, Protocols, |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 8436 | anyKindOf || LHS->isKindOfType()); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8437 | return getObjCObjectPointerType(Result); |
| 8438 | } |
| 8439 | |
| 8440 | return getObjCObjectPointerType(QualType(LHS, 0)); |
Fariborz Jahanian | 6c5a8e2 | 2009-10-30 01:13:23 +0000 | [diff] [blame] | 8441 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8442 | |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8443 | // Find the superclass. |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8444 | QualType LHSSuperType = LHS->getSuperClassType(); |
| 8445 | if (LHSSuperType.isNull()) |
| 8446 | break; |
| 8447 | |
| 8448 | LHS = LHSSuperType->castAs<ObjCObjectType>(); |
| 8449 | } |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8450 | |
| 8451 | // We didn't find anything by following the LHS to its root; now check |
| 8452 | // the RHS against the cached set of ancestors. |
| 8453 | while (true) { |
| 8454 | auto KnownLHS = LHSAncestors.find(RHS->getInterface()->getCanonicalDecl()); |
| 8455 | if (KnownLHS != LHSAncestors.end()) { |
| 8456 | LHS = KnownLHS->second; |
| 8457 | |
| 8458 | // Get the type arguments. |
| 8459 | ArrayRef<QualType> RHSTypeArgs = RHS->getTypeArgsAsWritten(); |
| 8460 | bool anyChanges = false; |
| 8461 | if (LHS->isSpecialized() && RHS->isSpecialized()) { |
| 8462 | // Both have type arguments, compare them. |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8463 | if (!sameObjCTypeArgs(*this, LHS->getInterface(), |
| 8464 | LHS->getTypeArgs(), RHS->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8465 | /*stripKindOf=*/true)) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8466 | return {}; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8467 | } else if (LHS->isSpecialized() != RHS->isSpecialized()) { |
| 8468 | // If only one has type arguments, the result will not have type |
| 8469 | // arguments. |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 8470 | RHSTypeArgs = {}; |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8471 | anyChanges = true; |
| 8472 | } |
| 8473 | |
| 8474 | // Compute the intersection of protocols. |
| 8475 | SmallVector<ObjCProtocolDecl *, 8> Protocols; |
| 8476 | getIntersectionOfProtocols(*this, RHS->getInterface(), Lptr, Rptr, |
| 8477 | Protocols); |
| 8478 | if (!Protocols.empty()) |
| 8479 | anyChanges = true; |
| 8480 | |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 8481 | // If we need to return a kindof type but RHS is not a kindof type, we |
| 8482 | // build a new result type. |
| 8483 | if (anyChanges || RHS->isKindOfType() != anyKindOf) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8484 | QualType Result = getObjCInterfaceType(RHS->getInterface()); |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8485 | Result = getObjCObjectType(Result, RHSTypeArgs, Protocols, |
Manman Ren | c46f7d1 | 2016-05-06 19:35:02 +0000 | [diff] [blame] | 8486 | anyKindOf || RHS->isKindOfType()); |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8487 | return getObjCObjectPointerType(Result); |
| 8488 | } |
| 8489 | |
| 8490 | return getObjCObjectPointerType(QualType(RHS, 0)); |
| 8491 | } |
| 8492 | |
| 8493 | // Find the superclass of the RHS. |
| 8494 | QualType RHSSuperType = RHS->getSuperClassType(); |
| 8495 | if (RHSSuperType.isNull()) |
| 8496 | break; |
| 8497 | |
| 8498 | RHS = RHSSuperType->castAs<ObjCObjectType>(); |
| 8499 | } |
| 8500 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8501 | return {}; |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 8502 | } |
| 8503 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8504 | bool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS, |
| 8505 | const ObjCObjectType *RHS) { |
| 8506 | assert(LHS->getInterface() && "LHS is not an interface type"); |
| 8507 | assert(RHS->getInterface() && "RHS is not an interface type"); |
| 8508 | |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 8509 | // Verify that the base decls are compatible: the RHS must be a subclass of |
| 8510 | // the LHS. |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8511 | ObjCInterfaceDecl *LHSInterface = LHS->getInterface(); |
| 8512 | bool IsSuperClass = LHSInterface->isSuperClassOf(RHS->getInterface()); |
| 8513 | if (!IsSuperClass) |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 8514 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8515 | |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8516 | // If the LHS has protocol qualifiers, determine whether all of them are |
| 8517 | // satisfied by the RHS (i.e., the RHS has a superset of the protocols in the |
| 8518 | // LHS). |
| 8519 | if (LHS->getNumProtocols() > 0) { |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 8520 | // OK if conversion of LHS to SuperClass results in narrowing of types |
| 8521 | // ; i.e., SuperClass may implement at least one of the protocols |
| 8522 | // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok. |
| 8523 | // But not SuperObj<P1,P2,P3> = lhs<P1,P2>. |
| 8524 | llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols; |
| 8525 | CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols); |
| 8526 | // Also, if RHS has explicit quelifiers, include them for comparing with LHS's |
| 8527 | // qualifiers. |
| 8528 | for (auto *RHSPI : RHS->quals()) |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8529 | CollectInheritedProtocols(RHSPI, SuperClassInheritedProtocols); |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 8530 | // If there is no protocols associated with RHS, it is not a match. |
| 8531 | if (SuperClassInheritedProtocols.empty()) |
Steve Naroff | 114aecb | 2009-03-01 16:12:44 +0000 | [diff] [blame] | 8532 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8533 | |
Fariborz Jahanian | 12f7ef3 | 2014-10-13 21:07:45 +0000 | [diff] [blame] | 8534 | for (const auto *LHSProto : LHS->quals()) { |
| 8535 | bool SuperImplementsProtocol = false; |
| 8536 | for (auto *SuperClassProto : SuperClassInheritedProtocols) |
| 8537 | if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) { |
| 8538 | SuperImplementsProtocol = true; |
| 8539 | break; |
| 8540 | } |
| 8541 | if (!SuperImplementsProtocol) |
| 8542 | return false; |
| 8543 | } |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 8544 | } |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8545 | |
| 8546 | // If the LHS is specialized, we may need to check type arguments. |
| 8547 | if (LHS->isSpecialized()) { |
| 8548 | // Follow the superclass chain until we've matched the LHS class in the |
| 8549 | // hierarchy. This substitutes type arguments through. |
| 8550 | const ObjCObjectType *RHSSuper = RHS; |
| 8551 | while (!declaresSameEntity(RHSSuper->getInterface(), LHSInterface)) |
| 8552 | RHSSuper = RHSSuper->getSuperClassType()->castAs<ObjCObjectType>(); |
| 8553 | |
| 8554 | // If the RHS is specializd, compare type arguments. |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8555 | if (RHSSuper->isSpecialized() && |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 8556 | !sameObjCTypeArgs(*this, LHS->getInterface(), |
| 8557 | LHS->getTypeArgs(), RHSSuper->getTypeArgs(), |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 8558 | /*stripKindOf=*/true)) { |
Douglas Gregor | c5e07f5 | 2015-07-07 03:58:01 +0000 | [diff] [blame] | 8559 | return false; |
Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 8560 | } |
| 8561 | } |
| 8562 | |
| 8563 | return true; |
Chris Lattner | 49af6a4 | 2008-04-07 06:51:04 +0000 | [diff] [blame] | 8564 | } |
| 8565 | |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 8566 | bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) { |
| 8567 | // get the "pointed to" types |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8568 | const auto *LHSOPT = LHS->getAs<ObjCObjectPointerType>(); |
| 8569 | const auto *RHSOPT = RHS->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8570 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8571 | if (!LHSOPT || !RHSOPT) |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 8572 | return false; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8573 | |
| 8574 | return canAssignObjCInterfaces(LHSOPT, RHSOPT) || |
| 8575 | canAssignObjCInterfaces(RHSOPT, LHSOPT); |
Steve Naroff | b760515 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 8576 | } |
| 8577 | |
Douglas Gregor | 8b2d2fe | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 8578 | bool ASTContext::canBindObjCObjectType(QualType To, QualType From) { |
| 8579 | return canAssignObjCInterfaces( |
| 8580 | getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(), |
| 8581 | getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>()); |
| 8582 | } |
| 8583 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8584 | /// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible, |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 8585 | /// both shall have the identically qualified version of a compatible type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8586 | /// 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] | 8587 | /// same. See 6.7.[2,3,5] for additional rules. |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8588 | bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS, |
| 8589 | bool CompareUnqualified) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8590 | if (getLangOpts().CPlusPlus) |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 8591 | return hasSameType(LHS, RHS); |
Joey Gouly | 5788b78 | 2016-11-18 14:10:54 +0000 | [diff] [blame] | 8592 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8593 | return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8594 | } |
| 8595 | |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 8596 | bool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) { |
Fariborz Jahanian | c87c879 | 2011-07-12 23:20:13 +0000 | [diff] [blame] | 8597 | return typesAreCompatible(LHS, RHS); |
Fariborz Jahanian | c0f6af2 | 2011-07-12 22:05:16 +0000 | [diff] [blame] | 8598 | } |
| 8599 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8600 | bool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) { |
| 8601 | return !mergeTypes(LHS, RHS, true).isNull(); |
| 8602 | } |
| 8603 | |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 8604 | /// mergeTransparentUnionType - if T is a transparent union type and a member |
| 8605 | /// of T is compatible with SubType, return the merged type, else return |
| 8606 | /// QualType() |
| 8607 | QualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType, |
| 8608 | bool OfBlockPointer, |
| 8609 | bool Unqualified) { |
| 8610 | if (const RecordType *UT = T->getAsUnionType()) { |
| 8611 | RecordDecl *UD = UT->getDecl(); |
| 8612 | if (UD->hasAttr<TransparentUnionAttr>()) { |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 8613 | for (const auto *I : UD->fields()) { |
| 8614 | QualType ET = I->getType().getUnqualifiedType(); |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 8615 | QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified); |
| 8616 | if (!MT.isNull()) |
| 8617 | return MT; |
| 8618 | } |
| 8619 | } |
| 8620 | } |
| 8621 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8622 | return {}; |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 8623 | } |
| 8624 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8625 | /// mergeFunctionParameterTypes - merge two types which appear as function |
| 8626 | /// parameter types |
| 8627 | QualType ASTContext::mergeFunctionParameterTypes(QualType lhs, QualType rhs, |
| 8628 | bool OfBlockPointer, |
| 8629 | bool Unqualified) { |
Peter Collingbourne | a99fdcf | 2010-10-24 18:30:18 +0000 | [diff] [blame] | 8630 | // GNU extension: two types are compatible if they appear as a function |
| 8631 | // argument, one of the types is a transparent union type and the other |
| 8632 | // type is compatible with a union member |
| 8633 | QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer, |
| 8634 | Unqualified); |
| 8635 | if (!lmerge.isNull()) |
| 8636 | return lmerge; |
| 8637 | |
| 8638 | QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer, |
| 8639 | Unqualified); |
| 8640 | if (!rmerge.isNull()) |
| 8641 | return rmerge; |
| 8642 | |
| 8643 | return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified); |
| 8644 | } |
| 8645 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8646 | QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8647 | bool OfBlockPointer, |
| 8648 | bool Unqualified) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8649 | const auto *lbase = lhs->getAs<FunctionType>(); |
| 8650 | const auto *rbase = rhs->getAs<FunctionType>(); |
| 8651 | const auto *lproto = dyn_cast<FunctionProtoType>(lbase); |
| 8652 | const auto *rproto = dyn_cast<FunctionProtoType>(rbase); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8653 | bool allLTypes = true; |
| 8654 | bool allRTypes = true; |
| 8655 | |
| 8656 | // Check return type |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8657 | QualType retType; |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 8658 | if (OfBlockPointer) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8659 | QualType RHS = rbase->getReturnType(); |
| 8660 | QualType LHS = lbase->getReturnType(); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 8661 | bool UnqualifiedResult = Unqualified; |
| 8662 | if (!UnqualifiedResult) |
| 8663 | UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers()); |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 8664 | retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true); |
Fariborz Jahanian | 1782597 | 2011-02-11 18:46:17 +0000 | [diff] [blame] | 8665 | } |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 8666 | else |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8667 | retType = mergeTypes(lbase->getReturnType(), rbase->getReturnType(), false, |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8668 | Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8669 | if (retType.isNull()) |
| 8670 | return {}; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8671 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8672 | if (Unqualified) |
| 8673 | retType = retType.getUnqualifiedType(); |
| 8674 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 8675 | CanQualType LRetType = getCanonicalType(lbase->getReturnType()); |
| 8676 | CanQualType RRetType = getCanonicalType(rbase->getReturnType()); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8677 | if (Unqualified) { |
| 8678 | LRetType = LRetType.getUnqualifiedType(); |
| 8679 | RRetType = RRetType.getUnqualifiedType(); |
| 8680 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8681 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8682 | if (getCanonicalType(retType) != LRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8683 | allLTypes = false; |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8684 | if (getCanonicalType(retType) != RRetType) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8685 | allRTypes = false; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8686 | |
Daniel Dunbar | edd5bae | 2010-04-28 16:20:58 +0000 | [diff] [blame] | 8687 | // FIXME: double check this |
| 8688 | // FIXME: should we error if lbase->getRegParmAttr() != 0 && |
| 8689 | // rbase->getRegParmAttr() != 0 && |
| 8690 | // lbase->getRegParmAttr() != rbase->getRegParmAttr()? |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 8691 | FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo(); |
| 8692 | FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8693 | |
Douglas Gregor | 8c94086 | 2010-01-18 17:14:39 +0000 | [diff] [blame] | 8694 | // Compatible functions must have compatible calling conventions |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 8695 | if (lbaseInfo.getCC() != rbaseInfo.getCC()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8696 | return {}; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8697 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8698 | // Regparm is part of the calling convention. |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 8699 | if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8700 | return {}; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8701 | if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8702 | return {}; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8703 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8704 | if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8705 | return {}; |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 8706 | if (lbaseInfo.getNoCallerSavedRegs() != rbaseInfo.getNoCallerSavedRegs()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8707 | return {}; |
Oren Ben Simhon | 220671a | 2018-03-17 13:31:35 +0000 | [diff] [blame] | 8708 | if (lbaseInfo.getNoCfCheck() != rbaseInfo.getNoCfCheck()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8709 | return {}; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8710 | |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8711 | // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. |
| 8712 | bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8713 | |
Rafael Espindola | 8778c28 | 2012-11-29 16:09:03 +0000 | [diff] [blame] | 8714 | if (lbaseInfo.getNoReturn() != NoReturn) |
| 8715 | allLTypes = false; |
| 8716 | if (rbaseInfo.getNoReturn() != NoReturn) |
| 8717 | allRTypes = false; |
| 8718 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8719 | FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8720 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8721 | if (lproto && rproto) { // two C99 style function prototypes |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 8722 | assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() && |
| 8723 | "C++ shouldn't be here"); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8724 | // Compatible functions must have the same number of parameters |
| 8725 | if (lproto->getNumParams() != rproto->getNumParams()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8726 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8727 | |
| 8728 | // Variadic and non-variadic functions aren't compatible |
| 8729 | if (lproto->isVariadic() != rproto->isVariadic()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8730 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8731 | |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 8732 | if (lproto->getMethodQuals() != rproto->getMethodQuals()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8733 | return {}; |
Argyrios Kyrtzidis | 22a3735 | 2008-10-26 16:43:14 +0000 | [diff] [blame] | 8734 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8735 | SmallVector<FunctionProtoType::ExtParameterInfo, 4> newParamInfos; |
| 8736 | bool canUseLeft, canUseRight; |
| 8737 | if (!mergeExtParameterInfo(lproto, rproto, canUseLeft, canUseRight, |
| 8738 | newParamInfos)) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8739 | return {}; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8740 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8741 | if (!canUseLeft) |
| 8742 | allLTypes = false; |
| 8743 | if (!canUseRight) |
| 8744 | allRTypes = false; |
| 8745 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8746 | // Check parameter type compatibility |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8747 | SmallVector<QualType, 10> types; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8748 | for (unsigned i = 0, n = lproto->getNumParams(); i < n; i++) { |
| 8749 | QualType lParamType = lproto->getParamType(i).getUnqualifiedType(); |
| 8750 | QualType rParamType = rproto->getParamType(i).getUnqualifiedType(); |
| 8751 | QualType paramType = mergeFunctionParameterTypes( |
| 8752 | lParamType, rParamType, OfBlockPointer, Unqualified); |
| 8753 | if (paramType.isNull()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8754 | return {}; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8755 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8756 | if (Unqualified) |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8757 | paramType = paramType.getUnqualifiedType(); |
| 8758 | |
| 8759 | types.push_back(paramType); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8760 | if (Unqualified) { |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8761 | lParamType = lParamType.getUnqualifiedType(); |
| 8762 | rParamType = rParamType.getUnqualifiedType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8763 | } |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8764 | |
| 8765 | if (getCanonicalType(paramType) != getCanonicalType(lParamType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8766 | allLTypes = false; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8767 | if (getCanonicalType(paramType) != getCanonicalType(rParamType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8768 | allRTypes = false; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8769 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8770 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8771 | if (allLTypes) return lhs; |
| 8772 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8773 | |
| 8774 | FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo(); |
| 8775 | EPI.ExtInfo = einfo; |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 8776 | EPI.ExtParameterInfos = |
| 8777 | newParamInfos.empty() ? nullptr : newParamInfos.data(); |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 8778 | return getFunctionType(retType, types, EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8779 | } |
| 8780 | |
| 8781 | if (lproto) allRTypes = false; |
| 8782 | if (rproto) allLTypes = false; |
| 8783 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8784 | const FunctionProtoType *proto = lproto ? lproto : rproto; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8785 | if (proto) { |
Sebastian Redl | 5068f77ac | 2009-05-27 22:11:52 +0000 | [diff] [blame] | 8786 | assert(!proto->hasExceptionSpec() && "C++ shouldn't be here"); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8787 | if (proto->isVariadic()) |
| 8788 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8789 | // Check that the types are compatible with the types that |
| 8790 | // would result from default argument promotions (C99 6.7.5.3p15). |
| 8791 | // The only types actually affected are promotable integer |
| 8792 | // types and floats, which would be passed as a different |
| 8793 | // type depending on whether the prototype is visible. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8794 | for (unsigned i = 0, n = proto->getNumParams(); i < n; ++i) { |
| 8795 | QualType paramTy = proto->getParamType(i); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8796 | |
Eli Friedman | 448ce40 | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 8797 | // 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] | 8798 | // to pass enum values. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8799 | if (const auto *Enum = paramTy->getAs<EnumType>()) { |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8800 | paramTy = Enum->getDecl()->getIntegerType(); |
| 8801 | if (paramTy.isNull()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8802 | return {}; |
Eli Friedman | 448ce40 | 2012-08-30 00:44:15 +0000 | [diff] [blame] | 8803 | } |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 8804 | |
| 8805 | if (paramTy->isPromotableIntegerType() || |
| 8806 | getCanonicalType(paramTy).getUnqualifiedType() == FloatTy) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8807 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8808 | } |
| 8809 | |
| 8810 | if (allLTypes) return lhs; |
| 8811 | if (allRTypes) return rhs; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8812 | |
| 8813 | FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo(); |
| 8814 | EPI.ExtInfo = einfo; |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 8815 | return getFunctionType(retType, proto->getParamTypes(), EPI); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8816 | } |
| 8817 | |
| 8818 | if (allLTypes) return lhs; |
| 8819 | if (allRTypes) return rhs; |
John McCall | 8c6b56f | 2010-12-15 01:06:38 +0000 | [diff] [blame] | 8820 | return getFunctionNoProtoType(retType, einfo); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8821 | } |
| 8822 | |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8823 | /// Given that we have an enum type and a non-enum type, try to merge them. |
| 8824 | static QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET, |
| 8825 | QualType other, bool isBlockReturnType) { |
| 8826 | // C99 6.7.2.2p4: Each enumerated type shall be compatible with char, |
| 8827 | // a signed integer type, or an unsigned integer type. |
| 8828 | // Compatibility is based on the underlying type, not the promotion |
| 8829 | // type. |
| 8830 | QualType underlyingType = ET->getDecl()->getIntegerType(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8831 | if (underlyingType.isNull()) |
| 8832 | return {}; |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8833 | if (Context.hasSameType(underlyingType, other)) |
| 8834 | return other; |
| 8835 | |
| 8836 | // In block return types, we're more permissive and accept any |
| 8837 | // integral type of the same size. |
| 8838 | if (isBlockReturnType && other->isIntegerType() && |
| 8839 | Context.getTypeSize(underlyingType) == Context.getTypeSize(other)) |
| 8840 | return other; |
| 8841 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8842 | return {}; |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8843 | } |
| 8844 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8845 | QualType ASTContext::mergeTypes(QualType LHS, QualType RHS, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8846 | bool OfBlockPointer, |
Fariborz Jahanian | 90186f8 | 2011-03-14 16:07:00 +0000 | [diff] [blame] | 8847 | bool Unqualified, bool BlockReturnType) { |
Bill Wendling | db4e349 | 2007-12-03 07:33:35 +0000 | [diff] [blame] | 8848 | // C++ [expr]: If an expression initially has the type "reference to T", the |
| 8849 | // type is adjusted to "T" prior to any further analysis, the expression |
| 8850 | // designates the object or function denoted by the reference, and the |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 8851 | // expression is an lvalue unless the reference is an rvalue reference and |
| 8852 | // the expression is a function call (possibly inside parentheses). |
Douglas Gregor | 21e771e | 2010-02-03 21:02:30 +0000 | [diff] [blame] | 8853 | assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?"); |
| 8854 | assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?"); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8855 | |
| 8856 | if (Unqualified) { |
| 8857 | LHS = LHS.getUnqualifiedType(); |
| 8858 | RHS = RHS.getUnqualifiedType(); |
| 8859 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8860 | |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8861 | QualType LHSCan = getCanonicalType(LHS), |
| 8862 | RHSCan = getCanonicalType(RHS); |
| 8863 | |
| 8864 | // If two types are identical, they are compatible. |
| 8865 | if (LHSCan == RHSCan) |
| 8866 | return LHS; |
| 8867 | |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8868 | // If the qualifiers are different, the types aren't compatible... mostly. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 8869 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 8870 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8871 | if (LQuals != RQuals) { |
| 8872 | // If any of these qualifiers are different, we have a type |
| 8873 | // mismatch. |
| 8874 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8875 | LQuals.getAddressSpace() != RQuals.getAddressSpace() || |
Roger Ferrer Ibanez | d93add3 | 2017-02-24 08:41:09 +0000 | [diff] [blame] | 8876 | LQuals.getObjCLifetime() != RQuals.getObjCLifetime() || |
| 8877 | LQuals.hasUnaligned() != RQuals.hasUnaligned()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8878 | return {}; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8879 | |
| 8880 | // Exactly one GC qualifier difference is allowed: __strong is |
| 8881 | // okay if the other type has no GC qualifier but is an Objective |
| 8882 | // C object pointer (i.e. implicitly strong by default). We fix |
| 8883 | // this by pretending that the unqualified type was actually |
| 8884 | // qualified __strong. |
| 8885 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 8886 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 8887 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
| 8888 | |
| 8889 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8890 | return {}; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8891 | |
| 8892 | if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) { |
| 8893 | return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong)); |
| 8894 | } |
| 8895 | if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) { |
| 8896 | return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS); |
| 8897 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8898 | return {}; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 8899 | } |
| 8900 | |
| 8901 | // Okay, qualifiers are equal. |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8902 | |
Eli Friedman | dcca633 | 2009-06-01 01:22:52 +0000 | [diff] [blame] | 8903 | Type::TypeClass LHSClass = LHSCan->getTypeClass(); |
| 8904 | Type::TypeClass RHSClass = RHSCan->getTypeClass(); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8905 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8906 | // We want to consider the two function types to be the same for these |
| 8907 | // comparisons, just force one to the other. |
| 8908 | if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto; |
| 8909 | if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto; |
Eli Friedman | 16f9096 | 2008-02-12 08:23:06 +0000 | [diff] [blame] | 8910 | |
| 8911 | // Same as above for arrays |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 8912 | if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray) |
| 8913 | LHSClass = Type::ConstantArray; |
| 8914 | if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray) |
| 8915 | RHSClass = Type::ConstantArray; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8916 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8917 | // ObjCInterfaces are just specialized ObjCObjects. |
| 8918 | if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject; |
| 8919 | if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject; |
| 8920 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 8921 | // Canonicalize ExtVector -> Vector. |
| 8922 | if (LHSClass == Type::ExtVector) LHSClass = Type::Vector; |
| 8923 | if (RHSClass == Type::ExtVector) RHSClass = Type::Vector; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8924 | |
Chris Lattner | 9555466 | 2008-04-07 05:43:21 +0000 | [diff] [blame] | 8925 | // If the canonical type classes don't match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8926 | if (LHSClass != RHSClass) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8927 | // Note that we only have special rules for turning block enum |
| 8928 | // returns into block int returns, not vice-versa. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8929 | if (const auto *ETy = LHS->getAs<EnumType>()) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8930 | return mergeEnumWithInteger(*this, ETy, RHS, false); |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 8931 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 8932 | if (const EnumType* ETy = RHS->getAs<EnumType>()) { |
John McCall | 433c2e6 | 2013-03-21 00:10:07 +0000 | [diff] [blame] | 8933 | return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType); |
Eli Friedman | a7bf7ed | 2008-02-12 08:46:17 +0000 | [diff] [blame] | 8934 | } |
Fariborz Jahanian | 26d8371 | 2012-01-26 00:45:38 +0000 | [diff] [blame] | 8935 | // allow block pointer type to match an 'id' type. |
Fariborz Jahanian | 194904e | 2012-01-26 17:08:50 +0000 | [diff] [blame] | 8936 | if (OfBlockPointer && !BlockReturnType) { |
| 8937 | if (LHS->isObjCIdType() && RHS->isBlockPointerType()) |
| 8938 | return LHS; |
| 8939 | if (RHS->isObjCIdType() && LHS->isBlockPointerType()) |
| 8940 | return RHS; |
| 8941 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8942 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8943 | return {}; |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 8944 | } |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8945 | |
Steve Naroff | c6edcbd | 2008-01-09 22:43:08 +0000 | [diff] [blame] | 8946 | // The canonical type classes match. |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8947 | switch (LHSClass) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8948 | #define TYPE(Class, Base) |
| 8949 | #define ABSTRACT_TYPE(Class, Base) |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 8950 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8951 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 8952 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
John McCall | 36b12a8 | 2019-10-02 06:35:23 +0000 | [diff] [blame] | 8953 | #include "clang/AST/TypeNodes.inc" |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8954 | llvm_unreachable("Non-canonical and dependent types shouldn't get here"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8955 | |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 8956 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 8957 | case Type::DeducedTemplateSpecialization: |
Sebastian Redl | 0f8b23f | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 8958 | case Type::LValueReference: |
| 8959 | case Type::RValueReference: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8960 | case Type::MemberPointer: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8961 | llvm_unreachable("C++ should never be in mergeTypes"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8962 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 8963 | case Type::ObjCInterface: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8964 | case Type::IncompleteArray: |
| 8965 | case Type::VariableArray: |
| 8966 | case Type::FunctionProto: |
| 8967 | case Type::ExtVector: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8968 | llvm_unreachable("Types are eliminated above"); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 8969 | |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 8970 | case Type::Pointer: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8971 | { |
| 8972 | // Merge two pointer types, while trying to preserve typedef info |
Simon Pilgrim | eed4b12 | 2019-10-02 11:48:06 +0000 | [diff] [blame] | 8973 | QualType LHSPointee = LHS->castAs<PointerType>()->getPointeeType(); |
| 8974 | QualType RHSPointee = RHS->castAs<PointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8975 | if (Unqualified) { |
| 8976 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 8977 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 8978 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 8979 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false, |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8980 | Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 8981 | if (ResultType.isNull()) |
| 8982 | return {}; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 8983 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8984 | return LHS; |
Eli Friedman | 091a9ac | 2009-06-02 05:28:56 +0000 | [diff] [blame] | 8985 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 8986 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 8987 | return getPointerType(ResultType); |
| 8988 | } |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 8989 | case Type::BlockPointer: |
| 8990 | { |
| 8991 | // Merge two block pointer types, while trying to preserve typedef info |
Simon Pilgrim | eed4b12 | 2019-10-02 11:48:06 +0000 | [diff] [blame] | 8992 | QualType LHSPointee = LHS->castAs<BlockPointerType>()->getPointeeType(); |
| 8993 | QualType RHSPointee = RHS->castAs<BlockPointerType>()->getPointeeType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 8994 | if (Unqualified) { |
| 8995 | LHSPointee = LHSPointee.getUnqualifiedType(); |
| 8996 | RHSPointee = RHSPointee.getUnqualifiedType(); |
| 8997 | } |
Anastasia Stulova | 81a25e35 | 2017-03-10 15:23:07 +0000 | [diff] [blame] | 8998 | if (getLangOpts().OpenCL) { |
| 8999 | Qualifiers LHSPteeQual = LHSPointee.getQualifiers(); |
| 9000 | Qualifiers RHSPteeQual = RHSPointee.getQualifiers(); |
| 9001 | // Blocks can't be an expression in a ternary operator (OpenCL v2.0 |
| 9002 | // 6.12.5) thus the following check is asymmetric. |
| 9003 | if (!LHSPteeQual.isAddressSpaceSupersetOf(RHSPteeQual)) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9004 | return {}; |
Anastasia Stulova | 81a25e35 | 2017-03-10 15:23:07 +0000 | [diff] [blame] | 9005 | LHSPteeQual.removeAddressSpace(); |
| 9006 | RHSPteeQual.removeAddressSpace(); |
| 9007 | LHSPointee = |
| 9008 | QualType(LHSPointee.getTypePtr(), LHSPteeQual.getAsOpaqueValue()); |
| 9009 | RHSPointee = |
| 9010 | QualType(RHSPointee.getTypePtr(), RHSPteeQual.getAsOpaqueValue()); |
| 9011 | } |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 9012 | QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer, |
| 9013 | Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9014 | if (ResultType.isNull()) |
| 9015 | return {}; |
Steve Naroff | 68e167d | 2008-12-10 17:49:55 +0000 | [diff] [blame] | 9016 | if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType)) |
| 9017 | return LHS; |
| 9018 | if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType)) |
| 9019 | return RHS; |
| 9020 | return getBlockPointerType(ResultType); |
| 9021 | } |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 9022 | case Type::Atomic: |
| 9023 | { |
| 9024 | // Merge two pointer types, while trying to preserve typedef info |
Simon Pilgrim | eed4b12 | 2019-10-02 11:48:06 +0000 | [diff] [blame] | 9025 | QualType LHSValue = LHS->castAs<AtomicType>()->getValueType(); |
| 9026 | QualType RHSValue = RHS->castAs<AtomicType>()->getValueType(); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 9027 | if (Unqualified) { |
| 9028 | LHSValue = LHSValue.getUnqualifiedType(); |
| 9029 | RHSValue = RHSValue.getUnqualifiedType(); |
| 9030 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9031 | QualType ResultType = mergeTypes(LHSValue, RHSValue, false, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 9032 | Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9033 | if (ResultType.isNull()) |
| 9034 | return {}; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 9035 | if (getCanonicalType(LHSValue) == getCanonicalType(ResultType)) |
| 9036 | return LHS; |
| 9037 | if (getCanonicalType(RHSValue) == getCanonicalType(ResultType)) |
| 9038 | return RHS; |
| 9039 | return getAtomicType(ResultType); |
| 9040 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 9041 | case Type::ConstantArray: |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 9042 | { |
| 9043 | const ConstantArrayType* LCAT = getAsConstantArrayType(LHS); |
| 9044 | const ConstantArrayType* RCAT = getAsConstantArrayType(RHS); |
| 9045 | if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9046 | return {}; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 9047 | |
| 9048 | QualType LHSElem = getAsArrayType(LHS)->getElementType(); |
| 9049 | QualType RHSElem = getAsArrayType(RHS)->getElementType(); |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 9050 | if (Unqualified) { |
| 9051 | LHSElem = LHSElem.getUnqualifiedType(); |
| 9052 | RHSElem = RHSElem.getUnqualifiedType(); |
| 9053 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9054 | |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 9055 | QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9056 | if (ResultType.isNull()) |
| 9057 | return {}; |
Jeremy Morse | 8129c5c | 2018-06-05 09:18:26 +0000 | [diff] [blame] | 9058 | |
| 9059 | const VariableArrayType* LVAT = getAsVariableArrayType(LHS); |
| 9060 | const VariableArrayType* RVAT = getAsVariableArrayType(RHS); |
| 9061 | |
| 9062 | // If either side is a variable array, and both are complete, check whether |
| 9063 | // the current dimension is definite. |
| 9064 | if (LVAT || RVAT) { |
| 9065 | auto SizeFetch = [this](const VariableArrayType* VAT, |
| 9066 | const ConstantArrayType* CAT) |
| 9067 | -> std::pair<bool,llvm::APInt> { |
| 9068 | if (VAT) { |
| 9069 | llvm::APSInt TheInt; |
| 9070 | Expr *E = VAT->getSizeExpr(); |
| 9071 | if (E && E->isIntegerConstantExpr(TheInt, *this)) |
| 9072 | return std::make_pair(true, TheInt); |
| 9073 | else |
| 9074 | return std::make_pair(false, TheInt); |
| 9075 | } else if (CAT) { |
| 9076 | return std::make_pair(true, CAT->getSize()); |
| 9077 | } else { |
| 9078 | return std::make_pair(false, llvm::APInt()); |
| 9079 | } |
| 9080 | }; |
| 9081 | |
| 9082 | bool HaveLSize, HaveRSize; |
| 9083 | llvm::APInt LSize, RSize; |
| 9084 | std::tie(HaveLSize, LSize) = SizeFetch(LVAT, LCAT); |
| 9085 | std::tie(HaveRSize, RSize) = SizeFetch(RVAT, RCAT); |
| 9086 | if (HaveLSize && HaveRSize && !llvm::APInt::isSameValue(LSize, RSize)) |
| 9087 | return {}; // Definite, but unequal, array dimension |
| 9088 | } |
| 9089 | |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 9090 | if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 9091 | return LHS; |
| 9092 | if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 9093 | return RHS; |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 9094 | if (LCAT) |
| 9095 | return getConstantArrayType(ResultType, LCAT->getSize(), |
| 9096 | LCAT->getSizeExpr(), |
| 9097 | ArrayType::ArraySizeModifier(), 0); |
| 9098 | if (RCAT) |
| 9099 | return getConstantArrayType(ResultType, RCAT->getSize(), |
| 9100 | RCAT->getSizeExpr(), |
| 9101 | ArrayType::ArraySizeModifier(), 0); |
Chris Lattner | 465fa32 | 2008-10-05 17:34:18 +0000 | [diff] [blame] | 9102 | if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType)) |
| 9103 | return LHS; |
| 9104 | if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType)) |
| 9105 | return RHS; |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 9106 | if (LVAT) { |
| 9107 | // FIXME: This isn't correct! But tricky to implement because |
| 9108 | // the array's size has to be the size of LHS, but the type |
| 9109 | // has to be different. |
| 9110 | return LHS; |
| 9111 | } |
| 9112 | if (RVAT) { |
| 9113 | // FIXME: This isn't correct! But tricky to implement because |
| 9114 | // the array's size has to be the size of RHS, but the type |
| 9115 | // has to be different. |
| 9116 | return RHS; |
| 9117 | } |
Eli Friedman | 3e62c21 | 2008-08-22 01:48:21 +0000 | [diff] [blame] | 9118 | if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS; |
| 9119 | if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS; |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 9120 | return getIncompleteArrayType(ResultType, |
| 9121 | ArrayType::ArraySizeModifier(), 0); |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 9122 | } |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 9123 | case Type::FunctionNoProto: |
Douglas Gregor | 17ea3f5 | 2010-07-29 15:18:02 +0000 | [diff] [blame] | 9124 | return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified); |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 9125 | case Type::Record: |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 9126 | case Type::Enum: |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9127 | return {}; |
Chris Lattner | fd65291 | 2008-01-14 05:45:46 +0000 | [diff] [blame] | 9128 | case Type::Builtin: |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 9129 | // Only exactly equal builtin types are compatible, which is tested above. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9130 | return {}; |
Daniel Dunbar | 804c044 | 2009-01-28 21:22:12 +0000 | [diff] [blame] | 9131 | case Type::Complex: |
| 9132 | // Distinct complex types are incompatible. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9133 | return {}; |
Chris Lattner | 7bbd3d7 | 2008-04-07 05:55:38 +0000 | [diff] [blame] | 9134 | case Type::Vector: |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 9135 | // FIXME: The merged type should be an ExtVector! |
Simon Pilgrim | 9588ae7 | 2019-10-03 21:47:42 +0000 | [diff] [blame] | 9136 | if (areCompatVectorTypes(LHSCan->castAs<VectorType>(), |
| 9137 | RHSCan->castAs<VectorType>())) |
Eli Friedman | 47f7711 | 2008-08-22 00:56:42 +0000 | [diff] [blame] | 9138 | return LHS; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9139 | return {}; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 9140 | case Type::ObjCObject: { |
| 9141 | // Check if the types are assignment compatible. |
Eli Friedman | cad9638 | 2009-02-27 23:04:43 +0000 | [diff] [blame] | 9142 | // FIXME: This should be type compatibility, e.g. whether |
| 9143 | // "LHS x; RHS x;" at global scope is legal. |
Simon Pilgrim | 9588ae7 | 2019-10-03 21:47:42 +0000 | [diff] [blame] | 9144 | if (canAssignObjCInterfaces(LHS->castAs<ObjCObjectType>(), |
| 9145 | RHS->castAs<ObjCObjectType>())) |
Steve Naroff | 7a7814c | 2009-02-21 16:18:07 +0000 | [diff] [blame] | 9146 | return LHS; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9147 | return {}; |
Cedric Venet | 4fc88b7 | 2009-02-21 17:14:49 +0000 | [diff] [blame] | 9148 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9149 | case Type::ObjCObjectPointer: |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 9150 | if (OfBlockPointer) { |
| 9151 | if (canAssignObjCInterfacesInBlockPointer( |
Simon Pilgrim | 9588ae7 | 2019-10-03 21:47:42 +0000 | [diff] [blame] | 9152 | LHS->castAs<ObjCObjectPointerType>(), |
| 9153 | RHS->castAs<ObjCObjectPointerType>(), BlockReturnType)) |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 9154 | return LHS; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9155 | return {}; |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 9156 | } |
Simon Pilgrim | 9588ae7 | 2019-10-03 21:47:42 +0000 | [diff] [blame] | 9157 | if (canAssignObjCInterfaces(LHS->castAs<ObjCObjectPointerType>(), |
| 9158 | RHS->castAs<ObjCObjectPointerType>())) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 9159 | return LHS; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9160 | return {}; |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 9161 | case Type::Pipe: |
Joey Gouly | e3c85de | 2016-12-01 11:30:49 +0000 | [diff] [blame] | 9162 | assert(LHS != RHS && |
| 9163 | "Equivalent pipe types should have already been handled!"); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9164 | return {}; |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 9165 | } |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 9166 | |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 9167 | llvm_unreachable("Invalid Type::Class!"); |
Steve Naroff | 32e44c0 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 9168 | } |
Ted Kremenek | fc581a9 | 2007-10-31 17:10:13 +0000 | [diff] [blame] | 9169 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 9170 | bool ASTContext::mergeExtParameterInfo( |
| 9171 | const FunctionProtoType *FirstFnType, const FunctionProtoType *SecondFnType, |
| 9172 | bool &CanUseFirst, bool &CanUseSecond, |
| 9173 | SmallVectorImpl<FunctionProtoType::ExtParameterInfo> &NewParamInfos) { |
| 9174 | assert(NewParamInfos.empty() && "param info list not empty"); |
| 9175 | CanUseFirst = CanUseSecond = true; |
| 9176 | bool FirstHasInfo = FirstFnType->hasExtParameterInfos(); |
| 9177 | bool SecondHasInfo = SecondFnType->hasExtParameterInfos(); |
| 9178 | |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 9179 | // Fast path: if the first type doesn't have ext parameter infos, |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 9180 | // we match if and only if the second type also doesn't have them. |
| 9181 | if (!FirstHasInfo && !SecondHasInfo) |
| 9182 | return true; |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 9183 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 9184 | bool NeedParamInfo = false; |
| 9185 | size_t E = FirstHasInfo ? FirstFnType->getExtParameterInfos().size() |
| 9186 | : SecondFnType->getExtParameterInfos().size(); |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 9187 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 9188 | for (size_t I = 0; I < E; ++I) { |
| 9189 | FunctionProtoType::ExtParameterInfo FirstParam, SecondParam; |
| 9190 | if (FirstHasInfo) |
| 9191 | FirstParam = FirstFnType->getExtParameterInfo(I); |
| 9192 | if (SecondHasInfo) |
| 9193 | SecondParam = SecondFnType->getExtParameterInfo(I); |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 9194 | |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 9195 | // Cannot merge unless everything except the noescape flag matches. |
| 9196 | if (FirstParam.withIsNoEscape(false) != SecondParam.withIsNoEscape(false)) |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 9197 | return false; |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 9198 | |
| 9199 | bool FirstNoEscape = FirstParam.isNoEscape(); |
| 9200 | bool SecondNoEscape = SecondParam.isNoEscape(); |
| 9201 | bool IsNoEscape = FirstNoEscape && SecondNoEscape; |
| 9202 | NewParamInfos.push_back(FirstParam.withIsNoEscape(IsNoEscape)); |
| 9203 | if (NewParamInfos.back().getOpaqueValue()) |
| 9204 | NeedParamInfo = true; |
| 9205 | if (FirstNoEscape != IsNoEscape) |
| 9206 | CanUseFirst = false; |
| 9207 | if (SecondNoEscape != IsNoEscape) |
| 9208 | CanUseSecond = false; |
John McCall | 18afab7 | 2016-03-01 00:49:02 +0000 | [diff] [blame] | 9209 | } |
Akira Hatanaka | 98a4933 | 2017-09-22 00:41:05 +0000 | [diff] [blame] | 9210 | |
| 9211 | if (!NeedParamInfo) |
| 9212 | NewParamInfos.clear(); |
| 9213 | |
Fariborz Jahanian | 9767697 | 2011-09-28 21:52:05 +0000 | [diff] [blame] | 9214 | return true; |
| 9215 | } |
| 9216 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 9217 | void ASTContext::ResetObjCLayout(const ObjCContainerDecl *CD) { |
| 9218 | ObjCLayouts[CD] = nullptr; |
| 9219 | } |
| 9220 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9221 | /// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and |
| 9222 | /// 'RHS' attributes and returns the merged version; including for function |
| 9223 | /// return types. |
| 9224 | QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) { |
| 9225 | QualType LHSCan = getCanonicalType(LHS), |
| 9226 | RHSCan = getCanonicalType(RHS); |
| 9227 | // If two types are identical, they are compatible. |
| 9228 | if (LHSCan == RHSCan) |
| 9229 | return LHS; |
| 9230 | if (RHSCan->isFunctionType()) { |
| 9231 | if (!LHSCan->isFunctionType()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9232 | return {}; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9233 | QualType OldReturnType = |
| 9234 | cast<FunctionType>(RHSCan.getTypePtr())->getReturnType(); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9235 | QualType NewReturnType = |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 9236 | cast<FunctionType>(LHSCan.getTypePtr())->getReturnType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9237 | QualType ResReturnType = |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9238 | mergeObjCGCQualifiers(NewReturnType, OldReturnType); |
| 9239 | if (ResReturnType.isNull()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9240 | return {}; |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9241 | if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) { |
| 9242 | // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo(); |
| 9243 | // In either case, use OldReturnType to build the new function type. |
Simon Pilgrim | 9588ae7 | 2019-10-03 21:47:42 +0000 | [diff] [blame] | 9244 | const auto *F = LHS->castAs<FunctionType>(); |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9245 | if (const auto *FPT = cast<FunctionProtoType>(F)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9246 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 9247 | EPI.ExtInfo = getFunctionExtInfo(LHS); |
Reid Kleckner | 896b32f | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 9248 | QualType ResultType = |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 9249 | getFunctionType(OldReturnType, FPT->getParamTypes(), EPI); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9250 | return ResultType; |
| 9251 | } |
| 9252 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9253 | return {}; |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9254 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9255 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9256 | // If the qualifiers are different, the types can still be merged. |
| 9257 | Qualifiers LQuals = LHSCan.getLocalQualifiers(); |
| 9258 | Qualifiers RQuals = RHSCan.getLocalQualifiers(); |
| 9259 | if (LQuals != RQuals) { |
| 9260 | // If any of these qualifiers are different, we have a type mismatch. |
| 9261 | if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() || |
| 9262 | LQuals.getAddressSpace() != RQuals.getAddressSpace()) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9263 | return {}; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9264 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9265 | // Exactly one GC qualifier difference is allowed: __strong is |
| 9266 | // okay if the other type has no GC qualifier but is an Objective |
| 9267 | // C object pointer (i.e. implicitly strong by default). We fix |
| 9268 | // this by pretending that the unqualified type was actually |
| 9269 | // qualified __strong. |
| 9270 | Qualifiers::GC GC_L = LQuals.getObjCGCAttr(); |
| 9271 | Qualifiers::GC GC_R = RQuals.getObjCGCAttr(); |
| 9272 | assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9273 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9274 | if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9275 | return {}; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9276 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9277 | if (GC_L == Qualifiers::Strong) |
| 9278 | return LHS; |
| 9279 | if (GC_R == Qualifiers::Strong) |
| 9280 | return RHS; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9281 | return {}; |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9282 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9283 | |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9284 | if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) { |
Simon Pilgrim | eed4b12 | 2019-10-02 11:48:06 +0000 | [diff] [blame] | 9285 | QualType LHSBaseQT = LHS->castAs<ObjCObjectPointerType>()->getPointeeType(); |
| 9286 | QualType RHSBaseQT = RHS->castAs<ObjCObjectPointerType>()->getPointeeType(); |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9287 | QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT); |
| 9288 | if (ResQT == LHSBaseQT) |
| 9289 | return LHS; |
| 9290 | if (ResQT == RHSBaseQT) |
| 9291 | return RHS; |
| 9292 | } |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9293 | return {}; |
Fariborz Jahanian | f633ebd | 2010-05-19 21:37:30 +0000 | [diff] [blame] | 9294 | } |
| 9295 | |
Chris Lattner | 4ba0cef | 2008-04-07 07:01:58 +0000 | [diff] [blame] | 9296 | //===----------------------------------------------------------------------===// |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 9297 | // Integer Predicates |
| 9298 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3c91971 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 9299 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 9300 | unsigned ASTContext::getIntWidth(QualType T) const { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9301 | if (const auto *ET = T->getAs<EnumType>()) |
Eli Friedman | ee275c8 | 2009-12-10 22:29:29 +0000 | [diff] [blame] | 9302 | T = ET->getDecl()->getIntegerType(); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 9303 | if (T->isBooleanType()) |
| 9304 | return 1; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 9305 | // For builtin types, just use the standard type sizing method |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 9306 | return (unsigned)getTypeSize(T); |
| 9307 | } |
| 9308 | |
Abramo Bagnara | 1364049 | 2012-09-09 10:21:24 +0000 | [diff] [blame] | 9309 | QualType ASTContext::getCorrespondingUnsignedType(QualType T) const { |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 9310 | assert((T->hasSignedIntegerRepresentation() || T->isSignedFixedPointType()) && |
| 9311 | "Unexpected type"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9312 | |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 9313 | // Turn <4 x signed int> -> <4 x unsigned int> |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9314 | if (const auto *VTy = T->getAs<VectorType>()) |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 9315 | return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()), |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 9316 | VTy->getNumElements(), VTy->getVectorKind()); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 9317 | |
| 9318 | // For enums, we return the unsigned version of the base type. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9319 | if (const auto *ETy = T->getAs<EnumType>()) |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 9320 | T = ETy->getDecl()->getIntegerType(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9321 | |
Simon Pilgrim | 9588ae7 | 2019-10-03 21:47:42 +0000 | [diff] [blame] | 9322 | switch (T->castAs<BuiltinType>()->getKind()) { |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 9323 | case BuiltinType::Char_S: |
| 9324 | case BuiltinType::SChar: |
| 9325 | return UnsignedCharTy; |
| 9326 | case BuiltinType::Short: |
| 9327 | return UnsignedShortTy; |
| 9328 | case BuiltinType::Int: |
| 9329 | return UnsignedIntTy; |
| 9330 | case BuiltinType::Long: |
| 9331 | return UnsignedLongTy; |
| 9332 | case BuiltinType::LongLong: |
| 9333 | return UnsignedLongLongTy; |
Chris Lattner | f122cef | 2009-04-30 02:43:43 +0000 | [diff] [blame] | 9334 | case BuiltinType::Int128: |
| 9335 | return UnsignedInt128Ty; |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 9336 | |
| 9337 | case BuiltinType::ShortAccum: |
| 9338 | return UnsignedShortAccumTy; |
| 9339 | case BuiltinType::Accum: |
| 9340 | return UnsignedAccumTy; |
| 9341 | case BuiltinType::LongAccum: |
| 9342 | return UnsignedLongAccumTy; |
| 9343 | case BuiltinType::SatShortAccum: |
| 9344 | return SatUnsignedShortAccumTy; |
| 9345 | case BuiltinType::SatAccum: |
| 9346 | return SatUnsignedAccumTy; |
| 9347 | case BuiltinType::SatLongAccum: |
| 9348 | return SatUnsignedLongAccumTy; |
| 9349 | case BuiltinType::ShortFract: |
| 9350 | return UnsignedShortFractTy; |
| 9351 | case BuiltinType::Fract: |
| 9352 | return UnsignedFractTy; |
| 9353 | case BuiltinType::LongFract: |
| 9354 | return UnsignedLongFractTy; |
| 9355 | case BuiltinType::SatShortFract: |
| 9356 | return SatUnsignedShortFractTy; |
| 9357 | case BuiltinType::SatFract: |
| 9358 | return SatUnsignedFractTy; |
| 9359 | case BuiltinType::SatLongFract: |
| 9360 | return SatUnsignedLongFractTy; |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 9361 | default: |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 9362 | llvm_unreachable("Unexpected signed integer or fixed point type"); |
Eli Friedman | 4f89ccb | 2008-06-28 06:23:08 +0000 | [diff] [blame] | 9363 | } |
| 9364 | } |
| 9365 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9366 | ASTMutationListener::~ASTMutationListener() = default; |
Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 9367 | |
Richard Smith | 1fa5d64 | 2013-05-11 05:45:24 +0000 | [diff] [blame] | 9368 | void ASTMutationListener::DeducedReturnType(const FunctionDecl *FD, |
| 9369 | QualType ReturnType) {} |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9370 | |
| 9371 | //===----------------------------------------------------------------------===// |
| 9372 | // Builtin Type Computation |
| 9373 | //===----------------------------------------------------------------------===// |
| 9374 | |
| 9375 | /// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9376 | /// pointer over the consumed characters. This returns the resultant type. If |
| 9377 | /// AllowTypeModifiers is false then modifier like * are not parsed, just basic |
| 9378 | /// types. This allows "v2i*" to be parsed as a pointer to a v2i instead of |
| 9379 | /// a vector of "i*". |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9380 | /// |
| 9381 | /// RequiresICE is filled in on return to indicate whether the value is required |
| 9382 | /// to be an Integer Constant Expression. |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 9383 | static QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context, |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9384 | ASTContext::GetBuiltinTypeError &Error, |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9385 | bool &RequiresICE, |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9386 | bool AllowTypeModifiers) { |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9387 | // Modifiers. |
| 9388 | int HowLong = 0; |
| 9389 | bool Signed = false, Unsigned = false; |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9390 | RequiresICE = false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9391 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9392 | // Read the prefixed modifiers first. |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9393 | bool Done = false; |
| 9394 | #ifndef NDEBUG |
Karl-Johan Karlsson | 0e525a4 | 2019-05-16 07:18:02 +0000 | [diff] [blame] | 9395 | bool IsSpecial = false; |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9396 | #endif |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9397 | while (!Done) { |
| 9398 | switch (*Str++) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9399 | default: Done = true; --Str; break; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 9400 | case 'I': |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9401 | RequiresICE = true; |
Chris Lattner | 8473339 | 2010-10-01 07:13:18 +0000 | [diff] [blame] | 9402 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9403 | case 'S': |
| 9404 | assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!"); |
| 9405 | assert(!Signed && "Can't use 'S' modifier multiple times!"); |
| 9406 | Signed = true; |
| 9407 | break; |
| 9408 | case 'U': |
| 9409 | assert(!Signed && "Can't use both 'S' and 'U' modifiers!"); |
Sean Silva | 2a99514 | 2015-01-16 21:44:26 +0000 | [diff] [blame] | 9410 | assert(!Unsigned && "Can't use 'U' modifier multiple times!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9411 | Unsigned = true; |
| 9412 | break; |
| 9413 | case 'L': |
Andrew Savonichev | fa8cd76 | 2019-06-03 12:34:59 +0000 | [diff] [blame] | 9414 | assert(!IsSpecial && "Can't use 'L' with 'W', 'N', 'Z' or 'O' modifiers"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9415 | assert(HowLong <= 2 && "Can't have LLLL modifier"); |
| 9416 | ++HowLong; |
| 9417 | break; |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9418 | case 'N': |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9419 | // 'N' behaves like 'L' for all non LP64 targets and 'int' otherwise. |
Andrew Savonichev | fa8cd76 | 2019-06-03 12:34:59 +0000 | [diff] [blame] | 9420 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!"); |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9421 | assert(HowLong == 0 && "Can't use both 'L' and 'N' modifiers!"); |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9422 | #ifndef NDEBUG |
Karl-Johan Karlsson | 0e525a4 | 2019-05-16 07:18:02 +0000 | [diff] [blame] | 9423 | IsSpecial = true; |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9424 | #endif |
Bruno Cardoso Lopes | afa47c9 | 2017-06-21 02:20:46 +0000 | [diff] [blame] | 9425 | if (Context.getTargetInfo().getLongWidth() == 32) |
| 9426 | ++HowLong; |
| 9427 | break; |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 9428 | case 'W': |
| 9429 | // This modifier represents int64 type. |
Andrew Savonichev | fa8cd76 | 2019-06-03 12:34:59 +0000 | [diff] [blame] | 9430 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!"); |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 9431 | assert(HowLong == 0 && "Can't use both 'L' and 'W' modifiers!"); |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9432 | #ifndef NDEBUG |
Karl-Johan Karlsson | 0e525a4 | 2019-05-16 07:18:02 +0000 | [diff] [blame] | 9433 | IsSpecial = true; |
Eric Christopher | 50daf5f | 2017-07-10 21:28:54 +0000 | [diff] [blame] | 9434 | #endif |
Kevin Qin | ad64f6d | 2014-02-24 02:45:03 +0000 | [diff] [blame] | 9435 | switch (Context.getTargetInfo().getInt64Type()) { |
| 9436 | default: |
| 9437 | llvm_unreachable("Unexpected integer type"); |
| 9438 | case TargetInfo::SignedLong: |
| 9439 | HowLong = 1; |
| 9440 | break; |
| 9441 | case TargetInfo::SignedLongLong: |
| 9442 | HowLong = 2; |
| 9443 | break; |
| 9444 | } |
Duncan P. N. Exon Smith | eae8caa | 2017-06-14 21:26:31 +0000 | [diff] [blame] | 9445 | break; |
Karl-Johan Karlsson | 0e525a4 | 2019-05-16 07:18:02 +0000 | [diff] [blame] | 9446 | case 'Z': |
| 9447 | // This modifier represents int32 type. |
Andrew Savonichev | fa8cd76 | 2019-06-03 12:34:59 +0000 | [diff] [blame] | 9448 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!"); |
Karl-Johan Karlsson | 0e525a4 | 2019-05-16 07:18:02 +0000 | [diff] [blame] | 9449 | assert(HowLong == 0 && "Can't use both 'L' and 'Z' modifiers!"); |
| 9450 | #ifndef NDEBUG |
| 9451 | IsSpecial = true; |
| 9452 | #endif |
| 9453 | switch (Context.getTargetInfo().getIntTypeByWidth(32, true)) { |
| 9454 | default: |
| 9455 | llvm_unreachable("Unexpected integer type"); |
| 9456 | case TargetInfo::SignedInt: |
| 9457 | HowLong = 0; |
| 9458 | break; |
| 9459 | case TargetInfo::SignedLong: |
| 9460 | HowLong = 1; |
| 9461 | break; |
| 9462 | case TargetInfo::SignedLongLong: |
| 9463 | HowLong = 2; |
| 9464 | break; |
| 9465 | } |
| 9466 | break; |
Andrew Savonichev | fa8cd76 | 2019-06-03 12:34:59 +0000 | [diff] [blame] | 9467 | case 'O': |
| 9468 | assert(!IsSpecial && "Can't use two 'N', 'W', 'Z' or 'O' modifiers!"); |
| 9469 | assert(HowLong == 0 && "Can't use both 'L' and 'O' modifiers!"); |
| 9470 | #ifndef NDEBUG |
| 9471 | IsSpecial = true; |
| 9472 | #endif |
| 9473 | if (Context.getLangOpts().OpenCL) |
| 9474 | HowLong = 1; |
| 9475 | else |
| 9476 | HowLong = 2; |
| 9477 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9478 | } |
| 9479 | } |
| 9480 | |
| 9481 | QualType Type; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9482 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9483 | // Read the base type. |
| 9484 | switch (*Str++) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 9485 | default: llvm_unreachable("Unknown builtin type letter!"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9486 | case 'v': |
| 9487 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 9488 | "Bad modifiers used with 'v'!"); |
| 9489 | Type = Context.VoidTy; |
| 9490 | break; |
Jack Carter | 24bef98 | 2013-08-15 15:16:57 +0000 | [diff] [blame] | 9491 | case 'h': |
| 9492 | assert(HowLong == 0 && !Signed && !Unsigned && |
Sean Silva | 2a99514 | 2015-01-16 21:44:26 +0000 | [diff] [blame] | 9493 | "Bad modifiers used with 'h'!"); |
Jack Carter | 24bef98 | 2013-08-15 15:16:57 +0000 | [diff] [blame] | 9494 | Type = Context.HalfTy; |
| 9495 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9496 | case 'f': |
| 9497 | assert(HowLong == 0 && !Signed && !Unsigned && |
| 9498 | "Bad modifiers used with 'f'!"); |
| 9499 | Type = Context.FloatTy; |
| 9500 | break; |
| 9501 | case 'd': |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9502 | assert(HowLong < 3 && !Signed && !Unsigned && |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9503 | "Bad modifiers used with 'd'!"); |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9504 | if (HowLong == 1) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9505 | Type = Context.LongDoubleTy; |
Benjamin Kramer | dfecbe9 | 2018-01-06 21:49:54 +0000 | [diff] [blame] | 9506 | else if (HowLong == 2) |
| 9507 | Type = Context.Float128Ty; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9508 | else |
| 9509 | Type = Context.DoubleTy; |
| 9510 | break; |
| 9511 | case 's': |
| 9512 | assert(HowLong == 0 && "Bad modifiers used with 's'!"); |
| 9513 | if (Unsigned) |
| 9514 | Type = Context.UnsignedShortTy; |
| 9515 | else |
| 9516 | Type = Context.ShortTy; |
| 9517 | break; |
| 9518 | case 'i': |
| 9519 | if (HowLong == 3) |
| 9520 | Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty; |
| 9521 | else if (HowLong == 2) |
| 9522 | Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 9523 | else if (HowLong == 1) |
| 9524 | Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 9525 | else |
| 9526 | Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy; |
| 9527 | break; |
| 9528 | case 'c': |
| 9529 | assert(HowLong == 0 && "Bad modifiers used with 'c'!"); |
| 9530 | if (Signed) |
| 9531 | Type = Context.SignedCharTy; |
| 9532 | else if (Unsigned) |
| 9533 | Type = Context.UnsignedCharTy; |
| 9534 | else |
| 9535 | Type = Context.CharTy; |
| 9536 | break; |
| 9537 | case 'b': // boolean |
| 9538 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!"); |
| 9539 | Type = Context.BoolTy; |
| 9540 | break; |
| 9541 | case 'z': // size_t. |
| 9542 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!"); |
| 9543 | Type = Context.getSizeType(); |
| 9544 | break; |
Richard Smith | 8110c9d | 2016-11-29 19:45:17 +0000 | [diff] [blame] | 9545 | case 'w': // wchar_t. |
| 9546 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'w'!"); |
| 9547 | Type = Context.getWideCharType(); |
| 9548 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9549 | case 'F': |
| 9550 | Type = Context.getCFConstantStringType(); |
| 9551 | break; |
Fariborz Jahanian | d11da7e | 2010-11-09 21:38:20 +0000 | [diff] [blame] | 9552 | case 'G': |
| 9553 | Type = Context.getObjCIdType(); |
| 9554 | break; |
| 9555 | case 'H': |
| 9556 | Type = Context.getObjCSelType(); |
| 9557 | break; |
Fariborz Jahanian | cb6c867 | 2013-01-04 18:45:40 +0000 | [diff] [blame] | 9558 | case 'M': |
| 9559 | Type = Context.getObjCSuperType(); |
| 9560 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9561 | case 'a': |
| 9562 | Type = Context.getBuiltinVaListType(); |
| 9563 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
| 9564 | break; |
| 9565 | case 'A': |
| 9566 | // This is a "reference" to a va_list; however, what exactly |
| 9567 | // this means depends on how va_list is defined. There are two |
| 9568 | // different kinds of va_list: ones passed by value, and ones |
| 9569 | // passed by reference. An example of a by-value va_list is |
| 9570 | // x86, where va_list is a char*. An example of by-ref va_list |
| 9571 | // is x86-64, where va_list is a __va_list_tag[1]. For x86, |
| 9572 | // we want this argument to be a char*&; for x86-64, we want |
| 9573 | // it to be a __va_list_tag*. |
| 9574 | Type = Context.getBuiltinVaListType(); |
| 9575 | assert(!Type.isNull() && "builtin va list type not initialized!"); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9576 | if (Type->isArrayType()) |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9577 | Type = Context.getArrayDecayedType(Type); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9578 | else |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9579 | Type = Context.getLValueReferenceType(Type); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9580 | break; |
| 9581 | case 'V': { |
| 9582 | char *End; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9583 | unsigned NumElements = strtoul(Str, &End, 10); |
| 9584 | assert(End != Str && "Missing vector size"); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9585 | Str = End; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9586 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9587 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9588 | RequiresICE, false); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9589 | assert(!RequiresICE && "Can't require vector ICE"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9590 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9591 | // TODO: No way to make AltiVec vectors in builtins yet. |
Chris Lattner | 37141f4 | 2010-06-23 06:00:24 +0000 | [diff] [blame] | 9592 | Type = Context.getVectorType(ElementType, NumElements, |
Bob Wilson | aeb5644 | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 9593 | VectorType::GenericVector); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9594 | break; |
| 9595 | } |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9596 | case 'E': { |
| 9597 | char *End; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9598 | |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9599 | unsigned NumElements = strtoul(Str, &End, 10); |
| 9600 | assert(End != Str && "Missing vector size"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9601 | |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9602 | Str = End; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9603 | |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9604 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9605 | false); |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9606 | Type = Context.getExtVectorType(ElementType, NumElements); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9607 | break; |
Douglas Gregor | fed6699 | 2012-06-07 18:08:25 +0000 | [diff] [blame] | 9608 | } |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 9609 | case 'X': { |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9610 | QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE, |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9611 | false); |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9612 | assert(!RequiresICE && "Can't require complex ICE"); |
Douglas Gregor | 40ef7c5 | 2009-09-28 21:45:01 +0000 | [diff] [blame] | 9613 | Type = Context.getComplexType(ElementType); |
| 9614 | break; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9615 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 9616 | case 'Y': |
Fariborz Jahanian | 73952fc | 2011-08-23 23:33:09 +0000 | [diff] [blame] | 9617 | Type = Context.getPointerDiffType(); |
| 9618 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 9619 | case 'P': |
Douglas Gregor | 27821ce | 2009-07-07 16:35:42 +0000 | [diff] [blame] | 9620 | Type = Context.getFILEType(); |
| 9621 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 9622 | Error = ASTContext::GE_Missing_stdio; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9623 | return {}; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9624 | } |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 9625 | break; |
Chris Lattner | a58b3af | 2009-07-28 22:49:34 +0000 | [diff] [blame] | 9626 | case 'J': |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 9627 | if (Signed) |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 9628 | Type = Context.getsigjmp_bufType(); |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 9629 | else |
| 9630 | Type = Context.getjmp_bufType(); |
| 9631 | |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 9632 | if (Type.isNull()) { |
Mike Stump | 93246cc | 2009-07-28 23:57:15 +0000 | [diff] [blame] | 9633 | Error = ASTContext::GE_Missing_setjmp; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9634 | return {}; |
Mike Stump | 2adb4da | 2009-07-28 23:47:15 +0000 | [diff] [blame] | 9635 | } |
| 9636 | break; |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 9637 | case 'K': |
| 9638 | assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!"); |
| 9639 | Type = Context.getucontext_tType(); |
| 9640 | |
| 9641 | if (Type.isNull()) { |
| 9642 | Error = ASTContext::GE_Missing_ucontext; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9643 | return {}; |
Rafael Espindola | 6cfa82b | 2011-11-13 21:51:09 +0000 | [diff] [blame] | 9644 | } |
| 9645 | break; |
Eli Friedman | 4e91899e | 2012-11-27 02:58:24 +0000 | [diff] [blame] | 9646 | case 'p': |
| 9647 | Type = Context.getProcessIDType(); |
| 9648 | break; |
Mike Stump | a4de80b | 2009-07-28 02:25:19 +0000 | [diff] [blame] | 9649 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9650 | |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9651 | // If there are modifiers and if we're allowed to parse them, go for it. |
| 9652 | Done = !AllowTypeModifiers; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9653 | while (!Done) { |
John McCall | b8b9466 | 2010-03-12 04:21:28 +0000 | [diff] [blame] | 9654 | switch (char c = *Str++) { |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9655 | default: Done = true; --Str; break; |
| 9656 | case '*': |
| 9657 | case '&': { |
| 9658 | // Both pointers and references can have their pointee types |
| 9659 | // qualified with an address space. |
| 9660 | char *End; |
| 9661 | unsigned AddrSpace = strtoul(Str, &End, 10); |
Matt Arsenault | c65f966 | 2018-08-02 12:14:28 +0000 | [diff] [blame] | 9662 | if (End != Str) { |
| 9663 | // Note AddrSpace == 0 is not the same as an unspecified address space. |
| 9664 | Type = Context.getAddrSpaceQualType( |
| 9665 | Type, |
| 9666 | Context.getLangASForBuiltinAddressSpace(AddrSpace)); |
Chris Lattner | dc226c2 | 2010-10-01 22:42:38 +0000 | [diff] [blame] | 9667 | Str = End; |
| 9668 | } |
| 9669 | if (c == '*') |
| 9670 | Type = Context.getPointerType(Type); |
| 9671 | else |
| 9672 | Type = Context.getLValueReferenceType(Type); |
| 9673 | break; |
| 9674 | } |
| 9675 | // FIXME: There's no way to have a built-in with an rvalue ref arg. |
| 9676 | case 'C': |
| 9677 | Type = Type.withConst(); |
| 9678 | break; |
| 9679 | case 'D': |
| 9680 | Type = Context.getVolatileType(Type); |
| 9681 | break; |
Ted Kremenek | f2a2f5f | 2012-01-20 21:40:12 +0000 | [diff] [blame] | 9682 | case 'R': |
| 9683 | Type = Type.withRestrict(); |
| 9684 | break; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9685 | } |
| 9686 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9687 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9688 | assert((!RequiresICE || Type->isIntegralOrEnumerationType()) && |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9689 | "Integer constant 'I' type must be an integer"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9690 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9691 | return Type; |
| 9692 | } |
| 9693 | |
| 9694 | /// GetBuiltinType - Return the type for the specified builtin. |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9695 | QualType ASTContext::GetBuiltinType(unsigned Id, |
| 9696 | GetBuiltinTypeError &Error, |
| 9697 | unsigned *IntegerConstantArgs) const { |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 9698 | const char *TypeStr = BuiltinInfo.getTypeString(Id); |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 9699 | if (TypeStr[0] == '\0') { |
| 9700 | Error = GE_Missing_type; |
| 9701 | return {}; |
| 9702 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9703 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9704 | SmallVector<QualType, 8> ArgTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9705 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9706 | bool RequiresICE = false; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9707 | Error = GE_None; |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9708 | QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error, |
| 9709 | RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9710 | if (Error != GE_None) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9711 | return {}; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9712 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9713 | assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9714 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9715 | while (TypeStr[0] && TypeStr[0] != '.') { |
Chandler Carruth | 45bbe01 | 2017-03-24 09:11:57 +0000 | [diff] [blame] | 9716 | QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9717 | if (Error != GE_None) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9718 | return {}; |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9719 | |
Chris Lattner | bd6e693 | 2010-10-01 22:53:11 +0000 | [diff] [blame] | 9720 | // If this argument is required to be an IntegerConstantExpression and the |
| 9721 | // caller cares, fill in the bitmask we return. |
| 9722 | if (RequiresICE && IntegerConstantArgs) |
| 9723 | *IntegerConstantArgs |= 1 << ArgTypes.size(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 9724 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9725 | // Do array -> pointer decay. The builtin should use the decayed type. |
| 9726 | if (Ty->isArrayType()) |
| 9727 | Ty = getArrayDecayedType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9728 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9729 | ArgTypes.push_back(Ty); |
| 9730 | } |
| 9731 | |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 9732 | if (Id == Builtin::BI__GetExceptionInfo) |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9733 | return {}; |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 9734 | |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9735 | assert((TypeStr[0] != '.' || TypeStr[1] == 0) && |
| 9736 | "'.' should only occur at end of builtin type list!"); |
| 9737 | |
Erich Keane | 881e83d | 2019-03-04 14:54:52 +0000 | [diff] [blame] | 9738 | bool Variadic = (TypeStr[0] == '.'); |
| 9739 | |
Erich Keane | 0002281 | 2019-05-23 16:05:21 +0000 | [diff] [blame] | 9740 | FunctionType::ExtInfo EI(getDefaultCallingConvention( |
| 9741 | Variadic, /*IsCXXMethod=*/false, /*IsBuiltin=*/true)); |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 9742 | if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true); |
| 9743 | |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 9744 | |
Richard Smith | 836de6b | 2016-12-19 23:59:34 +0000 | [diff] [blame] | 9745 | // We really shouldn't be making a no-proto type here. |
| 9746 | if (ArgTypes.empty() && Variadic && !getLangOpts().CPlusPlus) |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 9747 | return getFunctionNoProtoType(ResType, EI); |
Douglas Gregor | 36c569f | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 9748 | |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9749 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 991eb4b | 2010-12-21 00:44:39 +0000 | [diff] [blame] | 9750 | EPI.ExtInfo = EI; |
| 9751 | EPI.Variadic = Variadic; |
Richard Smith | 391fb86 | 2016-10-18 07:13:55 +0000 | [diff] [blame] | 9752 | if (getLangOpts().CPlusPlus && BuiltinInfo.isNoThrow(Id)) |
| 9753 | EPI.ExceptionSpec.Type = |
| 9754 | getLangOpts().CPlusPlus11 ? EST_BasicNoexcept : EST_DynamicNone; |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9755 | |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 9756 | return getFunctionType(ResType, ArgTypes, EPI); |
Chris Lattner | ecd79c6 | 2009-06-14 00:45:47 +0000 | [diff] [blame] | 9757 | } |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 9758 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9759 | static GVALinkage basicGVALinkageForFunction(const ASTContext &Context, |
| 9760 | const FunctionDecl *FD) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9761 | if (!FD->isExternallyVisible()) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9762 | return GVA_Internal; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9763 | |
Richard Smith | e2467b7 | 2017-11-16 23:54:56 +0000 | [diff] [blame] | 9764 | // Non-user-provided functions get emitted as weak definitions with every |
| 9765 | // use, no matter whether they've been explicitly instantiated etc. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9766 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
Richard Smith | e2467b7 | 2017-11-16 23:54:56 +0000 | [diff] [blame] | 9767 | if (!MD->isUserProvided()) |
| 9768 | return GVA_DiscardableODR; |
| 9769 | |
Yaron Keren | 4cd211b | 2017-02-22 14:32:39 +0000 | [diff] [blame] | 9770 | GVALinkage External; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9771 | switch (FD->getTemplateSpecializationKind()) { |
| 9772 | case TSK_Undeclared: |
| 9773 | case TSK_ExplicitSpecialization: |
| 9774 | External = GVA_StrongExternal; |
| 9775 | break; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9776 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9777 | case TSK_ExplicitInstantiationDefinition: |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 9778 | return GVA_StrongODR; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9779 | |
David Majnemer | c3d0733 | 2014-05-15 06:25:57 +0000 | [diff] [blame] | 9780 | // C++11 [temp.explicit]p10: |
| 9781 | // [ Note: The intent is that an inline function that is the subject of |
| 9782 | // an explicit instantiation declaration will still be implicitly |
| 9783 | // instantiated when used so that the body can be considered for |
| 9784 | // inlining, but that no out-of-line copy of the inline function would be |
| 9785 | // generated in the translation unit. -- end note ] |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9786 | case TSK_ExplicitInstantiationDeclaration: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9787 | return GVA_AvailableExternally; |
| 9788 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9789 | case TSK_ImplicitInstantiation: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9790 | External = GVA_DiscardableODR; |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9791 | break; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9792 | } |
| 9793 | |
| 9794 | if (!FD->isInlined()) |
| 9795 | return External; |
David Majnemer | 62f0ffd | 2013-08-01 17:26:42 +0000 | [diff] [blame] | 9796 | |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 9797 | if ((!Context.getLangOpts().CPlusPlus && |
| 9798 | !Context.getTargetInfo().getCXXABI().isMicrosoft() && |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9799 | !FD->hasAttr<DLLExportAttr>()) || |
David Majnemer | 62f0ffd | 2013-08-01 17:26:42 +0000 | [diff] [blame] | 9800 | FD->hasAttr<GNUInlineAttr>()) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9801 | // FIXME: This doesn't match gcc's behavior for dllexport inline functions. |
| 9802 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9803 | // GNU or C99 inline semantics. Determine whether this symbol should be |
| 9804 | // externally visible. |
| 9805 | if (FD->isInlineDefinitionExternallyVisible()) |
| 9806 | return External; |
| 9807 | |
| 9808 | // C99 inline semantics, where the symbol is not externally visible. |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9809 | return GVA_AvailableExternally; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9810 | } |
| 9811 | |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 9812 | // Functions specified with extern and inline in -fms-compatibility mode |
| 9813 | // forcibly get emitted. While the body of the function cannot be later |
| 9814 | // replaced, the function definition cannot be discarded. |
David Majnemer | 7376870 | 2015-03-20 00:02:27 +0000 | [diff] [blame] | 9815 | if (FD->isMSExternInline()) |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 9816 | return GVA_StrongODR; |
| 9817 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9818 | return GVA_DiscardableODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9819 | } |
| 9820 | |
Artem Belevich | ca2b951 | 2016-05-02 20:30:03 +0000 | [diff] [blame] | 9821 | static GVALinkage adjustGVALinkageForAttributes(const ASTContext &Context, |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9822 | const Decl *D, GVALinkage L) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9823 | // See http://msdn.microsoft.com/en-us/library/xa0d9ste.aspx |
| 9824 | // dllexport/dllimport on inline functions. |
| 9825 | if (D->hasAttr<DLLImportAttr>()) { |
| 9826 | if (L == GVA_DiscardableODR || L == GVA_StrongODR) |
| 9827 | return GVA_AvailableExternally; |
Artem Belevich | ca2b951 | 2016-05-02 20:30:03 +0000 | [diff] [blame] | 9828 | } else if (D->hasAttr<DLLExportAttr>()) { |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9829 | if (L == GVA_DiscardableODR) |
| 9830 | return GVA_StrongODR; |
Artem Belevich | ca2b951 | 2016-05-02 20:30:03 +0000 | [diff] [blame] | 9831 | } else if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice && |
| 9832 | D->hasAttr<CUDAGlobalAttr>()) { |
| 9833 | // Device-side functions with __global__ attribute must always be |
| 9834 | // visible externally so they can be launched from host. |
| 9835 | if (L == GVA_DiscardableODR || L == GVA_Internal) |
| 9836 | return GVA_StrongODR; |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9837 | } |
| 9838 | return L; |
| 9839 | } |
| 9840 | |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9841 | /// Adjust the GVALinkage for a declaration based on what an external AST source |
| 9842 | /// knows about whether there can be other definitions of this declaration. |
| 9843 | static GVALinkage |
| 9844 | adjustGVALinkageForExternalDefinitionKind(const ASTContext &Ctx, const Decl *D, |
| 9845 | GVALinkage L) { |
| 9846 | ExternalASTSource *Source = Ctx.getExternalSource(); |
| 9847 | if (!Source) |
| 9848 | return L; |
| 9849 | |
| 9850 | switch (Source->hasExternalDefinitions(D)) { |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9851 | case ExternalASTSource::EK_Never: |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9852 | // Other translation units rely on us to provide the definition. |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9853 | if (L == GVA_DiscardableODR) |
| 9854 | return GVA_StrongODR; |
| 9855 | break; |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9856 | |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9857 | case ExternalASTSource::EK_Always: |
| 9858 | return GVA_AvailableExternally; |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9859 | |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 9860 | case ExternalASTSource::EK_ReplyHazy: |
| 9861 | break; |
| 9862 | } |
| 9863 | return L; |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9864 | } |
| 9865 | |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9866 | GVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) const { |
| 9867 | return adjustGVALinkageForExternalDefinitionKind(*this, FD, |
| 9868 | adjustGVALinkageForAttributes(*this, FD, |
| 9869 | basicGVALinkageForFunction(*this, FD))); |
| 9870 | } |
| 9871 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9872 | static GVALinkage basicGVALinkageForVariable(const ASTContext &Context, |
| 9873 | const VarDecl *VD) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9874 | if (!VD->isExternallyVisible()) |
| 9875 | return GVA_Internal; |
| 9876 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9877 | if (VD->isStaticLocal()) { |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9878 | const DeclContext *LexicalContext = VD->getParentFunctionOrMethod(); |
| 9879 | while (LexicalContext && !isa<FunctionDecl>(LexicalContext)) |
| 9880 | LexicalContext = LexicalContext->getLexicalParent(); |
| 9881 | |
David Blaikie | eb21001 | 2017-01-27 23:11:10 +0000 | [diff] [blame] | 9882 | // ObjC Blocks can create local variables that don't have a FunctionDecl |
| 9883 | // LexicalContext. |
| 9884 | if (!LexicalContext) |
| 9885 | return GVA_DiscardableODR; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9886 | |
David Blaikie | eb21001 | 2017-01-27 23:11:10 +0000 | [diff] [blame] | 9887 | // Otherwise, let the static local variable inherit its linkage from the |
| 9888 | // nearest enclosing function. |
| 9889 | auto StaticLocalLinkage = |
| 9890 | Context.GetGVALinkageForFunction(cast<FunctionDecl>(LexicalContext)); |
| 9891 | |
| 9892 | // Itanium ABI 5.2.2: "Each COMDAT group [for a static local variable] must |
| 9893 | // be emitted in any object with references to the symbol for the object it |
| 9894 | // contains, whether inline or out-of-line." |
| 9895 | // Similar behavior is observed with MSVC. An alternative ABI could use |
| 9896 | // StrongODR/AvailableExternally to match the function, but none are |
| 9897 | // known/supported currently. |
| 9898 | if (StaticLocalLinkage == GVA_StrongODR || |
| 9899 | StaticLocalLinkage == GVA_AvailableExternally) |
| 9900 | return GVA_DiscardableODR; |
| 9901 | return StaticLocalLinkage; |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9902 | } |
| 9903 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 9904 | // MSVC treats in-class initialized static data members as definitions. |
| 9905 | // By giving them non-strong linkage, out-of-line definitions won't |
| 9906 | // cause link errors. |
| 9907 | if (Context.isMSStaticDataMemberInlineDefinition(VD)) |
| 9908 | return GVA_DiscardableODR; |
| 9909 | |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 9910 | // Most non-template variables have strong linkage; inline variables are |
| 9911 | // linkonce_odr or (occasionally, for compatibility) weak_odr. |
| 9912 | GVALinkage StrongLinkage; |
| 9913 | switch (Context.getInlineVariableDefinitionKind(VD)) { |
| 9914 | case ASTContext::InlineVariableDefinitionKind::None: |
| 9915 | StrongLinkage = GVA_StrongExternal; |
| 9916 | break; |
| 9917 | case ASTContext::InlineVariableDefinitionKind::Weak: |
| 9918 | case ASTContext::InlineVariableDefinitionKind::WeakUnknown: |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 9919 | StrongLinkage = GVA_DiscardableODR; |
Richard Smith | d9b9009 | 2016-07-02 01:32:16 +0000 | [diff] [blame] | 9920 | break; |
| 9921 | case ASTContext::InlineVariableDefinitionKind::Strong: |
| 9922 | StrongLinkage = GVA_StrongODR; |
| 9923 | break; |
| 9924 | } |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 9925 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 9926 | switch (VD->getTemplateSpecializationKind()) { |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9927 | case TSK_Undeclared: |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 9928 | return StrongLinkage; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9929 | |
David Majnemer | 6d1780c | 2015-07-17 23:36:49 +0000 | [diff] [blame] | 9930 | case TSK_ExplicitSpecialization: |
Hans Wennborg | 4bdd513 | 2019-09-25 11:09:46 +0000 | [diff] [blame] | 9931 | return Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 9932 | VD->isStaticDataMember() |
| 9933 | ? GVA_StrongODR |
| 9934 | : StrongLinkage; |
David Majnemer | 6d1780c | 2015-07-17 23:36:49 +0000 | [diff] [blame] | 9935 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9936 | case TSK_ExplicitInstantiationDefinition: |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 9937 | return GVA_StrongODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9938 | |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9939 | case TSK_ExplicitInstantiationDeclaration: |
| 9940 | return GVA_AvailableExternally; |
| 9941 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 9942 | case TSK_ImplicitInstantiation: |
David Majnemer | 27d69db | 2014-04-28 22:17:59 +0000 | [diff] [blame] | 9943 | return GVA_DiscardableODR; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9944 | } |
Rafael Espindola | 27699c8 | 2013-05-13 14:05:53 +0000 | [diff] [blame] | 9945 | |
| 9946 | llvm_unreachable("Invalid Linkage!"); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9947 | } |
| 9948 | |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9949 | GVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) { |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 9950 | return adjustGVALinkageForExternalDefinitionKind(*this, VD, |
| 9951 | adjustGVALinkageForAttributes(*this, VD, |
| 9952 | basicGVALinkageForVariable(*this, VD))); |
Hans Wennborg | b0f2f14 | 2014-05-15 22:07:49 +0000 | [diff] [blame] | 9953 | } |
| 9954 | |
David Blaikie | e6b7c28 | 2017-04-11 20:46:34 +0000 | [diff] [blame] | 9955 | bool ASTContext::DeclMustBeEmitted(const Decl *D) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9956 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 9957 | if (!VD->isFileVarDecl()) |
| 9958 | return false; |
Renato Golin | 9258aa5 | 2014-05-21 10:40:27 +0000 | [diff] [blame] | 9959 | // Global named register variables (GNU extension) are never emitted. |
| 9960 | if (VD->getStorageClass() == SC_Register) |
| 9961 | return false; |
Richard Smith | 7747ce2 | 2015-08-19 20:49:38 +0000 | [diff] [blame] | 9962 | if (VD->getDescribedVarTemplate() || |
| 9963 | isa<VarTemplatePartialSpecializationDecl>(VD)) |
| 9964 | return false; |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 9965 | } else if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 9966 | // We never need to emit an uninstantiated function template. |
| 9967 | if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate) |
| 9968 | return false; |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 9969 | } else if (isa<PragmaCommentDecl>(D)) |
| 9970 | return true; |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 9971 | else if (isa<PragmaDetectMismatchDecl>(D)) |
| 9972 | return true; |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 9973 | else if (isa<OMPThreadPrivateDecl>(D)) |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 9974 | return !D->getDeclContext()->isDependentContext(); |
Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 9975 | else if (isa<OMPAllocateDecl>(D)) |
| 9976 | return !D->getDeclContext()->isDependentContext(); |
Michael Kruse | d47b943 | 2019-08-05 18:43:21 +0000 | [diff] [blame] | 9977 | else if (isa<OMPDeclareReductionDecl>(D) || isa<OMPDeclareMapperDecl>(D)) |
Alexey Bataev | c5b1d32 | 2016-03-04 09:22:22 +0000 | [diff] [blame] | 9978 | return !D->getDeclContext()->isDependentContext(); |
Richard Smith | dc1f042 | 2016-07-20 19:10:16 +0000 | [diff] [blame] | 9979 | else if (isa<ImportDecl>(D)) |
| 9980 | return true; |
Alexey Bataev | 9772000 | 2014-11-11 04:05:39 +0000 | [diff] [blame] | 9981 | else |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 9982 | return false; |
| 9983 | |
Hans Wennborg | 08c5a7b | 2018-06-25 13:23:49 +0000 | [diff] [blame] | 9984 | if (D->isFromASTFile() && !LangOpts.BuildingPCHWithObjectFile) { |
| 9985 | assert(getExternalSource() && "It's from an AST file; must have a source."); |
| 9986 | // On Windows, PCH files are built together with an object file. If this |
| 9987 | // declaration comes from such a PCH and DeclMustBeEmitted would return |
| 9988 | // true, it would have returned true and the decl would have been emitted |
| 9989 | // into that object file, so it doesn't need to be emitted here. |
| 9990 | // Note that decls are still emitted if they're referenced, as usual; |
| 9991 | // DeclMustBeEmitted is used to decide whether a decl must be emitted even |
| 9992 | // if it's not referenced. |
| 9993 | // |
| 9994 | // Explicit template instantiation definitions are tricky. If there was an |
| 9995 | // explicit template instantiation decl in the PCH before, it will look like |
| 9996 | // the definition comes from there, even if that was just the declaration. |
| 9997 | // (Explicit instantiation defs of variable templates always get emitted.) |
| 9998 | bool IsExpInstDef = |
| 9999 | isa<FunctionDecl>(D) && |
| 10000 | cast<FunctionDecl>(D)->getTemplateSpecializationKind() == |
| 10001 | TSK_ExplicitInstantiationDefinition; |
| 10002 | |
Hans Wennborg | b51a703 | 2018-09-14 15:18:30 +0000 | [diff] [blame] | 10003 | // Implicit member function definitions, such as operator= might not be |
| 10004 | // marked as template specializations, since they're not coming from a |
| 10005 | // template but synthesized directly on the class. |
| 10006 | IsExpInstDef |= |
| 10007 | isa<CXXMethodDecl>(D) && |
| 10008 | cast<CXXMethodDecl>(D)->getParent()->getTemplateSpecializationKind() == |
| 10009 | TSK_ExplicitInstantiationDefinition; |
| 10010 | |
Hans Wennborg | 08c5a7b | 2018-06-25 13:23:49 +0000 | [diff] [blame] | 10011 | if (getExternalSource()->DeclIsFromPCHWithObjectFile(D) && !IsExpInstDef) |
| 10012 | return false; |
| 10013 | } |
| 10014 | |
Richard Smith | 5205a8c | 2013-04-01 20:22:16 +0000 | [diff] [blame] | 10015 | // If this is a member of a class template, we do not need to emit it. |
| 10016 | if (D->getDeclContext()->isDependentContext()) |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 10017 | return false; |
| 10018 | |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 10019 | // Weak references don't produce any output by themselves. |
| 10020 | if (D->hasAttr<WeakRefAttr>()) |
| 10021 | return false; |
| 10022 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 10023 | // Aliases and used decls are required. |
| 10024 | if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>()) |
| 10025 | return true; |
| 10026 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 10027 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 10028 | // Forward declarations aren't required. |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 10029 | if (!FD->doesThisDeclarationHaveABody()) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 10030 | return FD->doesDeclarationForceExternallyVisibleDefinition(); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 10031 | |
| 10032 | // Constructors and destructors are required. |
| 10033 | if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>()) |
| 10034 | return true; |
Hans Wennborg | 08c5a7b | 2018-06-25 13:23:49 +0000 | [diff] [blame] | 10035 | |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 10036 | // The key function for a class is required. This rule only comes |
| 10037 | // into play when inline functions can be key functions, though. |
| 10038 | if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) { |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 10039 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { |
John McCall | 6bd2a89 | 2013-01-25 22:31:03 +0000 | [diff] [blame] | 10040 | const CXXRecordDecl *RD = MD->getParent(); |
| 10041 | if (MD->isOutOfLine() && RD->isDynamicClass()) { |
| 10042 | const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD); |
| 10043 | if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl()) |
| 10044 | return true; |
| 10045 | } |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 10046 | } |
| 10047 | } |
| 10048 | |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 10049 | GVALinkage Linkage = GetGVALinkageForFunction(FD); |
| 10050 | |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 10051 | // static, static inline, always_inline, and extern inline functions can |
| 10052 | // always be deferred. Normal inline functions can be deferred in C99/C++. |
| 10053 | // Implicit template instantiations can also be deferred in C++. |
David Blaikie | 9ffe5a3 | 2017-01-30 05:00:26 +0000 | [diff] [blame] | 10054 | return !isDiscardableGVALinkage(Linkage); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 10055 | } |
Hans Wennborg | 08c5a7b | 2018-06-25 13:23:49 +0000 | [diff] [blame] | 10056 | |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 10057 | const auto *VD = cast<VarDecl>(D); |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 10058 | assert(VD->isFileVarDecl() && "Expected file scoped var"); |
| 10059 | |
Alexey Bataev | d01b749 | 2018-08-15 19:45:12 +0000 | [diff] [blame] | 10060 | // If the decl is marked as `declare target to`, it should be emitted for the |
| 10061 | // host and for the device. |
| 10062 | if (LangOpts.OpenMP && |
| 10063 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) |
| 10064 | return true; |
| 10065 | |
Hans Wennborg | 56fc62b | 2014-07-17 20:25:23 +0000 | [diff] [blame] | 10066 | if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly && |
| 10067 | !isMSStaticDataMemberInlineDefinition(VD)) |
Argyrios Kyrtzidis | 6e03a74 | 2010-07-29 20:07:52 +0000 | [diff] [blame] | 10068 | return false; |
| 10069 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 10070 | // Variables that can be needed in other TUs are required. |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 10071 | auto Linkage = GetGVALinkageForVariable(VD); |
| 10072 | if (!isDiscardableGVALinkage(Linkage)) |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 10073 | return true; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 10074 | |
Richard Smith | a465362 | 2017-09-06 20:01:14 +0000 | [diff] [blame] | 10075 | // We never need to emit a variable that is available in another TU. |
| 10076 | if (Linkage == GVA_AvailableExternally) |
| 10077 | return false; |
| 10078 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 10079 | // Variables that have destruction with side-effects are required. |
Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 10080 | if (VD->needsDestruction(*this)) |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 10081 | return true; |
| 10082 | |
| 10083 | // Variables that have initialization with side-effects are required. |
Richard Smith | 7747ce2 | 2015-08-19 20:49:38 +0000 | [diff] [blame] | 10084 | if (VD->getInit() && VD->getInit()->HasSideEffects(*this) && |
Richard Smith | 187ffb4 | 2017-01-20 01:19:46 +0000 | [diff] [blame] | 10085 | // We can get a value-dependent initializer during error recovery. |
| 10086 | (VD->getInit()->isValueDependent() || !VD->evaluateValue())) |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 10087 | return true; |
| 10088 | |
Richard Smith | da38363 | 2016-08-15 01:33:41 +0000 | [diff] [blame] | 10089 | // Likewise, variables with tuple-like bindings are required if their |
| 10090 | // bindings have side-effects. |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 10091 | if (const auto *DD = dyn_cast<DecompositionDecl>(VD)) |
| 10092 | for (const auto *BD : DD->bindings()) |
| 10093 | if (const auto *BindingVD = BD->getHoldingVar()) |
Richard Smith | da38363 | 2016-08-15 01:33:41 +0000 | [diff] [blame] | 10094 | if (DeclMustBeEmitted(BindingVD)) |
| 10095 | return true; |
| 10096 | |
Richard Smith | a0e5e54 | 2012-11-12 21:38:00 +0000 | [diff] [blame] | 10097 | return false; |
Argyrios Kyrtzidis | c81af03 | 2010-07-29 18:15:58 +0000 | [diff] [blame] | 10098 | } |
Charles Davis | 53c59df | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 10099 | |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 10100 | void ASTContext::forEachMultiversionedFunctionVersion( |
| 10101 | const FunctionDecl *FD, |
Erich Keane | 0fb1648 | 2018-08-13 18:33:20 +0000 | [diff] [blame] | 10102 | llvm::function_ref<void(FunctionDecl *)> Pred) const { |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 10103 | assert(FD->isMultiVersion() && "Only valid for multiversioned functions"); |
| 10104 | llvm::SmallDenseSet<const FunctionDecl*, 4> SeenDecls; |
Erich Keane | 7304f0a | 2018-11-28 20:58:43 +0000 | [diff] [blame] | 10105 | FD = FD->getMostRecentDecl(); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 10106 | for (auto *CurDecl : |
| 10107 | FD->getDeclContext()->getRedeclContext()->lookup(FD->getDeclName())) { |
Erich Keane | 7304f0a | 2018-11-28 20:58:43 +0000 | [diff] [blame] | 10108 | FunctionDecl *CurFD = CurDecl->getAsFunction()->getMostRecentDecl(); |
Erich Keane | 281d20b | 2018-01-08 21:34:17 +0000 | [diff] [blame] | 10109 | if (CurFD && hasSameType(CurFD->getType(), FD->getType()) && |
| 10110 | std::end(SeenDecls) == llvm::find(SeenDecls, CurFD)) { |
| 10111 | SeenDecls.insert(CurFD); |
| 10112 | Pred(CurFD); |
| 10113 | } |
| 10114 | } |
| 10115 | } |
| 10116 | |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 10117 | CallingConv ASTContext::getDefaultCallingConvention(bool IsVariadic, |
Erich Keane | 0002281 | 2019-05-23 16:05:21 +0000 | [diff] [blame] | 10118 | bool IsCXXMethod, |
| 10119 | bool IsBuiltin) const { |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 10120 | // Pass through to the C++ ABI object |
Reid Kleckner | 78af070 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 10121 | if (IsCXXMethod) |
| 10122 | return ABI->getDefaultMethodCallConv(IsVariadic); |
Timur Iskhodzhanov | c5098ad | 2012-07-12 09:50:54 +0000 | [diff] [blame] | 10123 | |
Erich Keane | 0002281 | 2019-05-23 16:05:21 +0000 | [diff] [blame] | 10124 | // Builtins ignore user-specified default calling convention and remain the |
| 10125 | // Target's default calling convention. |
| 10126 | if (!IsBuiltin) { |
| 10127 | switch (LangOpts.getDefaultCallingConv()) { |
| 10128 | case LangOptions::DCC_None: |
| 10129 | break; |
| 10130 | case LangOptions::DCC_CDecl: |
| 10131 | return CC_C; |
| 10132 | case LangOptions::DCC_FastCall: |
| 10133 | if (getTargetInfo().hasFeature("sse2") && !IsVariadic) |
| 10134 | return CC_X86FastCall; |
| 10135 | break; |
| 10136 | case LangOptions::DCC_StdCall: |
| 10137 | if (!IsVariadic) |
| 10138 | return CC_X86StdCall; |
| 10139 | break; |
| 10140 | case LangOptions::DCC_VectorCall: |
| 10141 | // __vectorcall cannot be applied to variadic functions. |
| 10142 | if (!IsVariadic) |
| 10143 | return CC_X86VectorCall; |
| 10144 | break; |
| 10145 | case LangOptions::DCC_RegCall: |
| 10146 | // __regcall cannot be applied to variadic functions. |
| 10147 | if (!IsVariadic) |
| 10148 | return CC_X86RegCall; |
| 10149 | break; |
| 10150 | } |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 10151 | } |
Erich Keane | 3930948 | 2019-07-25 17:14:45 +0000 | [diff] [blame] | 10152 | return Target->getDefaultCallingConv(); |
Charles Davis | 99202b3 | 2010-11-09 18:04:24 +0000 | [diff] [blame] | 10153 | } |
| 10154 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 10155 | bool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const { |
Anders Carlsson | 60a6263 | 2010-11-25 01:51:53 +0000 | [diff] [blame] | 10156 | // Pass through to the C++ ABI object |
| 10157 | return ABI->isNearlyEmpty(RD); |
| 10158 | } |
| 10159 | |
Reid Kleckner | 96f8f93 | 2014-02-05 17:27:08 +0000 | [diff] [blame] | 10160 | VTableContextBase *ASTContext::getVTableContext() { |
| 10161 | if (!VTContext.get()) { |
| 10162 | if (Target->getCXXABI().isMicrosoft()) |
| 10163 | VTContext.reset(new MicrosoftVTableContext(*this)); |
| 10164 | else |
| 10165 | VTContext.reset(new ItaniumVTableContext(*this)); |
| 10166 | } |
| 10167 | return VTContext.get(); |
| 10168 | } |
| 10169 | |
Yaxun Liu | c18e9ec | 2019-02-14 02:00:09 +0000 | [diff] [blame] | 10170 | MangleContext *ASTContext::createMangleContext(const TargetInfo *T) { |
| 10171 | if (!T) |
| 10172 | T = Target; |
| 10173 | switch (T->getCXXABI().getKind()) { |
Petr Hosek | 9c3f9b9 | 2019-09-06 18:59:43 -0700 | [diff] [blame] | 10174 | case TargetCXXABI::Fuchsia: |
Tim Northover | 9bb857a | 2013-01-31 12:13:10 +0000 | [diff] [blame] | 10175 | case TargetCXXABI::GenericAArch64: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 10176 | case TargetCXXABI::GenericItanium: |
| 10177 | case TargetCXXABI::GenericARM: |
Zoran Jovanovic | 26a1216 | 2015-02-18 15:21:35 +0000 | [diff] [blame] | 10178 | case TargetCXXABI::GenericMIPS: |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 10179 | case TargetCXXABI::iOS: |
Tim Northover | a2ee433 | 2014-03-29 15:09:45 +0000 | [diff] [blame] | 10180 | case TargetCXXABI::iOS64: |
Dan Gohman | c285307 | 2015-09-03 22:51:53 +0000 | [diff] [blame] | 10181 | case TargetCXXABI::WebAssembly: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 10182 | case TargetCXXABI::WatchOS: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 10183 | return ItaniumMangleContext::create(*this, getDiagnostics()); |
John McCall | 359b885 | 2013-01-25 22:30:49 +0000 | [diff] [blame] | 10184 | case TargetCXXABI::Microsoft: |
Timur Iskhodzhanov | 6745522 | 2013-10-03 06:26:13 +0000 | [diff] [blame] | 10185 | return MicrosoftMangleContext::create(*this, getDiagnostics()); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 10186 | } |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 10187 | llvm_unreachable("Unsupported ABI"); |
Peter Collingbourne | 0ff0b37 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 10188 | } |
| 10189 | |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 10190 | CXXABI::~CXXABI() = default; |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 10191 | |
| 10192 | size_t ASTContext::getSideTableAllocatedMemory() const { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 10193 | return ASTRecordLayouts.getMemorySize() + |
| 10194 | llvm::capacity_in_bytes(ObjCLayouts) + |
| 10195 | llvm::capacity_in_bytes(KeyFunctions) + |
| 10196 | llvm::capacity_in_bytes(ObjCImpls) + |
| 10197 | llvm::capacity_in_bytes(BlockVarCopyInits) + |
| 10198 | llvm::capacity_in_bytes(DeclAttrs) + |
| 10199 | llvm::capacity_in_bytes(TemplateOrInstantiation) + |
| 10200 | llvm::capacity_in_bytes(InstantiatedFromUsingDecl) + |
| 10201 | llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl) + |
| 10202 | llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl) + |
| 10203 | llvm::capacity_in_bytes(OverriddenMethods) + |
| 10204 | llvm::capacity_in_bytes(Types) + |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 10205 | llvm::capacity_in_bytes(VariableArrayTypes); |
Ted Kremenek | f5df0ce | 2011-04-28 04:53:38 +0000 | [diff] [blame] | 10206 | } |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 10207 | |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 10208 | /// getIntTypeForBitwidth - |
| 10209 | /// sets integer QualTy according to specified details: |
| 10210 | /// bitwidth, signed/unsigned. |
| 10211 | /// Returns empty type if there is no appropriate target types. |
| 10212 | QualType ASTContext::getIntTypeForBitwidth(unsigned DestWidth, |
| 10213 | unsigned Signed) const { |
| 10214 | TargetInfo::IntType Ty = getTargetInfo().getIntTypeByWidth(DestWidth, Signed); |
| 10215 | CanQualType QualTy = getFromTargetType(Ty); |
| 10216 | if (!QualTy && DestWidth == 128) |
| 10217 | return Signed ? Int128Ty : UnsignedInt128Ty; |
| 10218 | return QualTy; |
| 10219 | } |
| 10220 | |
| 10221 | /// getRealTypeForBitwidth - |
| 10222 | /// sets floating point QualTy according to specified bitwidth. |
| 10223 | /// Returns empty type if there is no appropriate target types. |
| 10224 | QualType ASTContext::getRealTypeForBitwidth(unsigned DestWidth) const { |
| 10225 | TargetInfo::RealType Ty = getTargetInfo().getRealTypeByWidth(DestWidth); |
| 10226 | switch (Ty) { |
| 10227 | case TargetInfo::Float: |
| 10228 | return FloatTy; |
| 10229 | case TargetInfo::Double: |
| 10230 | return DoubleTy; |
| 10231 | case TargetInfo::LongDouble: |
| 10232 | return LongDoubleTy; |
Nemanja Ivanovic | bb1ea2d | 2016-05-09 08:52:33 +0000 | [diff] [blame] | 10233 | case TargetInfo::Float128: |
| 10234 | return Float128Ty; |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 10235 | case TargetInfo::NoFloat: |
Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 10236 | return {}; |
Stepan Dyatkovskiy | 5a63792 | 2013-09-05 11:23:21 +0000 | [diff] [blame] | 10237 | } |
| 10238 | |
| 10239 | llvm_unreachable("Unhandled TargetInfo::RealType value"); |
| 10240 | } |
| 10241 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 10242 | void ASTContext::setManglingNumber(const NamedDecl *ND, unsigned Number) { |
| 10243 | if (Number > 1) |
| 10244 | MangleNumbers[ND] = Number; |
David Blaikie | 095deba | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 10245 | } |
| 10246 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 10247 | unsigned ASTContext::getManglingNumber(const NamedDecl *ND) const { |
Richard Smith | e9b02d6 | 2016-03-21 22:33:02 +0000 | [diff] [blame] | 10248 | auto I = MangleNumbers.find(ND); |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 10249 | return I != MangleNumbers.end() ? I->second : 1; |
David Blaikie | 095deba | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 10250 | } |
| 10251 | |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 10252 | void ASTContext::setStaticLocalNumber(const VarDecl *VD, unsigned Number) { |
| 10253 | if (Number > 1) |
| 10254 | StaticLocalNumbers[VD] = Number; |
| 10255 | } |
| 10256 | |
| 10257 | unsigned ASTContext::getStaticLocalNumber(const VarDecl *VD) const { |
Richard Smith | e9b02d6 | 2016-03-21 22:33:02 +0000 | [diff] [blame] | 10258 | auto I = StaticLocalNumbers.find(VD); |
David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 10259 | return I != StaticLocalNumbers.end() ? I->second : 1; |
| 10260 | } |
| 10261 | |
Eli Friedman | 3b7d46c | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 10262 | MangleNumberingContext & |
| 10263 | ASTContext::getManglingNumberContext(const DeclContext *DC) { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 10264 | assert(LangOpts.CPlusPlus); // We don't need mangling numbers for plain C. |
Justin Lebar | 20ebffc | 2016-10-10 16:26:19 +0000 | [diff] [blame] | 10265 | std::unique_ptr<MangleNumberingContext> &MCtx = MangleNumberingContexts[DC]; |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 10266 | if (!MCtx) |
| 10267 | MCtx = createMangleNumberingContext(); |
| 10268 | return *MCtx; |
| 10269 | } |
| 10270 | |
Reid Kleckner | da2bde9 | 2019-10-10 01:14:22 +0000 | [diff] [blame] | 10271 | MangleNumberingContext & |
| 10272 | ASTContext::getManglingNumberContext(NeedExtraManglingDecl_t, const Decl *D) { |
| 10273 | assert(LangOpts.CPlusPlus); // We don't need mangling numbers for plain C. |
| 10274 | std::unique_ptr<MangleNumberingContext> &MCtx = |
| 10275 | ExtraMangleNumberingContexts[D]; |
| 10276 | if (!MCtx) |
| 10277 | MCtx = createMangleNumberingContext(); |
| 10278 | return *MCtx; |
| 10279 | } |
| 10280 | |
Justin Lebar | 20ebffc | 2016-10-10 16:26:19 +0000 | [diff] [blame] | 10281 | std::unique_ptr<MangleNumberingContext> |
| 10282 | ASTContext::createMangleNumberingContext() const { |
Reid Kleckner | d8110b6 | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 10283 | return ABI->createMangleNumberingContext(); |
Douglas Gregor | 6379854 | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 10284 | } |
| 10285 | |
David Majnemer | e7a818f | 2015-03-06 18:53:55 +0000 | [diff] [blame] | 10286 | const CXXConstructorDecl * |
| 10287 | ASTContext::getCopyConstructorForExceptionObject(CXXRecordDecl *RD) { |
| 10288 | return ABI->getCopyConstructorForExceptionObject( |
| 10289 | cast<CXXRecordDecl>(RD->getFirstDecl())); |
| 10290 | } |
| 10291 | |
| 10292 | void ASTContext::addCopyConstructorForExceptionObject(CXXRecordDecl *RD, |
| 10293 | CXXConstructorDecl *CD) { |
| 10294 | return ABI->addCopyConstructorForExceptionObject( |
| 10295 | cast<CXXRecordDecl>(RD->getFirstDecl()), |
| 10296 | cast<CXXConstructorDecl>(CD->getFirstDecl())); |
| 10297 | } |
| 10298 | |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 10299 | void ASTContext::addTypedefNameForUnnamedTagDecl(TagDecl *TD, |
| 10300 | TypedefNameDecl *DD) { |
| 10301 | return ABI->addTypedefNameForUnnamedTagDecl(TD, DD); |
| 10302 | } |
| 10303 | |
| 10304 | TypedefNameDecl * |
| 10305 | ASTContext::getTypedefNameForUnnamedTagDecl(const TagDecl *TD) { |
| 10306 | return ABI->getTypedefNameForUnnamedTagDecl(TD); |
| 10307 | } |
| 10308 | |
| 10309 | void ASTContext::addDeclaratorForUnnamedTagDecl(TagDecl *TD, |
| 10310 | DeclaratorDecl *DD) { |
| 10311 | return ABI->addDeclaratorForUnnamedTagDecl(TD, DD); |
| 10312 | } |
| 10313 | |
| 10314 | DeclaratorDecl *ASTContext::getDeclaratorForUnnamedTagDecl(const TagDecl *TD) { |
| 10315 | return ABI->getDeclaratorForUnnamedTagDecl(TD); |
| 10316 | } |
| 10317 | |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 10318 | void ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) { |
| 10319 | ParamIndices[D] = index; |
| 10320 | } |
| 10321 | |
| 10322 | unsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const { |
| 10323 | ParameterIndexTable::const_iterator I = ParamIndices.find(D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 10324 | assert(I != ParamIndices.end() && |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 10325 | "ParmIndices lacks entry set by ParmVarDecl"); |
| 10326 | return I->second; |
| 10327 | } |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 10328 | |
Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 10329 | QualType ASTContext::getStringLiteralArrayType(QualType EltTy, |
| 10330 | unsigned Length) const { |
| 10331 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 10332 | if (getLangOpts().CPlusPlus || getLangOpts().ConstStrings) |
| 10333 | EltTy = EltTy.withConst(); |
| 10334 | |
| 10335 | EltTy = adjustStringLiteralBaseType(EltTy); |
| 10336 | |
| 10337 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 10338 | // the null terminator character. |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 10339 | return getConstantArrayType(EltTy, llvm::APInt(32, Length + 1), nullptr, |
Eric Fiselier | 708afb5 | 2019-05-16 21:04:15 +0000 | [diff] [blame] | 10340 | ArrayType::Normal, /*IndexTypeQuals*/ 0); |
| 10341 | } |
| 10342 | |
| 10343 | StringLiteral * |
| 10344 | ASTContext::getPredefinedStringLiteralFromCache(StringRef Key) const { |
| 10345 | StringLiteral *&Result = StringLiteralCache[Key]; |
| 10346 | if (!Result) |
| 10347 | Result = StringLiteral::Create( |
| 10348 | *this, Key, StringLiteral::Ascii, |
| 10349 | /*Pascal*/ false, getStringLiteralArrayType(CharTy, Key.size()), |
| 10350 | SourceLocation()); |
| 10351 | return Result; |
| 10352 | } |
| 10353 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 10354 | bool ASTContext::AtomicUsesUnsupportedLibcall(const AtomicExpr *E) const { |
| 10355 | const llvm::Triple &T = getTargetInfo().getTriple(); |
| 10356 | if (!T.isOSDarwin()) |
| 10357 | return false; |
| 10358 | |
Bob Wilson | 2c82c3d | 2013-11-02 23:27:49 +0000 | [diff] [blame] | 10359 | if (!(T.isiOS() && T.isOSVersionLT(7)) && |
| 10360 | !(T.isMacOSX() && T.isOSVersionLT(10, 9))) |
| 10361 | return false; |
| 10362 | |
Fariborz Jahanian | 615de76 | 2013-05-28 17:37:39 +0000 | [diff] [blame] | 10363 | QualType AtomicTy = E->getPtr()->getType()->getPointeeType(); |
| 10364 | CharUnits sizeChars = getTypeSizeInChars(AtomicTy); |
| 10365 | uint64_t Size = sizeChars.getQuantity(); |
| 10366 | CharUnits alignChars = getTypeAlignInChars(AtomicTy); |
| 10367 | unsigned Align = alignChars.getQuantity(); |
| 10368 | unsigned MaxInlineWidthInBits = getTargetInfo().getMaxAtomicInlineWidth(); |
| 10369 | return (Size != Align || toBits(sizeChars) > MaxInlineWidthInBits); |
| 10370 | } |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10371 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10372 | /// Template specializations to abstract away from pointers and TypeLocs. |
| 10373 | /// @{ |
| 10374 | template <typename T> |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10375 | static ast_type_traits::DynTypedNode createDynTypedNode(const T &Node) { |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10376 | return ast_type_traits::DynTypedNode::create(*Node); |
| 10377 | } |
| 10378 | template <> |
| 10379 | ast_type_traits::DynTypedNode createDynTypedNode(const TypeLoc &Node) { |
| 10380 | return ast_type_traits::DynTypedNode::create(Node); |
| 10381 | } |
| 10382 | template <> |
| 10383 | ast_type_traits::DynTypedNode |
| 10384 | createDynTypedNode(const NestedNameSpecifierLoc &Node) { |
| 10385 | return ast_type_traits::DynTypedNode::create(Node); |
| 10386 | } |
| 10387 | /// @} |
| 10388 | |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10389 | /// A \c RecursiveASTVisitor that builds a map from nodes to their |
| 10390 | /// parents as defined by the \c RecursiveASTVisitor. |
| 10391 | /// |
| 10392 | /// Note that the relationship described here is purely in terms of AST |
| 10393 | /// traversal - there are other relationships (for example declaration context) |
| 10394 | /// in the AST that are better modeled by special matchers. |
| 10395 | /// |
| 10396 | /// FIXME: Currently only builds up the map using \c Stmt and \c Decl nodes. |
| 10397 | class ASTContext::ParentMap::ASTVisitor |
| 10398 | : public RecursiveASTVisitor<ASTVisitor> { |
| 10399 | public: |
| 10400 | ASTVisitor(ParentMap &Map) : Map(Map) {} |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10401 | |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10402 | private: |
| 10403 | friend class RecursiveASTVisitor<ASTVisitor>; |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 10404 | |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10405 | using VisitorBase = RecursiveASTVisitor<ASTVisitor>; |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10406 | |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10407 | bool shouldVisitTemplateInstantiations() const { return true; } |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10408 | |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10409 | bool shouldVisitImplicitCode() const { return true; } |
| 10410 | |
| 10411 | template <typename T, typename MapNodeTy, typename BaseTraverseFn, |
| 10412 | typename MapTy> |
| 10413 | bool TraverseNode(T Node, MapNodeTy MapNode, BaseTraverseFn BaseTraverse, |
| 10414 | MapTy *Parents) { |
| 10415 | if (!Node) |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10416 | return true; |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10417 | if (ParentStack.size() > 0) { |
| 10418 | // FIXME: Currently we add the same parent multiple times, but only |
| 10419 | // when no memoization data is available for the type. |
| 10420 | // For example when we visit all subexpressions of template |
| 10421 | // instantiations; this is suboptimal, but benign: the only way to |
| 10422 | // visit those is with hasAncestor / hasParent, and those do not create |
| 10423 | // new matches. |
| 10424 | // The plan is to enable DynTypedNode to be storable in a map or hash |
| 10425 | // map. The main problem there is to implement hash functions / |
| 10426 | // comparison operators for all types that DynTypedNode supports that |
| 10427 | // do not have pointer identity. |
| 10428 | auto &NodeOrVector = (*Parents)[MapNode]; |
| 10429 | if (NodeOrVector.isNull()) { |
| 10430 | if (const auto *D = ParentStack.back().get<Decl>()) |
| 10431 | NodeOrVector = D; |
| 10432 | else if (const auto *S = ParentStack.back().get<Stmt>()) |
| 10433 | NodeOrVector = S; |
| 10434 | else |
| 10435 | NodeOrVector = new ast_type_traits::DynTypedNode(ParentStack.back()); |
| 10436 | } else { |
| 10437 | if (!NodeOrVector.template is<ParentVector *>()) { |
| 10438 | auto *Vector = new ParentVector( |
| 10439 | 1, getSingleDynTypedNodeFromParentMap(NodeOrVector)); |
| 10440 | delete NodeOrVector |
| 10441 | .template dyn_cast<ast_type_traits::DynTypedNode *>(); |
| 10442 | NodeOrVector = Vector; |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 10443 | } |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10444 | |
| 10445 | auto *Vector = NodeOrVector.template get<ParentVector *>(); |
| 10446 | // Skip duplicates for types that have memoization data. |
| 10447 | // We must check that the type has memoization data before calling |
| 10448 | // std::find() because DynTypedNode::operator== can't compare all |
| 10449 | // types. |
| 10450 | bool Found = ParentStack.back().getMemoizationData() && |
| 10451 | std::find(Vector->begin(), Vector->end(), |
| 10452 | ParentStack.back()) != Vector->end(); |
| 10453 | if (!Found) |
| 10454 | Vector->push_back(ParentStack.back()); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 10455 | } |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10456 | } |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10457 | ParentStack.push_back(createDynTypedNode(Node)); |
| 10458 | bool Result = BaseTraverse(); |
| 10459 | ParentStack.pop_back(); |
| 10460 | return Result; |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10461 | } |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10462 | |
| 10463 | bool TraverseDecl(Decl *DeclNode) { |
| 10464 | return TraverseNode( |
| 10465 | DeclNode, DeclNode, [&] { return VisitorBase::TraverseDecl(DeclNode); }, |
| 10466 | &Map.PointerParents); |
Manuel Klimek | 95403e6 | 2014-05-21 13:28:59 +0000 | [diff] [blame] | 10467 | } |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10468 | |
| 10469 | bool TraverseStmt(Stmt *StmtNode) { |
| 10470 | return TraverseNode( |
| 10471 | StmtNode, StmtNode, [&] { return VisitorBase::TraverseStmt(StmtNode); }, |
| 10472 | &Map.PointerParents); |
| 10473 | } |
| 10474 | |
| 10475 | bool TraverseTypeLoc(TypeLoc TypeLocNode) { |
| 10476 | return TraverseNode( |
| 10477 | TypeLocNode, ast_type_traits::DynTypedNode::create(TypeLocNode), |
| 10478 | [&] { return VisitorBase::TraverseTypeLoc(TypeLocNode); }, |
| 10479 | &Map.OtherParents); |
| 10480 | } |
| 10481 | |
| 10482 | bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc NNSLocNode) { |
| 10483 | return TraverseNode( |
| 10484 | NNSLocNode, ast_type_traits::DynTypedNode::create(NNSLocNode), |
| 10485 | [&] { return VisitorBase::TraverseNestedNameSpecifierLoc(NNSLocNode); }, |
| 10486 | &Map.OtherParents); |
| 10487 | } |
| 10488 | |
| 10489 | ParentMap ⤅ |
| 10490 | llvm::SmallVector<ast_type_traits::DynTypedNode, 16> ParentStack; |
| 10491 | }; |
| 10492 | |
| 10493 | ASTContext::ParentMap::ParentMap(ASTContext &Ctx) { |
| 10494 | ASTVisitor(*this).TraverseAST(Ctx); |
Reid Kleckner | 2ab0ac5 | 2013-06-17 12:56:08 +0000 | [diff] [blame] | 10495 | } |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10496 | |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10497 | ASTContext::DynTypedNodeList |
| 10498 | ASTContext::getParents(const ast_type_traits::DynTypedNode &Node) { |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10499 | if (!Parents) |
| 10500 | // 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] | 10501 | // hasAncestor can escape any subtree. |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 10502 | Parents = std::make_unique<ParentMap>(*this); |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 10503 | return Parents->getParents(Node); |
Benjamin Kramer | 94355ae | 2015-10-23 09:04:55 +0000 | [diff] [blame] | 10504 | } |
| 10505 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10506 | bool |
| 10507 | ASTContext::ObjCMethodsAreEqual(const ObjCMethodDecl *MethodDecl, |
| 10508 | const ObjCMethodDecl *MethodImpl) { |
| 10509 | // No point trying to match an unavailable/deprecated mothod. |
| 10510 | if (MethodDecl->hasAttr<UnavailableAttr>() |
| 10511 | || MethodDecl->hasAttr<DeprecatedAttr>()) |
| 10512 | return false; |
| 10513 | if (MethodDecl->getObjCDeclQualifier() != |
| 10514 | MethodImpl->getObjCDeclQualifier()) |
| 10515 | return false; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 10516 | if (!hasSameType(MethodDecl->getReturnType(), MethodImpl->getReturnType())) |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10517 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 10518 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10519 | if (MethodDecl->param_size() != MethodImpl->param_size()) |
| 10520 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 10521 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10522 | for (ObjCMethodDecl::param_const_iterator IM = MethodImpl->param_begin(), |
| 10523 | IF = MethodDecl->param_begin(), EM = MethodImpl->param_end(), |
| 10524 | EF = MethodDecl->param_end(); |
| 10525 | IM != EM && IF != EF; ++IM, ++IF) { |
| 10526 | const ParmVarDecl *DeclVar = (*IF); |
| 10527 | const ParmVarDecl *ImplVar = (*IM); |
| 10528 | if (ImplVar->getObjCDeclQualifier() != DeclVar->getObjCDeclQualifier()) |
| 10529 | return false; |
| 10530 | if (!hasSameType(DeclVar->getType(), ImplVar->getType())) |
| 10531 | return false; |
| 10532 | } |
Eugene Zelenko | 5e5e564 | 2017-11-23 01:20:07 +0000 | [diff] [blame] | 10533 | |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10534 | return (MethodDecl->isVariadic() == MethodImpl->isVariadic()); |
Fariborz Jahanian | d36150d | 2013-07-15 21:22:08 +0000 | [diff] [blame] | 10535 | } |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 10536 | |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 10537 | uint64_t ASTContext::getTargetNullPointerValue(QualType QT) const { |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 10538 | LangAS AS; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 10539 | if (QT->getUnqualifiedDesugaredType()->isNullPtrType()) |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 10540 | AS = LangAS::Default; |
Yaxun Liu | 402804b | 2016-12-15 08:09:08 +0000 | [diff] [blame] | 10541 | else |
| 10542 | AS = QT->getPointeeType().getAddressSpace(); |
| 10543 | |
| 10544 | return getTargetInfo().getNullPointerValue(AS); |
| 10545 | } |
| 10546 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 10547 | unsigned ASTContext::getTargetAddressSpace(LangAS AS) const { |
| 10548 | if (isTargetAddressSpace(AS)) |
| 10549 | return toTargetAddressSpace(AS); |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 10550 | else |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 10551 | return (*AddrSpaceMap)[(unsigned)AS]; |
Yaxun Liu | b34ec82 | 2017-04-11 17:24:23 +0000 | [diff] [blame] | 10552 | } |
| 10553 | |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 10554 | QualType ASTContext::getCorrespondingSaturatedType(QualType Ty) const { |
| 10555 | assert(Ty->isFixedPointType()); |
| 10556 | |
| 10557 | if (Ty->isSaturatedFixedPointType()) return Ty; |
| 10558 | |
Simon Pilgrim | eed4b12 | 2019-10-02 11:48:06 +0000 | [diff] [blame] | 10559 | switch (Ty->castAs<BuiltinType>()->getKind()) { |
Leonard Chan | ab80f3c | 2018-06-14 14:53:51 +0000 | [diff] [blame] | 10560 | default: |
| 10561 | llvm_unreachable("Not a fixed point type!"); |
| 10562 | case BuiltinType::ShortAccum: |
| 10563 | return SatShortAccumTy; |
| 10564 | case BuiltinType::Accum: |
| 10565 | return SatAccumTy; |
| 10566 | case BuiltinType::LongAccum: |
| 10567 | return SatLongAccumTy; |
| 10568 | case BuiltinType::UShortAccum: |
| 10569 | return SatUnsignedShortAccumTy; |
| 10570 | case BuiltinType::UAccum: |
| 10571 | return SatUnsignedAccumTy; |
| 10572 | case BuiltinType::ULongAccum: |
| 10573 | return SatUnsignedLongAccumTy; |
| 10574 | case BuiltinType::ShortFract: |
| 10575 | return SatShortFractTy; |
| 10576 | case BuiltinType::Fract: |
| 10577 | return SatFractTy; |
| 10578 | case BuiltinType::LongFract: |
| 10579 | return SatLongFractTy; |
| 10580 | case BuiltinType::UShortFract: |
| 10581 | return SatUnsignedShortFractTy; |
| 10582 | case BuiltinType::UFract: |
| 10583 | return SatUnsignedFractTy; |
| 10584 | case BuiltinType::ULongFract: |
| 10585 | return SatUnsignedLongFractTy; |
| 10586 | } |
| 10587 | } |
| 10588 | |
Matt Arsenault | c65f966 | 2018-08-02 12:14:28 +0000 | [diff] [blame] | 10589 | LangAS ASTContext::getLangASForBuiltinAddressSpace(unsigned AS) const { |
| 10590 | if (LangOpts.OpenCL) |
| 10591 | return getTargetInfo().getOpenCLBuiltinAddressSpace(AS); |
| 10592 | |
| 10593 | if (LangOpts.CUDA) |
| 10594 | return getTargetInfo().getCUDABuiltinAddressSpace(AS); |
| 10595 | |
| 10596 | return getLangASFromTargetAS(AS); |
| 10597 | } |
| 10598 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 10599 | // Explicitly instantiate this in case a Redeclarable<T> is used from a TU that |
| 10600 | // doesn't include ASTContext.h |
| 10601 | template |
| 10602 | clang::LazyGenerationalUpdatePtr< |
| 10603 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::ValueType |
| 10604 | clang::LazyGenerationalUpdatePtr< |
| 10605 | const Decl *, Decl *, &ExternalASTSource::CompleteRedeclChain>::makeValue( |
| 10606 | const clang::ASTContext &Ctx, Decl *Value); |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 10607 | |
| 10608 | unsigned char ASTContext::getFixedPointScale(QualType Ty) const { |
| 10609 | assert(Ty->isFixedPointType()); |
| 10610 | |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 10611 | const TargetInfo &Target = getTargetInfo(); |
Simon Pilgrim | 9588ae7 | 2019-10-03 21:47:42 +0000 | [diff] [blame] | 10612 | switch (Ty->castAs<BuiltinType>()->getKind()) { |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 10613 | default: |
| 10614 | llvm_unreachable("Not a fixed point type!"); |
| 10615 | case BuiltinType::ShortAccum: |
| 10616 | case BuiltinType::SatShortAccum: |
| 10617 | return Target.getShortAccumScale(); |
| 10618 | case BuiltinType::Accum: |
| 10619 | case BuiltinType::SatAccum: |
| 10620 | return Target.getAccumScale(); |
| 10621 | case BuiltinType::LongAccum: |
| 10622 | case BuiltinType::SatLongAccum: |
| 10623 | return Target.getLongAccumScale(); |
| 10624 | case BuiltinType::UShortAccum: |
| 10625 | case BuiltinType::SatUShortAccum: |
| 10626 | return Target.getUnsignedShortAccumScale(); |
| 10627 | case BuiltinType::UAccum: |
| 10628 | case BuiltinType::SatUAccum: |
| 10629 | return Target.getUnsignedAccumScale(); |
| 10630 | case BuiltinType::ULongAccum: |
| 10631 | case BuiltinType::SatULongAccum: |
| 10632 | return Target.getUnsignedLongAccumScale(); |
| 10633 | case BuiltinType::ShortFract: |
| 10634 | case BuiltinType::SatShortFract: |
| 10635 | return Target.getShortFractScale(); |
| 10636 | case BuiltinType::Fract: |
| 10637 | case BuiltinType::SatFract: |
| 10638 | return Target.getFractScale(); |
| 10639 | case BuiltinType::LongFract: |
| 10640 | case BuiltinType::SatLongFract: |
| 10641 | return Target.getLongFractScale(); |
| 10642 | case BuiltinType::UShortFract: |
| 10643 | case BuiltinType::SatUShortFract: |
| 10644 | return Target.getUnsignedShortFractScale(); |
| 10645 | case BuiltinType::UFract: |
| 10646 | case BuiltinType::SatUFract: |
| 10647 | return Target.getUnsignedFractScale(); |
| 10648 | case BuiltinType::ULongFract: |
| 10649 | case BuiltinType::SatULongFract: |
| 10650 | return Target.getUnsignedLongFractScale(); |
| 10651 | } |
| 10652 | } |
| 10653 | |
| 10654 | unsigned char ASTContext::getFixedPointIBits(QualType Ty) const { |
| 10655 | assert(Ty->isFixedPointType()); |
| 10656 | |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 10657 | const TargetInfo &Target = getTargetInfo(); |
Simon Pilgrim | 9588ae7 | 2019-10-03 21:47:42 +0000 | [diff] [blame] | 10658 | switch (Ty->castAs<BuiltinType>()->getKind()) { |
Leonard Chan | db01c3a | 2018-06-20 17:19:40 +0000 | [diff] [blame] | 10659 | default: |
| 10660 | llvm_unreachable("Not a fixed point type!"); |
| 10661 | case BuiltinType::ShortAccum: |
| 10662 | case BuiltinType::SatShortAccum: |
| 10663 | return Target.getShortAccumIBits(); |
| 10664 | case BuiltinType::Accum: |
| 10665 | case BuiltinType::SatAccum: |
| 10666 | return Target.getAccumIBits(); |
| 10667 | case BuiltinType::LongAccum: |
| 10668 | case BuiltinType::SatLongAccum: |
| 10669 | return Target.getLongAccumIBits(); |
| 10670 | case BuiltinType::UShortAccum: |
| 10671 | case BuiltinType::SatUShortAccum: |
| 10672 | return Target.getUnsignedShortAccumIBits(); |
| 10673 | case BuiltinType::UAccum: |
| 10674 | case BuiltinType::SatUAccum: |
| 10675 | return Target.getUnsignedAccumIBits(); |
| 10676 | case BuiltinType::ULongAccum: |
| 10677 | case BuiltinType::SatULongAccum: |
| 10678 | return Target.getUnsignedLongAccumIBits(); |
| 10679 | case BuiltinType::ShortFract: |
| 10680 | case BuiltinType::SatShortFract: |
| 10681 | case BuiltinType::Fract: |
| 10682 | case BuiltinType::SatFract: |
| 10683 | case BuiltinType::LongFract: |
| 10684 | case BuiltinType::SatLongFract: |
| 10685 | case BuiltinType::UShortFract: |
| 10686 | case BuiltinType::SatUShortFract: |
| 10687 | case BuiltinType::UFract: |
| 10688 | case BuiltinType::SatUFract: |
| 10689 | case BuiltinType::ULongFract: |
| 10690 | case BuiltinType::SatULongFract: |
| 10691 | return 0; |
| 10692 | } |
| 10693 | } |
Leonard Chan | a677942 | 2018-08-06 16:42:37 +0000 | [diff] [blame] | 10694 | |
| 10695 | FixedPointSemantics ASTContext::getFixedPointSemantics(QualType Ty) const { |
Erich Keane | 8e77216 | 2019-01-18 19:31:54 +0000 | [diff] [blame] | 10696 | assert((Ty->isFixedPointType() || Ty->isIntegerType()) && |
| 10697 | "Can only get the fixed point semantics for a " |
| 10698 | "fixed point or integer type."); |
Leonard Chan | 2044ac8 | 2019-01-16 18:13:59 +0000 | [diff] [blame] | 10699 | if (Ty->isIntegerType()) |
| 10700 | return FixedPointSemantics::GetIntegerSemantics(getIntWidth(Ty), |
| 10701 | Ty->isSignedIntegerType()); |
| 10702 | |
Leonard Chan | a677942 | 2018-08-06 16:42:37 +0000 | [diff] [blame] | 10703 | bool isSigned = Ty->isSignedFixedPointType(); |
| 10704 | return FixedPointSemantics( |
| 10705 | static_cast<unsigned>(getTypeSize(Ty)), getFixedPointScale(Ty), isSigned, |
| 10706 | Ty->isSaturatedFixedPointType(), |
| 10707 | !isSigned && getTargetInfo().doUnsignedFixedPointTypesHavePadding()); |
| 10708 | } |
| 10709 | |
| 10710 | APFixedPoint ASTContext::getFixedPointMax(QualType Ty) const { |
| 10711 | assert(Ty->isFixedPointType()); |
| 10712 | return APFixedPoint::getMax(getFixedPointSemantics(Ty)); |
| 10713 | } |
| 10714 | |
| 10715 | APFixedPoint ASTContext::getFixedPointMin(QualType Ty) const { |
| 10716 | assert(Ty->isFixedPointType()); |
| 10717 | return APFixedPoint::getMin(getFixedPointSemantics(Ty)); |
| 10718 | } |
Leonard Chan | 2044ac8 | 2019-01-16 18:13:59 +0000 | [diff] [blame] | 10719 | |
| 10720 | QualType ASTContext::getCorrespondingSignedFixedPointType(QualType Ty) const { |
| 10721 | assert(Ty->isUnsignedFixedPointType() && |
| 10722 | "Expected unsigned fixed point type"); |
Leonard Chan | 2044ac8 | 2019-01-16 18:13:59 +0000 | [diff] [blame] | 10723 | |
Simon Pilgrim | 9588ae7 | 2019-10-03 21:47:42 +0000 | [diff] [blame] | 10724 | switch (Ty->castAs<BuiltinType>()->getKind()) { |
Leonard Chan | 2044ac8 | 2019-01-16 18:13:59 +0000 | [diff] [blame] | 10725 | case BuiltinType::UShortAccum: |
| 10726 | return ShortAccumTy; |
| 10727 | case BuiltinType::UAccum: |
| 10728 | return AccumTy; |
| 10729 | case BuiltinType::ULongAccum: |
| 10730 | return LongAccumTy; |
| 10731 | case BuiltinType::SatUShortAccum: |
| 10732 | return SatShortAccumTy; |
| 10733 | case BuiltinType::SatUAccum: |
| 10734 | return SatAccumTy; |
| 10735 | case BuiltinType::SatULongAccum: |
| 10736 | return SatLongAccumTy; |
| 10737 | case BuiltinType::UShortFract: |
| 10738 | return ShortFractTy; |
| 10739 | case BuiltinType::UFract: |
| 10740 | return FractTy; |
| 10741 | case BuiltinType::ULongFract: |
| 10742 | return LongFractTy; |
| 10743 | case BuiltinType::SatUShortFract: |
| 10744 | return SatShortFractTy; |
| 10745 | case BuiltinType::SatUFract: |
| 10746 | return SatFractTy; |
| 10747 | case BuiltinType::SatULongFract: |
| 10748 | return SatLongFractTy; |
| 10749 | default: |
| 10750 | llvm_unreachable("Unexpected unsigned fixed point type"); |
| 10751 | } |
| 10752 | } |