| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1 | //===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/ |
| 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 |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 6 | //===----------------------------------------------------------------------===/ |
| 7 | // |
| 8 | // This file implements C++ template instantiation for declarations. |
| 9 | // |
| 10 | //===----------------------------------------------------------------------===/ |
| John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 11 | #include "clang/Sema/SemaInternal.h" |
| Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTConsumer.h" |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
| Richard Smith | d28ac5b | 2014-03-22 23:33:22 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTMutationListener.h" |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclTemplate.h" |
| 16 | #include "clang/AST/DeclVisitor.h" |
| John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 17 | #include "clang/AST/DependentDiagnostic.h" |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 18 | #include "clang/AST/Expr.h" |
| Douglas Gregor | 6131b44 | 2009-12-12 18:16:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
| Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 20 | #include "clang/AST/PrettyDeclStackTrace.h" |
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeLoc.h" |
| Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 22 | #include "clang/Sema/Initialization.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Lookup.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Template.h" |
| Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 25 | #include "clang/Sema/TemplateInstCallback.h" |
| Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/TimeProfiler.h" |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
| 29 | |
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 30 | static bool isDeclWithinFunction(const Decl *D) { |
| 31 | const DeclContext *DC = D->getDeclContext(); |
| 32 | if (DC->isFunctionOrMethod()) |
| 33 | return true; |
| 34 | |
| 35 | if (DC->isRecord()) |
| 36 | return cast<CXXRecordDecl>(DC)->isLocalClass(); |
| 37 | |
| 38 | return false; |
| 39 | } |
| 40 | |
| Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 41 | template<typename DeclT> |
| 42 | static bool SubstQualifier(Sema &SemaRef, const DeclT *OldDecl, DeclT *NewDecl, |
| 43 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 44 | if (!OldDecl->getQualifierLoc()) |
| 45 | return false; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 46 | |
| Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 47 | assert((NewDecl->getFriendObjectKind() || |
| 48 | !OldDecl->getLexicalDeclContext()->isDependentContext()) && |
| 49 | "non-friend with qualified name defined in dependent context"); |
| 50 | Sema::ContextRAII SavedContext( |
| 51 | SemaRef, |
| 52 | const_cast<DeclContext *>(NewDecl->getFriendObjectKind() |
| 53 | ? NewDecl->getLexicalDeclContext() |
| 54 | : OldDecl->getLexicalDeclContext())); |
| 55 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 56 | NestedNameSpecifierLoc NewQualifierLoc |
| Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 57 | = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(), |
| 58 | TemplateArgs); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 59 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 60 | if (!NewQualifierLoc) |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 61 | return true; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 62 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 63 | NewDecl->setQualifierInfo(NewQualifierLoc); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | |
| Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 67 | bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl, |
| 68 | DeclaratorDecl *NewDecl) { |
| 69 | return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs); |
| 70 | } |
| 71 | |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 72 | bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl, |
| 73 | TagDecl *NewDecl) { |
| Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 74 | return ::SubstQualifier(SemaRef, OldDecl, NewDecl, TemplateArgs); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| DeLesley Hutchins | ceec306 | 2012-01-20 22:37:06 +0000 | [diff] [blame] | 77 | // Include attribute instantiation code. |
| 78 | #include "clang/Sema/AttrTemplateInstantiate.inc" |
| 79 | |
| Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 80 | static void instantiateDependentAlignedAttr( |
| 81 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 82 | const AlignedAttr *Aligned, Decl *New, bool IsPackExpansion) { |
| 83 | if (Aligned->isAlignmentExpr()) { |
| 84 | // The alignment expression is a constant expression. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 85 | EnterExpressionEvaluationContext Unevaluated( |
| 86 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 87 | ExprResult Result = S.SubstExpr(Aligned->getAlignmentExpr(), TemplateArgs); |
| 88 | if (!Result.isInvalid()) |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 89 | S.AddAlignedAttr(Aligned->getLocation(), New, Result.getAs<Expr>(), |
| Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 90 | Aligned->getSpellingListIndex(), IsPackExpansion); |
| 91 | } else { |
| 92 | TypeSourceInfo *Result = S.SubstType(Aligned->getAlignmentType(), |
| 93 | TemplateArgs, Aligned->getLocation(), |
| 94 | DeclarationName()); |
| 95 | if (Result) |
| 96 | S.AddAlignedAttr(Aligned->getLocation(), New, Result, |
| 97 | Aligned->getSpellingListIndex(), IsPackExpansion); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | static void instantiateDependentAlignedAttr( |
| 102 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 103 | const AlignedAttr *Aligned, Decl *New) { |
| 104 | if (!Aligned->isPackExpansion()) { |
| 105 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 110 | if (Aligned->isAlignmentExpr()) |
| 111 | S.collectUnexpandedParameterPacks(Aligned->getAlignmentExpr(), |
| 112 | Unexpanded); |
| 113 | else |
| 114 | S.collectUnexpandedParameterPacks(Aligned->getAlignmentType()->getTypeLoc(), |
| 115 | Unexpanded); |
| 116 | assert(!Unexpanded.empty() && "Pack expansion without parameter packs?"); |
| 117 | |
| 118 | // Determine whether we can expand this attribute pack yet. |
| 119 | bool Expand = true, RetainExpansion = false; |
| 120 | Optional<unsigned> NumExpansions; |
| 121 | // FIXME: Use the actual location of the ellipsis. |
| 122 | SourceLocation EllipsisLoc = Aligned->getLocation(); |
| 123 | if (S.CheckParameterPacksForExpansion(EllipsisLoc, Aligned->getRange(), |
| 124 | Unexpanded, TemplateArgs, Expand, |
| 125 | RetainExpansion, NumExpansions)) |
| 126 | return; |
| 127 | |
| 128 | if (!Expand) { |
| 129 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, -1); |
| 130 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, true); |
| 131 | } else { |
| 132 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 133 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, I); |
| 134 | instantiateDependentAlignedAttr(S, TemplateArgs, Aligned, New, false); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 139 | static void instantiateDependentAssumeAlignedAttr( |
| 140 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 141 | const AssumeAlignedAttr *Aligned, Decl *New) { |
| 142 | // The alignment expression is a constant expression. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 143 | EnterExpressionEvaluationContext Unevaluated( |
| 144 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 145 | |
| 146 | Expr *E, *OE = nullptr; |
| 147 | ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs); |
| 148 | if (Result.isInvalid()) |
| 149 | return; |
| 150 | E = Result.getAs<Expr>(); |
| 151 | |
| 152 | if (Aligned->getOffset()) { |
| 153 | Result = S.SubstExpr(Aligned->getOffset(), TemplateArgs); |
| 154 | if (Result.isInvalid()) |
| 155 | return; |
| 156 | OE = Result.getAs<Expr>(); |
| 157 | } |
| 158 | |
| 159 | S.AddAssumeAlignedAttr(Aligned->getLocation(), New, E, OE, |
| 160 | Aligned->getSpellingListIndex()); |
| 161 | } |
| 162 | |
| Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 163 | static void instantiateDependentAlignValueAttr( |
| 164 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 165 | const AlignValueAttr *Aligned, Decl *New) { |
| 166 | // The alignment expression is a constant expression. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 167 | EnterExpressionEvaluationContext Unevaluated( |
| 168 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 169 | ExprResult Result = S.SubstExpr(Aligned->getAlignment(), TemplateArgs); |
| 170 | if (!Result.isInvalid()) |
| 171 | S.AddAlignValueAttr(Aligned->getLocation(), New, Result.getAs<Expr>(), |
| 172 | Aligned->getSpellingListIndex()); |
| 173 | } |
| 174 | |
| Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 175 | static void instantiateDependentAllocAlignAttr( |
| 176 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 177 | const AllocAlignAttr *Align, Decl *New) { |
| 178 | Expr *Param = IntegerLiteral::Create( |
| Joel E. Denny | 8150810 | 2018-03-13 14:51:22 +0000 | [diff] [blame] | 179 | S.getASTContext(), |
| 180 | llvm::APInt(64, Align->getParamIndex().getSourceIndex()), |
| Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 181 | S.getASTContext().UnsignedLongLongTy, Align->getLocation()); |
| 182 | S.AddAllocAlignAttr(Align->getLocation(), New, Param, |
| 183 | Align->getSpellingListIndex()); |
| 184 | } |
| 185 | |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 186 | static Expr *instantiateDependentFunctionAttrCondition( |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 187 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 188 | const Attr *A, Expr *OldCond, const Decl *Tmpl, FunctionDecl *New) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 189 | Expr *Cond = nullptr; |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 190 | { |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 191 | Sema::ContextRAII SwitchContext(S, New); |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 192 | EnterExpressionEvaluationContext Unevaluated( |
| 193 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 194 | ExprResult Result = S.SubstExpr(OldCond, TemplateArgs); |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 195 | if (Result.isInvalid()) |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 196 | return nullptr; |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 197 | Cond = Result.getAs<Expr>(); |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 198 | } |
| George Burgess IV | 0043195 | 2016-11-17 01:33:54 +0000 | [diff] [blame] | 199 | if (!Cond->isTypeDependent()) { |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 200 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 201 | if (Converted.isInvalid()) |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 202 | return nullptr; |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 203 | Cond = Converted.get(); |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 207 | if (OldCond->isValueDependent() && !Cond->isValueDependent() && |
| 208 | !Expr::isPotentialConstantExprUnevaluated(Cond, New, Diags)) { |
| 209 | S.Diag(A->getLocation(), diag::err_attr_cond_never_constant_expr) << A; |
| 210 | for (const auto &P : Diags) |
| 211 | S.Diag(P.first, P.second); |
| 212 | return nullptr; |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 213 | } |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 214 | return Cond; |
| 215 | } |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 216 | |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 217 | static void instantiateDependentEnableIfAttr( |
| 218 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 219 | const EnableIfAttr *EIA, const Decl *Tmpl, FunctionDecl *New) { |
| 220 | Expr *Cond = instantiateDependentFunctionAttrCondition( |
| 221 | S, TemplateArgs, EIA, EIA->getCond(), Tmpl, New); |
| 222 | |
| 223 | if (Cond) |
| 224 | New->addAttr(new (S.getASTContext()) EnableIfAttr( |
| 225 | EIA->getLocation(), S.getASTContext(), Cond, EIA->getMessage(), |
| 226 | EIA->getSpellingListIndex())); |
| 227 | } |
| 228 | |
| 229 | static void instantiateDependentDiagnoseIfAttr( |
| 230 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 231 | const DiagnoseIfAttr *DIA, const Decl *Tmpl, FunctionDecl *New) { |
| 232 | Expr *Cond = instantiateDependentFunctionAttrCondition( |
| 233 | S, TemplateArgs, DIA, DIA->getCond(), Tmpl, New); |
| 234 | |
| 235 | if (Cond) |
| 236 | New->addAttr(new (S.getASTContext()) DiagnoseIfAttr( |
| 237 | DIA->getLocation(), S.getASTContext(), Cond, DIA->getMessage(), |
| 238 | DIA->getDiagnosticType(), DIA->getArgDependent(), New, |
| 239 | DIA->getSpellingListIndex())); |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 242 | // Constructs and adds to New a new instance of CUDALaunchBoundsAttr using |
| 243 | // template A as the base and arguments from TemplateArgs. |
| 244 | static void instantiateDependentCUDALaunchBoundsAttr( |
| 245 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 246 | const CUDALaunchBoundsAttr &Attr, Decl *New) { |
| 247 | // The alignment expression is a constant expression. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 248 | EnterExpressionEvaluationContext Unevaluated( |
| 249 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 250 | |
| 251 | ExprResult Result = S.SubstExpr(Attr.getMaxThreads(), TemplateArgs); |
| 252 | if (Result.isInvalid()) |
| 253 | return; |
| 254 | Expr *MaxThreads = Result.getAs<Expr>(); |
| 255 | |
| 256 | Expr *MinBlocks = nullptr; |
| 257 | if (Attr.getMinBlocks()) { |
| 258 | Result = S.SubstExpr(Attr.getMinBlocks(), TemplateArgs); |
| 259 | if (Result.isInvalid()) |
| 260 | return; |
| 261 | MinBlocks = Result.getAs<Expr>(); |
| 262 | } |
| 263 | |
| 264 | S.AddLaunchBoundsAttr(Attr.getLocation(), New, MaxThreads, MinBlocks, |
| 265 | Attr.getSpellingListIndex()); |
| 266 | } |
| 267 | |
| Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 268 | static void |
| 269 | instantiateDependentModeAttr(Sema &S, |
| 270 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 271 | const ModeAttr &Attr, Decl *New) { |
| 272 | S.AddModeAttr(Attr.getRange(), New, Attr.getMode(), |
| 273 | Attr.getSpellingListIndex(), /*InInstantiation=*/true); |
| 274 | } |
| 275 | |
| Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 276 | /// Instantiation of 'declare simd' attribute and its arguments. |
| 277 | static void instantiateOMPDeclareSimdDeclAttr( |
| 278 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 279 | const OMPDeclareSimdDeclAttr &Attr, Decl *New) { |
| Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 280 | // Allow 'this' in clauses with varlists. |
| 281 | if (auto *FTD = dyn_cast<FunctionTemplateDecl>(New)) |
| 282 | New = FTD->getTemplatedDecl(); |
| 283 | auto *FD = cast<FunctionDecl>(New); |
| 284 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(FD->getDeclContext()); |
| Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 285 | SmallVector<Expr *, 4> Uniforms, Aligneds, Alignments, Linears, Steps; |
| 286 | SmallVector<unsigned, 4> LinModifiers; |
| Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 287 | |
| Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 288 | auto SubstExpr = [&](Expr *E) -> ExprResult { |
| Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 289 | if (auto *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts())) |
| 290 | if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) { |
| 291 | Sema::ContextRAII SavedContext(S, FD); |
| 292 | LocalInstantiationScope Local(S); |
| 293 | if (FD->getNumParams() > PVD->getFunctionScopeIndex()) |
| 294 | Local.InstantiatedLocal( |
| 295 | PVD, FD->getParamDecl(PVD->getFunctionScopeIndex())); |
| 296 | return S.SubstExpr(E, TemplateArgs); |
| 297 | } |
| Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 298 | Sema::CXXThisScopeRAII ThisScope(S, ThisContext, Qualifiers(), |
| Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 299 | FD->isCXXInstanceMember()); |
| 300 | return S.SubstExpr(E, TemplateArgs); |
| 301 | }; |
| 302 | |
| Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 303 | // Substitute a single OpenMP clause, which is a potentially-evaluated |
| 304 | // full-expression. |
| 305 | auto Subst = [&](Expr *E) -> ExprResult { |
| 306 | EnterExpressionEvaluationContext Evaluated( |
| 307 | S, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
| 308 | ExprResult Res = SubstExpr(E); |
| 309 | if (Res.isInvalid()) |
| 310 | return Res; |
| 311 | return S.ActOnFinishFullExpr(Res.get(), false); |
| 312 | }; |
| 313 | |
| Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 314 | ExprResult Simdlen; |
| 315 | if (auto *E = Attr.getSimdlen()) |
| 316 | Simdlen = Subst(E); |
| 317 | |
| Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 318 | if (Attr.uniforms_size() > 0) { |
| 319 | for(auto *E : Attr.uniforms()) { |
| 320 | ExprResult Inst = Subst(E); |
| 321 | if (Inst.isInvalid()) |
| 322 | continue; |
| 323 | Uniforms.push_back(Inst.get()); |
| 324 | } |
| Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 327 | auto AI = Attr.alignments_begin(); |
| 328 | for (auto *E : Attr.aligneds()) { |
| 329 | ExprResult Inst = Subst(E); |
| 330 | if (Inst.isInvalid()) |
| 331 | continue; |
| 332 | Aligneds.push_back(Inst.get()); |
| 333 | Inst = ExprEmpty(); |
| 334 | if (*AI) |
| 335 | Inst = S.SubstExpr(*AI, TemplateArgs); |
| 336 | Alignments.push_back(Inst.get()); |
| 337 | ++AI; |
| 338 | } |
| Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 339 | |
| 340 | auto SI = Attr.steps_begin(); |
| 341 | for (auto *E : Attr.linears()) { |
| 342 | ExprResult Inst = Subst(E); |
| 343 | if (Inst.isInvalid()) |
| 344 | continue; |
| 345 | Linears.push_back(Inst.get()); |
| 346 | Inst = ExprEmpty(); |
| 347 | if (*SI) |
| 348 | Inst = S.SubstExpr(*SI, TemplateArgs); |
| 349 | Steps.push_back(Inst.get()); |
| 350 | ++SI; |
| 351 | } |
| 352 | LinModifiers.append(Attr.modifiers_begin(), Attr.modifiers_end()); |
| Alexey Bataev | d93d376 | 2016-04-12 09:35:56 +0000 | [diff] [blame] | 353 | (void)S.ActOnOpenMPDeclareSimdDirective( |
| 354 | S.ConvertDeclToDeclGroup(New), Attr.getBranchState(), Simdlen.get(), |
| Alexey Bataev | ecba70f | 2016-04-12 11:02:11 +0000 | [diff] [blame] | 355 | Uniforms, Aligneds, Alignments, Linears, LinModifiers, Steps, |
| 356 | Attr.getRange()); |
| Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 359 | static void instantiateDependentAMDGPUFlatWorkGroupSizeAttr( |
| 360 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 361 | const AMDGPUFlatWorkGroupSizeAttr &Attr, Decl *New) { |
| 362 | // Both min and max expression are constant expressions. |
| 363 | EnterExpressionEvaluationContext Unevaluated( |
| 364 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 365 | |
| 366 | ExprResult Result = S.SubstExpr(Attr.getMin(), TemplateArgs); |
| 367 | if (Result.isInvalid()) |
| 368 | return; |
| 369 | Expr *MinExpr = Result.getAs<Expr>(); |
| 370 | |
| 371 | Result = S.SubstExpr(Attr.getMax(), TemplateArgs); |
| 372 | if (Result.isInvalid()) |
| 373 | return; |
| 374 | Expr *MaxExpr = Result.getAs<Expr>(); |
| 375 | |
| 376 | S.addAMDGPUFlatWorkGroupSizeAttr(Attr.getLocation(), New, MinExpr, MaxExpr, |
| 377 | Attr.getSpellingListIndex()); |
| 378 | } |
| 379 | |
| Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 380 | static ExplicitSpecifier |
| 381 | instantiateExplicitSpecifier(Sema &S, |
| 382 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 383 | ExplicitSpecifier ES, FunctionDecl *New) { |
| 384 | if (!ES.getExpr()) |
| 385 | return ES; |
| 386 | Expr *OldCond = ES.getExpr(); |
| 387 | Expr *Cond = nullptr; |
| 388 | { |
| 389 | EnterExpressionEvaluationContext Unevaluated( |
| 390 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 391 | ExprResult SubstResult = S.SubstExpr(OldCond, TemplateArgs); |
| 392 | if (SubstResult.isInvalid()) { |
| 393 | return ExplicitSpecifier::Invalid(); |
| 394 | } |
| 395 | Cond = SubstResult.get(); |
| 396 | } |
| 397 | ExplicitSpecifier Result(Cond, ES.getKind()); |
| 398 | if (!Cond->isTypeDependent()) |
| 399 | S.tryResolveExplicitSpecifier(Result); |
| 400 | return Result; |
| 401 | } |
| 402 | |
| Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 403 | static void instantiateDependentAMDGPUWavesPerEUAttr( |
| 404 | Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs, |
| 405 | const AMDGPUWavesPerEUAttr &Attr, Decl *New) { |
| 406 | // Both min and max expression are constant expressions. |
| 407 | EnterExpressionEvaluationContext Unevaluated( |
| 408 | S, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 409 | |
| 410 | ExprResult Result = S.SubstExpr(Attr.getMin(), TemplateArgs); |
| 411 | if (Result.isInvalid()) |
| 412 | return; |
| 413 | Expr *MinExpr = Result.getAs<Expr>(); |
| 414 | |
| 415 | Expr *MaxExpr = nullptr; |
| 416 | if (auto Max = Attr.getMax()) { |
| 417 | Result = S.SubstExpr(Max, TemplateArgs); |
| 418 | if (Result.isInvalid()) |
| 419 | return; |
| 420 | MaxExpr = Result.getAs<Expr>(); |
| 421 | } |
| 422 | |
| 423 | S.addAMDGPUWavesPerEUAttr(Attr.getLocation(), New, MinExpr, MaxExpr, |
| 424 | Attr.getSpellingListIndex()); |
| 425 | } |
| 426 | |
| Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 427 | void Sema::InstantiateAttrsForDecl( |
| 428 | const MultiLevelTemplateArgumentList &TemplateArgs, const Decl *Tmpl, |
| 429 | Decl *New, LateInstantiatedAttrVec *LateAttrs, |
| 430 | LocalInstantiationScope *OuterMostScope) { |
| 431 | if (NamedDecl *ND = dyn_cast<NamedDecl>(New)) { |
| 432 | for (const auto *TmplAttr : Tmpl->attrs()) { |
| 433 | // FIXME: If any of the special case versions from InstantiateAttrs become |
| 434 | // applicable to template declaration, we'll need to add them here. |
| 435 | CXXThisScopeRAII ThisScope( |
| 436 | *this, dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()), |
| Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 437 | Qualifiers(), ND->isCXXInstanceMember()); |
| Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 438 | |
| 439 | Attr *NewAttr = sema::instantiateTemplateAttributeForDecl( |
| 440 | TmplAttr, Context, *this, TemplateArgs); |
| Richard Smith | 33bddbd | 2018-01-04 23:42:29 +0000 | [diff] [blame] | 441 | if (NewAttr) |
| Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 442 | New->addAttr(NewAttr); |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 447 | static Sema::RetainOwnershipKind |
| 448 | attrToRetainOwnershipKind(const Attr *A) { |
| 449 | switch (A->getKind()) { |
| 450 | case clang::attr::CFConsumed: |
| 451 | return Sema::RetainOwnershipKind::CF; |
| 452 | case clang::attr::OSConsumed: |
| 453 | return Sema::RetainOwnershipKind::OS; |
| 454 | case clang::attr::NSConsumed: |
| 455 | return Sema::RetainOwnershipKind::NS; |
| 456 | default: |
| 457 | llvm_unreachable("Wrong argument supplied"); |
| 458 | } |
| 459 | } |
| 460 | |
| John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 461 | void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, |
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 462 | const Decl *Tmpl, Decl *New, |
| 463 | LateInstantiatedAttrVec *LateAttrs, |
| 464 | LocalInstantiationScope *OuterMostScope) { |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 465 | for (const auto *TmplAttr : Tmpl->attrs()) { |
| Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 466 | // FIXME: This should be generalized to more than just the AlignedAttr. |
| Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 467 | const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr); |
| 468 | if (Aligned && Aligned->isAlignmentDependent()) { |
| 469 | instantiateDependentAlignedAttr(*this, TemplateArgs, Aligned, New); |
| 470 | continue; |
| Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| Erich Keane | 23384a1 | 2019-08-23 15:58:35 +0000 | [diff] [blame] | 473 | if (const auto *AssumeAligned = dyn_cast<AssumeAlignedAttr>(TmplAttr)) { |
| Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 474 | instantiateDependentAssumeAlignedAttr(*this, TemplateArgs, AssumeAligned, New); |
| 475 | continue; |
| 476 | } |
| 477 | |
| Erich Keane | 23384a1 | 2019-08-23 15:58:35 +0000 | [diff] [blame] | 478 | if (const auto *AlignValue = dyn_cast<AlignValueAttr>(TmplAttr)) { |
| Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 479 | instantiateDependentAlignValueAttr(*this, TemplateArgs, AlignValue, New); |
| 480 | continue; |
| 481 | } |
| 482 | |
| Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 483 | if (const auto *AllocAlign = dyn_cast<AllocAlignAttr>(TmplAttr)) { |
| 484 | instantiateDependentAllocAlignAttr(*this, TemplateArgs, AllocAlign, New); |
| 485 | continue; |
| 486 | } |
| 487 | |
| 488 | |
| George Burgess IV | 0043195 | 2016-11-17 01:33:54 +0000 | [diff] [blame] | 489 | if (const auto *EnableIf = dyn_cast<EnableIfAttr>(TmplAttr)) { |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 490 | instantiateDependentEnableIfAttr(*this, TemplateArgs, EnableIf, Tmpl, |
| George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 491 | cast<FunctionDecl>(New)); |
| 492 | continue; |
| 493 | } |
| 494 | |
| 495 | if (const auto *DiagnoseIf = dyn_cast<DiagnoseIfAttr>(TmplAttr)) { |
| 496 | instantiateDependentDiagnoseIfAttr(*this, TemplateArgs, DiagnoseIf, Tmpl, |
| 497 | cast<FunctionDecl>(New)); |
| Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 498 | continue; |
| 499 | } |
| 500 | |
| Erich Keane | 23384a1 | 2019-08-23 15:58:35 +0000 | [diff] [blame] | 501 | if (const auto *CUDALaunchBounds = |
| Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 502 | dyn_cast<CUDALaunchBoundsAttr>(TmplAttr)) { |
| 503 | instantiateDependentCUDALaunchBoundsAttr(*this, TemplateArgs, |
| 504 | *CUDALaunchBounds, New); |
| 505 | continue; |
| 506 | } |
| 507 | |
| Erich Keane | 23384a1 | 2019-08-23 15:58:35 +0000 | [diff] [blame] | 508 | if (const auto *Mode = dyn_cast<ModeAttr>(TmplAttr)) { |
| Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 509 | instantiateDependentModeAttr(*this, TemplateArgs, *Mode, New); |
| 510 | continue; |
| 511 | } |
| 512 | |
| Alexey Bataev | 2af33e3 | 2016-04-07 12:45:37 +0000 | [diff] [blame] | 513 | if (const auto *OMPAttr = dyn_cast<OMPDeclareSimdDeclAttr>(TmplAttr)) { |
| 514 | instantiateOMPDeclareSimdDeclAttr(*this, TemplateArgs, *OMPAttr, New); |
| 515 | continue; |
| 516 | } |
| 517 | |
| Erich Keane | 23384a1 | 2019-08-23 15:58:35 +0000 | [diff] [blame] | 518 | if (const auto *AMDGPUFlatWorkGroupSize = |
| Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 519 | dyn_cast<AMDGPUFlatWorkGroupSizeAttr>(TmplAttr)) { |
| 520 | instantiateDependentAMDGPUFlatWorkGroupSizeAttr( |
| 521 | *this, TemplateArgs, *AMDGPUFlatWorkGroupSize, New); |
| 522 | } |
| 523 | |
| Erich Keane | 23384a1 | 2019-08-23 15:58:35 +0000 | [diff] [blame] | 524 | if (const auto *AMDGPUFlatWorkGroupSize = |
| Michael Liao | 7557afa | 2019-02-26 18:49:36 +0000 | [diff] [blame] | 525 | dyn_cast<AMDGPUWavesPerEUAttr>(TmplAttr)) { |
| 526 | instantiateDependentAMDGPUWavesPerEUAttr(*this, TemplateArgs, |
| 527 | *AMDGPUFlatWorkGroupSize, New); |
| 528 | } |
| 529 | |
| Hans Wennborg | c2b7f7a | 2014-08-24 00:12:36 +0000 | [diff] [blame] | 530 | // Existing DLL attribute on the instantiation takes precedence. |
| 531 | if (TmplAttr->getKind() == attr::DLLExport || |
| 532 | TmplAttr->getKind() == attr::DLLImport) { |
| 533 | if (New->hasAttr<DLLExportAttr>() || New->hasAttr<DLLImportAttr>()) { |
| 534 | continue; |
| 535 | } |
| 536 | } |
| 537 | |
| Erich Keane | 23384a1 | 2019-08-23 15:58:35 +0000 | [diff] [blame] | 538 | if (const auto *ABIAttr = dyn_cast<ParameterABIAttr>(TmplAttr)) { |
| John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 539 | AddParameterABIAttr(ABIAttr->getRange(), New, ABIAttr->getABI(), |
| 540 | ABIAttr->getSpellingListIndex()); |
| 541 | continue; |
| 542 | } |
| 543 | |
| George Karpenkov | 1657f36 | 2018-11-30 02:18:37 +0000 | [diff] [blame] | 544 | if (isa<NSConsumedAttr>(TmplAttr) || isa<OSConsumedAttr>(TmplAttr) || |
| 545 | isa<CFConsumedAttr>(TmplAttr)) { |
| 546 | AddXConsumedAttr(New, TmplAttr->getRange(), |
| 547 | TmplAttr->getSpellingListIndex(), |
| 548 | attrToRetainOwnershipKind(TmplAttr), |
| 549 | /*template instantiation=*/true); |
| John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 550 | continue; |
| 551 | } |
| 552 | |
| Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 553 | assert(!TmplAttr->isPackExpansion()); |
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 554 | if (TmplAttr->isLateParsed() && LateAttrs) { |
| 555 | // Late parsed attributes must be instantiated and attached after the |
| 556 | // enclosing class has been instantiated. See Sema::InstantiateClass. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 557 | LocalInstantiationScope *Saved = nullptr; |
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 558 | if (CurrentInstantiationScope) |
| 559 | Saved = CurrentInstantiationScope->cloneScopes(OuterMostScope); |
| 560 | LateAttrs->push_back(LateInstantiatedAttribute(TmplAttr, Saved, New)); |
| 561 | } else { |
| Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 562 | // Allow 'this' within late-parsed attributes. |
| 563 | NamedDecl *ND = dyn_cast<NamedDecl>(New); |
| 564 | CXXRecordDecl *ThisContext = |
| 565 | dyn_cast_or_null<CXXRecordDecl>(ND->getDeclContext()); |
| Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 566 | CXXThisScopeRAII ThisScope(*this, ThisContext, Qualifiers(), |
| Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 567 | ND && ND->isCXXInstanceMember()); |
| 568 | |
| Benjamin Kramer | bf8da9d | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 569 | Attr *NewAttr = sema::instantiateTemplateAttribute(TmplAttr, Context, |
| 570 | *this, TemplateArgs); |
| Richard Smith | 33bddbd | 2018-01-04 23:42:29 +0000 | [diff] [blame] | 571 | if (NewAttr) |
| Rafael Espindola | 7f90b7d | 2012-05-15 14:09:55 +0000 | [diff] [blame] | 572 | New->addAttr(NewAttr); |
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 573 | } |
| Anders Carlsson | 3d70975 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
| Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 577 | /// Get the previous declaration of a declaration for the purposes of template |
| 578 | /// instantiation. If this finds a previous declaration, then the previous |
| 579 | /// declaration of the instantiation of D should be an instantiation of the |
| 580 | /// result of this function. |
| 581 | template<typename DeclT> |
| 582 | static DeclT *getPreviousDeclForInstantiation(DeclT *D) { |
| 583 | DeclT *Result = D->getPreviousDecl(); |
| 584 | |
| 585 | // If the declaration is within a class, and the previous declaration was |
| 586 | // merged from a different definition of that class, then we don't have a |
| 587 | // previous declaration for the purpose of template instantiation. |
| 588 | if (Result && isa<CXXRecordDecl>(D->getDeclContext()) && |
| 589 | D->getLexicalDeclContext() != Result->getLexicalDeclContext()) |
| 590 | return nullptr; |
| 591 | |
| 592 | return Result; |
| 593 | } |
| 594 | |
| Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 595 | Decl * |
| 596 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 597 | llvm_unreachable("Translation units cannot be instantiated"); |
| Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | Decl * |
| Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 601 | TemplateDeclInstantiator::VisitPragmaCommentDecl(PragmaCommentDecl *D) { |
| 602 | llvm_unreachable("pragma comment cannot be instantiated"); |
| 603 | } |
| 604 | |
| Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 605 | Decl *TemplateDeclInstantiator::VisitPragmaDetectMismatchDecl( |
| 606 | PragmaDetectMismatchDecl *D) { |
| 607 | llvm_unreachable("pragma comment cannot be instantiated"); |
| 608 | } |
| 609 | |
| Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 610 | Decl * |
| Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 611 | TemplateDeclInstantiator::VisitExternCContextDecl(ExternCContextDecl *D) { |
| 612 | llvm_unreachable("extern \"C\" context cannot be instantiated"); |
| 613 | } |
| 614 | |
| 615 | Decl * |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 616 | TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) { |
| 617 | LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 618 | D->getIdentifier()); |
| 619 | Owner->addDecl(Inst); |
| 620 | return Inst; |
| 621 | } |
| 622 | |
| 623 | Decl * |
| Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 624 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 625 | llvm_unreachable("Namespaces cannot be instantiated"); |
| Douglas Gregor | 8a65553 | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 628 | Decl * |
| 629 | TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 630 | NamespaceAliasDecl *Inst |
| 631 | = NamespaceAliasDecl::Create(SemaRef.Context, Owner, |
| 632 | D->getNamespaceLoc(), |
| 633 | D->getAliasLoc(), |
| Douglas Gregor | c05ba2e | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 634 | D->getIdentifier(), |
| 635 | D->getQualifierLoc(), |
| John McCall | d8d0d43 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 636 | D->getTargetNameLoc(), |
| 637 | D->getNamespace()); |
| 638 | Owner->addDecl(Inst); |
| 639 | return Inst; |
| 640 | } |
| 641 | |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 642 | Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D, |
| 643 | bool IsTypeAlias) { |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 644 | bool Invalid = false; |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 645 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
| Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 646 | if (DI->getType()->isInstantiationDependentType() || |
| Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 647 | DI->getType()->isVariablyModifiedType()) { |
| John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 648 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 649 | D->getLocation(), D->getDeclName()); |
| 650 | if (!DI) { |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 651 | Invalid = true; |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 652 | DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy); |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 653 | } |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 654 | } else { |
| 655 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 656 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | |
| Richard Smith | 2ddcbab | 2012-10-23 00:32:41 +0000 | [diff] [blame] | 658 | // HACK: g++ has a bug where it gets the value kind of ?: wrong. |
| 659 | // libstdc++ relies upon this bug in its implementation of common_type. |
| 660 | // If we happen to be processing that implementation, fake up the g++ ?: |
| 661 | // semantics. See LWG issue 2141 for more information on the bug. |
| 662 | const DecltypeType *DT = DI->getType()->getAs<DecltypeType>(); |
| 663 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext()); |
| 664 | if (DT && RD && isa<ConditionalOperator>(DT->getUnderlyingExpr()) && |
| 665 | DT->isReferenceType() && |
| 666 | RD->getEnclosingNamespaceContext() == SemaRef.getStdNamespace() && |
| 667 | RD->getIdentifier() && RD->getIdentifier()->isStr("common_type") && |
| 668 | D->getIdentifier() && D->getIdentifier()->isStr("type") && |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 669 | SemaRef.getSourceManager().isInSystemHeader(D->getBeginLoc())) |
| Richard Smith | 2ddcbab | 2012-10-23 00:32:41 +0000 | [diff] [blame] | 670 | // Fold it to the (non-reference) type which g++ would have produced. |
| 671 | DI = SemaRef.Context.getTrivialTypeSourceInfo( |
| 672 | DI->getType().getNonReferenceType()); |
| 673 | |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 674 | // Create the new typedef |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 675 | TypedefNameDecl *Typedef; |
| 676 | if (IsTypeAlias) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 677 | Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 678 | D->getLocation(), D->getIdentifier(), DI); |
| 679 | else |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 680 | Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 681 | D->getLocation(), D->getIdentifier(), DI); |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 682 | if (Invalid) |
| 683 | Typedef->setInvalidDecl(); |
| 684 | |
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 685 | // If the old typedef was the name for linkage purposes of an anonymous |
| 686 | // tag decl, re-establish that relationship for the new typedef. |
| 687 | if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) { |
| 688 | TagDecl *oldTag = oldTagType->getDecl(); |
| Douglas Gregor | d831d95 | 2013-03-08 22:15:15 +0000 | [diff] [blame] | 689 | if (oldTag->getTypedefNameForAnonDecl() == D && !Invalid) { |
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 690 | TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl(); |
| John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 691 | assert(!newTag->hasNameForLinkage()); |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 692 | newTag->setTypedefNameForAnonDecl(Typedef); |
| John McCall | 04fcd0d | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 693 | } |
| Douglas Gregor | 83eb503 | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 694 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 695 | |
| Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 696 | if (TypedefNameDecl *Prev = getPreviousDeclForInstantiation(D)) { |
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 697 | NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev, |
| 698 | TemplateArgs); |
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 699 | if (!InstPrev) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 700 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 701 | |
| Rafael Espindola | cde2c8f | 2011-12-26 22:42:47 +0000 | [diff] [blame] | 702 | TypedefNameDecl *InstPrevTypedef = cast<TypedefNameDecl>(InstPrev); |
| 703 | |
| 704 | // If the typedef types are not identical, reject them. |
| 705 | SemaRef.isIncompatibleTypedef(InstPrevTypedef, Typedef); |
| 706 | |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 707 | Typedef->setPreviousDecl(InstPrevTypedef); |
| John McCall | 91f1a02 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| John McCall | 6602bb1 | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 710 | SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef); |
| Douglas Gregor | 83eb503 | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 711 | |
| John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 712 | Typedef->setAccess(D->getAccess()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 714 | return Typedef; |
| 715 | } |
| 716 | |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 717 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 718 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false); |
| Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 719 | if (Typedef) |
| 720 | Owner->addDecl(Typedef); |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 721 | return Typedef; |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) { |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 725 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true); |
| Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 726 | if (Typedef) |
| 727 | Owner->addDecl(Typedef); |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 728 | return Typedef; |
| 729 | } |
| 730 | |
| 731 | Decl * |
| 732 | TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 733 | // Create a local instantiation scope for this type alias template, which |
| 734 | // will contain the instantiations of the template parameters. |
| 735 | LocalInstantiationScope Scope(SemaRef); |
| 736 | |
| 737 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 738 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 739 | if (!InstParams) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 740 | return nullptr; |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 741 | |
| 742 | TypeAliasDecl *Pattern = D->getTemplatedDecl(); |
| 743 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 744 | TypeAliasTemplateDecl *PrevAliasTemplate = nullptr; |
| Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 745 | if (getPreviousDeclForInstantiation<TypedefNameDecl>(Pattern)) { |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 746 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 747 | if (!Found.empty()) { |
| 748 | PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Found.front()); |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
| 752 | TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>( |
| 753 | InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true)); |
| 754 | if (!AliasInst) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 755 | return nullptr; |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 756 | |
| 757 | TypeAliasTemplateDecl *Inst |
| 758 | = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 759 | D->getDeclName(), InstParams, AliasInst); |
| Richard Smith | 43ccec8e | 2014-08-26 03:52:16 +0000 | [diff] [blame] | 760 | AliasInst->setDescribedAliasTemplate(Inst); |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 761 | if (PrevAliasTemplate) |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 762 | Inst->setPreviousDecl(PrevAliasTemplate); |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 763 | |
| 764 | Inst->setAccess(D->getAccess()); |
| 765 | |
| 766 | if (!PrevAliasTemplate) |
| 767 | Inst->setInstantiatedFromMemberTemplate(D); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 768 | |
| Richard Smith | 3f1b5d0 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 769 | Owner->addDecl(Inst); |
| 770 | |
| 771 | return Inst; |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 774 | Decl *TemplateDeclInstantiator::VisitBindingDecl(BindingDecl *D) { |
| Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 775 | auto *NewBD = BindingDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 776 | D->getIdentifier()); |
| Richard Smith | 81df9eb | 2017-10-02 22:43:36 +0000 | [diff] [blame] | 777 | NewBD->setReferenced(D->isReferenced()); |
| Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 778 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewBD); |
| 779 | return NewBD; |
| Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | Decl *TemplateDeclInstantiator::VisitDecompositionDecl(DecompositionDecl *D) { |
| Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 783 | // Transform the bindings first. |
| 784 | SmallVector<BindingDecl*, 16> NewBindings; |
| 785 | for (auto *OldBD : D->bindings()) |
| 786 | NewBindings.push_back(cast<BindingDecl>(VisitBindingDecl(OldBD))); |
| 787 | ArrayRef<BindingDecl*> NewBindingArray = NewBindings; |
| 788 | |
| 789 | auto *NewDD = cast_or_null<DecompositionDecl>( |
| 790 | VisitVarDecl(D, /*InstantiatingVarTemplate=*/false, &NewBindingArray)); |
| 791 | |
| 792 | if (!NewDD || NewDD->isInvalidDecl()) |
| 793 | for (auto *NewBD : NewBindings) |
| 794 | NewBD->setInvalidDecl(); |
| 795 | |
| 796 | return NewDD; |
| Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 797 | } |
| 798 | |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 799 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 800 | return VisitVarDecl(D, /*InstantiatingVarTemplate=*/false); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 803 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D, |
| Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 804 | bool InstantiatingVarTemplate, |
| 805 | ArrayRef<BindingDecl*> *Bindings) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 806 | |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 807 | // Do substitution on the type of the declaration |
| Richard Smith | ee57984 | 2017-01-30 20:39:26 +0000 | [diff] [blame] | 808 | TypeSourceInfo *DI = SemaRef.SubstType( |
| 809 | D->getTypeSourceInfo(), TemplateArgs, D->getTypeSpecStartLoc(), |
| 810 | D->getDeclName(), /*AllowDeducedTST*/true); |
| John McCall | f1abcdc | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 811 | if (!DI) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 812 | return nullptr; |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 813 | |
| Douglas Gregor | 6162334 | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 814 | if (DI->getType()->isFunctionType()) { |
| 815 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 816 | << D->isStaticDataMember() << DI->getType(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 817 | return nullptr; |
| Douglas Gregor | 6162334 | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 818 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 819 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 820 | DeclContext *DC = Owner; |
| 821 | if (D->isLocalExternDecl()) |
| 822 | SemaRef.adjustContextForLocalExternDecl(DC); |
| 823 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 824 | // Build the instantiated declaration. |
| Richard Smith | 3997b1b | 2016-08-12 01:55:21 +0000 | [diff] [blame] | 825 | VarDecl *Var; |
| 826 | if (Bindings) |
| 827 | Var = DecompositionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
| 828 | D->getLocation(), DI->getType(), DI, |
| 829 | D->getStorageClass(), *Bindings); |
| 830 | else |
| 831 | Var = VarDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
| 832 | D->getLocation(), D->getIdentifier(), DI->getType(), |
| 833 | DI, D->getStorageClass()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
| Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 835 | // In ARC, infer 'retaining' for variables of retainable type. |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 836 | if (SemaRef.getLangOpts().ObjCAutoRefCount && |
| Douglas Gregor | 8ca0c64 | 2011-12-10 01:22:52 +0000 | [diff] [blame] | 837 | SemaRef.inferObjCARCLifetime(Var)) |
| 838 | Var->setInvalidDecl(); |
| 839 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 840 | // Substitute the nested name specifier, if any. |
| 841 | if (SubstQualifier(D, Var)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 842 | return nullptr; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 844 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner, |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 845 | StartingScope, InstantiatingVarTemplate); |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 846 | |
| 847 | if (D->isNRVOVariable()) { |
| 848 | QualType ReturnType = cast<FunctionDecl>(DC)->getReturnType(); |
| Richard Trieu | 09c163b | 2018-03-15 03:00:55 +0000 | [diff] [blame] | 849 | if (SemaRef.isCopyElisionCandidate(ReturnType, Var, Sema::CES_Strict)) |
| Taiju Tsuiki | 3be68e1 | 2018-06-19 05:35:30 +0000 | [diff] [blame] | 850 | Var->setNRVOVariable(true); |
| Nick Lewycky | d78f92f | 2014-05-03 00:41:18 +0000 | [diff] [blame] | 851 | } |
| Taiju Tsuiki | 3be68e1 | 2018-06-19 05:35:30 +0000 | [diff] [blame] | 852 | |
| Alexander Kornienko | 83a4e18 | 2014-05-27 21:29:22 +0000 | [diff] [blame] | 853 | Var->setImplicit(D->isImplicit()); |
| 854 | |
| Hans Wennborg | 262baa4 | 2018-10-31 10:34:46 +0000 | [diff] [blame] | 855 | if (Var->isStaticLocal()) |
| 856 | SemaRef.CheckStaticLocalForDllExport(Var); |
| 857 | |
| Douglas Gregor | ef1a09a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 858 | return Var; |
| 859 | } |
| 860 | |
| Abramo Bagnara | d734058 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 861 | Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| 862 | AccessSpecDecl* AD |
| 863 | = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner, |
| 864 | D->getAccessSpecifierLoc(), D->getColonLoc()); |
| 865 | Owner->addHiddenDecl(AD); |
| 866 | return AD; |
| 867 | } |
| 868 | |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 869 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 870 | bool Invalid = false; |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 871 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
| Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 872 | if (DI->getType()->isInstantiationDependentType() || |
| Douglas Gregor | 5a5073e | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 873 | DI->getType()->isVariablyModifiedType()) { |
| John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 874 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 875 | D->getLocation(), D->getDeclName()); |
| 876 | if (!DI) { |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 877 | DI = D->getTypeSourceInfo(); |
| John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 878 | Invalid = true; |
| 879 | } else if (DI->getType()->isFunctionType()) { |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 880 | // C++ [temp.arg.type]p3: |
| 881 | // If a declaration acquires a function type through a type |
| 882 | // dependent on a template-parameter and this causes a |
| 883 | // declaration that does not use the syntactic form of a |
| 884 | // function declarator to have function type, the program is |
| 885 | // ill-formed. |
| 886 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
| John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 887 | << DI->getType(); |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 888 | Invalid = true; |
| 889 | } |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 890 | } else { |
| 891 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | Expr *BitWidth = D->getBitWidth(); |
| 895 | if (Invalid) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 896 | BitWidth = nullptr; |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 897 | else if (BitWidth) { |
| Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 898 | // The bit-width expression is a constant expression. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 899 | EnterExpressionEvaluationContext Unevaluated( |
| 900 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 901 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 902 | ExprResult InstantiatedBitWidth |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 903 | = SemaRef.SubstExpr(BitWidth, TemplateArgs); |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 904 | if (InstantiatedBitWidth.isInvalid()) { |
| 905 | Invalid = true; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 906 | BitWidth = nullptr; |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 907 | } else |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 908 | BitWidth = InstantiatedBitWidth.getAs<Expr>(); |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 909 | } |
| 910 | |
| John McCall | 90459c5 | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 911 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), |
| 912 | DI->getType(), DI, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | cast<RecordDecl>(Owner), |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 914 | D->getLocation(), |
| 915 | D->isMutable(), |
| 916 | BitWidth, |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 917 | D->getInClassInitStyle(), |
| Richard Smith | 47ad017 | 2012-05-23 04:22:22 +0000 | [diff] [blame] | 918 | D->getInnerLocStart(), |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 919 | D->getAccess(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 920 | nullptr); |
| Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 921 | if (!Field) { |
| 922 | cast<Decl>(Owner)->setInvalidDecl(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 923 | return nullptr; |
| Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 924 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 925 | |
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 926 | SemaRef.InstantiateAttrs(TemplateArgs, D, Field, LateAttrs, StartingScope); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 927 | |
| Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 928 | if (Field->hasAttrs()) |
| 929 | SemaRef.CheckAlignasUnderalignment(Field); |
| 930 | |
| Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 931 | if (Invalid) |
| 932 | Field->setInvalidDecl(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 933 | |
| Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 934 | if (!Field->getDeclName()) { |
| 935 | // Keep track of where this decl came from. |
| 936 | SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 937 | } |
| Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 938 | if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) { |
| 939 | if (Parent->isAnonymousStructOrUnion() && |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 940 | Parent->getRedeclContext()->isFunctionOrMethod()) |
| Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 941 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field); |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 942 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | |
| Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 944 | Field->setImplicit(D->isImplicit()); |
| John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 945 | Field->setAccess(D->getAccess()); |
| Anders Carlsson | 2e56cc6 | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 946 | Owner->addDecl(Field); |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 947 | |
| 948 | return Field; |
| 949 | } |
| 950 | |
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 951 | Decl *TemplateDeclInstantiator::VisitMSPropertyDecl(MSPropertyDecl *D) { |
| 952 | bool Invalid = false; |
| 953 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
| 954 | |
| 955 | if (DI->getType()->isVariablyModifiedType()) { |
| 956 | SemaRef.Diag(D->getLocation(), diag::err_property_is_variably_modified) |
| Aaron Ballman | 1bda459 | 2014-01-03 01:09:27 +0000 | [diff] [blame] | 957 | << D; |
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 958 | Invalid = true; |
| 959 | } else if (DI->getType()->isInstantiationDependentType()) { |
| 960 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 961 | D->getLocation(), D->getDeclName()); |
| 962 | if (!DI) { |
| 963 | DI = D->getTypeSourceInfo(); |
| 964 | Invalid = true; |
| 965 | } else if (DI->getType()->isFunctionType()) { |
| 966 | // C++ [temp.arg.type]p3: |
| 967 | // If a declaration acquires a function type through a type |
| 968 | // dependent on a template-parameter and this causes a |
| 969 | // declaration that does not use the syntactic form of a |
| 970 | // function declarator to have function type, the program is |
| 971 | // ill-formed. |
| 972 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
| 973 | << DI->getType(); |
| 974 | Invalid = true; |
| 975 | } |
| 976 | } else { |
| 977 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
| 978 | } |
| 979 | |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 980 | MSPropertyDecl *Property = MSPropertyDecl::Create( |
| 981 | SemaRef.Context, Owner, D->getLocation(), D->getDeclName(), DI->getType(), |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 982 | DI, D->getBeginLoc(), D->getGetterId(), D->getSetterId()); |
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 983 | |
| 984 | SemaRef.InstantiateAttrs(TemplateArgs, D, Property, LateAttrs, |
| 985 | StartingScope); |
| 986 | |
| 987 | if (Invalid) |
| 988 | Property->setInvalidDecl(); |
| 989 | |
| 990 | Property->setAccess(D->getAccess()); |
| 991 | Owner->addDecl(Property); |
| 992 | |
| 993 | return Property; |
| 994 | } |
| 995 | |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 996 | Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 997 | NamedDecl **NamedChain = |
| 998 | new (SemaRef.Context)NamedDecl*[D->getChainingSize()]; |
| 999 | |
| 1000 | int i = 0; |
| Aaron Ballman | 29c9460 | 2014-03-07 18:36:15 +0000 | [diff] [blame] | 1001 | for (auto *PI : D->chain()) { |
| Aaron Ballman | 1391608 | 2014-03-07 18:11:58 +0000 | [diff] [blame] | 1002 | NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), PI, |
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 1003 | TemplateArgs); |
| 1004 | if (!Next) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1005 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1006 | |
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 1007 | NamedChain[i++] = Next; |
| 1008 | } |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1009 | |
| Francois Pichet | dbafc19 | 2010-12-09 10:07:54 +0000 | [diff] [blame] | 1010 | QualType T = cast<FieldDecl>(NamedChain[i-1])->getType(); |
| Aaron Ballman | 260995b | 2014-10-15 16:58:18 +0000 | [diff] [blame] | 1011 | IndirectFieldDecl *IndirectField = IndirectFieldDecl::Create( |
| 1012 | SemaRef.Context, Owner, D->getLocation(), D->getIdentifier(), T, |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 1013 | {NamedChain, D->getChainingSize()}); |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1014 | |
| NAKAMURA Takumi | 729be14 | 2014-10-27 12:37:26 +0000 | [diff] [blame] | 1015 | for (const auto *Attr : D->attrs()) |
| 1016 | IndirectField->addAttr(Attr->clone(SemaRef.Context)); |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1017 | |
| 1018 | IndirectField->setImplicit(D->isImplicit()); |
| 1019 | IndirectField->setAccess(D->getAccess()); |
| 1020 | Owner->addDecl(IndirectField); |
| 1021 | return IndirectField; |
| 1022 | } |
| 1023 | |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1024 | Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1025 | // Handle friend type expressions by simply substituting template |
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 1026 | // parameters into the pattern type and checking the result. |
| John McCall | 15ad096 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 1027 | if (TypeSourceInfo *Ty = D->getFriendType()) { |
| Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1028 | TypeSourceInfo *InstTy; |
| 1029 | // If this is an unsupported friend, don't bother substituting template |
| 1030 | // arguments into it. The actual type referred to won't be used by any |
| 1031 | // parts of Clang, and may not be valid for instantiating. Just use the |
| 1032 | // same info for the instantiated friend. |
| 1033 | if (D->isUnsupportedFriend()) { |
| 1034 | InstTy = Ty; |
| 1035 | } else { |
| 1036 | InstTy = SemaRef.SubstType(Ty, TemplateArgs, |
| 1037 | D->getLocation(), DeclarationName()); |
| 1038 | } |
| 1039 | if (!InstTy) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1040 | return nullptr; |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1041 | |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1042 | FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getBeginLoc(), |
| Abramo Bagnara | 254b630 | 2011-10-29 20:52:52 +0000 | [diff] [blame] | 1043 | D->getFriendLoc(), InstTy); |
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 1044 | if (!FD) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1045 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1046 | |
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 1047 | FD->setAccess(AS_public); |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1048 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 1049 | Owner->addDecl(FD); |
| 1050 | return FD; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 1053 | NamedDecl *ND = D->getFriendDecl(); |
| 1054 | assert(ND && "friend decl must be a decl or a type!"); |
| 1055 | |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1056 | // All of the Visit implementations for the various potential friend |
| 1057 | // declarations have to be carefully written to work for friend |
| 1058 | // objects, with the most important detail being that the target |
| 1059 | // decl should almost certainly not be placed in Owner. |
| 1060 | Decl *NewND = Visit(ND); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1061 | if (!NewND) return nullptr; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1062 | |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1063 | FriendDecl *FD = |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1064 | FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| Douglas Gregor | 3b4abb6 | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 1065 | cast<NamedDecl>(NewND), D->getFriendLoc()); |
| John McCall | 75c03bb | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 1066 | FD->setAccess(AS_public); |
| John McCall | ace48cd | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 1067 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1068 | Owner->addDecl(FD); |
| 1069 | return FD; |
| John McCall | 58de358 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1072 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 1073 | Expr *AssertExpr = D->getAssertExpr(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1074 | |
| Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 1075 | // The expression in a static assertion is a constant expression. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1076 | EnterExpressionEvaluationContext Unevaluated( |
| 1077 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1079 | ExprResult InstantiatedAssertExpr |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1080 | = SemaRef.SubstExpr(AssertExpr, TemplateArgs); |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1081 | if (InstantiatedAssertExpr.isInvalid()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1082 | return nullptr; |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1083 | |
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 1084 | return SemaRef.BuildStaticAssertDeclaration(D->getLocation(), |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1085 | InstantiatedAssertExpr.get(), |
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 1086 | D->getMessage(), |
| 1087 | D->getRParenLoc(), |
| 1088 | D->isFailed()); |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1092 | EnumDecl *PrevDecl = nullptr; |
| Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1093 | if (EnumDecl *PatternPrev = getPreviousDeclForInstantiation(D)) { |
| Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 1094 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1095 | PatternPrev, |
| Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 1096 | TemplateArgs); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1097 | if (!Prev) return nullptr; |
| Richard Smith | 2e6610a | 2012-03-26 04:58:10 +0000 | [diff] [blame] | 1098 | PrevDecl = cast<EnumDecl>(Prev); |
| 1099 | } |
| 1100 | |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1101 | EnumDecl *Enum = |
| 1102 | EnumDecl::Create(SemaRef.Context, Owner, D->getBeginLoc(), |
| 1103 | D->getLocation(), D->getIdentifier(), PrevDecl, |
| 1104 | D->isScoped(), D->isScopedUsingClassTag(), D->isFixed()); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1105 | if (D->isFixed()) { |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1106 | if (TypeSourceInfo *TI = D->getIntegerTypeSourceInfo()) { |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1107 | // If we have type source information for the underlying type, it means it |
| 1108 | // has been explicitly set by the user. Perform substitution on it before |
| 1109 | // moving on. |
| 1110 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1111 | TypeSourceInfo *NewTI = SemaRef.SubstType(TI, TemplateArgs, UnderlyingLoc, |
| 1112 | DeclarationName()); |
| 1113 | if (!NewTI || SemaRef.CheckEnumUnderlyingType(NewTI)) |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1114 | Enum->setIntegerType(SemaRef.Context.IntTy); |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1115 | else |
| 1116 | Enum->setIntegerTypeSourceInfo(NewTI); |
| 1117 | } else { |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 1118 | assert(!D->getIntegerType()->isDependentType() |
| 1119 | && "Dependent type without type source info"); |
| 1120 | Enum->setIntegerType(D->getIntegerType()); |
| 1121 | } |
| 1122 | } |
| 1123 | |
| John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 1124 | SemaRef.InstantiateAttrs(TemplateArgs, D, Enum); |
| 1125 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1126 | Enum->setInstantiationOfMemberEnum(D, TSK_ImplicitInstantiation); |
| Douglas Gregor | 6c2adff | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 1127 | Enum->setAccess(D->getAccess()); |
| David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 1128 | // Forward the mangling number from the template to the instantiated decl. |
| 1129 | SemaRef.Context.setManglingNumber(Enum, SemaRef.Context.getManglingNumber(D)); |
| David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 1130 | // See if the old tag was defined along with a declarator. |
| 1131 | // If it did, mark the new tag as being associated with that declarator. |
| 1132 | if (DeclaratorDecl *DD = SemaRef.Context.getDeclaratorForUnnamedTagDecl(D)) |
| 1133 | SemaRef.Context.addDeclaratorForUnnamedTagDecl(Enum, DD); |
| 1134 | // See if the old tag was defined along with a typedef. |
| 1135 | // If it did, mark the new tag as being associated with that typedef. |
| 1136 | if (TypedefNameDecl *TND = SemaRef.Context.getTypedefNameForUnnamedTagDecl(D)) |
| 1137 | SemaRef.Context.addTypedefNameForUnnamedTagDecl(Enum, TND); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1138 | if (SubstQualifier(D, Enum)) return nullptr; |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1139 | Owner->addDecl(Enum); |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1140 | |
| Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 1141 | EnumDecl *Def = D->getDefinition(); |
| 1142 | if (Def && Def != D) { |
| 1143 | // If this is an out-of-line definition of an enum member template, check |
| 1144 | // that the underlying types match in the instantiation of both |
| 1145 | // declarations. |
| 1146 | if (TypeSourceInfo *TI = Def->getIntegerTypeSourceInfo()) { |
| 1147 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
| 1148 | QualType DefnUnderlying = |
| 1149 | SemaRef.SubstType(TI->getType(), TemplateArgs, |
| 1150 | UnderlyingLoc, DeclarationName()); |
| 1151 | SemaRef.CheckEnumRedeclaration(Def->getLocation(), Def->isScoped(), |
| Reid Kleckner | b0a17ed | 2018-02-12 17:37:06 +0000 | [diff] [blame] | 1152 | DefnUnderlying, /*IsFixed=*/true, Enum); |
| Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 1153 | } |
| 1154 | } |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1155 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1156 | // C++11 [temp.inst]p1: The implicit instantiation of a class template |
| 1157 | // specialization causes the implicit instantiation of the declarations, but |
| 1158 | // not the definitions of scoped member enumerations. |
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1159 | // |
| 1160 | // DR1484 clarifies that enumeration definitions inside of a template |
| 1161 | // declaration aren't considered entities that can be separately instantiated |
| 1162 | // from the rest of the entity they are declared inside of. |
| 1163 | if (isDeclWithinFunction(D) ? D == Def : Def && !Enum->isScoped()) { |
| 1164 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum); |
| Richard Smith | 258a744 | 2012-03-26 04:08:46 +0000 | [diff] [blame] | 1165 | InstantiateEnumDefinition(Enum, Def); |
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1166 | } |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1167 | |
| 1168 | return Enum; |
| 1169 | } |
| 1170 | |
| 1171 | void TemplateDeclInstantiator::InstantiateEnumDefinition( |
| 1172 | EnumDecl *Enum, EnumDecl *Pattern) { |
| 1173 | Enum->startDefinition(); |
| 1174 | |
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 1175 | // Update the location to refer to the definition. |
| 1176 | Enum->setLocation(Pattern->getLocation()); |
| 1177 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1178 | SmallVector<Decl*, 4> Enumerators; |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1179 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1180 | EnumConstantDecl *LastEnumConst = nullptr; |
| Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1181 | for (auto *EC : Pattern->enumerators()) { |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1182 | // The specified value for the enumerator. |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1183 | ExprResult Value((Expr *)nullptr); |
| Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 1184 | if (Expr *UninstValue = EC->getInitExpr()) { |
| Richard Smith | 764d2fe | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 1185 | // The enumerator's value expression is a constant expression. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 1186 | EnterExpressionEvaluationContext Unevaluated( |
| 1187 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1189 | Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); |
| Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 1190 | } |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1191 | |
| 1192 | // Drop the initial value and continue. |
| 1193 | bool isInvalid = false; |
| 1194 | if (Value.isInvalid()) { |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1195 | Value = nullptr; |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1196 | isInvalid = true; |
| 1197 | } |
| 1198 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | EnumConstantDecl *EnumConst |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1200 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 1201 | EC->getLocation(), EC->getIdentifier(), |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1202 | Value.get()); |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1203 | |
| 1204 | if (isInvalid) { |
| 1205 | if (EnumConst) |
| 1206 | EnumConst->setInvalidDecl(); |
| 1207 | Enum->setInvalidDecl(); |
| 1208 | } |
| 1209 | |
| 1210 | if (EnumConst) { |
| Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1211 | SemaRef.InstantiateAttrs(TemplateArgs, EC, EnumConst); |
| John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 1212 | |
| John McCall | f9b528c | 2010-01-23 22:37:59 +0000 | [diff] [blame] | 1213 | EnumConst->setAccess(Enum->getAccess()); |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1214 | Enum->addDecl(EnumConst); |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1215 | Enumerators.push_back(EnumConst); |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1216 | LastEnumConst = EnumConst; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1217 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 1218 | if (Pattern->getDeclContext()->isFunctionOrMethod() && |
| 1219 | !Enum->isScoped()) { |
| Douglas Gregor | aff9c1a | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 1220 | // If the enumeration is within a function or method, record the enum |
| 1221 | // constant as a local. |
| Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 1222 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(EC, EnumConst); |
| Douglas Gregor | aff9c1a | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 1223 | } |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1224 | } |
| 1225 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | |
| Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 1227 | SemaRef.ActOnEnumBody(Enum->getLocation(), Enum->getBraceRange(), Enum, |
| Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 1228 | Enumerators, nullptr, ParsedAttributesView()); |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| Douglas Gregor | 9106b82 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 1231 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1232 | llvm_unreachable("EnumConstantDecls can only occur within EnumDecls."); |
| Douglas Gregor | 9106b82 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| David Majnemer | d9b1a4f | 2015-11-04 03:40:30 +0000 | [diff] [blame] | 1235 | Decl * |
| 1236 | TemplateDeclInstantiator::VisitBuiltinTemplateDecl(BuiltinTemplateDecl *D) { |
| 1237 | llvm_unreachable("BuiltinTemplateDecls cannot be instantiated."); |
| 1238 | } |
| 1239 | |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1240 | Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1241 | bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1242 | |
| Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1243 | // Create a local instantiation scope for this class template, which |
| 1244 | // will contain the instantiations of the template parameters. |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1245 | LocalInstantiationScope Scope(SemaRef); |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1246 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1247 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1248 | if (!InstParams) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1249 | return nullptr; |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1250 | |
| 1251 | CXXRecordDecl *Pattern = D->getTemplatedDecl(); |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1252 | |
| 1253 | // Instantiate the qualifier. We have to do this first in case |
| 1254 | // we're a friend declaration, because if we are then we need to put |
| 1255 | // the new declaration in the appropriate context. |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1256 | NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc(); |
| 1257 | if (QualifierLoc) { |
| 1258 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 1259 | TemplateArgs); |
| 1260 | if (!QualifierLoc) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1261 | return nullptr; |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1264 | CXXRecordDecl *PrevDecl = nullptr; |
| 1265 | ClassTemplateDecl *PrevClassTemplate = nullptr; |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1266 | |
| Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1267 | if (!isFriend && getPreviousDeclForInstantiation(Pattern)) { |
| Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 1268 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1269 | if (!Found.empty()) { |
| 1270 | PrevClassTemplate = dyn_cast<ClassTemplateDecl>(Found.front()); |
| Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 1271 | if (PrevClassTemplate) |
| 1272 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 1273 | } |
| 1274 | } |
| 1275 | |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1276 | // If this isn't a friend, then it's a member template, in which |
| 1277 | // case we just want to build the instantiation in the |
| 1278 | // specialization. If it is a friend, we want to build it in |
| 1279 | // the appropriate context. |
| 1280 | DeclContext *DC = Owner; |
| 1281 | if (isFriend) { |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1282 | if (QualifierLoc) { |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1283 | CXXScopeSpec SS; |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1284 | SS.Adopt(QualifierLoc); |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1285 | DC = SemaRef.computeDeclContext(SS); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1286 | if (!DC) return nullptr; |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1287 | } else { |
| 1288 | DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(), |
| 1289 | Pattern->getDeclContext(), |
| 1290 | TemplateArgs); |
| 1291 | } |
| 1292 | |
| 1293 | // Look for a previous declaration of the template in the owning |
| 1294 | // context. |
| 1295 | LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(), |
| Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1296 | Sema::LookupOrdinaryName, |
| 1297 | SemaRef.forRedeclarationInCurContext()); |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1298 | SemaRef.LookupQualifiedName(R, DC); |
| 1299 | |
| 1300 | if (R.isSingleResult()) { |
| 1301 | PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>(); |
| 1302 | if (PrevClassTemplate) |
| 1303 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 1304 | } |
| 1305 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1306 | if (!PrevClassTemplate && QualifierLoc) { |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1307 | SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope) |
| Douglas Gregor | f5af358 | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 1308 | << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1309 | << QualifierLoc.getSourceRange(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1310 | return nullptr; |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1313 | bool AdoptedPreviousTemplateParams = false; |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1314 | if (PrevClassTemplate) { |
| Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1315 | bool Complain = true; |
| 1316 | |
| 1317 | // HACK: libstdc++ 4.2.1 contains an ill-formed friend class |
| 1318 | // template for struct std::tr1::__detail::_Map_base, where the |
| 1319 | // template parameters of the friend declaration don't match the |
| 1320 | // template parameters of the original declaration. In this one |
| 1321 | // case, we don't complain about the ill-formed friend |
| 1322 | // declaration. |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1323 | if (isFriend && Pattern->getIdentifier() && |
| Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1324 | Pattern->getIdentifier()->isStr("_Map_base") && |
| 1325 | DC->isNamespace() && |
| 1326 | cast<NamespaceDecl>(DC)->getIdentifier() && |
| 1327 | cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) { |
| 1328 | DeclContext *DCParent = DC->getParent(); |
| 1329 | if (DCParent->isNamespace() && |
| 1330 | cast<NamespaceDecl>(DCParent)->getIdentifier() && |
| 1331 | cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) { |
| Richard Trieu | c771d5d | 2014-05-28 02:16:01 +0000 | [diff] [blame] | 1332 | if (cast<Decl>(DCParent)->isInStdNamespace()) |
| Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1333 | Complain = false; |
| 1334 | } |
| 1335 | } |
| 1336 | |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1337 | TemplateParameterList *PrevParams |
| Richard Smith | c457766 | 2018-09-12 02:13:47 +0000 | [diff] [blame] | 1338 | = PrevClassTemplate->getMostRecentDecl()->getTemplateParameters(); |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1339 | |
| 1340 | // Make sure the parameter lists match. |
| 1341 | if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams, |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1342 | Complain, |
| Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1343 | Sema::TPL_TemplateMatch)) { |
| 1344 | if (Complain) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1345 | return nullptr; |
| Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1346 | |
| 1347 | AdoptedPreviousTemplateParams = true; |
| 1348 | InstParams = PrevParams; |
| 1349 | } |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1350 | |
| 1351 | // Do some additional validation, then merge default arguments |
| 1352 | // from the existing declarations. |
| Douglas Gregor | 01e09d9 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 1353 | if (!AdoptedPreviousTemplateParams && |
| 1354 | SemaRef.CheckTemplateParameterList(InstParams, PrevParams, |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1355 | Sema::TPC_ClassTemplate)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1356 | return nullptr; |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1357 | } |
| 1358 | } |
| 1359 | |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1360 | CXXRecordDecl *RecordInst = CXXRecordDecl::Create( |
| 1361 | SemaRef.Context, Pattern->getTagKind(), DC, Pattern->getBeginLoc(), |
| 1362 | Pattern->getLocation(), Pattern->getIdentifier(), PrevDecl, |
| 1363 | /*DelayTypeCreation=*/true); |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1364 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1365 | if (QualifierLoc) |
| 1366 | RecordInst->setQualifierInfo(QualifierLoc); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1367 | |
| Louis Dionne | 3c011e1 | 2018-09-14 14:07:16 +0000 | [diff] [blame] | 1368 | SemaRef.InstantiateAttrsForDecl(TemplateArgs, Pattern, RecordInst, LateAttrs, |
| 1369 | StartingScope); |
| 1370 | |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1371 | ClassTemplateDecl *Inst |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1372 | = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(), |
| Vassil Vassilev | 352e441 | 2017-01-12 09:16:26 +0000 | [diff] [blame] | 1373 | D->getIdentifier(), InstParams, RecordInst); |
| 1374 | assert(!(isFriend && Owner->isDependentContext())); |
| 1375 | Inst->setPreviousDecl(PrevClassTemplate); |
| 1376 | |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1377 | RecordInst->setDescribedClassTemplate(Inst); |
| John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1378 | |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1379 | if (isFriend) { |
| John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1380 | if (PrevClassTemplate) |
| 1381 | Inst->setAccess(PrevClassTemplate->getAccess()); |
| 1382 | else |
| 1383 | Inst->setAccess(D->getAccess()); |
| 1384 | |
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1385 | Inst->setObjectOfFriendDecl(); |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1386 | // TODO: do we want to track the instantiation progeny of this |
| 1387 | // friend target decl? |
| 1388 | } else { |
| Douglas Gregor | 412e8bc | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 1389 | Inst->setAccess(D->getAccess()); |
| Nick Lewycky | 6147891 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 1390 | if (!PrevClassTemplate) |
| 1391 | Inst->setInstantiatedFromMemberTemplate(D); |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1392 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1393 | |
| Douglas Gregor | ef06ccf | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 1394 | // Trigger creation of the type for the instantiation. |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1395 | SemaRef.Context.getInjectedClassNameType(RecordInst, |
| Douglas Gregor | 9961ce9 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1396 | Inst->getInjectedClassNameSpecialization()); |
| John McCall | 17762b8 | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 1397 | |
| Douglas Gregor | bb3b46e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 1398 | // Finish handling of friends. |
| John McCall | 598b440 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 1399 | if (isFriend) { |
| Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1400 | DC->makeDeclVisibleInContext(Inst); |
| Abramo Bagnara | edf99ff | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 1401 | Inst->setLexicalDeclContext(Owner); |
| 1402 | RecordInst->setLexicalDeclContext(Owner); |
| Douglas Gregor | 412e8bc | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 1403 | return Inst; |
| Douglas Gregor | bb3b46e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 1404 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1405 | |
| Abramo Bagnara | edf99ff | 2011-11-26 13:33:46 +0000 | [diff] [blame] | 1406 | if (D->isOutOfLine()) { |
| 1407 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1408 | RecordInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1409 | } |
| 1410 | |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1411 | Owner->addDecl(Inst); |
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1412 | |
| 1413 | if (!PrevClassTemplate) { |
| 1414 | // Queue up any out-of-line partial specializations of this member |
| 1415 | // class template; the client will force their instantiation once |
| 1416 | // the enclosing class has been instantiated. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1417 | SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1418 | D->getPartialSpecializations(PartialSpecs); |
| 1419 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1420 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) |
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1421 | OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I])); |
| 1422 | } |
| 1423 | |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1424 | return Inst; |
| 1425 | } |
| 1426 | |
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1427 | Decl * |
| Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1428 | TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl( |
| 1429 | ClassTemplatePartialSpecializationDecl *D) { |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1430 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1431 | |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1432 | // Lookup the already-instantiated declaration in the instantiation |
| 1433 | // of the class template and return that. |
| 1434 | DeclContext::lookup_result Found |
| 1435 | = Owner->lookup(ClassTemplate->getDeclName()); |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1436 | if (Found.empty()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1437 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1438 | |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1439 | ClassTemplateDecl *InstClassTemplate |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1440 | = dyn_cast<ClassTemplateDecl>(Found.front()); |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1441 | if (!InstClassTemplate) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1442 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1443 | |
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1444 | if (ClassTemplatePartialSpecializationDecl *Result |
| 1445 | = InstClassTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 1446 | return Result; |
| 1447 | |
| 1448 | return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D); |
| Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1451 | Decl *TemplateDeclInstantiator::VisitVarTemplateDecl(VarTemplateDecl *D) { |
| 1452 | assert(D->getTemplatedDecl()->isStaticDataMember() && |
| 1453 | "Only static data member templates are allowed."); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1454 | |
| 1455 | // Create a local instantiation scope for this variable template, which |
| 1456 | // will contain the instantiations of the template parameters. |
| 1457 | LocalInstantiationScope Scope(SemaRef); |
| 1458 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1459 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1460 | if (!InstParams) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1461 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1462 | |
| 1463 | VarDecl *Pattern = D->getTemplatedDecl(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1464 | VarTemplateDecl *PrevVarTemplate = nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1465 | |
| Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1466 | if (getPreviousDeclForInstantiation(Pattern)) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1467 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
| 1468 | if (!Found.empty()) |
| 1469 | PrevVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); |
| 1470 | } |
| 1471 | |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 1472 | VarDecl *VarInst = |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 1473 | cast_or_null<VarDecl>(VisitVarDecl(Pattern, |
| 1474 | /*InstantiatingVarTemplate=*/true)); |
| Nick Lewycky | 6ca07ca | 2015-08-10 21:54:08 +0000 | [diff] [blame] | 1475 | if (!VarInst) return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1476 | |
| 1477 | DeclContext *DC = Owner; |
| 1478 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1479 | VarTemplateDecl *Inst = VarTemplateDecl::Create( |
| 1480 | SemaRef.Context, DC, D->getLocation(), D->getIdentifier(), InstParams, |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 1481 | VarInst); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1482 | VarInst->setDescribedVarTemplate(Inst); |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 1483 | Inst->setPreviousDecl(PrevVarTemplate); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1484 | |
| 1485 | Inst->setAccess(D->getAccess()); |
| 1486 | if (!PrevVarTemplate) |
| 1487 | Inst->setInstantiatedFromMemberTemplate(D); |
| 1488 | |
| 1489 | if (D->isOutOfLine()) { |
| 1490 | Inst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1491 | VarInst->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 1492 | } |
| 1493 | |
| 1494 | Owner->addDecl(Inst); |
| 1495 | |
| 1496 | if (!PrevVarTemplate) { |
| 1497 | // Queue up any out-of-line partial specializations of this member |
| 1498 | // variable template; the client will force their instantiation once |
| 1499 | // the enclosing class has been instantiated. |
| 1500 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 1501 | D->getPartialSpecializations(PartialSpecs); |
| 1502 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1503 | if (PartialSpecs[I]->getFirstDecl()->isOutOfLine()) |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1504 | OutOfLineVarPartialSpecs.push_back( |
| 1505 | std::make_pair(Inst, PartialSpecs[I])); |
| 1506 | } |
| 1507 | |
| 1508 | return Inst; |
| 1509 | } |
| 1510 | |
| 1511 | Decl *TemplateDeclInstantiator::VisitVarTemplatePartialSpecializationDecl( |
| 1512 | VarTemplatePartialSpecializationDecl *D) { |
| 1513 | assert(D->isStaticDataMember() && |
| 1514 | "Only static data member templates are allowed."); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1515 | |
| 1516 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); |
| 1517 | |
| 1518 | // Lookup the already-instantiated declaration and return that. |
| 1519 | DeclContext::lookup_result Found = Owner->lookup(VarTemplate->getDeclName()); |
| 1520 | assert(!Found.empty() && "Instantiation found nothing?"); |
| 1521 | |
| 1522 | VarTemplateDecl *InstVarTemplate = dyn_cast<VarTemplateDecl>(Found.front()); |
| 1523 | assert(InstVarTemplate && "Instantiation did not find a variable template?"); |
| 1524 | |
| 1525 | if (VarTemplatePartialSpecializationDecl *Result = |
| 1526 | InstVarTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 1527 | return Result; |
| 1528 | |
| 1529 | return InstantiateVarTemplatePartialSpecialization(InstVarTemplate, D); |
| 1530 | } |
| 1531 | |
| Douglas Gregor | e4b0516 | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 1532 | Decl * |
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1533 | TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1534 | // Create a local instantiation scope for this function template, which |
| 1535 | // will contain the instantiations of the template parameters and then get |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1536 | // merged with the local instantiation scope for the function template |
| Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1537 | // itself. |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1538 | LocalInstantiationScope Scope(SemaRef); |
| Douglas Gregor | 14cf752 | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1539 | |
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1540 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1541 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1542 | if (!InstParams) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1543 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1544 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1545 | FunctionDecl *Instantiated = nullptr; |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1546 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl())) |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1547 | Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod, |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1548 | InstParams)); |
| 1549 | else |
| 1550 | Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl( |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1551 | D->getTemplatedDecl(), |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1552 | InstParams)); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1553 | |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1554 | if (!Instantiated) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1555 | return nullptr; |
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1556 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1557 | // Link the instantiated function template declaration to the function |
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1558 | // template from which it was instantiated. |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1559 | FunctionTemplateDecl *InstTemplate |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1560 | = Instantiated->getDescribedFunctionTemplate(); |
| Douglas Gregor | ca027af | 2009-10-12 22:27:17 +0000 | [diff] [blame] | 1561 | InstTemplate->setAccess(D->getAccess()); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1562 | assert(InstTemplate && |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1563 | "VisitFunctionDecl/CXXMethodDecl didn't create a template!"); |
| John McCall | 2079d0b | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 1564 | |
| John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1565 | bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1566 | |
| John McCall | 2079d0b | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 1567 | // Link the instantiation back to the pattern *unless* this is a |
| 1568 | // non-definition friend declaration. |
| 1569 | if (!InstTemplate->getInstantiatedFromMemberTemplate() && |
| John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1570 | !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition())) |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1571 | InstTemplate->setInstantiatedFromMemberTemplate(D); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1572 | |
| John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1573 | // Make declarations visible in the appropriate context. |
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1574 | if (!isFriend) { |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1575 | Owner->addDecl(InstTemplate); |
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1576 | } else if (InstTemplate->getDeclContext()->isRecord() && |
| Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1577 | !getPreviousDeclForInstantiation(D)) { |
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 1578 | SemaRef.CheckFriendAccess(InstTemplate); |
| 1579 | } |
| John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1580 | |
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1581 | return InstTemplate; |
| 1582 | } |
| 1583 | |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1584 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1585 | CXXRecordDecl *PrevDecl = nullptr; |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1586 | if (D->isInjectedClassName()) |
| 1587 | PrevDecl = cast<CXXRecordDecl>(Owner); |
| Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1588 | else if (CXXRecordDecl *PatternPrev = getPreviousDeclForInstantiation(D)) { |
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1589 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 1590 | PatternPrev, |
| John McCall | e9f92a0 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 1591 | TemplateArgs); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1592 | if (!Prev) return nullptr; |
| John McCall | e9f92a0 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 1593 | PrevDecl = cast<CXXRecordDecl>(Prev); |
| 1594 | } |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1595 | |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1596 | CXXRecordDecl *Record = CXXRecordDecl::Create( |
| 1597 | SemaRef.Context, D->getTagKind(), Owner, D->getBeginLoc(), |
| 1598 | D->getLocation(), D->getIdentifier(), PrevDecl); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1599 | |
| 1600 | // Substitute the nested name specifier, if any. |
| 1601 | if (SubstQualifier(D, Record)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1602 | return nullptr; |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1603 | |
| Louis Dionne | 3c011e1 | 2018-09-14 14:07:16 +0000 | [diff] [blame] | 1604 | SemaRef.InstantiateAttrsForDecl(TemplateArgs, D, Record, LateAttrs, |
| 1605 | StartingScope); |
| 1606 | |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1607 | Record->setImplicit(D->isImplicit()); |
| Eli Friedman | bda4ef1 | 2009-08-27 19:11:42 +0000 | [diff] [blame] | 1608 | // FIXME: Check against AS_none is an ugly hack to work around the issue that |
| 1609 | // the tag decls introduced by friend class declarations don't have an access |
| 1610 | // specifier. Remove once this area of the code gets sorted out. |
| 1611 | if (D->getAccess() != AS_none) |
| 1612 | Record->setAccess(D->getAccess()); |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1613 | if (!D->isInjectedClassName()) |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1614 | Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1615 | |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1616 | // If the original function was part of a friend declaration, |
| 1617 | // inherit its namespace state. |
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 1618 | if (D->getFriendObjectKind()) |
| 1619 | Record->setObjectOfFriendDecl(); |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1620 | |
| Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1621 | // Make sure that anonymous structs and unions are recorded. |
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1622 | if (D->isAnonymousStructOrUnion()) |
| Douglas Gregor | 0416318 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 1623 | Record->setAnonymousStructOrUnion(true); |
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1624 | |
| 1625 | if (D->isLocalClass()) |
| 1626 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record); |
| Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1627 | |
| David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 1628 | // Forward the mangling number from the template to the instantiated decl. |
| 1629 | SemaRef.Context.setManglingNumber(Record, |
| 1630 | SemaRef.Context.getManglingNumber(D)); |
| 1631 | |
| David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 1632 | // See if the old tag was defined along with a declarator. |
| 1633 | // If it did, mark the new tag as being associated with that declarator. |
| 1634 | if (DeclaratorDecl *DD = SemaRef.Context.getDeclaratorForUnnamedTagDecl(D)) |
| 1635 | SemaRef.Context.addDeclaratorForUnnamedTagDecl(Record, DD); |
| 1636 | |
| 1637 | // See if the old tag was defined along with a typedef. |
| 1638 | // If it did, mark the new tag as being associated with that typedef. |
| 1639 | if (TypedefNameDecl *TND = SemaRef.Context.getTypedefNameForUnnamedTagDecl(D)) |
| 1640 | SemaRef.Context.addTypedefNameForUnnamedTagDecl(Record, TND); |
| 1641 | |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1642 | Owner->addDecl(Record); |
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1643 | |
| 1644 | // DR1484 clarifies that the members of a local class are instantiated as part |
| 1645 | // of the instantiation of their enclosing entity. |
| 1646 | if (D->isCompleteDefinition() && D->isLocalClass()) { |
| Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 1647 | Sema::LocalEagerInstantiationScope LocalInstantiations(SemaRef); |
| Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 1648 | |
| David Majnemer | a64cb5a | 2014-02-22 00:17:46 +0000 | [diff] [blame] | 1649 | SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs, |
| 1650 | TSK_ImplicitInstantiation, |
| 1651 | /*Complain=*/true); |
| Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 1652 | |
| Richard Smith | ece4758 | 2017-01-04 23:45:01 +0000 | [diff] [blame] | 1653 | // For nested local classes, we will instantiate the members when we |
| 1654 | // reach the end of the outermost (non-nested) local class. |
| 1655 | if (!D->isCXXClassMember()) |
| 1656 | SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs, |
| 1657 | TSK_ImplicitInstantiation); |
| Richard Smith | b0b6801 | 2015-05-11 23:09:06 +0000 | [diff] [blame] | 1658 | |
| 1659 | // This class may have local implicit instantiations that need to be |
| 1660 | // performed within this scope. |
| Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 1661 | LocalInstantiations.perform(); |
| David Majnemer | 192d179 | 2013-11-27 08:20:38 +0000 | [diff] [blame] | 1662 | } |
| Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 1663 | |
| 1664 | SemaRef.DiagnoseUnusedNestedTypedefs(Record); |
| 1665 | |
| Douglas Gregor | 8ea8fd4 | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1666 | return Record; |
| 1667 | } |
| 1668 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1669 | /// Adjust the given function type for an instantiation of the |
| Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1670 | /// given declaration, to cope with modifications to the function's type that |
| 1671 | /// aren't reflected in the type-source information. |
| 1672 | /// |
| 1673 | /// \param D The declaration we're instantiating. |
| 1674 | /// \param TInfo The already-instantiated type. |
| 1675 | static QualType adjustFunctionTypeForInstantiation(ASTContext &Context, |
| 1676 | FunctionDecl *D, |
| 1677 | TypeSourceInfo *TInfo) { |
| Douglas Gregor | 1af8ad4 | 2012-09-13 22:01:49 +0000 | [diff] [blame] | 1678 | const FunctionProtoType *OrigFunc |
| 1679 | = D->getType()->castAs<FunctionProtoType>(); |
| 1680 | const FunctionProtoType *NewFunc |
| 1681 | = TInfo->getType()->castAs<FunctionProtoType>(); |
| 1682 | if (OrigFunc->getExtInfo() == NewFunc->getExtInfo()) |
| 1683 | return TInfo->getType(); |
| 1684 | |
| 1685 | FunctionProtoType::ExtProtoInfo NewEPI = NewFunc->getExtProtoInfo(); |
| 1686 | NewEPI.ExtInfo = OrigFunc->getExtInfo(); |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1687 | return Context.getFunctionType(NewFunc->getReturnType(), |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1688 | NewFunc->getParamTypes(), NewEPI); |
| Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1691 | /// Normal class members are of more specific types and therefore |
| Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1692 | /// don't make it here. This function serves three purposes: |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1693 | /// 1) instantiating function templates |
| 1694 | /// 2) substituting friend declarations |
| Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1695 | /// 3) substituting deduction guide declarations for nested class templates |
| Douglas Gregor | 33636e6 | 2009-12-24 20:56:24 +0000 | [diff] [blame] | 1696 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D, |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1697 | TemplateParameterList *TemplateParams) { |
| Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1698 | // Check whether there is already a function template specialization for |
| 1699 | // this declaration. |
| 1700 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1701 | if (FunctionTemplate && !TemplateParams) { |
| Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1702 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1703 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1704 | void *InsertPos = nullptr; |
| Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1705 | FunctionDecl *SpecFunc |
| Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 1706 | = FunctionTemplate->findSpecialization(Innermost, InsertPos); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | |
| Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1708 | // If we already have a function template specialization, return it. |
| Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1709 | if (SpecFunc) |
| 1710 | return SpecFunc; |
| Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1711 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1712 | |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1713 | bool isFriend; |
| 1714 | if (FunctionTemplate) |
| 1715 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1716 | else |
| 1717 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1718 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1719 | bool MergeWithParentScope = (TemplateParams != nullptr) || |
| Douglas Gregor | 9f44d14 | 2010-05-21 21:25:08 +0000 | [diff] [blame] | 1720 | Owner->isFunctionOrMethod() || |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1721 | !(isa<Decl>(Owner) && |
| Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1722 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1723 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1724 | |
| Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 1725 | ExplicitSpecifier InstantiatedExplicitSpecifier; |
| 1726 | if (auto *DGuide = dyn_cast<CXXDeductionGuideDecl>(D)) { |
| 1727 | InstantiatedExplicitSpecifier = instantiateExplicitSpecifier( |
| 1728 | SemaRef, TemplateArgs, DGuide->getExplicitSpecifier(), DGuide); |
| 1729 | if (InstantiatedExplicitSpecifier.isInvalid()) |
| 1730 | return nullptr; |
| 1731 | } |
| 1732 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1733 | SmallVector<ParmVarDecl *, 4> Params; |
| David Blaikie | 4d14296 | 2011-11-10 05:42:04 +0000 | [diff] [blame] | 1734 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1735 | if (!TInfo) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1736 | return nullptr; |
| Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 1737 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
| John McCall | 58de358 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 1738 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1739 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 1740 | if (QualifierLoc) { |
| 1741 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 1742 | TemplateArgs); |
| 1743 | if (!QualifierLoc) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1744 | return nullptr; |
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
| John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1747 | // If we're instantiating a local function declaration, put the result |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1748 | // in the enclosing namespace; otherwise we need to find the instantiated |
| 1749 | // context. |
| John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1750 | DeclContext *DC; |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1751 | if (D->isLocalExternDecl()) { |
| John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1752 | DC = Owner; |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1753 | SemaRef.adjustContextForLocalExternDecl(DC); |
| 1754 | } else if (isFriend && QualifierLoc) { |
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1755 | CXXScopeSpec SS; |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1756 | SS.Adopt(QualifierLoc); |
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1757 | DC = SemaRef.computeDeclContext(SS); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1758 | if (!DC) return nullptr; |
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1759 | } else { |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1760 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(), |
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1761 | TemplateArgs); |
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1762 | } |
| John McCall | ce41066 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1763 | |
| Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1764 | DeclarationNameInfo NameInfo |
| 1765 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 1766 | |
| Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1767 | FunctionDecl *Function; |
| Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1768 | if (auto *DGuide = dyn_cast<CXXDeductionGuideDecl>(D)) { |
| Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1769 | Function = CXXDeductionGuideDecl::Create( |
| Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 1770 | SemaRef.Context, DC, D->getInnerLocStart(), |
| 1771 | InstantiatedExplicitSpecifier, NameInfo, T, TInfo, |
| 1772 | D->getSourceRange().getEnd()); |
| Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1773 | if (DGuide->isCopyDeductionCandidate()) |
| 1774 | cast<CXXDeductionGuideDecl>(Function)->setIsCopyDeductionCandidate(); |
| Richard Smith | c660c8f | 2018-03-16 13:36:56 +0000 | [diff] [blame] | 1775 | Function->setAccess(D->getAccess()); |
| Faisal Vali | 81b756e | 2017-10-22 14:45:08 +0000 | [diff] [blame] | 1776 | } else { |
| Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1777 | Function = FunctionDecl::Create( |
| Richard Smith | 4fa14515 | 2017-12-21 19:43:39 +0000 | [diff] [blame] | 1778 | SemaRef.Context, DC, D->getInnerLocStart(), NameInfo, T, TInfo, |
| Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1779 | D->getCanonicalDecl()->getStorageClass(), D->isInlineSpecified(), |
| Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 1780 | D->hasWrittenPrototype(), D->getConstexprKind()); |
| Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1781 | Function->setRangeEnd(D->getSourceRange().getEnd()); |
| 1782 | } |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1783 | |
| Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 1784 | if (D->isInlined()) |
| 1785 | Function->setImplicitlyInline(); |
| 1786 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1787 | if (QualifierLoc) |
| 1788 | Function->setQualifierInfo(QualifierLoc); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1789 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1790 | if (D->isLocalExternDecl()) |
| 1791 | Function->setLocalExternDecl(); |
| 1792 | |
| John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1793 | DeclContext *LexicalDC = Owner; |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1794 | if (!isFriend && D->isOutOfLine() && !D->isLocalExternDecl()) { |
| John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1795 | assert(D->getDeclContext()->isFileContext()); |
| 1796 | LexicalDC = D->getDeclContext(); |
| 1797 | } |
| 1798 | |
| 1799 | Function->setLexicalDeclContext(LexicalDC); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1800 | |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1801 | // Attach the parameters |
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 1802 | for (unsigned P = 0; P < Params.size(); ++P) |
| 1803 | if (Params[P]) |
| 1804 | Params[P]->setOwningFunction(Function); |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1805 | Function->setParams(Params); |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1806 | |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1807 | if (TemplateParams) { |
| 1808 | // Our resulting instantiation is actually a function template, since we |
| 1809 | // are substituting only the outer template parameters. For example, given |
| 1810 | // |
| 1811 | // template<typename T> |
| 1812 | // struct X { |
| 1813 | // template<typename U> friend void f(T, U); |
| 1814 | // }; |
| 1815 | // |
| 1816 | // X<int> x; |
| 1817 | // |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1818 | // We are instantiating the friend function template "f" within X<int>, |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1819 | // which means substituting int for T, but leaving "f" as a friend function |
| 1820 | // template. |
| 1821 | // Build the function template itself. |
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1822 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC, |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1823 | Function->getLocation(), |
| 1824 | Function->getDeclName(), |
| 1825 | TemplateParams, Function); |
| 1826 | Function->setDescribedFunctionTemplate(FunctionTemplate); |
| John McCall | 3083710 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1827 | |
| 1828 | FunctionTemplate->setLexicalDeclContext(LexicalDC); |
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1829 | |
| 1830 | if (isFriend && D->isThisDeclarationADefinition()) { |
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1831 | FunctionTemplate->setInstantiatedFromMemberTemplate( |
| 1832 | D->getDescribedFunctionTemplate()); |
| 1833 | } |
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1834 | } else if (FunctionTemplate) { |
| 1835 | // Record this function template specialization. |
| Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 1836 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
| Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1837 | Function->setFunctionTemplateSpecialization(FunctionTemplate, |
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1838 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
| David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 1839 | Innermost), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1840 | /*InsertPos=*/nullptr); |
| Richard Smith | 152bcd2 | 2017-01-28 02:56:07 +0000 | [diff] [blame] | 1841 | } else if (isFriend && D->isThisDeclarationADefinition()) { |
| 1842 | // Do not connect the friend to the template unless it's actually a |
| 1843 | // definition. We don't want non-template functions to be marked as being |
| 1844 | // template instantiations. |
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1845 | Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1846 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1847 | |
| Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1848 | if (isFriend) |
| 1849 | Function->setObjectOfFriendDecl(); |
| 1850 | |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1851 | if (InitFunctionInstantiation(Function, D)) |
| 1852 | Function->setInvalidDecl(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | |
| Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1854 | bool IsExplicitSpecialization = false; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1855 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1856 | LookupResult Previous( |
| 1857 | SemaRef, Function->getDeclName(), SourceLocation(), |
| 1858 | D->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage |
| 1859 | : Sema::LookupOrdinaryName, |
| Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1860 | D->isLocalExternDecl() ? Sema::ForExternalRedeclaration |
| 1861 | : SemaRef.forRedeclarationInCurContext()); |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1862 | |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1863 | if (DependentFunctionTemplateSpecializationInfo *Info |
| 1864 | = D->getDependentSpecializationInfo()) { |
| 1865 | assert(isFriend && "non-friend has dependent specialization info?"); |
| 1866 | |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1867 | // Instantiate the explicit template arguments. |
| 1868 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 1869 | Info->getRAngleLoc()); |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1870 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 1871 | ExplicitArgs, TemplateArgs)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1872 | return nullptr; |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1873 | |
| 1874 | // Map the candidate templates to their instantiations. |
| 1875 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 1876 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 1877 | Info->getTemplate(I), |
| 1878 | TemplateArgs); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1879 | if (!Temp) return nullptr; |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1880 | |
| 1881 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 1882 | } |
| 1883 | |
| 1884 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 1885 | &ExplicitArgs, |
| 1886 | Previous)) |
| 1887 | Function->setInvalidDecl(); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1888 | |
| Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1889 | IsExplicitSpecialization = true; |
| 1890 | } else if (const ASTTemplateArgumentListInfo *Info = |
| 1891 | D->getTemplateSpecializationArgsAsWritten()) { |
| 1892 | // The name of this function was written as a template-id. |
| 1893 | SemaRef.LookupQualifiedName(Previous, DC); |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1894 | |
| Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1895 | // Instantiate the explicit template arguments. |
| 1896 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 1897 | Info->getRAngleLoc()); |
| 1898 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 1899 | ExplicitArgs, TemplateArgs)) |
| 1900 | return nullptr; |
| 1901 | |
| 1902 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 1903 | &ExplicitArgs, |
| 1904 | Previous)) |
| 1905 | Function->setInvalidDecl(); |
| 1906 | |
| 1907 | IsExplicitSpecialization = true; |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1908 | } else if (TemplateParams || !FunctionTemplate) { |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1909 | // Look only into the namespace where the friend would be declared to |
| 1910 | // find a previous declaration. This is the innermost enclosing namespace, |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1911 | // as described in ActOnFriendFunctionDecl. |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1912 | SemaRef.LookupQualifiedName(Previous, DC); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1913 | |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1914 | // In C++, the previous declaration we find might be a tag type |
| 1915 | // (class or enum). In this case, the new declaration will hide the |
| 1916 | // tag type. Note that this does does not apply if we're declaring a |
| 1917 | // typedef (C++ [dcl.typedef]p4). |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1918 | if (Previous.isSingleTagDecl()) |
| 1919 | Previous.clear(); |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1920 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 1921 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1922 | SemaRef.CheckFunctionDeclaration(/*Scope*/ nullptr, Function, Previous, |
| Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 1923 | IsExplicitSpecialization); |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1924 | |
| John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1925 | NamedDecl *PrincipalDecl = (TemplateParams |
| 1926 | ? cast<NamedDecl>(FunctionTemplate) |
| 1927 | : Function); |
| 1928 | |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1929 | // If the original function was part of a friend declaration, |
| 1930 | // inherit its namespace state and add it to the owner. |
| John McCall | e0b2ddb | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1931 | if (isFriend) { |
| Serge Pavlov | acfcd78 | 2018-12-06 09:35:04 +0000 | [diff] [blame] | 1932 | Function->setObjectOfFriendDecl(); |
| 1933 | if (FunctionTemplateDecl *FT = Function->getDescribedFunctionTemplate()) |
| 1934 | FT->setObjectOfFriendDecl(); |
| Richard Smith | 05afe5e | 2012-03-13 03:12:56 +0000 | [diff] [blame] | 1935 | DC->makeDeclVisibleInContext(PrincipalDecl); |
| Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1936 | |
| Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1937 | bool QueuedInstantiation = false; |
| Gabor Greif | 718d515 | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1938 | |
| Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1939 | // C++11 [temp.friend]p4 (DR329): |
| 1940 | // When a function is defined in a friend function declaration in a class |
| 1941 | // template, the function is instantiated when the function is odr-used. |
| 1942 | // The same restrictions on multiple declarations and definitions that |
| 1943 | // apply to non-template function declarations and definitions also apply |
| 1944 | // to these implicit definitions. |
| 1945 | if (D->isThisDeclarationADefinition()) { |
| Serge Pavlov | e6e534c | 2018-03-01 07:04:11 +0000 | [diff] [blame] | 1946 | SemaRef.CheckForFunctionRedefinition(Function); |
| 1947 | if (!Function->isInvalidDecl()) { |
| 1948 | for (auto R : Function->redecls()) { |
| 1949 | if (R == Function) |
| 1950 | continue; |
| Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1951 | |
| Serge Pavlov | e6e534c | 2018-03-01 07:04:11 +0000 | [diff] [blame] | 1952 | // If some prior declaration of this function has been used, we need |
| 1953 | // to instantiate its definition. |
| 1954 | if (!QueuedInstantiation && R->isUsed(false)) { |
| 1955 | if (MemberSpecializationInfo *MSInfo = |
| 1956 | Function->getMemberSpecializationInfo()) { |
| 1957 | if (MSInfo->getPointOfInstantiation().isInvalid()) { |
| 1958 | SourceLocation Loc = R->getLocation(); // FIXME |
| 1959 | MSInfo->setPointOfInstantiation(Loc); |
| 1960 | SemaRef.PendingLocalImplicitInstantiations.push_back( |
| 1961 | std::make_pair(Function, Loc)); |
| 1962 | QueuedInstantiation = true; |
| 1963 | } |
| Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1964 | } |
| Richard Smith | 91dfaac | 2014-02-03 02:37:59 +0000 | [diff] [blame] | 1965 | } |
| Douglas Gregor | b92ea59 | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1966 | } |
| 1967 | } |
| 1968 | } |
| Richard Smith | f359765 | 2017-05-10 00:01:13 +0000 | [diff] [blame] | 1969 | |
| 1970 | // Check the template parameter list against the previous declaration. The |
| 1971 | // goal here is to pick up default arguments added since the friend was |
| 1972 | // declared; we know the template parameter lists match, since otherwise |
| 1973 | // we would not have picked this template as the previous declaration. |
| 1974 | if (TemplateParams && FunctionTemplate->getPreviousDecl()) { |
| 1975 | SemaRef.CheckTemplateParameterList( |
| 1976 | TemplateParams, |
| 1977 | FunctionTemplate->getPreviousDecl()->getTemplateParameters(), |
| 1978 | Function->isThisDeclarationADefinition() |
| 1979 | ? Sema::TPC_FriendFunctionTemplateDefinition |
| 1980 | : Sema::TPC_FriendFunctionTemplate); |
| 1981 | } |
| Douglas Gregor | 3a88c1d | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 1984 | if (Function->isLocalExternDecl() && !Function->getPreviousDecl()) |
| 1985 | DC->makeDeclVisibleInContext(PrincipalDecl); |
| 1986 | |
| John McCall | b9467b6 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1987 | if (Function->isOverloadedOperator() && !DC->isRecord() && |
| 1988 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 1989 | PrincipalDecl->setNonMemberOperator(); |
| 1990 | |
| Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 1991 | assert(!D->isDefaulted() && "only methods should be defaulted"); |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1992 | return Function; |
| 1993 | } |
| 1994 | |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 1995 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl( |
| 1996 | CXXMethodDecl *D, TemplateParameterList *TemplateParams, |
| 1997 | Optional<const ASTTemplateArgumentListInfo *> |
| 1998 | ClassScopeSpecializationArgs) { |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1999 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2000 | if (FunctionTemplate && !TemplateParams) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2001 | // We are creating a function template specialization from a function |
| 2002 | // template. Check whether there is already a function template |
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2003 | // specialization for this particular set of template arguments. |
| Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 2004 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2005 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2006 | void *InsertPos = nullptr; |
| Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 2007 | FunctionDecl *SpecFunc |
| Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2008 | = FunctionTemplate->findSpecialization(Innermost, InsertPos); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2010 | // If we already have a function template specialization, return it. |
| Argyrios Kyrtzidis | dde5790 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 2011 | if (SpecFunc) |
| 2012 | return SpecFunc; |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2015 | bool isFriend; |
| 2016 | if (FunctionTemplate) |
| 2017 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 2018 | else |
| 2019 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 2020 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2021 | bool MergeWithParentScope = (TemplateParams != nullptr) || |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2022 | !(isa<Decl>(Owner) && |
| Douglas Gregor | f5974fa | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 2023 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2024 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
| Douglas Gregor | 3725652 | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 2025 | |
| John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 2026 | // Instantiate enclosing template arguments for friends. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2027 | SmallVector<TemplateParameterList *, 4> TempParamLists; |
| John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 2028 | unsigned NumTempParamLists = 0; |
| 2029 | if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) { |
| Benjamin Kramer | 9dc549b | 2015-08-04 14:46:06 +0000 | [diff] [blame] | 2030 | TempParamLists.resize(NumTempParamLists); |
| John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 2031 | for (unsigned I = 0; I != NumTempParamLists; ++I) { |
| 2032 | TemplateParameterList *TempParams = D->getTemplateParameterList(I); |
| 2033 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 2034 | if (!InstParams) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2035 | return nullptr; |
| John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 2036 | TempParamLists[I] = InstParams; |
| 2037 | } |
| 2038 | } |
| 2039 | |
| Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 2040 | ExplicitSpecifier InstantiatedExplicitSpecifier = |
| 2041 | instantiateExplicitSpecifier(SemaRef, TemplateArgs, |
| 2042 | ExplicitSpecifier::getFromDecl(D), D); |
| 2043 | if (InstantiatedExplicitSpecifier.isInvalid()) |
| 2044 | return nullptr; |
| 2045 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2046 | SmallVector<ParmVarDecl *, 4> Params; |
| Benjamin Kramer | 1dd48bc | 2012-01-20 14:42:32 +0000 | [diff] [blame] | 2047 | TypeSourceInfo *TInfo = SubstFunctionType(D, Params); |
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2048 | if (!TInfo) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2049 | return nullptr; |
| Douglas Gregor | 89f593a | 2012-09-13 21:56:43 +0000 | [diff] [blame] | 2050 | QualType T = adjustFunctionTypeForInstantiation(SemaRef.Context, D, TInfo); |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2051 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2052 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 2053 | if (QualifierLoc) { |
| 2054 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2055 | TemplateArgs); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2056 | if (!QualifierLoc) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2057 | return nullptr; |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | DeclContext *DC = Owner; |
| 2061 | if (isFriend) { |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2062 | if (QualifierLoc) { |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2063 | CXXScopeSpec SS; |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2064 | SS.Adopt(QualifierLoc); |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2065 | DC = SemaRef.computeDeclContext(SS); |
| John McCall | 1a1b53e | 2010-10-19 05:01:53 +0000 | [diff] [blame] | 2066 | |
| 2067 | if (DC && SemaRef.RequireCompleteDeclContext(SS, DC)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2068 | return nullptr; |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2069 | } else { |
| 2070 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), |
| 2071 | D->getDeclContext(), |
| 2072 | TemplateArgs); |
| 2073 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2074 | if (!DC) return nullptr; |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2077 | // Build the instantiated method declaration. |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2078 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2079 | CXXMethodDecl *Method = nullptr; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2080 | |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2081 | SourceLocation StartLoc = D->getInnerLocStart(); |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2082 | DeclarationNameInfo NameInfo |
| 2083 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 2084 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
| Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 2085 | Method = CXXConstructorDecl::Create( |
| 2086 | SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, |
| 2087 | InstantiatedExplicitSpecifier, Constructor->isInlineSpecified(), false, |
| Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 2088 | Constructor->getConstexprKind()); |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2089 | Method->setRangeEnd(Constructor->getEndLoc()); |
| Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 2090 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 2091 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2092 | StartLoc, NameInfo, T, TInfo, |
| Reid Kleckner | 0f764e5 | 2015-04-07 20:46:51 +0000 | [diff] [blame] | 2093 | Destructor->isInlineSpecified(), |
| Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2094 | false); |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2095 | Method->setRangeEnd(Destructor->getEndLoc()); |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2096 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2097 | Method = CXXConversionDecl::Create( |
| 2098 | SemaRef.Context, Record, StartLoc, NameInfo, T, TInfo, |
| Richard Smith | 76b9027 | 2019-05-09 03:59:21 +0000 | [diff] [blame] | 2099 | Conversion->isInlineSpecified(), InstantiatedExplicitSpecifier, |
| Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 2100 | Conversion->getConstexprKind(), Conversion->getEndLoc()); |
| Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2101 | } else { |
| Rafael Espindola | 29cda59 | 2013-04-15 12:38:20 +0000 | [diff] [blame] | 2102 | StorageClass SC = D->isStatic() ? SC_Static : SC_None; |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2103 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, StartLoc, NameInfo, |
| 2104 | T, TInfo, SC, D->isInlineSpecified(), |
| Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 2105 | D->getConstexprKind(), D->getEndLoc()); |
| Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2106 | } |
| Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2107 | |
| Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 2108 | if (D->isInlined()) |
| 2109 | Method->setImplicitlyInline(); |
| 2110 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2111 | if (QualifierLoc) |
| 2112 | Method->setQualifierInfo(QualifierLoc); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2113 | |
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2114 | if (TemplateParams) { |
| 2115 | // Our resulting instantiation is actually a function template, since we |
| 2116 | // are substituting only the outer template parameters. For example, given |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | // |
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2118 | // template<typename T> |
| 2119 | // struct X { |
| 2120 | // template<typename U> void f(T, U); |
| 2121 | // }; |
| 2122 | // |
| 2123 | // X<int> x; |
| 2124 | // |
| 2125 | // We are instantiating the member template "f" within X<int>, which means |
| 2126 | // substituting int for T, but leaving "f" as a member function template. |
| 2127 | // Build the function template itself. |
| 2128 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, |
| 2129 | Method->getLocation(), |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2130 | Method->getDeclName(), |
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2131 | TemplateParams, Method); |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2132 | if (isFriend) { |
| 2133 | FunctionTemplate->setLexicalDeclContext(Owner); |
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 2134 | FunctionTemplate->setObjectOfFriendDecl(); |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2135 | } else if (D->isOutOfLine()) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2136 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
| Douglas Gregor | e704c9d | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 2137 | Method->setDescribedFunctionTemplate(FunctionTemplate); |
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2138 | } else if (FunctionTemplate) { |
| 2139 | // Record this function template specialization. |
| Richard Smith | 47752e4 | 2013-05-03 23:46:09 +0000 | [diff] [blame] | 2140 | ArrayRef<TemplateArgument> Innermost = TemplateArgs.getInnermost(); |
| Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2141 | Method->setFunctionTemplateSpecialization(FunctionTemplate, |
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2142 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
| David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2143 | Innermost), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2144 | /*InsertPos=*/nullptr); |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2145 | } else if (!isFriend) { |
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2146 | // Record that this is an instantiation of a member function. |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2147 | Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2148 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2149 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2150 | // If we are instantiating a member function defined |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2151 | // out-of-line, the instantiation will have the same lexical |
| 2152 | // context (which will be a namespace scope) as the template. |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2153 | if (isFriend) { |
| John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 2154 | if (NumTempParamLists) |
| Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 2155 | Method->setTemplateParameterListsInfo( |
| 2156 | SemaRef.Context, |
| 2157 | llvm::makeArrayRef(TempParamLists.data(), NumTempParamLists)); |
| John McCall | d0e23ec | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 2158 | |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2159 | Method->setLexicalDeclContext(Owner); |
| Richard Smith | 6401768 | 2013-07-17 23:53:16 +0000 | [diff] [blame] | 2160 | Method->setObjectOfFriendDecl(); |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2161 | } else if (D->isOutOfLine()) |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2162 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2163 | |
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2164 | // Attach the parameters |
| 2165 | for (unsigned P = 0; P < Params.size(); ++P) |
| 2166 | Params[P]->setOwningFunction(Method); |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2167 | Method->setParams(Params); |
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2168 | |
| 2169 | if (InitMethodInstantiation(Method, D)) |
| 2170 | Method->setInvalidDecl(); |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2171 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2172 | LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName, |
| Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 2173 | Sema::ForExternalRedeclaration); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2174 | |
| Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 2175 | bool IsExplicitSpecialization = false; |
| 2176 | |
| 2177 | // If the name of this function was written as a template-id, instantiate |
| 2178 | // the explicit template arguments. |
| 2179 | if (DependentFunctionTemplateSpecializationInfo *Info |
| 2180 | = D->getDependentSpecializationInfo()) { |
| 2181 | assert(isFriend && "non-friend has dependent specialization info?"); |
| 2182 | |
| 2183 | // Instantiate the explicit template arguments. |
| 2184 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 2185 | Info->getRAngleLoc()); |
| 2186 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 2187 | ExplicitArgs, TemplateArgs)) |
| 2188 | return nullptr; |
| 2189 | |
| 2190 | // Map the candidate templates to their instantiations. |
| 2191 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 2192 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 2193 | Info->getTemplate(I), |
| 2194 | TemplateArgs); |
| 2195 | if (!Temp) return nullptr; |
| 2196 | |
| 2197 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 2198 | } |
| 2199 | |
| 2200 | if (SemaRef.CheckFunctionTemplateSpecialization(Method, |
| 2201 | &ExplicitArgs, |
| 2202 | Previous)) |
| 2203 | Method->setInvalidDecl(); |
| 2204 | |
| 2205 | IsExplicitSpecialization = true; |
| 2206 | } else if (const ASTTemplateArgumentListInfo *Info = |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 2207 | ClassScopeSpecializationArgs.getValueOr( |
| 2208 | D->getTemplateSpecializationArgsAsWritten())) { |
| Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 2209 | SemaRef.LookupQualifiedName(Previous, DC); |
| 2210 | |
| 2211 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 2212 | Info->getRAngleLoc()); |
| 2213 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 2214 | ExplicitArgs, TemplateArgs)) |
| 2215 | return nullptr; |
| 2216 | |
| 2217 | if (SemaRef.CheckFunctionTemplateSpecialization(Method, |
| 2218 | &ExplicitArgs, |
| 2219 | Previous)) |
| 2220 | Method->setInvalidDecl(); |
| 2221 | |
| 2222 | IsExplicitSpecialization = true; |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 2223 | } else if (ClassScopeSpecializationArgs) { |
| 2224 | // Class-scope explicit specialization written without explicit template |
| 2225 | // arguments. |
| 2226 | SemaRef.LookupQualifiedName(Previous, DC); |
| 2227 | if (SemaRef.CheckFunctionTemplateSpecialization(Method, nullptr, Previous)) |
| 2228 | Method->setInvalidDecl(); |
| 2229 | |
| 2230 | IsExplicitSpecialization = true; |
| Richard Smith | 64095fc | 2019-01-11 01:59:33 +0000 | [diff] [blame] | 2231 | } else if (!FunctionTemplate || TemplateParams || isFriend) { |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2232 | SemaRef.LookupQualifiedName(Previous, Record); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | |
| Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2234 | // In C++, the previous declaration we find might be a tag type |
| 2235 | // (class or enum). In this case, the new declaration will hide the |
| 2236 | // tag type. Note that this does does not apply if we're declaring a |
| 2237 | // typedef (C++ [dcl.typedef]p4). |
| John McCall | 1f82f24 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 2238 | if (Previous.isSingleTagDecl()) |
| 2239 | Previous.clear(); |
| Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2240 | } |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2241 | |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 2242 | SemaRef.CheckFunctionDeclaration(nullptr, Method, Previous, |
| 2243 | IsExplicitSpecialization); |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2244 | |
| Douglas Gregor | 21920e37 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 2245 | if (D->isPure()) |
| 2246 | SemaRef.CheckPureMethod(Method, SourceRange()); |
| 2247 | |
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2248 | // Propagate access. For a non-friend declaration, the access is |
| 2249 | // whatever we're propagating from. For a friend, it should be the |
| 2250 | // previous declaration we just found. |
| 2251 | if (isFriend && Method->getPreviousDecl()) |
| 2252 | Method->setAccess(Method->getPreviousDecl()->getAccess()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2253 | else |
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2254 | Method->setAccess(D->getAccess()); |
| 2255 | if (FunctionTemplate) |
| 2256 | FunctionTemplate->setAccess(Method->getAccess()); |
| John McCall | 401982f | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 2257 | |
| Anders Carlsson | 7c812f5 | 2011-01-20 06:52:44 +0000 | [diff] [blame] | 2258 | SemaRef.CheckOverrideControl(Method); |
| 2259 | |
| Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 2260 | // If a function is defined as defaulted or deleted, mark it as such now. |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 2261 | if (D->isExplicitlyDefaulted()) |
| 2262 | SemaRef.SetDeclDefaulted(Method, Method->getLocation()); |
| Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 2263 | if (D->isDeletedAsWritten()) |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 2264 | SemaRef.SetDeclDeleted(Method, Method->getLocation()); |
| Eli Friedman | 4134073 | 2011-11-15 22:39:08 +0000 | [diff] [blame] | 2265 | |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 2266 | // If this is an explicit specialization, mark the implicitly-instantiated |
| 2267 | // template specialization as being an explicit specialization too. |
| 2268 | // FIXME: Is this necessary? |
| 2269 | if (IsExplicitSpecialization && !isFriend) |
| 2270 | SemaRef.CompleteMemberSpecialization(Method, Previous); |
| 2271 | |
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2272 | // If there's a function template, let our caller handle it. |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2273 | if (FunctionTemplate) { |
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2274 | // do nothing |
| 2275 | |
| 2276 | // Don't hide a (potentially) valid declaration with an invalid one. |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2277 | } else if (Method->isInvalidDecl() && !Previous.empty()) { |
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2278 | // do nothing |
| 2279 | |
| 2280 | // Otherwise, check access to friends and make them visible. |
| 2281 | } else if (isFriend) { |
| 2282 | // We only need to re-check access for methods which we didn't |
| 2283 | // manage to match during parsing. |
| 2284 | if (!D->getPreviousDecl()) |
| 2285 | SemaRef.CheckFriendAccess(Method); |
| 2286 | |
| 2287 | Record->makeDeclVisibleInContext(Method); |
| 2288 | |
| 2289 | // Otherwise, add the declaration. We don't need to do this for |
| 2290 | // class-scope specializations because we'll have matched them with |
| 2291 | // the appropriate template. |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 2292 | } else { |
| John McCall | a0a9689 | 2012-08-10 03:15:35 +0000 | [diff] [blame] | 2293 | Owner->addDecl(Method); |
| John McCall | 2f88d7d | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 2294 | } |
| Alexis Hunt | 1fb4e76 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 2295 | |
| Rafael Auler | 9dde31e | 2019-03-20 19:22:24 +0000 | [diff] [blame] | 2296 | // PR17480: Honor the used attribute to instantiate member function |
| 2297 | // definitions |
| 2298 | if (Method->hasAttr<UsedAttr>()) { |
| 2299 | if (const auto *A = dyn_cast<CXXRecordDecl>(Owner)) { |
| 2300 | SourceLocation Loc; |
| 2301 | if (const MemberSpecializationInfo *MSInfo = |
| 2302 | A->getMemberSpecializationInfo()) |
| 2303 | Loc = MSInfo->getPointOfInstantiation(); |
| 2304 | else if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(A)) |
| 2305 | Loc = Spec->getPointOfInstantiation(); |
| 2306 | SemaRef.MarkFunctionReferenced(Loc, Method); |
| 2307 | } |
| 2308 | } |
| 2309 | |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2310 | return Method; |
| 2311 | } |
| 2312 | |
| Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 2313 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
| Douglas Gregor | 5ed5ae4 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 2314 | return VisitCXXMethodDecl(D); |
| Douglas Gregor | 4044d99 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
| Douglas Gregor | 654b07e | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 2317 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
| Douglas Gregor | e839486 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 2318 | return VisitCXXMethodDecl(D); |
| Douglas Gregor | 654b07e | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
| Douglas Gregor | 1880ba5 | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 2321 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2322 | return VisitCXXMethodDecl(D); |
| Douglas Gregor | 1880ba5 | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 2325 | Decl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
| David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 2326 | return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, None, |
| 2327 | /*ExpectParameterPack=*/ false); |
| Douglas Gregor | f4f296d | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2330 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( |
| 2331 | TemplateTypeParmDecl *D) { |
| 2332 | // TODO: don't always clone when decls are refcounted. |
| Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 2333 | assert(D->getTypeForDecl()->isTemplateTypeParmType()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | |
| Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2335 | TemplateTypeParmDecl *Inst = TemplateTypeParmDecl::Create( |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2336 | SemaRef.Context, Owner, D->getBeginLoc(), D->getLocation(), |
| Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2337 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), D->getIndex(), |
| 2338 | D->getIdentifier(), D->wasDeclaredWithTypename(), D->isParameterPack()); |
| Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2339 | Inst->setAccess(AS_public); |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2340 | |
| Richard Smith | 5293379 | 2015-06-16 21:57:05 +0000 | [diff] [blame] | 2341 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
| David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2342 | TypeSourceInfo *InstantiatedDefaultArg = |
| 2343 | SemaRef.SubstType(D->getDefaultArgumentInfo(), TemplateArgs, |
| 2344 | D->getDefaultArgumentLoc(), D->getDeclName()); |
| 2345 | if (InstantiatedDefaultArg) |
| Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2346 | Inst->setDefaultArgument(InstantiatedDefaultArg); |
| David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2347 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2348 | |
| 2349 | // Introduce this template parameter's instantiation into the instantiation |
| Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2350 | // scope. |
| 2351 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2352 | |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 2353 | return Inst; |
| 2354 | } |
| 2355 | |
| Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2356 | Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( |
| 2357 | NonTypeTemplateParmDecl *D) { |
| 2358 | // Substitute into the type of the non-type template parameter. |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2359 | TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc(); |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2360 | SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten; |
| 2361 | SmallVector<QualType, 4> ExpandedParameterPackTypes; |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2362 | bool IsExpandedParameterPack = false; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2363 | TypeSourceInfo *DI; |
| Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2364 | QualType T; |
| Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2365 | bool Invalid = false; |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2366 | |
| 2367 | if (D->isExpandedParameterPack()) { |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2368 | // The non-type template parameter pack is an already-expanded pack |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2369 | // expansion of types. Substitute into each of the expanded types. |
| 2370 | ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes()); |
| 2371 | ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes()); |
| 2372 | for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { |
| Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2373 | TypeSourceInfo *NewDI = |
| 2374 | SemaRef.SubstType(D->getExpansionTypeSourceInfo(I), TemplateArgs, |
| 2375 | D->getLocation(), D->getDeclName()); |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2376 | if (!NewDI) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2377 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2378 | |
| Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2379 | QualType NewT = |
| 2380 | SemaRef.CheckNonTypeTemplateParameterType(NewDI, D->getLocation()); |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2381 | if (NewT.isNull()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2382 | return nullptr; |
| Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2383 | |
| 2384 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2385 | ExpandedParameterPackTypes.push_back(NewT); |
| 2386 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2387 | |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2388 | IsExpandedParameterPack = true; |
| 2389 | DI = D->getTypeSourceInfo(); |
| 2390 | T = DI->getType(); |
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2391 | } else if (D->isPackExpansion()) { |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2392 | // The non-type template parameter pack's type is a pack expansion of types. |
| 2393 | // Determine whether we need to expand this parameter pack into separate |
| 2394 | // types. |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 2395 | PackExpansionTypeLoc Expansion = TL.castAs<PackExpansionTypeLoc>(); |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2396 | TypeLoc Pattern = Expansion.getPatternLoc(); |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2397 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2398 | SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2399 | |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2400 | // Determine whether the set of unexpanded parameter packs can and should |
| 2401 | // be expanded. |
| 2402 | bool Expand = true; |
| 2403 | bool RetainExpansion = false; |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2404 | Optional<unsigned> OrigNumExpansions |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2405 | = Expansion.getTypePtr()->getNumExpansions(); |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2406 | Optional<unsigned> NumExpansions = OrigNumExpansions; |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2407 | if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(), |
| 2408 | Pattern.getSourceRange(), |
| David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 2409 | Unexpanded, |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2410 | TemplateArgs, |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2411 | Expand, RetainExpansion, |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2412 | NumExpansions)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2413 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2414 | |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2415 | if (Expand) { |
| 2416 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2417 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2418 | TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs, |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2419 | D->getLocation(), |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2420 | D->getDeclName()); |
| 2421 | if (!NewDI) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2422 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2423 | |
| Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2424 | QualType NewT = |
| 2425 | SemaRef.CheckNonTypeTemplateParameterType(NewDI, D->getLocation()); |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2426 | if (NewT.isNull()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2427 | return nullptr; |
| Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2428 | |
| 2429 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2430 | ExpandedParameterPackTypes.push_back(NewT); |
| 2431 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2432 | |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2433 | // Note that we have an expanded parameter pack. The "type" of this |
| 2434 | // expanded parameter pack is the original expansion type, but callers |
| 2435 | // will end up using the expanded parameter pack types for type-checking. |
| 2436 | IsExpandedParameterPack = true; |
| 2437 | DI = D->getTypeSourceInfo(); |
| 2438 | T = DI->getType(); |
| 2439 | } else { |
| 2440 | // We cannot fully expand the pack expansion now, so substitute into the |
| 2441 | // pattern and create a new pack expansion type. |
| 2442 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2443 | TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs, |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2444 | D->getLocation(), |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2445 | D->getDeclName()); |
| 2446 | if (!NewPattern) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2447 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2448 | |
| Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2449 | SemaRef.CheckNonTypeTemplateParameterType(NewPattern, D->getLocation()); |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2450 | DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(), |
| 2451 | NumExpansions); |
| 2452 | if (!DI) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2453 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2454 | |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2455 | T = DI->getType(); |
| 2456 | } |
| 2457 | } else { |
| 2458 | // Simple case: substitution into a parameter that is not a parameter pack. |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2459 | DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2460 | D->getLocation(), D->getDeclName()); |
| 2461 | if (!DI) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2462 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2463 | |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2464 | // Check that this type is acceptable for a non-type template parameter. |
| Richard Smith | 15361a2 | 2016-12-28 06:27:18 +0000 | [diff] [blame] | 2465 | T = SemaRef.CheckNonTypeTemplateParameterType(DI, D->getLocation()); |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2466 | if (T.isNull()) { |
| 2467 | T = SemaRef.Context.IntTy; |
| 2468 | Invalid = true; |
| 2469 | } |
| Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2470 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2471 | |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2472 | NonTypeTemplateParmDecl *Param; |
| 2473 | if (IsExpandedParameterPack) |
| David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 2474 | Param = NonTypeTemplateParmDecl::Create( |
| 2475 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
| Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2476 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2477 | D->getPosition(), D->getIdentifier(), T, DI, ExpandedParameterPackTypes, |
| David Majnemer | dfecf1a | 2016-07-06 04:19:16 +0000 | [diff] [blame] | 2478 | ExpandedParameterPackTypesAsWritten); |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2479 | else |
| Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2480 | Param = NonTypeTemplateParmDecl::Create( |
| 2481 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
| 2482 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2483 | D->getPosition(), D->getIdentifier(), T, D->isParameterPack(), DI); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2484 | |
| Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2485 | Param->setAccess(AS_public); |
| Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2486 | if (Invalid) |
| 2487 | Param->setInvalidDecl(); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2488 | |
| Richard Smith | 5293379 | 2015-06-16 21:57:05 +0000 | [diff] [blame] | 2489 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2490 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 2491 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2492 | ExprResult Value = SemaRef.SubstExpr(D->getDefaultArgument(), TemplateArgs); |
| 2493 | if (!Value.isInvalid()) |
| Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2494 | Param->setDefaultArgument(Value.get()); |
| David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2495 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2496 | |
| 2497 | // Introduce this template parameter's instantiation into the instantiation |
| Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2498 | // scope. |
| 2499 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
| Douglas Gregor | 6b815c8 | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 2500 | return Param; |
| 2501 | } |
| 2502 | |
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2503 | static void collectUnexpandedParameterPacks( |
| 2504 | Sema &S, |
| 2505 | TemplateParameterList *Params, |
| 2506 | SmallVectorImpl<UnexpandedParameterPack> &Unexpanded) { |
| Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 2507 | for (const auto &P : *Params) { |
| 2508 | if (P->isTemplateParameterPack()) |
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2509 | continue; |
| Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 2510 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) |
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2511 | S.collectUnexpandedParameterPacks(NTTP->getTypeSourceInfo()->getTypeLoc(), |
| 2512 | Unexpanded); |
| Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 2513 | if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(P)) |
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2514 | collectUnexpandedParameterPacks(S, TTP->getTemplateParameters(), |
| 2515 | Unexpanded); |
| 2516 | } |
| 2517 | } |
| 2518 | |
| Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2519 | Decl * |
| Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2520 | TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( |
| 2521 | TemplateTemplateParmDecl *D) { |
| 2522 | // Instantiate the template parameter list of the template template parameter. |
| 2523 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 2524 | TemplateParameterList *InstParams; |
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2525 | SmallVector<TemplateParameterList*, 8> ExpandedParams; |
| 2526 | |
| 2527 | bool IsExpandedParameterPack = false; |
| 2528 | |
| 2529 | if (D->isExpandedParameterPack()) { |
| 2530 | // The template template parameter pack is an already-expanded pack |
| 2531 | // expansion of template parameters. Substitute into each of the expanded |
| 2532 | // parameters. |
| 2533 | ExpandedParams.reserve(D->getNumExpansionTemplateParameters()); |
| 2534 | for (unsigned I = 0, N = D->getNumExpansionTemplateParameters(); |
| 2535 | I != N; ++I) { |
| 2536 | LocalInstantiationScope Scope(SemaRef); |
| 2537 | TemplateParameterList *Expansion = |
| 2538 | SubstTemplateParams(D->getExpansionTemplateParameters(I)); |
| 2539 | if (!Expansion) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2540 | return nullptr; |
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2541 | ExpandedParams.push_back(Expansion); |
| 2542 | } |
| 2543 | |
| 2544 | IsExpandedParameterPack = true; |
| 2545 | InstParams = TempParams; |
| 2546 | } else if (D->isPackExpansion()) { |
| 2547 | // The template template parameter pack expands to a pack of template |
| 2548 | // template parameters. Determine whether we need to expand this parameter |
| 2549 | // pack into separate parameters. |
| 2550 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2551 | collectUnexpandedParameterPacks(SemaRef, D->getTemplateParameters(), |
| 2552 | Unexpanded); |
| 2553 | |
| 2554 | // Determine whether the set of unexpanded parameter packs can and should |
| 2555 | // be expanded. |
| 2556 | bool Expand = true; |
| 2557 | bool RetainExpansion = false; |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 2558 | Optional<unsigned> NumExpansions; |
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2559 | if (SemaRef.CheckParameterPacksForExpansion(D->getLocation(), |
| 2560 | TempParams->getSourceRange(), |
| 2561 | Unexpanded, |
| 2562 | TemplateArgs, |
| 2563 | Expand, RetainExpansion, |
| 2564 | NumExpansions)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2565 | return nullptr; |
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2566 | |
| 2567 | if (Expand) { |
| 2568 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2569 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2570 | LocalInstantiationScope Scope(SemaRef); |
| 2571 | TemplateParameterList *Expansion = SubstTemplateParams(TempParams); |
| 2572 | if (!Expansion) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2573 | return nullptr; |
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2574 | ExpandedParams.push_back(Expansion); |
| 2575 | } |
| 2576 | |
| 2577 | // Note that we have an expanded parameter pack. The "type" of this |
| 2578 | // expanded parameter pack is the original expansion type, but callers |
| 2579 | // will end up using the expanded parameter pack types for type-checking. |
| 2580 | IsExpandedParameterPack = true; |
| 2581 | InstParams = TempParams; |
| 2582 | } else { |
| 2583 | // We cannot fully expand the pack expansion now, so just substitute |
| 2584 | // into the pattern. |
| 2585 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2586 | |
| 2587 | LocalInstantiationScope Scope(SemaRef); |
| 2588 | InstParams = SubstTemplateParams(TempParams); |
| 2589 | if (!InstParams) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2590 | return nullptr; |
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2591 | } |
| 2592 | } else { |
| Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2593 | // Perform the actual substitution of template parameters within a new, |
| 2594 | // local instantiation scope. |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2595 | LocalInstantiationScope Scope(SemaRef); |
| Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2596 | InstParams = SubstTemplateParams(TempParams); |
| 2597 | if (!InstParams) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2598 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
| Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2601 | // Build the template template parameter. |
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2602 | TemplateTemplateParmDecl *Param; |
| 2603 | if (IsExpandedParameterPack) |
| Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2604 | Param = TemplateTemplateParmDecl::Create( |
| 2605 | SemaRef.Context, Owner, D->getLocation(), |
| 2606 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2607 | D->getPosition(), D->getIdentifier(), InstParams, ExpandedParams); |
| Richard Smith | 1fde8ec | 2012-09-07 02:06:42 +0000 | [diff] [blame] | 2608 | else |
| Richard Smith | b4f9625 | 2017-02-21 06:30:38 +0000 | [diff] [blame] | 2609 | Param = TemplateTemplateParmDecl::Create( |
| 2610 | SemaRef.Context, Owner, D->getLocation(), |
| 2611 | D->getDepth() - TemplateArgs.getNumSubstitutedLevels(), |
| 2612 | D->getPosition(), D->isParameterPack(), D->getIdentifier(), InstParams); |
| Richard Smith | 5293379 | 2015-06-16 21:57:05 +0000 | [diff] [blame] | 2613 | if (D->hasDefaultArgument() && !D->defaultArgumentWasInherited()) { |
| David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2614 | NestedNameSpecifierLoc QualifierLoc = |
| 2615 | D->getDefaultArgument().getTemplateQualifierLoc(); |
| 2616 | QualifierLoc = |
| 2617 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgs); |
| 2618 | TemplateName TName = SemaRef.SubstTemplateName( |
| 2619 | QualifierLoc, D->getDefaultArgument().getArgument().getAsTemplate(), |
| 2620 | D->getDefaultArgument().getTemplateNameLoc(), TemplateArgs); |
| 2621 | if (!TName.isNull()) |
| 2622 | Param->setDefaultArgument( |
| Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2623 | SemaRef.Context, |
| David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2624 | TemplateArgumentLoc(TemplateArgument(TName), |
| 2625 | D->getDefaultArgument().getTemplateQualifierLoc(), |
| Richard Smith | 1469b91 | 2015-06-10 00:29:03 +0000 | [diff] [blame] | 2626 | D->getDefaultArgument().getTemplateNameLoc())); |
| David Majnemer | 8918920 | 2013-08-28 23:48:32 +0000 | [diff] [blame] | 2627 | } |
| Douglas Gregor | fd7c225 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 2628 | Param->setAccess(AS_public); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2629 | |
| 2630 | // Introduce this template parameter's instantiation into the instantiation |
| Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2631 | // scope. |
| 2632 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2633 | |
| Douglas Gregor | 38fee96 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 2634 | return Param; |
| 2635 | } |
| 2636 | |
| Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2637 | Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2638 | // Using directives are never dependent (and never contain any types or |
| 2639 | // expressions), so they require no explicit instantiation work. |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2640 | |
| Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2641 | UsingDirectiveDecl *Inst |
| 2642 | = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2643 | D->getNamespaceKeyLocation(), |
| Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2644 | D->getQualifierLoc(), |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2645 | D->getIdentLocation(), |
| 2646 | D->getNominatedNamespace(), |
| Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2647 | D->getCommonAncestor()); |
| Abramo Bagnara | 8843f9f | 2012-09-05 09:55:10 +0000 | [diff] [blame] | 2648 | |
| 2649 | // Add the using directive to its declaration context |
| 2650 | // only if this is not a function or method. |
| 2651 | if (!Owner->isFunctionOrMethod()) |
| 2652 | Owner->addDecl(Inst); |
| 2653 | |
| Douglas Gregor | e0b2866 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 2654 | return Inst; |
| 2655 | } |
| 2656 | |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2657 | Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { |
| Douglas Gregor | ac2e430 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 2658 | |
| 2659 | // The nested name specifier may be dependent, for example |
| 2660 | // template <typename T> struct t { |
| 2661 | // struct s1 { T f1(); }; |
| 2662 | // struct s2 : s1 { using s1::f1; }; |
| 2663 | // }; |
| 2664 | // template struct t<int>; |
| 2665 | // Here, in using s1::f1, s1 refers to t<T>::s1; |
| 2666 | // we need to substitute for t<int>::s1. |
| Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2667 | NestedNameSpecifierLoc QualifierLoc |
| 2668 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
| 2669 | TemplateArgs); |
| 2670 | if (!QualifierLoc) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2671 | return nullptr; |
| Douglas Gregor | ac2e430 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 2672 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2673 | // For an inheriting constructor declaration, the name of the using |
| 2674 | // declaration is the name of a constructor in this class, not in the |
| 2675 | // base class. |
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2676 | DeclarationNameInfo NameInfo = D->getNameInfo(); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2677 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) |
| 2678 | if (auto *RD = dyn_cast<CXXRecordDecl>(SemaRef.CurContext)) |
| 2679 | NameInfo.setName(SemaRef.Context.DeclarationNames.getCXXConstructorName( |
| 2680 | SemaRef.Context.getCanonicalType(SemaRef.Context.getRecordType(RD)))); |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2681 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2682 | // We only need to do redeclaration lookups if we're in a class |
| 2683 | // scope (in fact, it's not really even possible in non-class |
| 2684 | // scopes). |
| 2685 | bool CheckRedeclaration = Owner->isRecord(); |
| 2686 | |
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2687 | LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName, |
| Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 2688 | Sema::ForVisibleRedeclaration); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2689 | |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2690 | UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2691 | D->getUsingLoc(), |
| Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2692 | QualifierLoc, |
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2693 | NameInfo, |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2694 | D->hasTypename()); |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2695 | |
| Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2696 | CXXScopeSpec SS; |
| 2697 | SS.Adopt(QualifierLoc); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2698 | if (CheckRedeclaration) { |
| 2699 | Prev.setHideTags(false); |
| 2700 | SemaRef.LookupQualifiedName(Prev, Owner); |
| 2701 | |
| 2702 | // Check for invalid redeclarations. |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2703 | if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLoc(), |
| 2704 | D->hasTypename(), SS, |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2705 | D->getLocation(), Prev)) |
| 2706 | NewUD->setInvalidDecl(); |
| 2707 | |
| 2708 | } |
| 2709 | |
| 2710 | if (!NewUD->isInvalidDecl() && |
| Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 2711 | SemaRef.CheckUsingDeclQualifier(D->getUsingLoc(), D->hasTypename(), |
| 2712 | SS, NameInfo, D->getLocation())) |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2713 | NewUD->setInvalidDecl(); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2714 | |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2715 | SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); |
| 2716 | NewUD->setAccess(D->getAccess()); |
| 2717 | Owner->addDecl(NewUD); |
| 2718 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2719 | // Don't process the shadow decls for an invalid decl. |
| 2720 | if (NewUD->isInvalidDecl()) |
| 2721 | return NewUD; |
| 2722 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2723 | if (NameInfo.getName().getNameKind() == DeclarationName::CXXConstructorName) |
| Richard Smith | 09d5b3a | 2014-05-01 00:35:04 +0000 | [diff] [blame] | 2724 | SemaRef.CheckInheritingConstructorUsingDecl(NewUD); |
| Richard Smith | 23d5587 | 2012-04-02 01:30:27 +0000 | [diff] [blame] | 2725 | |
| John McCall | a1d8550 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2726 | bool isFunctionScope = Owner->isFunctionOrMethod(); |
| 2727 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2728 | // Process the shadow decls. |
| Aaron Ballman | 91cdc28 | 2014-03-13 18:07:29 +0000 | [diff] [blame] | 2729 | for (auto *Shadow : D->shadows()) { |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2730 | // FIXME: UsingShadowDecl doesn't preserve its immediate target, so |
| 2731 | // reconstruct it in the case where it matters. |
| 2732 | NamedDecl *OldTarget = Shadow->getTargetDecl(); |
| 2733 | if (auto *CUSD = dyn_cast<ConstructorUsingShadowDecl>(Shadow)) |
| 2734 | if (auto *BaseShadow = CUSD->getNominatedBaseClassShadowDecl()) |
| 2735 | OldTarget = BaseShadow; |
| 2736 | |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2737 | NamedDecl *InstTarget = |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2738 | cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl( |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2739 | Shadow->getLocation(), OldTarget, TemplateArgs)); |
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 2740 | if (!InstTarget) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2741 | return nullptr; |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2742 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2743 | UsingShadowDecl *PrevDecl = nullptr; |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2744 | if (CheckRedeclaration) { |
| 2745 | if (SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev, PrevDecl)) |
| 2746 | continue; |
| Richard Smith | 41c79d9 | 2014-10-11 00:37:16 +0000 | [diff] [blame] | 2747 | } else if (UsingShadowDecl *OldPrev = |
| 2748 | getPreviousDeclForInstantiation(Shadow)) { |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2749 | PrevDecl = cast_or_null<UsingShadowDecl>(SemaRef.FindInstantiatedDecl( |
| 2750 | Shadow->getLocation(), OldPrev, TemplateArgs)); |
| 2751 | } |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2752 | |
| Richard Smith | fd8634a | 2013-10-23 02:17:46 +0000 | [diff] [blame] | 2753 | UsingShadowDecl *InstShadow = |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2754 | SemaRef.BuildUsingShadowDecl(/*Scope*/nullptr, NewUD, InstTarget, |
| 2755 | PrevDecl); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2756 | SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); |
| John McCall | a1d8550 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 2757 | |
| 2758 | if (isFunctionScope) |
| 2759 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow); |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2760 | } |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2761 | |
| 2762 | return NewUD; |
| 2763 | } |
| 2764 | |
| 2765 | Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { |
| John McCall | 84d8767 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2766 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2767 | return nullptr; |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2770 | Decl *TemplateDeclInstantiator::VisitConstructorUsingShadowDecl( |
| 2771 | ConstructorUsingShadowDecl *D) { |
| 2772 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
| 2773 | return nullptr; |
| 2774 | } |
| 2775 | |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2776 | template <typename T> |
| 2777 | Decl *TemplateDeclInstantiator::instantiateUnresolvedUsingDecl( |
| 2778 | T *D, bool InstantiatingPackElement) { |
| 2779 | // If this is a pack expansion, expand it now. |
| 2780 | if (D->isPackExpansion() && !InstantiatingPackElement) { |
| 2781 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2782 | SemaRef.collectUnexpandedParameterPacks(D->getQualifierLoc(), Unexpanded); |
| 2783 | SemaRef.collectUnexpandedParameterPacks(D->getNameInfo(), Unexpanded); |
| 2784 | |
| 2785 | // Determine whether the set of unexpanded parameter packs can and should |
| 2786 | // be expanded. |
| 2787 | bool Expand = true; |
| 2788 | bool RetainExpansion = false; |
| 2789 | Optional<unsigned> NumExpansions; |
| 2790 | if (SemaRef.CheckParameterPacksForExpansion( |
| 2791 | D->getEllipsisLoc(), D->getSourceRange(), Unexpanded, TemplateArgs, |
| 2792 | Expand, RetainExpansion, NumExpansions)) |
| 2793 | return nullptr; |
| 2794 | |
| 2795 | // This declaration cannot appear within a function template signature, |
| 2796 | // so we can't have a partial argument list for a parameter pack. |
| 2797 | assert(!RetainExpansion && |
| 2798 | "should never need to retain an expansion for UsingPackDecl"); |
| 2799 | |
| 2800 | if (!Expand) { |
| 2801 | // We cannot fully expand the pack expansion now, so substitute into the |
| 2802 | // pattern and create a new pack expansion. |
| 2803 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2804 | return instantiateUnresolvedUsingDecl(D, true); |
| 2805 | } |
| 2806 | |
| 2807 | // Within a function, we don't have any normal way to check for conflicts |
| 2808 | // between shadow declarations from different using declarations in the |
| 2809 | // same pack expansion, but this is always ill-formed because all expansions |
| 2810 | // must produce (conflicting) enumerators. |
| 2811 | // |
| 2812 | // Sadly we can't just reject this in the template definition because it |
| 2813 | // could be valid if the pack is empty or has exactly one expansion. |
| 2814 | if (D->getDeclContext()->isFunctionOrMethod() && *NumExpansions > 1) { |
| 2815 | SemaRef.Diag(D->getEllipsisLoc(), |
| 2816 | diag::err_using_decl_redeclaration_expansion); |
| 2817 | return nullptr; |
| 2818 | } |
| 2819 | |
| 2820 | // Instantiate the slices of this pack and build a UsingPackDecl. |
| 2821 | SmallVector<NamedDecl*, 8> Expansions; |
| 2822 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 2823 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 2824 | Decl *Slice = instantiateUnresolvedUsingDecl(D, true); |
| 2825 | if (!Slice) |
| 2826 | return nullptr; |
| 2827 | // Note that we can still get unresolved using declarations here, if we |
| 2828 | // had arguments for all packs but the pattern also contained other |
| 2829 | // template arguments (this only happens during partial substitution, eg |
| 2830 | // into the body of a generic lambda in a function template). |
| 2831 | Expansions.push_back(cast<NamedDecl>(Slice)); |
| 2832 | } |
| 2833 | |
| 2834 | auto *NewD = SemaRef.BuildUsingPackDecl(D, Expansions); |
| 2835 | if (isDeclWithinFunction(D)) |
| 2836 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewD); |
| 2837 | return NewD; |
| 2838 | } |
| 2839 | |
| 2840 | UnresolvedUsingTypenameDecl *TD = dyn_cast<UnresolvedUsingTypenameDecl>(D); |
| 2841 | SourceLocation TypenameLoc = TD ? TD->getTypenameLoc() : SourceLocation(); |
| 2842 | |
| Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2843 | NestedNameSpecifierLoc QualifierLoc |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 2844 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
| Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2845 | TemplateArgs); |
| 2846 | if (!QualifierLoc) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2847 | return nullptr; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | |
| Anders Carlsson | 4bd7875 | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 2849 | CXXScopeSpec SS; |
| Douglas Gregor | 0499ab6 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 2850 | SS.Adopt(QualifierLoc); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | |
| Daniel Jasper | 9949ead | 2016-12-19 10:09:25 +0000 | [diff] [blame] | 2852 | DeclarationNameInfo NameInfo |
| 2853 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 2854 | |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2855 | // Produce a pack expansion only if we're not instantiating a particular |
| 2856 | // slice of a pack expansion. |
| 2857 | bool InstantiatingSlice = D->getEllipsisLoc().isValid() && |
| 2858 | SemaRef.ArgumentPackSubstitutionIndex != -1; |
| 2859 | SourceLocation EllipsisLoc = |
| 2860 | InstantiatingSlice ? SourceLocation() : D->getEllipsisLoc(); |
| 2861 | |
| 2862 | NamedDecl *UD = SemaRef.BuildUsingDeclaration( |
| 2863 | /*Scope*/ nullptr, D->getAccess(), D->getUsingLoc(), |
| Erich Keane | c480f30 | 2018-07-12 21:09:05 +0000 | [diff] [blame] | 2864 | /*HasTypename*/ TD, TypenameLoc, SS, NameInfo, EllipsisLoc, |
| 2865 | ParsedAttributesView(), |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2866 | /*IsInstantiation*/ true); |
| Daniel Jasper | 9949ead | 2016-12-19 10:09:25 +0000 | [diff] [blame] | 2867 | if (UD) |
| 2868 | SemaRef.Context.setInstantiatedFromUsingDecl(UD, D); |
| 2869 | |
| 2870 | return UD; |
| Richard Smith | 22a250c | 2016-12-19 04:08:53 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2873 | Decl *TemplateDeclInstantiator::VisitUnresolvedUsingTypenameDecl( |
| 2874 | UnresolvedUsingTypenameDecl *D) { |
| 2875 | return instantiateUnresolvedUsingDecl(D); |
| 2876 | } |
| 2877 | |
| 2878 | Decl *TemplateDeclInstantiator::VisitUnresolvedUsingValueDecl( |
| 2879 | UnresolvedUsingValueDecl *D) { |
| 2880 | return instantiateUnresolvedUsingDecl(D); |
| 2881 | } |
| 2882 | |
| 2883 | Decl *TemplateDeclInstantiator::VisitUsingPackDecl(UsingPackDecl *D) { |
| 2884 | SmallVector<NamedDecl*, 8> Expansions; |
| 2885 | for (auto *UD : D->expansions()) { |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 2886 | if (NamedDecl *NewUD = |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2887 | SemaRef.FindInstantiatedDecl(D->getLocation(), UD, TemplateArgs)) |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 2888 | Expansions.push_back(NewUD); |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2889 | else |
| 2890 | return nullptr; |
| 2891 | } |
| 2892 | |
| 2893 | auto *NewD = SemaRef.BuildUsingPackDecl(D, Expansions); |
| 2894 | if (isDeclWithinFunction(D)) |
| 2895 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewD); |
| 2896 | return NewD; |
| 2897 | } |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2898 | |
| 2899 | Decl *TemplateDeclInstantiator::VisitClassScopeFunctionSpecializationDecl( |
| Richard Smith | 6e67142 | 2018-12-04 22:26:32 +0000 | [diff] [blame] | 2900 | ClassScopeFunctionSpecializationDecl *Decl) { |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2901 | CXXMethodDecl *OldFD = Decl->getSpecialization(); |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 2902 | return cast_or_null<CXXMethodDecl>( |
| 2903 | VisitCXXMethodDecl(OldFD, nullptr, Decl->getTemplateArgsAsWritten())); |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
| Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2906 | Decl *TemplateDeclInstantiator::VisitOMPThreadPrivateDecl( |
| 2907 | OMPThreadPrivateDecl *D) { |
| Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 2908 | SmallVector<Expr *, 5> Vars; |
| Aaron Ballman | 2205d2a | 2014-03-14 15:55:35 +0000 | [diff] [blame] | 2909 | for (auto *I : D->varlists()) { |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2910 | Expr *Var = SemaRef.SubstExpr(I, TemplateArgs).get(); |
| Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2911 | assert(isa<DeclRefExpr>(Var) && "threadprivate arg is not a DeclRefExpr"); |
| Alexey Bataev | 6f6f3b4 | 2013-05-13 04:18:18 +0000 | [diff] [blame] | 2912 | Vars.push_back(Var); |
| Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | OMPThreadPrivateDecl *TD = |
| 2916 | SemaRef.CheckOMPThreadPrivateDecl(D->getLocation(), Vars); |
| 2917 | |
| Alexey Bataev | d3db6ac | 2014-03-07 09:46:29 +0000 | [diff] [blame] | 2918 | TD->setAccess(AS_public); |
| 2919 | Owner->addDecl(TD); |
| 2920 | |
| Alexey Bataev | a769e07 | 2013-03-22 06:34:35 +0000 | [diff] [blame] | 2921 | return TD; |
| 2922 | } |
| 2923 | |
| Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 2924 | Decl *TemplateDeclInstantiator::VisitOMPAllocateDecl(OMPAllocateDecl *D) { |
| 2925 | SmallVector<Expr *, 5> Vars; |
| 2926 | for (auto *I : D->varlists()) { |
| 2927 | Expr *Var = SemaRef.SubstExpr(I, TemplateArgs).get(); |
| 2928 | assert(isa<DeclRefExpr>(Var) && "allocate arg is not a DeclRefExpr"); |
| 2929 | Vars.push_back(Var); |
| 2930 | } |
| Alexey Bataev | 9cc10fc | 2019-03-12 18:52:33 +0000 | [diff] [blame] | 2931 | SmallVector<OMPClause *, 4> Clauses; |
| 2932 | // Copy map clauses from the original mapper. |
| 2933 | for (OMPClause *C : D->clauselists()) { |
| 2934 | auto *AC = cast<OMPAllocatorClause>(C); |
| 2935 | ExprResult NewE = SemaRef.SubstExpr(AC->getAllocator(), TemplateArgs); |
| 2936 | if (!NewE.isUsable()) |
| 2937 | continue; |
| 2938 | OMPClause *IC = SemaRef.ActOnOpenMPAllocatorClause( |
| 2939 | NewE.get(), AC->getBeginLoc(), AC->getLParenLoc(), AC->getEndLoc()); |
| 2940 | Clauses.push_back(IC); |
| 2941 | } |
| Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 2942 | |
| Alexey Bataev | 9cc10fc | 2019-03-12 18:52:33 +0000 | [diff] [blame] | 2943 | Sema::DeclGroupPtrTy Res = SemaRef.ActOnOpenMPAllocateDirective( |
| 2944 | D->getLocation(), Vars, Clauses, Owner); |
| Alexey Bataev | 25ed0c0 | 2019-03-07 17:54:44 +0000 | [diff] [blame] | 2945 | if (Res.get().isNull()) |
| 2946 | return nullptr; |
| 2947 | return Res.get().getSingleDecl(); |
| 2948 | } |
| 2949 | |
| Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 2950 | Decl *TemplateDeclInstantiator::VisitOMPRequiresDecl(OMPRequiresDecl *D) { |
| 2951 | llvm_unreachable( |
| 2952 | "Requires directive cannot be instantiated within a dependent context"); |
| 2953 | } |
| 2954 | |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2955 | Decl *TemplateDeclInstantiator::VisitOMPDeclareReductionDecl( |
| 2956 | OMPDeclareReductionDecl *D) { |
| 2957 | // Instantiate type and check if it is allowed. |
| Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2958 | const bool RequiresInstantiation = |
| 2959 | D->getType()->isDependentType() || |
| 2960 | D->getType()->isInstantiationDependentType() || |
| 2961 | D->getType()->containsUnexpandedParameterPack(); |
| 2962 | QualType SubstReductionType; |
| 2963 | if (RequiresInstantiation) { |
| 2964 | SubstReductionType = SemaRef.ActOnOpenMPDeclareReductionType( |
| 2965 | D->getLocation(), |
| 2966 | ParsedType::make(SemaRef.SubstType( |
| 2967 | D->getType(), TemplateArgs, D->getLocation(), DeclarationName()))); |
| 2968 | } else { |
| 2969 | SubstReductionType = D->getType(); |
| 2970 | } |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 2971 | if (SubstReductionType.isNull()) |
| 2972 | return nullptr; |
| 2973 | bool IsCorrect = !SubstReductionType.isNull(); |
| 2974 | // Create instantiated copy. |
| 2975 | std::pair<QualType, SourceLocation> ReductionTypes[] = { |
| 2976 | std::make_pair(SubstReductionType, D->getLocation())}; |
| 2977 | auto *PrevDeclInScope = D->getPrevDeclInScope(); |
| 2978 | if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) { |
| 2979 | PrevDeclInScope = cast<OMPDeclareReductionDecl>( |
| 2980 | SemaRef.CurrentInstantiationScope->findInstantiationOf(PrevDeclInScope) |
| 2981 | ->get<Decl *>()); |
| 2982 | } |
| 2983 | auto DRD = SemaRef.ActOnOpenMPDeclareReductionDirectiveStart( |
| 2984 | /*S=*/nullptr, Owner, D->getDeclName(), ReductionTypes, D->getAccess(), |
| 2985 | PrevDeclInScope); |
| 2986 | auto *NewDRD = cast<OMPDeclareReductionDecl>(DRD.get().getSingleDecl()); |
| Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 2987 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewDRD); |
| 2988 | if (!RequiresInstantiation) { |
| 2989 | if (Expr *Combiner = D->getCombiner()) { |
| 2990 | NewDRD->setCombinerData(D->getCombinerIn(), D->getCombinerOut()); |
| 2991 | NewDRD->setCombiner(Combiner); |
| 2992 | if (Expr *Init = D->getInitializer()) { |
| 2993 | NewDRD->setInitializerData(D->getInitOrig(), D->getInitPriv()); |
| 2994 | NewDRD->setInitializer(Init, D->getInitializerKind()); |
| 2995 | } |
| 2996 | } |
| 2997 | (void)SemaRef.ActOnOpenMPDeclareReductionDirectiveEnd( |
| 2998 | /*S=*/nullptr, DRD, IsCorrect && !D->isInvalidDecl()); |
| 2999 | return NewDRD; |
| 3000 | } |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 3001 | Expr *SubstCombiner = nullptr; |
| 3002 | Expr *SubstInitializer = nullptr; |
| 3003 | // Combiners instantiation sequence. |
| 3004 | if (D->getCombiner()) { |
| 3005 | SemaRef.ActOnOpenMPDeclareReductionCombinerStart( |
| 3006 | /*S=*/nullptr, NewDRD); |
| Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 3007 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 3008 | cast<DeclRefExpr>(D->getCombinerIn())->getDecl(), |
| 3009 | cast<DeclRefExpr>(NewDRD->getCombinerIn())->getDecl()); |
| 3010 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 3011 | cast<DeclRefExpr>(D->getCombinerOut())->getDecl(), |
| 3012 | cast<DeclRefExpr>(NewDRD->getCombinerOut())->getDecl()); |
| 3013 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(Owner); |
| Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 3014 | Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, Qualifiers(), |
| Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 3015 | ThisContext); |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 3016 | SubstCombiner = SemaRef.SubstExpr(D->getCombiner(), TemplateArgs).get(); |
| 3017 | SemaRef.ActOnOpenMPDeclareReductionCombinerEnd(NewDRD, SubstCombiner); |
| 3018 | // Initializers instantiation sequence. |
| 3019 | if (D->getInitializer()) { |
| Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 3020 | VarDecl *OmpPrivParm = |
| 3021 | SemaRef.ActOnOpenMPDeclareReductionInitializerStart( |
| 3022 | /*S=*/nullptr, NewDRD); |
| Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 3023 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 3024 | cast<DeclRefExpr>(D->getInitOrig())->getDecl(), |
| 3025 | cast<DeclRefExpr>(NewDRD->getInitOrig())->getDecl()); |
| 3026 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 3027 | cast<DeclRefExpr>(D->getInitPriv())->getDecl(), |
| 3028 | cast<DeclRefExpr>(NewDRD->getInitPriv())->getDecl()); |
| Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 3029 | if (D->getInitializerKind() == OMPDeclareReductionDecl::CallInit) { |
| 3030 | SubstInitializer = |
| 3031 | SemaRef.SubstExpr(D->getInitializer(), TemplateArgs).get(); |
| 3032 | } else { |
| 3033 | IsCorrect = IsCorrect && OmpPrivParm->hasInit(); |
| 3034 | } |
| 3035 | SemaRef.ActOnOpenMPDeclareReductionInitializerEnd( |
| 3036 | NewDRD, SubstInitializer, OmpPrivParm); |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 3037 | } |
| Alexey Bataev | 070f43a | 2017-09-06 14:49:58 +0000 | [diff] [blame] | 3038 | IsCorrect = |
| 3039 | IsCorrect && SubstCombiner && |
| 3040 | (!D->getInitializer() || |
| 3041 | (D->getInitializerKind() == OMPDeclareReductionDecl::CallInit && |
| 3042 | SubstInitializer) || |
| 3043 | (D->getInitializerKind() != OMPDeclareReductionDecl::CallInit && |
| 3044 | !SubstInitializer && !SubstInitializer)); |
| Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 3045 | } else { |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 3046 | IsCorrect = false; |
| Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 3047 | } |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 3048 | |
| 3049 | (void)SemaRef.ActOnOpenMPDeclareReductionDirectiveEnd(/*S=*/nullptr, DRD, |
| 3050 | IsCorrect); |
| 3051 | |
| 3052 | return NewDRD; |
| 3053 | } |
| 3054 | |
| Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 3055 | Decl * |
| 3056 | TemplateDeclInstantiator::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) { |
| 3057 | // Instantiate type and check if it is allowed. |
| 3058 | const bool RequiresInstantiation = |
| 3059 | D->getType()->isDependentType() || |
| 3060 | D->getType()->isInstantiationDependentType() || |
| 3061 | D->getType()->containsUnexpandedParameterPack(); |
| 3062 | QualType SubstMapperTy; |
| 3063 | DeclarationName VN = D->getVarName(); |
| 3064 | if (RequiresInstantiation) { |
| 3065 | SubstMapperTy = SemaRef.ActOnOpenMPDeclareMapperType( |
| 3066 | D->getLocation(), |
| 3067 | ParsedType::make(SemaRef.SubstType(D->getType(), TemplateArgs, |
| 3068 | D->getLocation(), VN))); |
| 3069 | } else { |
| 3070 | SubstMapperTy = D->getType(); |
| 3071 | } |
| 3072 | if (SubstMapperTy.isNull()) |
| 3073 | return nullptr; |
| 3074 | // Create an instantiated copy of mapper. |
| 3075 | auto *PrevDeclInScope = D->getPrevDeclInScope(); |
| 3076 | if (PrevDeclInScope && !PrevDeclInScope->isInvalidDecl()) { |
| 3077 | PrevDeclInScope = cast<OMPDeclareMapperDecl>( |
| 3078 | SemaRef.CurrentInstantiationScope->findInstantiationOf(PrevDeclInScope) |
| 3079 | ->get<Decl *>()); |
| 3080 | } |
| 3081 | OMPDeclareMapperDecl *NewDMD = SemaRef.ActOnOpenMPDeclareMapperDirectiveStart( |
| 3082 | /*S=*/nullptr, Owner, D->getDeclName(), SubstMapperTy, D->getLocation(), |
| 3083 | VN, D->getAccess(), PrevDeclInScope); |
| 3084 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, NewDMD); |
| 3085 | SmallVector<OMPClause *, 6> Clauses; |
| 3086 | bool IsCorrect = true; |
| 3087 | if (!RequiresInstantiation) { |
| 3088 | // Copy the mapper variable. |
| 3089 | NewDMD->setMapperVarRef(D->getMapperVarRef()); |
| 3090 | // Copy map clauses from the original mapper. |
| 3091 | for (OMPClause *C : D->clauselists()) |
| 3092 | Clauses.push_back(C); |
| 3093 | } else { |
| 3094 | // Instantiate the mapper variable. |
| 3095 | DeclarationNameInfo DirName; |
| 3096 | SemaRef.StartOpenMPDSABlock(OMPD_declare_mapper, DirName, /*S=*/nullptr, |
| 3097 | (*D->clauselist_begin())->getBeginLoc()); |
| 3098 | SemaRef.ActOnOpenMPDeclareMapperDirectiveVarDecl( |
| 3099 | NewDMD, /*S=*/nullptr, SubstMapperTy, D->getLocation(), VN); |
| 3100 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
| 3101 | cast<DeclRefExpr>(D->getMapperVarRef())->getDecl(), |
| 3102 | cast<DeclRefExpr>(NewDMD->getMapperVarRef())->getDecl()); |
| 3103 | auto *ThisContext = dyn_cast_or_null<CXXRecordDecl>(Owner); |
| 3104 | Sema::CXXThisScopeRAII ThisScope(SemaRef, ThisContext, Qualifiers(), |
| 3105 | ThisContext); |
| 3106 | // Instantiate map clauses. |
| 3107 | for (OMPClause *C : D->clauselists()) { |
| 3108 | auto *OldC = cast<OMPMapClause>(C); |
| 3109 | SmallVector<Expr *, 4> NewVars; |
| 3110 | for (Expr *OE : OldC->varlists()) { |
| 3111 | Expr *NE = SemaRef.SubstExpr(OE, TemplateArgs).get(); |
| 3112 | if (!NE) { |
| 3113 | IsCorrect = false; |
| 3114 | break; |
| 3115 | } |
| 3116 | NewVars.push_back(NE); |
| 3117 | } |
| 3118 | if (!IsCorrect) |
| 3119 | break; |
| Michael Kruse | 4304e9d | 2019-02-19 16:38:20 +0000 | [diff] [blame] | 3120 | NestedNameSpecifierLoc NewQualifierLoc = |
| 3121 | SemaRef.SubstNestedNameSpecifierLoc(OldC->getMapperQualifierLoc(), |
| 3122 | TemplateArgs); |
| 3123 | CXXScopeSpec SS; |
| 3124 | SS.Adopt(NewQualifierLoc); |
| 3125 | DeclarationNameInfo NewNameInfo = SemaRef.SubstDeclarationNameInfo( |
| 3126 | OldC->getMapperIdInfo(), TemplateArgs); |
| 3127 | OMPVarListLocTy Locs(OldC->getBeginLoc(), OldC->getLParenLoc(), |
| 3128 | OldC->getEndLoc()); |
| Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 3129 | OMPClause *NewC = SemaRef.ActOnOpenMPMapClause( |
| Michael Kruse | 4304e9d | 2019-02-19 16:38:20 +0000 | [diff] [blame] | 3130 | OldC->getMapTypeModifiers(), OldC->getMapTypeModifiersLoc(), SS, |
| 3131 | NewNameInfo, OldC->getMapType(), OldC->isImplicitMapType(), |
| 3132 | OldC->getMapLoc(), OldC->getColonLoc(), NewVars, Locs); |
| Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 3133 | Clauses.push_back(NewC); |
| 3134 | } |
| 3135 | SemaRef.EndOpenMPDSABlock(nullptr); |
| 3136 | } |
| 3137 | (void)SemaRef.ActOnOpenMPDeclareMapperDirectiveEnd(NewDMD, /*S=*/nullptr, |
| 3138 | Clauses); |
| 3139 | if (!IsCorrect) |
| 3140 | return nullptr; |
| 3141 | return NewDMD; |
| 3142 | } |
| 3143 | |
| Alexey Bataev | 4244be2 | 2016-02-11 05:35:55 +0000 | [diff] [blame] | 3144 | Decl *TemplateDeclInstantiator::VisitOMPCapturedExprDecl( |
| 3145 | OMPCapturedExprDecl * /*D*/) { |
| Alexey Bataev | 90c228f | 2016-02-08 09:29:13 +0000 | [diff] [blame] | 3146 | llvm_unreachable("Should not be met in templates"); |
| 3147 | } |
| 3148 | |
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3149 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3150 | return VisitFunctionDecl(D, nullptr); |
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 3153 | Decl * |
| 3154 | TemplateDeclInstantiator::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) { |
| Richard Smith | 2600c63 | 2018-05-30 20:24:10 +0000 | [diff] [blame] | 3155 | Decl *Inst = VisitFunctionDecl(D, nullptr); |
| 3156 | if (Inst && !D->getDescribedFunctionTemplate()) |
| 3157 | Owner->addDecl(Inst); |
| 3158 | return Inst; |
| Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3161 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3162 | return VisitCXXMethodDecl(D, nullptr); |
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | Decl *TemplateDeclInstantiator::VisitRecordDecl(RecordDecl *D) { |
| 3166 | llvm_unreachable("There are only CXXRecordDecls in C++"); |
| 3167 | } |
| 3168 | |
| 3169 | Decl * |
| 3170 | TemplateDeclInstantiator::VisitClassTemplateSpecializationDecl( |
| 3171 | ClassTemplateSpecializationDecl *D) { |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3172 | // As a MS extension, we permit class-scope explicit specialization |
| 3173 | // of member class templates. |
| 3174 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
| 3175 | assert(ClassTemplate->getDeclContext()->isRecord() && |
| 3176 | D->getTemplateSpecializationKind() == TSK_ExplicitSpecialization && |
| 3177 | "can only instantiate an explicit specialization " |
| 3178 | "for a member class template"); |
| 3179 | |
| 3180 | // Lookup the already-instantiated declaration in the instantiation |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 3181 | // of the class template. |
| 3182 | ClassTemplateDecl *InstClassTemplate = |
| 3183 | cast_or_null<ClassTemplateDecl>(SemaRef.FindInstantiatedDecl( |
| 3184 | D->getLocation(), ClassTemplate, TemplateArgs)); |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3185 | if (!InstClassTemplate) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3186 | return nullptr; |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3187 | |
| 3188 | // Substitute into the template arguments of the class template explicit |
| 3189 | // specialization. |
| 3190 | TemplateSpecializationTypeLoc Loc = D->getTypeAsWritten()->getTypeLoc(). |
| 3191 | castAs<TemplateSpecializationTypeLoc>(); |
| 3192 | TemplateArgumentListInfo InstTemplateArgs(Loc.getLAngleLoc(), |
| 3193 | Loc.getRAngleLoc()); |
| 3194 | SmallVector<TemplateArgumentLoc, 4> ArgLocs; |
| 3195 | for (unsigned I = 0; I != Loc.getNumArgs(); ++I) |
| 3196 | ArgLocs.push_back(Loc.getArgLoc(I)); |
| 3197 | if (SemaRef.Subst(ArgLocs.data(), ArgLocs.size(), |
| 3198 | InstTemplateArgs, TemplateArgs)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3199 | return nullptr; |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3200 | |
| 3201 | // Check that the template argument list is well-formed for this |
| 3202 | // class template. |
| 3203 | SmallVector<TemplateArgument, 4> Converted; |
| 3204 | if (SemaRef.CheckTemplateArgumentList(InstClassTemplate, |
| 3205 | D->getLocation(), |
| 3206 | InstTemplateArgs, |
| 3207 | false, |
| 3208 | Converted)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3209 | return nullptr; |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3210 | |
| 3211 | // Figure out where to insert this class template explicit specialization |
| 3212 | // in the member template's set of class template explicit specializations. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3213 | void *InsertPos = nullptr; |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3214 | ClassTemplateSpecializationDecl *PrevDecl = |
| Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3215 | InstClassTemplate->findSpecialization(Converted, InsertPos); |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3216 | |
| 3217 | // Check whether we've already seen a conflicting instantiation of this |
| 3218 | // declaration (for instance, if there was a prior implicit instantiation). |
| 3219 | bool Ignored; |
| 3220 | if (PrevDecl && |
| 3221 | SemaRef.CheckSpecializationInstantiationRedecl(D->getLocation(), |
| 3222 | D->getSpecializationKind(), |
| 3223 | PrevDecl, |
| 3224 | PrevDecl->getSpecializationKind(), |
| 3225 | PrevDecl->getPointOfInstantiation(), |
| 3226 | Ignored)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3227 | return nullptr; |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3228 | |
| 3229 | // If PrevDecl was a definition and D is also a definition, diagnose. |
| 3230 | // This happens in cases like: |
| 3231 | // |
| 3232 | // template<typename T, typename U> |
| 3233 | // struct Outer { |
| 3234 | // template<typename X> struct Inner; |
| 3235 | // template<> struct Inner<T> {}; |
| 3236 | // template<> struct Inner<U> {}; |
| 3237 | // }; |
| 3238 | // |
| 3239 | // Outer<int, int> outer; // error: the explicit specializations of Inner |
| 3240 | // // have the same signature. |
| 3241 | if (PrevDecl && PrevDecl->getDefinition() && |
| 3242 | D->isThisDeclarationADefinition()) { |
| 3243 | SemaRef.Diag(D->getLocation(), diag::err_redefinition) << PrevDecl; |
| 3244 | SemaRef.Diag(PrevDecl->getDefinition()->getLocation(), |
| 3245 | diag::note_previous_definition); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3246 | return nullptr; |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | // Create the class template partial specialization declaration. |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3250 | ClassTemplateSpecializationDecl *InstD = |
| 3251 | ClassTemplateSpecializationDecl::Create( |
| 3252 | SemaRef.Context, D->getTagKind(), Owner, D->getBeginLoc(), |
| 3253 | D->getLocation(), InstClassTemplate, Converted, PrevDecl); |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3254 | |
| 3255 | // Add this partial specialization to the set of class template partial |
| 3256 | // specializations. |
| 3257 | if (!PrevDecl) |
| 3258 | InstClassTemplate->AddSpecialization(InstD, InsertPos); |
| 3259 | |
| 3260 | // Substitute the nested name specifier, if any. |
| 3261 | if (SubstQualifier(D, InstD)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3262 | return nullptr; |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3263 | |
| 3264 | // Build the canonical type that describes the converted template |
| 3265 | // arguments of the class template explicit specialization. |
| 3266 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
| David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3267 | TemplateName(InstClassTemplate), Converted, |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3268 | SemaRef.Context.getRecordType(InstD)); |
| 3269 | |
| 3270 | // Build the fully-sugared type for this class template |
| 3271 | // specialization as the user wrote in the specialization |
| 3272 | // itself. This means that we'll pretty-print the type retrieved |
| 3273 | // from the specialization's declaration the way that the user |
| 3274 | // actually wrote the specialization, rather than formatting the |
| 3275 | // name based on the "canonical" representation used to store the |
| 3276 | // template arguments in the specialization. |
| 3277 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 3278 | TemplateName(InstClassTemplate), D->getLocation(), InstTemplateArgs, |
| 3279 | CanonType); |
| 3280 | |
| 3281 | InstD->setAccess(D->getAccess()); |
| 3282 | InstD->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
| 3283 | InstD->setSpecializationKind(D->getSpecializationKind()); |
| 3284 | InstD->setTypeAsWritten(WrittenTy); |
| 3285 | InstD->setExternLoc(D->getExternLoc()); |
| 3286 | InstD->setTemplateKeywordLoc(D->getTemplateKeywordLoc()); |
| 3287 | |
| 3288 | Owner->addDecl(InstD); |
| 3289 | |
| 3290 | // Instantiate the members of the class-scope explicit specialization eagerly. |
| 3291 | // We don't have support for lazy instantiation of an explicit specialization |
| 3292 | // yet, and MSVC eagerly instantiates in this case. |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 3293 | // FIXME: This is wrong in standard C++. |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3294 | if (D->isThisDeclarationADefinition() && |
| 3295 | SemaRef.InstantiateClass(D->getLocation(), InstD, D, TemplateArgs, |
| 3296 | TSK_ImplicitInstantiation, |
| 3297 | /*Complain=*/true)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3298 | return nullptr; |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 3299 | |
| 3300 | return InstD; |
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3303 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( |
| 3304 | VarTemplateSpecializationDecl *D) { |
| 3305 | |
| 3306 | TemplateArgumentListInfo VarTemplateArgsInfo; |
| 3307 | VarTemplateDecl *VarTemplate = D->getSpecializedTemplate(); |
| 3308 | assert(VarTemplate && |
| 3309 | "A template specialization without specialized template?"); |
| 3310 | |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 3311 | VarTemplateDecl *InstVarTemplate = |
| 3312 | cast_or_null<VarTemplateDecl>(SemaRef.FindInstantiatedDecl( |
| 3313 | D->getLocation(), VarTemplate, TemplateArgs)); |
| 3314 | if (!InstVarTemplate) |
| 3315 | return nullptr; |
| 3316 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3317 | // Substitute the current template arguments. |
| 3318 | const TemplateArgumentListInfo &TemplateArgsInfo = D->getTemplateArgsInfo(); |
| 3319 | VarTemplateArgsInfo.setLAngleLoc(TemplateArgsInfo.getLAngleLoc()); |
| 3320 | VarTemplateArgsInfo.setRAngleLoc(TemplateArgsInfo.getRAngleLoc()); |
| 3321 | |
| 3322 | if (SemaRef.Subst(TemplateArgsInfo.getArgumentArray(), |
| 3323 | TemplateArgsInfo.size(), VarTemplateArgsInfo, TemplateArgs)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3324 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3325 | |
| 3326 | // Check that the template argument list is well-formed for this template. |
| 3327 | SmallVector<TemplateArgument, 4> Converted; |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 3328 | if (SemaRef.CheckTemplateArgumentList(InstVarTemplate, D->getLocation(), |
| 3329 | VarTemplateArgsInfo, false, Converted)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3330 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3331 | |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 3332 | // Check whether we've already seen a declaration of this specialization. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3333 | void *InsertPos = nullptr; |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 3334 | VarTemplateSpecializationDecl *PrevDecl = |
| 3335 | InstVarTemplate->findSpecialization(Converted, InsertPos); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3336 | |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 3337 | // Check whether we've already seen a conflicting instantiation of this |
| 3338 | // declaration (for instance, if there was a prior implicit instantiation). |
| 3339 | bool Ignored; |
| 3340 | if (PrevDecl && SemaRef.CheckSpecializationInstantiationRedecl( |
| 3341 | D->getLocation(), D->getSpecializationKind(), PrevDecl, |
| 3342 | PrevDecl->getSpecializationKind(), |
| 3343 | PrevDecl->getPointOfInstantiation(), Ignored)) |
| 3344 | return nullptr; |
| 3345 | |
| 3346 | return VisitVarTemplateSpecializationDecl( |
| 3347 | InstVarTemplate, D, InsertPos, VarTemplateArgsInfo, Converted, PrevDecl); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | Decl *TemplateDeclInstantiator::VisitVarTemplateSpecializationDecl( |
| 3351 | VarTemplateDecl *VarTemplate, VarDecl *D, void *InsertPos, |
| 3352 | const TemplateArgumentListInfo &TemplateArgsInfo, |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 3353 | ArrayRef<TemplateArgument> Converted, |
| 3354 | VarTemplateSpecializationDecl *PrevDecl) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3355 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3356 | // Do substitution on the type of the declaration |
| 3357 | TypeSourceInfo *DI = |
| 3358 | SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
| 3359 | D->getTypeSpecStartLoc(), D->getDeclName()); |
| 3360 | if (!DI) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3361 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3362 | |
| 3363 | if (DI->getType()->isFunctionType()) { |
| 3364 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 3365 | << D->isStaticDataMember() << DI->getType(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3366 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | // Build the instantiated declaration |
| 3370 | VarTemplateSpecializationDecl *Var = VarTemplateSpecializationDecl::Create( |
| 3371 | SemaRef.Context, Owner, D->getInnerLocStart(), D->getLocation(), |
| David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3372 | VarTemplate, DI->getType(), DI, D->getStorageClass(), Converted); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3373 | Var->setTemplateArgsInfo(TemplateArgsInfo); |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 3374 | if (InsertPos) |
| 3375 | VarTemplate->AddSpecialization(Var, InsertPos); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3376 | |
| 3377 | // Substitute the nested name specifier, if any. |
| 3378 | if (SubstQualifier(D, Var)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3379 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3380 | |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 3381 | SemaRef.BuildVariableInstantiation(Var, D, TemplateArgs, LateAttrs, Owner, |
| 3382 | StartingScope, false, PrevDecl); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3383 | |
| 3384 | return Var; |
| 3385 | } |
| 3386 | |
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3387 | Decl *TemplateDeclInstantiator::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) { |
| 3388 | llvm_unreachable("@defs is not supported in Objective-C++"); |
| 3389 | } |
| 3390 | |
| 3391 | Decl *TemplateDeclInstantiator::VisitFriendTemplateDecl(FriendTemplateDecl *D) { |
| 3392 | // FIXME: We need to be able to instantiate FriendTemplateDecls. |
| 3393 | unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID( |
| 3394 | DiagnosticsEngine::Error, |
| 3395 | "cannot instantiate %0 yet"); |
| 3396 | SemaRef.Diag(D->getLocation(), DiagID) |
| 3397 | << D->getDeclKindName(); |
| 3398 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3399 | return nullptr; |
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
| Saar Raz | d7aae33 | 2019-07-10 21:25:49 +0000 | [diff] [blame] | 3402 | Decl *TemplateDeclInstantiator::VisitConceptDecl(ConceptDecl *D) { |
| 3403 | llvm_unreachable("Concept definitions cannot reside inside a template"); |
| 3404 | } |
| 3405 | |
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3406 | Decl *TemplateDeclInstantiator::VisitDecl(Decl *D) { |
| 3407 | llvm_unreachable("Unexpected decl"); |
| 3408 | } |
| 3409 | |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3410 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, |
| Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 3411 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| Douglas Gregor | d002c7b | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 3412 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
| Douglas Gregor | 71ad477 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 3413 | if (D->isInvalidDecl()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3414 | return nullptr; |
| Douglas Gregor | 71ad477 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 3415 | |
| Richard Smith | 26a92d5 | 2019-08-26 18:18:07 +0000 | [diff] [blame^] | 3416 | Decl *SubstD; |
| 3417 | runWithSufficientStackSpace(D->getLocation(), [&] { |
| 3418 | SubstD = Instantiator.Visit(D); |
| 3419 | }); |
| 3420 | return SubstD; |
| Douglas Gregor | d7e7a51 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3423 | /// Instantiates a nested template parameter list in the current |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3424 | /// instantiation context. |
| 3425 | /// |
| 3426 | /// \param L The parameter list to instantiate |
| 3427 | /// |
| 3428 | /// \returns NULL if there was an error |
| 3429 | TemplateParameterList * |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3430 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3431 | // Get errors for all the parameters before bailing out. |
| 3432 | bool Invalid = false; |
| 3433 | |
| 3434 | unsigned N = L->size(); |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3435 | typedef SmallVector<NamedDecl *, 8> ParamVector; |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3436 | ParamVector Params; |
| 3437 | Params.reserve(N); |
| Davide Italiano | 18960b9 | 2015-07-02 19:20:11 +0000 | [diff] [blame] | 3438 | for (auto &P : *L) { |
| 3439 | NamedDecl *D = cast_or_null<NamedDecl>(Visit(P)); |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3440 | Params.push_back(D); |
| Douglas Gregor | e62e6a0 | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 3441 | Invalid = Invalid || !D || D->isInvalidDecl(); |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
| 3444 | // Clean up if we had an error. |
| Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 3445 | if (Invalid) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3446 | return nullptr; |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3447 | |
| Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 3448 | // Note: we substitute into associated constraints later |
| 3449 | Expr *const UninstantiatedRequiresClause = L->getRequiresClause(); |
| 3450 | |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3451 | TemplateParameterList *InstL |
| 3452 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), |
| David Majnemer | 902f8c6 | 2015-12-27 07:16:27 +0000 | [diff] [blame] | 3453 | L->getLAngleLoc(), Params, |
| Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 3454 | L->getRAngleLoc(), |
| 3455 | UninstantiatedRequiresClause); |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3456 | return InstL; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3457 | } |
| John McCall | 87a44eb | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 3458 | |
| Richard Smith | 5d33102 | 2018-03-08 01:07:33 +0000 | [diff] [blame] | 3459 | TemplateParameterList * |
| 3460 | Sema::SubstTemplateParams(TemplateParameterList *Params, DeclContext *Owner, |
| 3461 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3462 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
| 3463 | return Instantiator.SubstTemplateParams(Params); |
| 3464 | } |
| 3465 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3466 | /// Instantiate the declaration of a class template partial |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3467 | /// specialization. |
| 3468 | /// |
| 3469 | /// \param ClassTemplate the (instantiated) class template that is partially |
| 3470 | // specialized by the instantiation of \p PartialSpec. |
| 3471 | /// |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3472 | /// \param PartialSpec the (uninstantiated) class template partial |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3473 | /// specialization that we are instantiating. |
| 3474 | /// |
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 3475 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 3476 | /// NULL to indicate an error. |
| 3477 | ClassTemplatePartialSpecializationDecl * |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3478 | TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( |
| 3479 | ClassTemplateDecl *ClassTemplate, |
| 3480 | ClassTemplatePartialSpecializationDecl *PartialSpec) { |
| Douglas Gregor | 954de17 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 3481 | // Create a local instantiation scope for this class template partial |
| 3482 | // specialization, which will contain the instantiations of the template |
| 3483 | // parameters. |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3484 | LocalInstantiationScope Scope(SemaRef); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3485 | |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3486 | // Substitute into the template parameters of the class template partial |
| 3487 | // specialization. |
| 3488 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 3489 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 3490 | if (!InstParams) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3491 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3492 | |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3493 | // Substitute into the template arguments of the class template partial |
| 3494 | // specialization. |
| Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 3495 | const ASTTemplateArgumentListInfo *TemplArgInfo |
| 3496 | = PartialSpec->getTemplateArgsAsWritten(); |
| 3497 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, |
| 3498 | TemplArgInfo->RAngleLoc); |
| 3499 | if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), |
| 3500 | TemplArgInfo->NumTemplateArgs, |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 3501 | InstTemplateArgs, TemplateArgs)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3502 | return nullptr; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3503 | |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3504 | // Check that the template argument list is well-formed for this |
| 3505 | // class template. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3506 | SmallVector<TemplateArgument, 4> Converted; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3507 | if (SemaRef.CheckTemplateArgumentList(ClassTemplate, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3508 | PartialSpec->getLocation(), |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3509 | InstTemplateArgs, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3510 | false, |
| 3511 | Converted)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3512 | return nullptr; |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3513 | |
| Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3514 | // Check these arguments are valid for a template partial specialization. |
| 3515 | if (SemaRef.CheckTemplatePartialSpecializationArgs( |
| 3516 | PartialSpec->getLocation(), ClassTemplate, InstTemplateArgs.size(), |
| 3517 | Converted)) |
| 3518 | return nullptr; |
| 3519 | |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3520 | // Figure out where to insert this class template partial specialization |
| 3521 | // in the member template's set of class template partial specializations. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3522 | void *InsertPos = nullptr; |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3523 | ClassTemplateSpecializationDecl *PrevDecl |
| Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3524 | = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3525 | |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3526 | // Build the canonical type that describes the converted template |
| 3527 | // arguments of the class template partial specialization. |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3528 | QualType CanonType |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3529 | = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), |
| David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3530 | Converted); |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3531 | |
| 3532 | // Build the fully-sugared type for this class template |
| 3533 | // specialization as the user wrote in the specialization |
| 3534 | // itself. This means that we'll pretty-print the type retrieved |
| 3535 | // from the specialization's declaration the way that the user |
| 3536 | // actually wrote the specialization, rather than formatting the |
| 3537 | // name based on the "canonical" representation used to store the |
| 3538 | // template arguments in the specialization. |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3539 | TypeSourceInfo *WrittenTy |
| 3540 | = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 3541 | TemplateName(ClassTemplate), |
| 3542 | PartialSpec->getLocation(), |
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3543 | InstTemplateArgs, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3544 | CanonType); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3545 | |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3546 | if (PrevDecl) { |
| 3547 | // We've already seen a partial specialization with the same template |
| 3548 | // parameters and template arguments. This can happen, for example, when |
| 3549 | // substituting the outer template arguments ends up causing two |
| 3550 | // class template partial specializations of a member class template |
| 3551 | // to have identical forms, e.g., |
| 3552 | // |
| 3553 | // template<typename T, typename U> |
| 3554 | // struct Outer { |
| 3555 | // template<typename X, typename Y> struct Inner; |
| 3556 | // template<typename Y> struct Inner<T, Y>; |
| 3557 | // template<typename Y> struct Inner<U, Y>; |
| 3558 | // }; |
| 3559 | // |
| 3560 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 3561 | // // have the same signature. |
| 3562 | SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) |
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 3563 | << WrittenTy->getType(); |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3564 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) |
| 3565 | << SemaRef.Context.getTypeDeclType(PrevDecl); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3566 | return nullptr; |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3567 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3568 | |
| 3569 | |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3570 | // Create the class template partial specialization declaration. |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3571 | ClassTemplatePartialSpecializationDecl *InstPartialSpec = |
| 3572 | ClassTemplatePartialSpecializationDecl::Create( |
| 3573 | SemaRef.Context, PartialSpec->getTagKind(), Owner, |
| 3574 | PartialSpec->getBeginLoc(), PartialSpec->getLocation(), InstParams, |
| 3575 | ClassTemplate, Converted, InstTemplateArgs, CanonType, nullptr); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3576 | // Substitute the nested name specifier, if any. |
| 3577 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3578 | return nullptr; |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3579 | |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3580 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
| Douglas Gregor | 6044d69 | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 3581 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3582 | |
| Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3583 | // Check the completed partial specialization. |
| 3584 | SemaRef.CheckTemplatePartialSpecialization(InstPartialSpec); |
| 3585 | |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3586 | // Add this partial specialization to the set of class template partial |
| 3587 | // specializations. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3588 | ClassTemplate->AddPartialSpecialization(InstPartialSpec, |
| 3589 | /*InsertPos=*/nullptr); |
| Douglas Gregor | 869853e | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 3590 | return InstPartialSpec; |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3593 | /// Instantiate the declaration of a variable template partial |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3594 | /// specialization. |
| 3595 | /// |
| 3596 | /// \param VarTemplate the (instantiated) variable template that is partially |
| 3597 | /// specialized by the instantiation of \p PartialSpec. |
| 3598 | /// |
| 3599 | /// \param PartialSpec the (uninstantiated) variable template partial |
| 3600 | /// specialization that we are instantiating. |
| 3601 | /// |
| 3602 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 3603 | /// NULL to indicate an error. |
| 3604 | VarTemplatePartialSpecializationDecl * |
| 3605 | TemplateDeclInstantiator::InstantiateVarTemplatePartialSpecialization( |
| 3606 | VarTemplateDecl *VarTemplate, |
| 3607 | VarTemplatePartialSpecializationDecl *PartialSpec) { |
| 3608 | // Create a local instantiation scope for this variable template partial |
| 3609 | // specialization, which will contain the instantiations of the template |
| 3610 | // parameters. |
| 3611 | LocalInstantiationScope Scope(SemaRef); |
| 3612 | |
| 3613 | // Substitute into the template parameters of the variable template partial |
| 3614 | // specialization. |
| 3615 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 3616 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 3617 | if (!InstParams) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3618 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3619 | |
| 3620 | // Substitute into the template arguments of the variable template partial |
| 3621 | // specialization. |
| Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 3622 | const ASTTemplateArgumentListInfo *TemplArgInfo |
| 3623 | = PartialSpec->getTemplateArgsAsWritten(); |
| 3624 | TemplateArgumentListInfo InstTemplateArgs(TemplArgInfo->LAngleLoc, |
| 3625 | TemplArgInfo->RAngleLoc); |
| 3626 | if (SemaRef.Subst(TemplArgInfo->getTemplateArgs(), |
| 3627 | TemplArgInfo->NumTemplateArgs, |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3628 | InstTemplateArgs, TemplateArgs)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3629 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3630 | |
| 3631 | // Check that the template argument list is well-formed for this |
| 3632 | // class template. |
| 3633 | SmallVector<TemplateArgument, 4> Converted; |
| 3634 | if (SemaRef.CheckTemplateArgumentList(VarTemplate, PartialSpec->getLocation(), |
| 3635 | InstTemplateArgs, false, Converted)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3636 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3637 | |
| Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3638 | // Check these arguments are valid for a template partial specialization. |
| 3639 | if (SemaRef.CheckTemplatePartialSpecializationArgs( |
| 3640 | PartialSpec->getLocation(), VarTemplate, InstTemplateArgs.size(), |
| 3641 | Converted)) |
| 3642 | return nullptr; |
| 3643 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3644 | // Figure out where to insert this variable template partial specialization |
| 3645 | // in the member template's set of variable template partial specializations. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3646 | void *InsertPos = nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3647 | VarTemplateSpecializationDecl *PrevDecl = |
| Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 3648 | VarTemplate->findPartialSpecialization(Converted, InsertPos); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3649 | |
| 3650 | // Build the canonical type that describes the converted template |
| 3651 | // arguments of the variable template partial specialization. |
| 3652 | QualType CanonType = SemaRef.Context.getTemplateSpecializationType( |
| David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 3653 | TemplateName(VarTemplate), Converted); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3654 | |
| 3655 | // Build the fully-sugared type for this variable template |
| 3656 | // specialization as the user wrote in the specialization |
| 3657 | // itself. This means that we'll pretty-print the type retrieved |
| 3658 | // from the specialization's declaration the way that the user |
| 3659 | // actually wrote the specialization, rather than formatting the |
| 3660 | // name based on the "canonical" representation used to store the |
| 3661 | // template arguments in the specialization. |
| 3662 | TypeSourceInfo *WrittenTy = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 3663 | TemplateName(VarTemplate), PartialSpec->getLocation(), InstTemplateArgs, |
| 3664 | CanonType); |
| 3665 | |
| 3666 | if (PrevDecl) { |
| 3667 | // We've already seen a partial specialization with the same template |
| 3668 | // parameters and template arguments. This can happen, for example, when |
| 3669 | // substituting the outer template arguments ends up causing two |
| 3670 | // variable template partial specializations of a member variable template |
| 3671 | // to have identical forms, e.g., |
| 3672 | // |
| 3673 | // template<typename T, typename U> |
| 3674 | // struct Outer { |
| 3675 | // template<typename X, typename Y> pair<X,Y> p; |
| 3676 | // template<typename Y> pair<T, Y> p; |
| 3677 | // template<typename Y> pair<U, Y> p; |
| 3678 | // }; |
| 3679 | // |
| 3680 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 3681 | // // have the same signature. |
| 3682 | SemaRef.Diag(PartialSpec->getLocation(), |
| 3683 | diag::err_var_partial_spec_redeclared) |
| 3684 | << WrittenTy->getType(); |
| 3685 | SemaRef.Diag(PrevDecl->getLocation(), |
| 3686 | diag::note_var_prev_partial_spec_here); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3687 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | // Do substitution on the type of the declaration |
| 3691 | TypeSourceInfo *DI = SemaRef.SubstType( |
| 3692 | PartialSpec->getTypeSourceInfo(), TemplateArgs, |
| 3693 | PartialSpec->getTypeSpecStartLoc(), PartialSpec->getDeclName()); |
| 3694 | if (!DI) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3695 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3696 | |
| 3697 | if (DI->getType()->isFunctionType()) { |
| 3698 | SemaRef.Diag(PartialSpec->getLocation(), |
| 3699 | diag::err_variable_instantiates_to_function) |
| 3700 | << PartialSpec->isStaticDataMember() << DI->getType(); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3701 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | // Create the variable template partial specialization declaration. |
| 3705 | VarTemplatePartialSpecializationDecl *InstPartialSpec = |
| 3706 | VarTemplatePartialSpecializationDecl::Create( |
| 3707 | SemaRef.Context, Owner, PartialSpec->getInnerLocStart(), |
| 3708 | PartialSpec->getLocation(), InstParams, VarTemplate, DI->getType(), |
| David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3709 | DI, PartialSpec->getStorageClass(), Converted, InstTemplateArgs); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3710 | |
| 3711 | // Substitute the nested name specifier, if any. |
| 3712 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3713 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3714 | |
| 3715 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
| 3716 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
| 3717 | |
| Richard Smith | 57aae07 | 2016-12-28 02:37:25 +0000 | [diff] [blame] | 3718 | // Check the completed partial specialization. |
| 3719 | SemaRef.CheckTemplatePartialSpecialization(InstPartialSpec); |
| 3720 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3721 | // Add this partial specialization to the set of variable template partial |
| 3722 | // specializations. The instantiation of the initializer is not necessary. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3723 | VarTemplate->AddPartialSpecialization(InstPartialSpec, /*InsertPos=*/nullptr); |
| Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3724 | |
| Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3725 | SemaRef.BuildVariableInstantiation(InstPartialSpec, PartialSpec, TemplateArgs, |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 3726 | LateAttrs, Owner, StartingScope); |
| Larisse Voufo | 4cda461 | 2013-08-22 00:28:27 +0000 | [diff] [blame] | 3727 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 3728 | return InstPartialSpec; |
| 3729 | } |
| 3730 | |
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3731 | TypeSourceInfo* |
| John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3732 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3733 | SmallVectorImpl<ParmVarDecl *> &Params) { |
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3734 | TypeSourceInfo *OldTInfo = D->getTypeSourceInfo(); |
| 3735 | assert(OldTInfo && "substituting function without type source info"); |
| 3736 | assert(Params.empty() && "parameter vector is non-empty at start"); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3737 | |
| 3738 | CXXRecordDecl *ThisContext = nullptr; |
| Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 3739 | Qualifiers ThisTypeQuals; |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3740 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 3741 | ThisContext = cast<CXXRecordDecl>(Owner); |
| Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 3742 | ThisTypeQuals = Method->getMethodQualifiers(); |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3743 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3744 | |
| John McCall | b29f78f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 3745 | TypeSourceInfo *NewTInfo |
| 3746 | = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs, |
| 3747 | D->getTypeSpecStartLoc(), |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3748 | D->getDeclName(), |
| 3749 | ThisContext, ThisTypeQuals); |
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3750 | if (!NewTInfo) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3751 | return nullptr; |
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3752 | |
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3753 | TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); |
| 3754 | if (FunctionProtoTypeLoc OldProtoLoc = OldTL.getAs<FunctionProtoTypeLoc>()) { |
| 3755 | if (NewTInfo != OldTInfo) { |
| 3756 | // Get parameters from the new type info. |
| Abramo Bagnara | a44c902 | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 3757 | TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens(); |
| David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3758 | FunctionProtoTypeLoc NewProtoLoc = NewTL.castAs<FunctionProtoTypeLoc>(); |
| Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3759 | unsigned NewIdx = 0; |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3760 | for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc.getNumParams(); |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3761 | OldIdx != NumOldParams; ++OldIdx) { |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3762 | ParmVarDecl *OldParam = OldProtoLoc.getParam(OldIdx); |
| Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3763 | LocalInstantiationScope *Scope = SemaRef.CurrentInstantiationScope; |
| 3764 | |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3765 | Optional<unsigned> NumArgumentsInExpansion; |
| Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3766 | if (OldParam->isParameterPack()) |
| 3767 | NumArgumentsInExpansion = |
| 3768 | SemaRef.getNumArgumentsInExpansion(OldParam->getType(), |
| 3769 | TemplateArgs); |
| 3770 | if (!NumArgumentsInExpansion) { |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 3771 | // Simple case: normal parameter, or a parameter pack that's |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3772 | // instantiated to a (still-dependent) parameter pack. |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3773 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3774 | Params.push_back(NewParam); |
| Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3775 | Scope->InstantiatedLocal(OldParam, NewParam); |
| 3776 | } else { |
| 3777 | // Parameter pack expansion: make the instantiation an argument pack. |
| 3778 | Scope->MakeInstantiatedLocalArgPack(OldParam); |
| 3779 | for (unsigned I = 0; I != *NumArgumentsInExpansion; ++I) { |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3780 | ParmVarDecl *NewParam = NewProtoLoc.getParam(NewIdx++); |
| Richard Smith | 198223b | 2012-07-18 01:29:05 +0000 | [diff] [blame] | 3781 | Params.push_back(NewParam); |
| 3782 | Scope->InstantiatedLocalPackArg(OldParam, NewParam); |
| 3783 | } |
| Douglas Gregor | f301011 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 3784 | } |
| Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3785 | } |
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3786 | } else { |
| 3787 | // The function type itself was not dependent and therefore no |
| 3788 | // substitution occurred. However, we still need to instantiate |
| 3789 | // the function parameters themselves. |
| 3790 | const FunctionProtoType *OldProto = |
| 3791 | cast<FunctionProtoType>(OldProtoLoc.getType()); |
| Alp Toker | b3fd5cf | 2014-01-21 00:32:38 +0000 | [diff] [blame] | 3792 | for (unsigned i = 0, i_end = OldProtoLoc.getNumParams(); i != i_end; |
| 3793 | ++i) { |
| 3794 | ParmVarDecl *OldParam = OldProtoLoc.getParam(i); |
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3795 | if (!OldParam) { |
| 3796 | Params.push_back(SemaRef.BuildParmVarDeclForTypedef( |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3797 | D, D->getLocation(), OldProto->getParamType(i))); |
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3798 | continue; |
| 3799 | } |
| 3800 | |
| Eli Friedman | cb9cd6c | 2013-06-27 23:21:55 +0000 | [diff] [blame] | 3801 | ParmVarDecl *Parm = |
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3802 | cast_or_null<ParmVarDecl>(VisitParmVarDecl(OldParam)); |
| Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3803 | if (!Parm) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3804 | return nullptr; |
| Douglas Gregor | 95c70ec | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 3805 | Params.push_back(Parm); |
| 3806 | } |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 3807 | } |
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3808 | } else { |
| 3809 | // If the type of this function, after ignoring parentheses, is not |
| 3810 | // *directly* a function type, then we're instantiating a function that |
| 3811 | // was declared via a typedef or with attributes, e.g., |
| 3812 | // |
| 3813 | // typedef int functype(int, int); |
| 3814 | // functype func; |
| 3815 | // int __cdecl meth(int, int); |
| 3816 | // |
| 3817 | // In this case, we'll just go instantiate the ParmVarDecls that we |
| 3818 | // synthesized in the method declaration. |
| 3819 | SmallVector<QualType, 4> ParamTypes; |
| John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 3820 | Sema::ExtParameterInfoBuilder ExtParamInfos; |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3821 | if (SemaRef.SubstParmTypes(D->getLocation(), D->parameters(), nullptr, |
| 3822 | TemplateArgs, ParamTypes, &Params, |
| 3823 | ExtParamInfos)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3824 | return nullptr; |
| Douglas Gregor | 940bca7 | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 3825 | } |
| Reid Kleckner | a09e44c | 2013-07-31 21:00:18 +0000 | [diff] [blame] | 3826 | |
| John McCall | 58f10c3 | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 3827 | return NewTInfo; |
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 3828 | } |
| 3829 | |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3830 | /// Introduce the instantiated function parameters into the local |
| 3831 | /// instantiation scope, and set the parameter names to those used |
| 3832 | /// in the template. |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3833 | static bool addInstantiatedParametersToScope(Sema &S, FunctionDecl *Function, |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3834 | const FunctionDecl *PatternDecl, |
| 3835 | LocalInstantiationScope &Scope, |
| 3836 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3837 | unsigned FParamIdx = 0; |
| 3838 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) { |
| 3839 | const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I); |
| 3840 | if (!PatternParam->isParameterPack()) { |
| 3841 | // Simple case: not a parameter pack. |
| 3842 | assert(FParamIdx < Function->getNumParams()); |
| 3843 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3844 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
| Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3845 | // If the parameter's type is not dependent, update it to match the type |
| 3846 | // in the pattern. They can differ in top-level cv-qualifiers, and we want |
| 3847 | // the pattern's type here. If the type is dependent, they can't differ, |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3848 | // per core issue 1668. Substitute into the type from the pattern, in case |
| 3849 | // it's instantiation-dependent. |
| Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3850 | // FIXME: Updating the type to work around this is at best fragile. |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3851 | if (!PatternDecl->getType()->isDependentType()) { |
| 3852 | QualType T = S.SubstType(PatternParam->getType(), TemplateArgs, |
| 3853 | FunctionParam->getLocation(), |
| 3854 | FunctionParam->getDeclName()); |
| 3855 | if (T.isNull()) |
| 3856 | return true; |
| 3857 | FunctionParam->setType(T); |
| 3858 | } |
| Richard Smith | aae4058 | 2014-03-13 00:28:45 +0000 | [diff] [blame] | 3859 | |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3860 | Scope.InstantiatedLocal(PatternParam, FunctionParam); |
| 3861 | ++FParamIdx; |
| 3862 | continue; |
| 3863 | } |
| 3864 | |
| 3865 | // Expand the parameter pack. |
| 3866 | Scope.MakeInstantiatedLocalArgPack(PatternParam); |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 3867 | Optional<unsigned> NumArgumentsInExpansion |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3868 | = S.getNumArgumentsInExpansion(PatternParam->getType(), TemplateArgs); |
| Richard Trieu | ef1e06d | 2019-03-15 04:26:02 +0000 | [diff] [blame] | 3869 | if (NumArgumentsInExpansion) { |
| 3870 | QualType PatternType = |
| 3871 | PatternParam->getType()->castAs<PackExpansionType>()->getPattern(); |
| 3872 | for (unsigned Arg = 0; Arg < *NumArgumentsInExpansion; ++Arg) { |
| 3873 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
| 3874 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
| 3875 | if (!PatternDecl->getType()->isDependentType()) { |
| 3876 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(S, Arg); |
| 3877 | QualType T = S.SubstType(PatternType, TemplateArgs, |
| 3878 | FunctionParam->getLocation(), |
| 3879 | FunctionParam->getDeclName()); |
| 3880 | if (T.isNull()) |
| 3881 | return true; |
| 3882 | FunctionParam->setType(T); |
| 3883 | } |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3884 | |
| Richard Trieu | ef1e06d | 2019-03-15 04:26:02 +0000 | [diff] [blame] | 3885 | Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam); |
| 3886 | ++FParamIdx; |
| 3887 | } |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3888 | } |
| 3889 | } |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3890 | |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3891 | return false; |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | void Sema::InstantiateExceptionSpec(SourceLocation PointOfInstantiation, |
| 3895 | FunctionDecl *Decl) { |
| Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3896 | const FunctionProtoType *Proto = Decl->getType()->castAs<FunctionProtoType>(); |
| 3897 | if (Proto->getExceptionSpecType() != EST_Uninstantiated) |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3898 | return; |
| 3899 | |
| 3900 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Decl, |
| 3901 | InstantiatingTemplate::ExceptionSpecification()); |
| Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3902 | if (Inst.isInvalid()) { |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3903 | // We hit the instantiation depth limit. Clear the exception specification |
| 3904 | // so that our callers don't have to cope with EST_Uninstantiated. |
| Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3905 | UpdateExceptionSpec(Decl, EST_None); |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3906 | return; |
| Richard Smith | d3b5c908 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 3907 | } |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 3908 | if (Inst.isAlreadyInstantiating()) { |
| 3909 | // This exception specification indirectly depends on itself. Reject. |
| 3910 | // FIXME: Corresponding rule in the standard? |
| 3911 | Diag(PointOfInstantiation, diag::err_exception_spec_cycle) << Decl; |
| 3912 | UpdateExceptionSpec(Decl, EST_None); |
| 3913 | return; |
| 3914 | } |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3915 | |
| 3916 | // Enter the scope of this instantiation. We don't use |
| 3917 | // PushDeclContext because we don't have a scope. |
| 3918 | Sema::ContextRAII savedContext(*this, Decl); |
| 3919 | LocalInstantiationScope Scope(*this); |
| 3920 | |
| 3921 | MultiLevelTemplateArgumentList TemplateArgs = |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3922 | getTemplateInstantiationArgs(Decl, nullptr, /*RelativeToPrimary*/true); |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3923 | |
| Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3924 | FunctionDecl *Template = Proto->getExceptionSpecTemplate(); |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3925 | if (addInstantiatedParametersToScope(*this, Decl, Template, Scope, |
| 3926 | TemplateArgs)) { |
| 3927 | UpdateExceptionSpec(Decl, EST_None); |
| 3928 | return; |
| 3929 | } |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3930 | |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 3931 | SubstExceptionSpec(Decl, Template->getType()->castAs<FunctionProtoType>(), |
| 3932 | TemplateArgs); |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3933 | } |
| 3934 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3935 | /// Initializes the common fields of an instantiation function |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3936 | /// declaration (New) from the corresponding fields of its template (Tmpl). |
| 3937 | /// |
| 3938 | /// \returns true if there was an error |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3939 | bool |
| 3940 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3941 | FunctionDecl *Tmpl) { |
| David Blaikie | 5a0956e | 2012-07-16 18:50:45 +0000 | [diff] [blame] | 3942 | if (Tmpl->isDeleted()) |
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 3943 | New->setDeletedAsWritten(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3944 | |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3945 | New->setImplicit(Tmpl->isImplicit()); |
| 3946 | |
| David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 3947 | // Forward the mangling number from the template to the instantiated decl. |
| 3948 | SemaRef.Context.setManglingNumber(New, |
| 3949 | SemaRef.Context.getManglingNumber(Tmpl)); |
| 3950 | |
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3951 | // If we are performing substituting explicitly-specified template arguments |
| 3952 | // or deduced template arguments into a function template and we reach this |
| 3953 | // point, we are now past the point where SFINAE applies and have committed |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3954 | // to keeping the new function template specialization. We therefore |
| 3955 | // convert the active template instantiation for the function template |
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3956 | // into a template instantiation for this specific function template |
| 3957 | // specialization, which is not a SFINAE context, so that we diagnose any |
| 3958 | // further errors in the declaration itself. |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 3959 | typedef Sema::CodeSynthesisContext ActiveInstType; |
| 3960 | ActiveInstType &ActiveInst = SemaRef.CodeSynthesisContexts.back(); |
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3961 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || |
| 3962 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3963 | if (FunctionTemplateDecl *FunTmpl |
| Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 3964 | = dyn_cast<FunctionTemplateDecl>(ActiveInst.Entity)) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3965 | assert(FunTmpl->getTemplatedDecl() == Tmpl && |
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3966 | "Deduction from the wrong function template?"); |
| Daniel Dunbar | 54c5964 | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 3967 | (void) FunTmpl; |
| Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 3968 | atTemplateEnd(SemaRef.TemplateInstCallbacks, SemaRef, ActiveInst); |
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3969 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; |
| Nick Lewycky | cc8990f | 2012-11-16 08:40:59 +0000 | [diff] [blame] | 3970 | ActiveInst.Entity = New; |
| Gabor Horvath | 207e7b1 | 2018-02-10 14:04:45 +0000 | [diff] [blame] | 3971 | atTemplateBegin(SemaRef.TemplateInstCallbacks, SemaRef, ActiveInst); |
| Douglas Gregor | ff6cbdf | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 3972 | } |
| 3973 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3974 | |
| Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 3975 | const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); |
| 3976 | assert(Proto && "Function template without prototype?"); |
| 3977 | |
| Sebastian Redl | fa453cf | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 3978 | if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) { |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3979 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 3980 | |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3981 | // DR1330: In C++11, defer instantiation of a non-trivial |
| 3982 | // exception specification. |
| Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3983 | // DR1484: Local classes and their members are instantiated along with the |
| 3984 | // containing function. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3985 | if (SemaRef.getLangOpts().CPlusPlus11 && |
| Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3986 | EPI.ExceptionSpec.Type != EST_None && |
| 3987 | EPI.ExceptionSpec.Type != EST_DynamicNone && |
| Serge Pavlov | 3739f5e7 | 2015-06-29 17:50:19 +0000 | [diff] [blame] | 3988 | EPI.ExceptionSpec.Type != EST_BasicNoexcept && |
| Serge Pavlov | 73c6a24 | 2015-08-23 10:22:28 +0000 | [diff] [blame] | 3989 | !Tmpl->isLexicallyWithinFunctionOrMethod()) { |
| Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3990 | FunctionDecl *ExceptionSpecTemplate = Tmpl; |
| Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3991 | if (EPI.ExceptionSpec.Type == EST_Uninstantiated) |
| 3992 | ExceptionSpecTemplate = EPI.ExceptionSpec.SourceTemplate; |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 3993 | ExceptionSpecificationType NewEST = EST_Uninstantiated; |
| Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 3994 | if (EPI.ExceptionSpec.Type == EST_Unevaluated) |
| Richard Smith | 185be18 | 2013-04-10 05:48:59 +0000 | [diff] [blame] | 3995 | NewEST = EST_Unevaluated; |
| Richard Smith | d372942 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 3996 | |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 3997 | // Mark the function has having an uninstantiated exception specification. |
| 3998 | const FunctionProtoType *NewProto |
| 3999 | = New->getType()->getAs<FunctionProtoType>(); |
| 4000 | assert(NewProto && "Template instantiation without function prototype?"); |
| 4001 | EPI = NewProto->getExtProtoInfo(); |
| Richard Smith | 8acb428 | 2014-07-31 21:57:55 +0000 | [diff] [blame] | 4002 | EPI.ExceptionSpec.Type = NewEST; |
| 4003 | EPI.ExceptionSpec.SourceDecl = New; |
| 4004 | EPI.ExceptionSpec.SourceTemplate = ExceptionSpecTemplate; |
| Reid Kleckner | 896b32f | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 4005 | New->setType(SemaRef.Context.getFunctionType( |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4006 | NewProto->getReturnType(), NewProto->getParamTypes(), EPI)); |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 4007 | } else { |
| Faisal Vali | 40fd4ce | 2017-05-09 04:17:15 +0000 | [diff] [blame] | 4008 | Sema::ContextRAII SwitchContext(SemaRef, New); |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 4009 | SemaRef.SubstExceptionSpec(New, Proto, TemplateArgs); |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 4010 | } |
| Douglas Gregor | 049bdca | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
| Rafael Espindola | ba195cf | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 4013 | // Get the definition. Leaves the variable unchanged if undefined. |
| Richard Smith | f623c96 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 4014 | const FunctionDecl *Definition = Tmpl; |
| Rafael Espindola | ba195cf | 2011-07-06 15:46:09 +0000 | [diff] [blame] | 4015 | Tmpl->isDefined(Definition); |
| 4016 | |
| DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 4017 | SemaRef.InstantiateAttrs(TemplateArgs, Definition, New, |
| 4018 | LateAttrs, StartingScope); |
| Douglas Gregor | 0832963 | 2010-06-15 17:05:35 +0000 | [diff] [blame] | 4019 | |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4020 | return false; |
| 4021 | } |
| 4022 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4023 | /// Initializes common fields of an instantiated method |
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 4024 | /// declaration (New) from the corresponding fields of its template |
| 4025 | /// (Tmpl). |
| 4026 | /// |
| 4027 | /// \returns true if there was an error |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4028 | bool |
| 4029 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 4030 | CXXMethodDecl *Tmpl) { |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4031 | if (InitFunctionInstantiation(New, Tmpl)) |
| 4032 | return true; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4033 | |
| Richard Smith | 5159bbad | 2018-09-05 22:30:37 +0000 | [diff] [blame] | 4034 | if (isa<CXXDestructorDecl>(New) && SemaRef.getLangOpts().CPlusPlus11) |
| 4035 | SemaRef.AdjustDestructorExceptionSpec(cast<CXXDestructorDecl>(New)); |
| 4036 | |
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 4037 | New->setAccess(Tmpl->getAccess()); |
| Fariborz Jahanian | 6dfc197 | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 4038 | if (Tmpl->isVirtualAsWritten()) |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 4039 | New->setVirtualAsWritten(true); |
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 4040 | |
| Douglas Gregor | 2134209 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 4041 | // FIXME: New needs a pointer to Tmpl |
| 4042 | return false; |
| 4043 | } |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4044 | |
| Richard Smith | 50e291e | 2018-01-02 23:52:42 +0000 | [diff] [blame] | 4045 | /// Instantiate (or find existing instantiation of) a function template with a |
| 4046 | /// given set of template arguments. |
| 4047 | /// |
| 4048 | /// Usually this should not be used, and template argument deduction should be |
| 4049 | /// used in its place. |
| 4050 | FunctionDecl * |
| 4051 | Sema::InstantiateFunctionDeclaration(FunctionTemplateDecl *FTD, |
| 4052 | const TemplateArgumentList *Args, |
| 4053 | SourceLocation Loc) { |
| 4054 | FunctionDecl *FD = FTD->getTemplatedDecl(); |
| 4055 | |
| 4056 | sema::TemplateDeductionInfo Info(Loc); |
| 4057 | InstantiatingTemplate Inst( |
| 4058 | *this, Loc, FTD, Args->asArray(), |
| 4059 | CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); |
| 4060 | if (Inst.isInvalid()) |
| 4061 | return nullptr; |
| 4062 | |
| 4063 | ContextRAII SavedContext(*this, FD); |
| 4064 | MultiLevelTemplateArgumentList MArgs(*Args); |
| 4065 | |
| 4066 | return cast_or_null<FunctionDecl>(SubstDecl(FD, FD->getParent(), MArgs)); |
| 4067 | } |
| 4068 | |
| Reid Kleckner | 61195e1 | 2017-01-05 01:08:22 +0000 | [diff] [blame] | 4069 | /// In the MS ABI, we need to instantiate default arguments of dllexported |
| 4070 | /// default constructors along with the constructor definition. This allows IR |
| 4071 | /// gen to emit a constructor closure which calls the default constructor with |
| 4072 | /// its default arguments. |
| 4073 | static void InstantiateDefaultCtorDefaultArgs(Sema &S, |
| 4074 | CXXConstructorDecl *Ctor) { |
| 4075 | assert(S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 4076 | Ctor->isDefaultConstructor()); |
| 4077 | unsigned NumParams = Ctor->getNumParams(); |
| 4078 | if (NumParams == 0) |
| 4079 | return; |
| 4080 | DLLExportAttr *Attr = Ctor->getAttr<DLLExportAttr>(); |
| 4081 | if (!Attr) |
| 4082 | return; |
| 4083 | for (unsigned I = 0; I != NumParams; ++I) { |
| 4084 | (void)S.CheckCXXDefaultArgExpr(Attr->getLocation(), Ctor, |
| 4085 | Ctor->getParamDecl(I)); |
| 4086 | S.DiscardCleanupsInEvaluationContext(); |
| 4087 | } |
| 4088 | } |
| 4089 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4090 | /// Instantiate the definition of the given function from its |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4091 | /// template. |
| 4092 | /// |
| Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 4093 | /// \param PointOfInstantiation the point at which the instantiation was |
| 4094 | /// required. Note that this is not precisely a "point of instantiation" |
| 4095 | /// for the function, but it's close. |
| 4096 | /// |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4097 | /// \param Function the already-instantiated declaration of a |
| Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 4098 | /// function template specialization or member function of a class template |
| 4099 | /// specialization. |
| 4100 | /// |
| 4101 | /// \param Recursive if true, recursively instantiates any functions that |
| 4102 | /// are required by this instantiation. |
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4103 | /// |
| 4104 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 4105 | /// instantiation where the body of the function is required. Complain if |
| 4106 | /// there is no such body. |
| Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 4107 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
| Douglas Gregor | dda7ced | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 4108 | FunctionDecl *Function, |
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4109 | bool Recursive, |
| Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 4110 | bool DefinitionRequired, |
| 4111 | bool AtEndOfTU) { |
| Richard Smith | cb18957 | 2017-10-28 01:15:00 +0000 | [diff] [blame] | 4112 | if (Function->isInvalidDecl() || Function->isDefined() || |
| 4113 | isa<CXXDeductionGuideDecl>(Function)) |
| Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 4114 | return; |
| 4115 | |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 4116 | // Never instantiate an explicit specialization except if it is a class scope |
| 4117 | // explicit specialization. |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 4118 | TemplateSpecializationKind TSK = |
| 4119 | Function->getTemplateSpecializationKindForInstantiation(); |
| 4120 | if (TSK == TSK_ExplicitSpecialization) |
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4121 | return; |
| Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 4122 | |
| Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 4123 | // Find the function body that we'll be substituting. |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 4124 | const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); |
| Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 4125 | assert(PatternDecl && "instantiating a non-template"); |
| 4126 | |
| Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4127 | const FunctionDecl *PatternDef = PatternDecl->getDefinition(); |
| Richard Smith | 3f6865a8 | 2016-08-23 21:12:54 +0000 | [diff] [blame] | 4128 | Stmt *Pattern = nullptr; |
| 4129 | if (PatternDef) { |
| 4130 | Pattern = PatternDef->getBody(PatternDef); |
| Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4131 | PatternDecl = PatternDef; |
| Richard Smith | 6c716116 | 2017-08-12 01:46:03 +0000 | [diff] [blame] | 4132 | if (PatternDef->willHaveBody()) |
| 4133 | PatternDef = nullptr; |
| Richard Smith | 3f6865a8 | 2016-08-23 21:12:54 +0000 | [diff] [blame] | 4134 | } |
| Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 4135 | |
| Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 4136 | // FIXME: We need to track the instantiation stack in order to know which |
| 4137 | // definitions should be visible within this instantiation. |
| 4138 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Function, |
| 4139 | Function->getInstantiatedFromMemberFunction(), |
| Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4140 | PatternDecl, PatternDef, TSK, |
| 4141 | /*Complain*/DefinitionRequired)) { |
| 4142 | if (DefinitionRequired) |
| 4143 | Function->setInvalidDecl(); |
| 4144 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 4145 | // Try again at the end of the translation unit (at which point a |
| 4146 | // definition will be required). |
| 4147 | assert(!Recursive); |
| Sunil Srivastava | 15ed292 | 2017-06-20 22:08:44 +0000 | [diff] [blame] | 4148 | Function->setInstantiationIsPending(true); |
| Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4149 | PendingInstantiations.push_back( |
| 4150 | std::make_pair(Function, PointOfInstantiation)); |
| 4151 | } else if (TSK == TSK_ImplicitInstantiation) { |
| Nick Lewycky | 2adab1b | 2018-01-02 19:10:12 +0000 | [diff] [blame] | 4152 | if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() && |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4153 | !getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) { |
| Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4154 | Diag(PointOfInstantiation, diag::warn_func_template_missing) |
| 4155 | << Function; |
| 4156 | Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); |
| 4157 | if (getLangOpts().CPlusPlus11) |
| 4158 | Diag(PointOfInstantiation, diag::note_inst_declaration_hint) |
| 4159 | << Function; |
| 4160 | } |
| 4161 | } |
| Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 4162 | |
| Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4163 | return; |
| 4164 | } |
| Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 4165 | |
| Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 4166 | // Postpone late parsed template instantiations. |
| Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 4167 | if (PatternDecl->isLateTemplateParsed() && |
| Nick Lewycky | 610128e | 2011-05-12 03:51:24 +0000 | [diff] [blame] | 4168 | !LateTemplateParser) { |
| Sunil Srivastava | 15ed292 | 2017-06-20 22:08:44 +0000 | [diff] [blame] | 4169 | Function->setInstantiationIsPending(true); |
| Reid Kleckner | 24bd88c | 2018-03-26 18:22:47 +0000 | [diff] [blame] | 4170 | LateParsedInstantiations.push_back( |
| 4171 | std::make_pair(Function, PointOfInstantiation)); |
| Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 4172 | return; |
| 4173 | } |
| 4174 | |
| Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 4175 | llvm::TimeTraceScope TimeScope("InstantiateFunction", [&]() { |
| Lubos Lunak | ab8cde4 | 2019-05-12 10:39:21 +0000 | [diff] [blame] | 4176 | std::string Name; |
| 4177 | llvm::raw_string_ostream OS(Name); |
| 4178 | Function->getNameForDiagnostic(OS, getPrintingPolicy(), |
| 4179 | /*Qualified=*/true); |
| 4180 | return Name; |
| Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 4181 | }); |
| 4182 | |
| Nico Weber | ae4bb8c | 2014-08-15 23:21:41 +0000 | [diff] [blame] | 4183 | // If we're performing recursive template instantiation, create our own |
| 4184 | // queue of pending implicit instantiations that we will instantiate later, |
| 4185 | // while we're still within our own instantiation context. |
| 4186 | // This has to happen before LateTemplateParser below is called, so that |
| 4187 | // it marks vtables used in late parsed templates as used. |
| Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4188 | GlobalEagerInstantiationScope GlobalInstantiations(*this, |
| 4189 | /*Enabled=*/Recursive); |
| 4190 | LocalEagerInstantiationScope LocalInstantiations(*this); |
| Nico Weber | ae4bb8c | 2014-08-15 23:21:41 +0000 | [diff] [blame] | 4191 | |
| David Majnemer | f0a84f2 | 2013-08-16 08:29:13 +0000 | [diff] [blame] | 4192 | // Call the LateTemplateParser callback if there is a need to late parse |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4193 | // a templated function definition. |
| Alexis Hunt | 23f6b83 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 4194 | if (!Pattern && PatternDecl->isLateTemplateParsed() && |
| Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 4195 | LateTemplateParser) { |
| Richard Smith | e40f2ba | 2013-08-07 21:41:30 +0000 | [diff] [blame] | 4196 | // FIXME: Optimize to allow individual templates to be deserialized. |
| 4197 | if (PatternDecl->isFromASTFile()) |
| 4198 | ExternalSource->ReadLateParsedTemplates(LateParsedTemplateMap); |
| 4199 | |
| Justin Lebar | 28f09c5 | 2016-10-10 16:26:08 +0000 | [diff] [blame] | 4200 | auto LPTIter = LateParsedTemplateMap.find(PatternDecl); |
| 4201 | assert(LPTIter != LateParsedTemplateMap.end() && |
| 4202 | "missing LateParsedTemplate"); |
| 4203 | LateTemplateParser(OpaqueParser, *LPTIter->second); |
| Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 4204 | Pattern = PatternDecl->getBody(PatternDecl); |
| 4205 | } |
| 4206 | |
| Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4207 | // Note, we should never try to instantiate a deleted function template. |
| Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4208 | assert((Pattern || PatternDecl->isDefaulted() || |
| 4209 | PatternDecl->hasSkippedBody()) && |
| Richard Smith | 6f4e2e0 | 2016-08-23 19:41:39 +0000 | [diff] [blame] | 4210 | "unexpected kind of function template definition"); |
| Douglas Gregor | 24c332b | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 4211 | |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4212 | // C++1y [temp.explicit]p10: |
| 4213 | // Except for inline functions, declarations with types deduced from their |
| 4214 | // initializer or return value, and class template specializations, other |
| 4215 | // explicit instantiation declarations have the effect of suppressing the |
| 4216 | // implicit instantiation of the entity to which they refer. |
| Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 4217 | if (TSK == TSK_ExplicitInstantiationDeclaration && |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4218 | !PatternDecl->isInlined() && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4219 | !PatternDecl->getReturnType()->getContainedAutoType()) |
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 4220 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4221 | |
| Richard Smith | 195d8ef | 2014-05-29 03:15:31 +0000 | [diff] [blame] | 4222 | if (PatternDecl->isInlined()) { |
| 4223 | // Function, and all later redeclarations of it (from imported modules, |
| 4224 | // for instance), are now implicitly inline. |
| 4225 | for (auto *D = Function->getMostRecentDecl(); /**/; |
| 4226 | D = D->getPreviousDecl()) { |
| 4227 | D->setImplicitlyInline(); |
| 4228 | if (D == Function) |
| 4229 | break; |
| 4230 | } |
| 4231 | } |
| Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 4232 | |
| Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 4233 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4234 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4235 | return; |
| Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 4236 | PrettyDeclStackTraceEntry CrashInfo(Context, Function, SourceLocation(), |
| Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 4237 | "instantiating function definition"); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4238 | |
| Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 4239 | // The instantiation is visible here, even if it was first declared in an |
| 4240 | // unimported module. |
| Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 4241 | Function->setVisibleDespiteOwningModule(); |
| Vassil Vassilev | b21ee08 | 2016-08-18 22:01:25 +0000 | [diff] [blame] | 4242 | |
| Abramo Bagnara | 12dcbf3 | 2011-11-18 08:08:52 +0000 | [diff] [blame] | 4243 | // Copy the inner loc start from the pattern. |
| 4244 | Function->setInnerLocStart(PatternDecl->getInnerLocStart()); |
| 4245 | |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4246 | EnterExpressionEvaluationContext EvalContext( |
| 4247 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated); |
| Douglas Gregor | 67da0d9 | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 4248 | |
| Douglas Gregor | b485046 | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 4249 | // Introduce a new scope where local variable instantiations will be |
| Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4250 | // recorded, unless we're actually a member function within a local |
| 4251 | // class, in which case we need to merge our results with the parent |
| 4252 | // scope (of the enclosing function). |
| 4253 | bool MergeWithParentScope = false; |
| 4254 | if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext())) |
| 4255 | MergeWithParentScope = Rec->isLocalClass(); |
| 4256 | |
| 4257 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4258 | |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4259 | if (PatternDecl->isDefaulted()) |
| Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 4260 | SetDeclDefaulted(Function, PatternDecl->getLocation()); |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4261 | else { |
| Richard Smith | cc92866 | 2014-10-17 20:37:29 +0000 | [diff] [blame] | 4262 | MultiLevelTemplateArgumentList TemplateArgs = |
| 4263 | getTemplateInstantiationArgs(Function, nullptr, false, PatternDecl); |
| 4264 | |
| 4265 | // Substitute into the qualifier; we can get a substitution failure here |
| 4266 | // through evil use of alias templates. |
| 4267 | // FIXME: Is CurContext correct for this? Should we go to the (instantiation |
| 4268 | // of the) lexical context of the pattern? |
| 4269 | SubstQualifier(*this, PatternDecl, Function, TemplateArgs); |
| 4270 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4271 | ActOnStartOfFunctionDef(nullptr, Function); |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4272 | |
| 4273 | // Enter the scope of this instantiation. We don't use |
| 4274 | // PushDeclContext because we don't have a scope. |
| 4275 | Sema::ContextRAII savedContext(*this, Function); |
| 4276 | |
| Richard Smith | 2e32155 | 2014-11-12 02:00:47 +0000 | [diff] [blame] | 4277 | if (addInstantiatedParametersToScope(*this, Function, PatternDecl, Scope, |
| 4278 | TemplateArgs)) |
| 4279 | return; |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4280 | |
| Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 4281 | StmtResult Body; |
| Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4282 | if (PatternDecl->hasSkippedBody()) { |
| 4283 | ActOnSkippedFunctionBody(Function); |
| Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 4284 | Body = nullptr; |
| Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4285 | } else { |
| Ilya Biryukov | 95f0d32 | 2017-12-28 13:05:46 +0000 | [diff] [blame] | 4286 | if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(Function)) { |
| 4287 | // If this is a constructor, instantiate the member initializers. |
| 4288 | InstantiateMemInitializers(Ctor, cast<CXXConstructorDecl>(PatternDecl), |
| 4289 | TemplateArgs); |
| 4290 | |
| 4291 | // If this is an MS ABI dllexport default constructor, instantiate any |
| 4292 | // default arguments. |
| 4293 | if (Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 4294 | Ctor->isDefaultConstructor()) { |
| 4295 | InstantiateDefaultCtorDefaultArgs(*this, Ctor); |
| 4296 | } |
| 4297 | } |
| 4298 | |
| Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4299 | // Instantiate the function body. |
| Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 4300 | Body = SubstStmt(Pattern, TemplateArgs); |
| Alexis Hunt | 61ae8d3 | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 4301 | |
| Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4302 | if (Body.isInvalid()) |
| 4303 | Function->setInvalidDecl(); |
| Ilya Biryukov | a27eca2 | 2017-12-20 14:32:38 +0000 | [diff] [blame] | 4304 | } |
| Ilya Biryukov | 0ee4a08 | 2018-03-14 13:18:30 +0000 | [diff] [blame] | 4305 | // FIXME: finishing the function body while in an expression evaluation |
| 4306 | // context seems wrong. Investigate more. |
| 4307 | ActOnFinishFunctionBody(Function, Body.get(), /*IsInstantiation=*/true); |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4308 | |
| 4309 | PerformDependentDiagnostics(PatternDecl, TemplateArgs); |
| 4310 | |
| Richard Smith | d28ac5b | 2014-03-22 23:33:22 +0000 | [diff] [blame] | 4311 | if (auto *Listener = getASTMutationListener()) |
| 4312 | Listener->FunctionDefinitionInstantiated(Function); |
| Richard Smith | 0ac1b8f | 2014-03-22 01:43:32 +0000 | [diff] [blame] | 4313 | |
| Richard Smith | bd30512 | 2012-12-11 01:14:52 +0000 | [diff] [blame] | 4314 | savedContext.pop(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4315 | } |
| 4316 | |
| Douglas Gregor | 28ad4b5 | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 4317 | DeclGroupRef DG(Function); |
| 4318 | Consumer.HandleTopLevelDecl(DG); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4319 | |
| Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4320 | // This class may have local implicit instantiations that need to be |
| 4321 | // instantiation within this scope. |
| Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4322 | LocalInstantiations.perform(); |
| Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 4323 | Scope.Exit(); |
| Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4324 | GlobalInstantiations.perform(); |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4325 | } |
| 4326 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4327 | VarTemplateSpecializationDecl *Sema::BuildVarTemplateInstantiation( |
| 4328 | VarTemplateDecl *VarTemplate, VarDecl *FromVar, |
| 4329 | const TemplateArgumentList &TemplateArgList, |
| 4330 | const TemplateArgumentListInfo &TemplateArgsInfo, |
| 4331 | SmallVectorImpl<TemplateArgument> &Converted, |
| 4332 | SourceLocation PointOfInstantiation, void *InsertPos, |
| 4333 | LateInstantiatedAttrVec *LateAttrs, |
| 4334 | LocalInstantiationScope *StartingScope) { |
| 4335 | if (FromVar->isInvalidDecl()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4336 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4337 | |
| 4338 | InstantiatingTemplate Inst(*this, PointOfInstantiation, FromVar); |
| Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 4339 | if (Inst.isInvalid()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4340 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4341 | |
| 4342 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4343 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgList); |
| 4344 | |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4345 | // Instantiate the first declaration of the variable template: for a partial |
| 4346 | // specialization of a static data member template, the first declaration may |
| 4347 | // or may not be the declaration in the class; if it's in the class, we want |
| 4348 | // to instantiate a member in the class (a declaration), and if it's outside, |
| 4349 | // we want to instantiate a definition. |
| Richard Smith | beef345 | 2014-01-16 23:39:20 +0000 | [diff] [blame] | 4350 | // |
| 4351 | // If we're instantiating an explicitly-specialized member template or member |
| 4352 | // partial specialization, don't do this. The member specialization completely |
| 4353 | // replaces the original declaration in this case. |
| 4354 | bool IsMemberSpec = false; |
| 4355 | if (VarTemplatePartialSpecializationDecl *PartialSpec = |
| 4356 | dyn_cast<VarTemplatePartialSpecializationDecl>(FromVar)) |
| 4357 | IsMemberSpec = PartialSpec->isMemberSpecialization(); |
| 4358 | else if (VarTemplateDecl *FromTemplate = FromVar->getDescribedVarTemplate()) |
| 4359 | IsMemberSpec = FromTemplate->isMemberSpecialization(); |
| 4360 | if (!IsMemberSpec) |
| 4361 | FromVar = FromVar->getFirstDecl(); |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4362 | |
| Manuel Klimek | 5843add | 2013-09-30 13:29:01 +0000 | [diff] [blame] | 4363 | MultiLevelTemplateArgumentList MultiLevelList(TemplateArgList); |
| 4364 | TemplateDeclInstantiator Instantiator(*this, FromVar->getDeclContext(), |
| 4365 | MultiLevelList); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4366 | |
| 4367 | // TODO: Set LateAttrs and StartingScope ... |
| 4368 | |
| 4369 | return cast_or_null<VarTemplateSpecializationDecl>( |
| 4370 | Instantiator.VisitVarTemplateSpecializationDecl( |
| 4371 | VarTemplate, FromVar, InsertPos, TemplateArgsInfo, Converted)); |
| 4372 | } |
| 4373 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4374 | /// Instantiates a variable template specialization by completing it |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4375 | /// with appropriate type information and initializer. |
| 4376 | VarTemplateSpecializationDecl *Sema::CompleteVarTemplateSpecializationDecl( |
| 4377 | VarTemplateSpecializationDecl *VarSpec, VarDecl *PatternDecl, |
| 4378 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| Richard Smith | 435e647 | 2017-12-02 02:48:42 +0000 | [diff] [blame] | 4379 | assert(PatternDecl->isThisDeclarationADefinition() && |
| 4380 | "don't have a definition to instantiate from"); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4381 | |
| 4382 | // Do substitution on the type of the declaration |
| 4383 | TypeSourceInfo *DI = |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4384 | SubstType(PatternDecl->getTypeSourceInfo(), TemplateArgs, |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4385 | PatternDecl->getTypeSpecStartLoc(), PatternDecl->getDeclName()); |
| 4386 | if (!DI) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4387 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4388 | |
| 4389 | // Update the type of this variable template specialization. |
| 4390 | VarSpec->setType(DI->getType()); |
| 4391 | |
| Richard Smith | 435e647 | 2017-12-02 02:48:42 +0000 | [diff] [blame] | 4392 | // Convert the declaration into a definition now. |
| 4393 | VarSpec->setCompleteDefinition(); |
| 4394 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4395 | // Instantiate the initializer. |
| 4396 | InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs); |
| 4397 | |
| 4398 | return VarSpec; |
| 4399 | } |
| 4400 | |
| 4401 | /// BuildVariableInstantiation - Used after a new variable has been created. |
| 4402 | /// Sets basic variable data and decides whether to postpone the |
| 4403 | /// variable instantiation. |
| 4404 | void Sema::BuildVariableInstantiation( |
| 4405 | VarDecl *NewVar, VarDecl *OldVar, |
| 4406 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4407 | LateInstantiatedAttrVec *LateAttrs, DeclContext *Owner, |
| 4408 | LocalInstantiationScope *StartingScope, |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 4409 | bool InstantiatingVarTemplate, |
| 4410 | VarTemplateSpecializationDecl *PrevDeclForVarTemplateSpecialization) { |
| 4411 | // Instantiating a partial specialization to produce a partial |
| 4412 | // specialization. |
| 4413 | bool InstantiatingVarTemplatePartialSpec = |
| 4414 | isa<VarTemplatePartialSpecializationDecl>(OldVar) && |
| 4415 | isa<VarTemplatePartialSpecializationDecl>(NewVar); |
| 4416 | // Instantiating from a variable template (or partial specialization) to |
| 4417 | // produce a variable template specialization. |
| 4418 | bool InstantiatingSpecFromTemplate = |
| 4419 | isa<VarTemplateSpecializationDecl>(NewVar) && |
| 4420 | (OldVar->getDescribedVarTemplate() || |
| 4421 | isa<VarTemplatePartialSpecializationDecl>(OldVar)); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4422 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4423 | // If we are instantiating a local extern declaration, the |
| 4424 | // instantiation belongs lexically to the containing function. |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4425 | // If we are instantiating a static data member defined |
| 4426 | // out-of-line, the instantiation will have the same lexical |
| 4427 | // context (which will be a namespace scope) as the template. |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4428 | if (OldVar->isLocalExternDecl()) { |
| 4429 | NewVar->setLocalExternDecl(); |
| 4430 | NewVar->setLexicalDeclContext(Owner); |
| 4431 | } else if (OldVar->isOutOfLine()) |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4432 | NewVar->setLexicalDeclContext(OldVar->getLexicalDeclContext()); |
| 4433 | NewVar->setTSCSpec(OldVar->getTSCSpec()); |
| 4434 | NewVar->setInitStyle(OldVar->getInitStyle()); |
| 4435 | NewVar->setCXXForRangeDecl(OldVar->isCXXForRangeDecl()); |
| George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 4436 | NewVar->setObjCForDecl(OldVar->isObjCForDecl()); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4437 | NewVar->setConstexpr(OldVar->isConstexpr()); |
| Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4438 | NewVar->setInitCapture(OldVar->isInitCapture()); |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4439 | NewVar->setPreviousDeclInSameBlockScope( |
| 4440 | OldVar->isPreviousDeclInSameBlockScope()); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4441 | NewVar->setAccess(OldVar->getAccess()); |
| 4442 | |
| Richard Smith | 0b55119 | 2013-09-23 23:12:22 +0000 | [diff] [blame] | 4443 | if (!OldVar->isStaticDataMember()) { |
| Rafael Espindola | e4865d2 | 2013-10-23 16:46:34 +0000 | [diff] [blame] | 4444 | if (OldVar->isUsed(false)) |
| 4445 | NewVar->setIsUsed(); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4446 | NewVar->setReferenced(OldVar->isReferenced()); |
| 4447 | } |
| 4448 | |
| 4449 | InstantiateAttrs(TemplateArgs, OldVar, NewVar, LateAttrs, StartingScope); |
| 4450 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4451 | LookupResult Previous( |
| 4452 | *this, NewVar->getDeclName(), NewVar->getLocation(), |
| 4453 | NewVar->isLocalExternDecl() ? Sema::LookupRedeclarationWithLinkage |
| 4454 | : Sema::LookupOrdinaryName, |
| Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 4455 | NewVar->isLocalExternDecl() ? Sema::ForExternalRedeclaration |
| 4456 | : forRedeclarationInCurContext()); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4457 | |
| Argyrios Kyrtzidis | 9148622 | 2013-11-27 08:34:14 +0000 | [diff] [blame] | 4458 | if (NewVar->isLocalExternDecl() && OldVar->getPreviousDecl() && |
| 4459 | (!OldVar->getPreviousDecl()->getDeclContext()->isDependentContext() || |
| 4460 | OldVar->getPreviousDecl()->getDeclContext()==OldVar->getDeclContext())) { |
| Richard Smith | 1c34fb7 | 2013-08-13 18:18:50 +0000 | [diff] [blame] | 4461 | // We have a previous declaration. Use that one, so we merge with the |
| 4462 | // right type. |
| 4463 | if (NamedDecl *NewPrev = FindInstantiatedDecl( |
| 4464 | NewVar->getLocation(), OldVar->getPreviousDecl(), TemplateArgs)) |
| 4465 | Previous.addDecl(NewPrev); |
| 4466 | } else if (!isa<VarTemplateSpecializationDecl>(NewVar) && |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 4467 | OldVar->hasLinkage()) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4468 | LookupQualifiedName(Previous, NewVar->getDeclContext(), false); |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 4469 | } else if (PrevDeclForVarTemplateSpecialization) { |
| 4470 | Previous.addDecl(PrevDeclForVarTemplateSpecialization); |
| 4471 | } |
| Larisse Voufo | 72caf2b | 2013-08-22 00:59:14 +0000 | [diff] [blame] | 4472 | CheckVariableDeclaration(NewVar, Previous); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4473 | |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4474 | if (!InstantiatingVarTemplate) { |
| 4475 | NewVar->getLexicalDeclContext()->addHiddenDecl(NewVar); |
| 4476 | if (!NewVar->isLocalExternDecl() || !NewVar->getPreviousDecl()) |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4477 | NewVar->getDeclContext()->makeDeclVisibleInContext(NewVar); |
| Richard Smith | 541b38b | 2013-09-20 01:15:31 +0000 | [diff] [blame] | 4478 | } |
| 4479 | |
| 4480 | if (!OldVar->isOutOfLine()) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4481 | if (NewVar->getDeclContext()->isFunctionOrMethod()) |
| 4482 | CurrentInstantiationScope->InstantiatedLocal(OldVar, NewVar); |
| 4483 | } |
| 4484 | |
| 4485 | // Link instantiations of static data members back to the template from |
| 4486 | // which they were instantiated. |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 4487 | // |
| 4488 | // Don't do this when instantiating a template (we link the template itself |
| 4489 | // back in that case) nor when instantiating a static data member template |
| 4490 | // (that's not a member specialization). |
| 4491 | if (NewVar->isStaticDataMember() && !InstantiatingVarTemplate && |
| 4492 | !InstantiatingSpecFromTemplate) |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4493 | NewVar->setInstantiationOfStaticDataMember(OldVar, |
| 4494 | TSK_ImplicitInstantiation); |
| 4495 | |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 4496 | // If the pattern is an (in-class) explicit specialization, then the result |
| 4497 | // is also an explicit specialization. |
| 4498 | if (VarTemplateSpecializationDecl *OldVTSD = |
| 4499 | dyn_cast<VarTemplateSpecializationDecl>(OldVar)) { |
| 4500 | if (OldVTSD->getSpecializationKind() == TSK_ExplicitSpecialization && |
| 4501 | !isa<VarTemplatePartialSpecializationDecl>(OldVTSD)) |
| 4502 | cast<VarTemplateSpecializationDecl>(NewVar)->setSpecializationKind( |
| 4503 | TSK_ExplicitSpecialization); |
| 4504 | } |
| 4505 | |
| David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 4506 | // Forward the mangling number from the template to the instantiated decl. |
| 4507 | Context.setManglingNumber(NewVar, Context.getManglingNumber(OldVar)); |
| David Majnemer | 2206bf5 | 2014-03-05 08:57:59 +0000 | [diff] [blame] | 4508 | Context.setStaticLocalNumber(NewVar, Context.getStaticLocalNumber(OldVar)); |
| David Majnemer | dbc0c8f | 2013-12-04 09:01:55 +0000 | [diff] [blame] | 4509 | |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 4510 | // Figure out whether to eagerly instantiate the initializer. |
| 4511 | if (InstantiatingVarTemplate || InstantiatingVarTemplatePartialSpec) { |
| 4512 | // We're producing a template. Don't instantiate the initializer yet. |
| 4513 | } else if (NewVar->getType()->isUndeducedType()) { |
| 4514 | // We need the type to complete the declaration of the variable. |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4515 | InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs); |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 4516 | } else if (InstantiatingSpecFromTemplate || |
| 4517 | (OldVar->isInline() && OldVar->isThisDeclarationADefinition() && |
| 4518 | !NewVar->isThisDeclarationADefinition())) { |
| 4519 | // Delay instantiation of the initializer for variable template |
| 4520 | // specializations or inline static data members until a definition of the |
| 4521 | // variable is needed. |
| 4522 | } else { |
| 4523 | InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs); |
| 4524 | } |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4525 | |
| 4526 | // Diagnose unused local variables with dependent types, where the diagnostic |
| 4527 | // will have been deferred. |
| 4528 | if (!NewVar->isInvalidDecl() && |
| Nico Weber | 7288943 | 2014-09-06 01:25:55 +0000 | [diff] [blame] | 4529 | NewVar->getDeclContext()->isFunctionOrMethod() && |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4530 | OldVar->getType()->isDependentType()) |
| 4531 | DiagnoseUnusedDecl(NewVar); |
| 4532 | } |
| 4533 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4534 | /// Instantiate the initializer of a variable. |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4535 | void Sema::InstantiateVariableInitializer( |
| 4536 | VarDecl *Var, VarDecl *OldVar, |
| 4537 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4538 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 4539 | L->VariableDefinitionInstantiated(Var); |
| 4540 | |
| Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4541 | // We propagate the 'inline' flag with the initializer, because it |
| 4542 | // would otherwise imply that the variable is a definition for a |
| 4543 | // non-static data member. |
| 4544 | if (OldVar->isInlineSpecified()) |
| 4545 | Var->setInlineSpecified(); |
| 4546 | else if (OldVar->isInline()) |
| 4547 | Var->setImplicitlyInline(); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4548 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4549 | if (OldVar->getInit()) { |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 4550 | EnterExpressionEvaluationContext Evaluated( |
| 4551 | *this, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, Var); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4552 | |
| 4553 | // Instantiate the initializer. |
| Akira Hatanaka | b87faff | 2016-04-28 23:50:12 +0000 | [diff] [blame] | 4554 | ExprResult Init; |
| 4555 | |
| 4556 | { |
| 4557 | ContextRAII SwitchContext(*this, Var->getDeclContext()); |
| 4558 | Init = SubstInitializer(OldVar->getInit(), TemplateArgs, |
| 4559 | OldVar->getInitStyle() == VarDecl::CallInit); |
| 4560 | } |
| 4561 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4562 | if (!Init.isInvalid()) { |
| Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 4563 | Expr *InitExpr = Init.get(); |
| 4564 | |
| Richard Smith | 95b83e9 | 2014-07-10 20:53:43 +0000 | [diff] [blame] | 4565 | if (Var->hasAttr<DLLImportAttr>() && |
| 4566 | (!InitExpr || |
| 4567 | !InitExpr->isConstantInitializer(getASTContext(), false))) { |
| Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 4568 | // Do not dynamically initialize dllimport variables. |
| Hans Wennborg | 91ebe6e | 2014-06-10 00:55:51 +0000 | [diff] [blame] | 4569 | } else if (InitExpr) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4570 | bool DirectInit = OldVar->isDirectInit(); |
| Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4571 | AddInitializerToDecl(Var, InitExpr, DirectInit); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4572 | } else |
| Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4573 | ActOnUninitializedDecl(Var); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4574 | } else { |
| 4575 | // FIXME: Not too happy about invalidating the declaration |
| 4576 | // because of a bogus initializer. |
| 4577 | Var->setInvalidDecl(); |
| 4578 | } |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4579 | } else { |
| George Burgess IV | 18b28a8 | 2018-03-20 03:27:44 +0000 | [diff] [blame] | 4580 | // `inline` variables are a definition and declaration all in one; we won't |
| 4581 | // pick up an initializer from anywhere else. |
| 4582 | if (Var->isStaticDataMember() && !Var->isInline()) { |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4583 | if (!Var->isOutOfLine()) |
| 4584 | return; |
| 4585 | |
| 4586 | // If the declaration inside the class had an initializer, don't add |
| 4587 | // another one to the out-of-line definition. |
| 4588 | if (OldVar->getFirstDecl()->hasInit()) |
| 4589 | return; |
| 4590 | } |
| 4591 | |
| 4592 | // We'll add an initializer to a for-range declaration later. |
| George Karpenkov | ec38cf7 | 2018-03-29 00:56:24 +0000 | [diff] [blame] | 4593 | if (Var->isCXXForRangeDecl() || Var->isObjCForDecl()) |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4594 | return; |
| 4595 | |
| Richard Smith | 3beb7c6 | 2017-01-12 02:27:38 +0000 | [diff] [blame] | 4596 | ActOnUninitializedDecl(Var); |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4597 | } |
| Artem Belevich | e9fa53a | 2018-06-06 22:37:25 +0000 | [diff] [blame] | 4598 | |
| 4599 | if (getLangOpts().CUDA) |
| 4600 | checkAllowedCUDAInitializer(Var); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4603 | /// Instantiate the definition of the given variable from its |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4604 | /// template. |
| 4605 | /// |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4606 | /// \param PointOfInstantiation the point at which the instantiation was |
| 4607 | /// required. Note that this is not precisely a "point of instantiation" |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4608 | /// for the variable, but it's close. |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4609 | /// |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4610 | /// \param Var the already-instantiated declaration of a templated variable. |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4611 | /// |
| 4612 | /// \param Recursive if true, recursively instantiates any functions that |
| 4613 | /// are required by this instantiation. |
| Douglas Gregor | a8b89d2 | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 4614 | /// |
| 4615 | /// \param DefinitionRequired if true, then we are performing an explicit |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4616 | /// instantiation where a definition of the variable is required. Complain |
| 4617 | /// if there is no such definition. |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4618 | void Sema::InstantiateVariableDefinition(SourceLocation PointOfInstantiation, |
| 4619 | VarDecl *Var, bool Recursive, |
| Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 4620 | bool DefinitionRequired, bool AtEndOfTU) { |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4621 | if (Var->isInvalidDecl()) |
| 4622 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4623 | |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 4624 | // Never instantiate an explicitly-specialized entity. |
| 4625 | TemplateSpecializationKind TSK = |
| 4626 | Var->getTemplateSpecializationKindForInstantiation(); |
| 4627 | if (TSK == TSK_ExplicitSpecialization) |
| 4628 | return; |
| 4629 | |
| 4630 | // Find the pattern and the arguments to substitute into it. |
| 4631 | VarDecl *PatternDecl = Var->getTemplateInstantiationPattern(); |
| 4632 | assert(PatternDecl && "no pattern for templated variable"); |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4633 | MultiLevelTemplateArgumentList TemplateArgs = |
| 4634 | getTemplateInstantiationArgs(Var); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4635 | |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 4636 | VarTemplateSpecializationDecl *VarSpec = |
| 4637 | dyn_cast<VarTemplateSpecializationDecl>(Var); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4638 | if (VarSpec) { |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4639 | // If this is a variable template specialization, make sure that it is |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 4640 | // non-dependent. |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4641 | bool InstantiationDependent = false; |
| 4642 | assert(!TemplateSpecializationType::anyDependentTemplateArguments( |
| 4643 | VarSpec->getTemplateArgsInfo(), InstantiationDependent) && |
| 4644 | "Only instantiate variable template specializations that are " |
| 4645 | "not type-dependent"); |
| Larisse Voufo | 4154f46 | 2013-08-06 03:57:41 +0000 | [diff] [blame] | 4646 | (void)InstantiationDependent; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4647 | |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4648 | // If this is a static data member template, there might be an |
| 4649 | // uninstantiated initializer on the declaration. If so, instantiate |
| 4650 | // it now. |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 4651 | // |
| 4652 | // FIXME: This largely duplicates what we would do below. The difference |
| 4653 | // is that along this path we may instantiate an initializer from an |
| 4654 | // in-class declaration of the template and instantiate the definition |
| 4655 | // from a separate out-of-class definition. |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4656 | if (PatternDecl->isStaticDataMember() && |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 4657 | (PatternDecl = PatternDecl->getFirstDecl())->hasInit() && |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4658 | !Var->hasInit()) { |
| 4659 | // FIXME: Factor out the duplicated instantiation context setup/tear down |
| 4660 | // code here. |
| 4661 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4662 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4663 | return; |
| Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 4664 | PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
| Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 4665 | "instantiating variable initializer"); |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4666 | |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4667 | // The instantiation is visible here, even if it was first declared in an |
| 4668 | // unimported module. |
| Richard Smith | 90dc525 | 2017-06-23 01:04:34 +0000 | [diff] [blame] | 4669 | Var->setVisibleDespiteOwningModule(); |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4670 | |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4671 | // If we're performing recursive template instantiation, create our own |
| 4672 | // queue of pending implicit instantiations that we will instantiate |
| 4673 | // later, while we're still within our own instantiation context. |
| Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4674 | GlobalEagerInstantiationScope GlobalInstantiations(*this, |
| 4675 | /*Enabled=*/Recursive); |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4676 | LocalInstantiationScope Local(*this); |
| Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4677 | LocalEagerInstantiationScope LocalInstantiations(*this); |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4678 | |
| 4679 | // Enter the scope of this instantiation. We don't use |
| 4680 | // PushDeclContext because we don't have a scope. |
| 4681 | ContextRAII PreviousContext(*this, Var->getDeclContext()); |
| 4682 | InstantiateVariableInitializer(Var, PatternDecl, TemplateArgs); |
| 4683 | PreviousContext.pop(); |
| 4684 | |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4685 | // This variable may have local implicit instantiations that need to be |
| 4686 | // instantiated within this scope. |
| Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4687 | LocalInstantiations.perform(); |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4688 | Local.Exit(); |
| Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4689 | GlobalInstantiations.perform(); |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4690 | } |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4691 | } else { |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 4692 | assert(Var->isStaticDataMember() && PatternDecl->isStaticDataMember() && |
| 4693 | "not a static data member?"); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 4696 | VarDecl *Def = PatternDecl->getDefinition(getASTContext()); |
| Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4697 | |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4698 | // If we don't have a definition of the variable template, we won't perform |
| 4699 | // any instantiation. Rather, we rely on the user to instantiate this |
| 4700 | // definition (or provide a specialization for it) in another translation |
| 4701 | // unit. |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4702 | if (!Def && !DefinitionRequired) { |
| 4703 | if (TSK == TSK_ExplicitInstantiationDefinition) { |
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 4704 | PendingInstantiations.push_back( |
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4705 | std::make_pair(Var, PointOfInstantiation)); |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4706 | } else if (TSK == TSK_ImplicitInstantiation) { |
| Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 4707 | // Warn about missing definition at the end of translation unit. |
| Nick Lewycky | 2adab1b | 2018-01-02 19:10:12 +0000 | [diff] [blame] | 4708 | if (AtEndOfTU && !getDiagnostics().hasErrorOccurred() && |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4709 | !getSourceManager().isInSystemHeader(PatternDecl->getBeginLoc())) { |
| Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 4710 | Diag(PointOfInstantiation, diag::warn_var_template_missing) |
| 4711 | << Var; |
| 4712 | Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); |
| 4713 | if (getLangOpts().CPlusPlus11) |
| 4714 | Diag(PointOfInstantiation, diag::note_inst_declaration_hint) << Var; |
| 4715 | } |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4716 | return; |
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 4717 | } |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 4720 | // FIXME: We need to track the instantiation stack in order to know which |
| 4721 | // definitions should be visible within this instantiation. |
| 4722 | // FIXME: Produce diagnostics when Var->getInstantiatedFromStaticDataMember(). |
| 4723 | if (DiagnoseUninstantiableTemplate(PointOfInstantiation, Var, |
| 4724 | /*InstantiatedFromMember*/false, |
| 4725 | PatternDecl, Def, TSK, |
| 4726 | /*Complain*/DefinitionRequired)) |
| 4727 | return; |
| 4728 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4729 | // C++11 [temp.explicit]p10: |
| Richard Smith | 4309b66 | 2017-10-18 22:45:01 +0000 | [diff] [blame] | 4730 | // Except for inline functions, const variables of literal types, variables |
| 4731 | // of reference types, [...] explicit instantiation declarations |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4732 | // have the effect of suppressing the implicit instantiation of the entity |
| 4733 | // to which they refer. |
| Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 4734 | // |
| 4735 | // FIXME: That's not exactly the same as "might be usable in constant |
| 4736 | // expressions", which only allows constexpr variables and const integral |
| 4737 | // types, not arbitrary const literal types. |
| Richard Smith | 4309b66 | 2017-10-18 22:45:01 +0000 | [diff] [blame] | 4738 | if (TSK == TSK_ExplicitInstantiationDeclaration && |
| Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 4739 | !Var->mightBeUsableInConstantExpressions(getASTContext())) |
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 4740 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4741 | |
| Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 4742 | // Make sure to pass the instantiated variable to the consumer at the end. |
| 4743 | struct PassToConsumerRAII { |
| 4744 | ASTConsumer &Consumer; |
| 4745 | VarDecl *Var; |
| 4746 | |
| 4747 | PassToConsumerRAII(ASTConsumer &Consumer, VarDecl *Var) |
| 4748 | : Consumer(Consumer), Var(Var) { } |
| 4749 | |
| 4750 | ~PassToConsumerRAII() { |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4751 | Consumer.HandleCXXStaticMemberVarInstantiation(Var); |
| Argyrios Kyrtzidis | 8a27b2b | 2013-02-24 00:05:01 +0000 | [diff] [blame] | 4752 | } |
| 4753 | } PassToConsumerRAII(Consumer, Var); |
| Rafael Espindola | df88f6f | 2012-03-08 15:51:03 +0000 | [diff] [blame] | 4754 | |
| Reid Kleckner | e07140e | 2015-04-15 01:08:06 +0000 | [diff] [blame] | 4755 | // If we already have a definition, we're done. |
| 4756 | if (VarDecl *Def = Var->getDefinition()) { |
| 4757 | // We may be explicitly instantiating something we've already implicitly |
| 4758 | // instantiated. |
| 4759 | Def->setTemplateSpecializationKind(Var->getTemplateSpecializationKind(), |
| 4760 | PointOfInstantiation); |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4761 | return; |
| Reid Kleckner | e07140e | 2015-04-15 01:08:06 +0000 | [diff] [blame] | 4762 | } |
| Douglas Gregor | 57d4f97 | 2011-06-03 03:35:07 +0000 | [diff] [blame] | 4763 | |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4764 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
| Richard Smith | 54f18e8 | 2016-08-31 02:15:21 +0000 | [diff] [blame] | 4765 | if (Inst.isInvalid() || Inst.isAlreadyInstantiating()) |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4766 | return; |
| Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 4767 | PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
| Richard Smith | e19b95d | 2016-05-26 20:23:13 +0000 | [diff] [blame] | 4768 | "instantiating variable definition"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4769 | |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4770 | // If we're performing recursive template instantiation, create our own |
| 4771 | // queue of pending implicit instantiations that we will instantiate later, |
| 4772 | // while we're still within our own instantiation context. |
| Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4773 | GlobalEagerInstantiationScope GlobalInstantiations(*this, |
| 4774 | /*Enabled=*/Recursive); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4775 | |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4776 | // Enter the scope of this instantiation. We don't use |
| 4777 | // PushDeclContext because we don't have a scope. |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4778 | ContextRAII PreviousContext(*this, Var->getDeclContext()); |
| Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 4779 | LocalInstantiationScope Local(*this); |
| John McCall | 2957e3e | 2011-02-14 20:37:25 +0000 | [diff] [blame] | 4780 | |
| Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4781 | LocalEagerInstantiationScope LocalInstantiations(*this); |
| 4782 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4783 | VarDecl *OldVar = Var; |
| Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4784 | if (Def->isStaticDataMember() && !Def->isOutOfLine()) { |
| 4785 | // We're instantiating an inline static data member whose definition was |
| 4786 | // provided inside the class. |
| Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4787 | InstantiateVariableInitializer(Var, Def, TemplateArgs); |
| 4788 | } else if (!VarSpec) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4789 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4790 | TemplateArgs)); |
| Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 4791 | } else if (Var->isStaticDataMember() && |
| 4792 | Var->getLexicalDeclContext()->isRecord()) { |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4793 | // We need to instantiate the definition of a static data member template, |
| 4794 | // and all we have is the in-class declaration of it. Instantiate a separate |
| 4795 | // declaration of the definition. |
| 4796 | TemplateDeclInstantiator Instantiator(*this, Var->getDeclContext(), |
| 4797 | TemplateArgs); |
| 4798 | Var = cast_or_null<VarDecl>(Instantiator.VisitVarTemplateSpecializationDecl( |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4799 | VarSpec->getSpecializedTemplate(), Def, nullptr, |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4800 | VarSpec->getTemplateArgsInfo(), VarSpec->getTemplateArgs().asArray())); |
| 4801 | if (Var) { |
| 4802 | llvm::PointerUnion<VarTemplateDecl *, |
| 4803 | VarTemplatePartialSpecializationDecl *> PatternPtr = |
| 4804 | VarSpec->getSpecializedTemplateOrPartial(); |
| 4805 | if (VarTemplatePartialSpecializationDecl *Partial = |
| 4806 | PatternPtr.dyn_cast<VarTemplatePartialSpecializationDecl *>()) |
| 4807 | cast<VarTemplateSpecializationDecl>(Var)->setInstantiationOf( |
| 4808 | Partial, &VarSpec->getTemplateInstantiationArgs()); |
| 4809 | |
| 4810 | // Merge the definition with the declaration. |
| 4811 | LookupResult R(*this, Var->getDeclName(), Var->getLocation(), |
| Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 4812 | LookupOrdinaryName, forRedeclarationInCurContext()); |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4813 | R.addDecl(OldVar); |
| 4814 | MergeVarDecl(Var, R); |
| 4815 | |
| 4816 | // Attach the initializer. |
| 4817 | InstantiateVariableInitializer(Var, Def, TemplateArgs); |
| 4818 | } |
| 4819 | } else |
| 4820 | // Complete the existing variable's definition with an appropriately |
| 4821 | // substituted type and initializer. |
| 4822 | Var = CompleteVarTemplateSpecializationDecl(VarSpec, Def, TemplateArgs); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4823 | |
| 4824 | PreviousContext.pop(); |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4825 | |
| 4826 | if (Var) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4827 | PassToConsumerRAII.Var = Var; |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 4828 | Var->setTemplateSpecializationKind(OldVar->getTemplateSpecializationKind(), |
| 4829 | OldVar->getPointOfInstantiation()); |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 4830 | } |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4831 | |
| 4832 | // This variable may have local implicit instantiations that need to be |
| 4833 | // instantiated within this scope. |
| Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4834 | LocalInstantiations.perform(); |
| Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 4835 | Local.Exit(); |
| Richard Smith | 4f3e381 | 2017-05-20 01:36:41 +0000 | [diff] [blame] | 4836 | GlobalInstantiations.perform(); |
| Douglas Gregor | bbbb02d | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 4837 | } |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 4838 | |
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4839 | void |
| 4840 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, |
| 4841 | const CXXConstructorDecl *Tmpl, |
| 4842 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4843 | |
| Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 4844 | SmallVector<CXXCtorInitializer*, 4> NewInits; |
| Richard Smith | 60f2e1e | 2012-09-25 00:23:05 +0000 | [diff] [blame] | 4845 | bool AnyErrors = Tmpl->isInvalidDecl(); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4846 | |
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4847 | // Instantiate all the initializers. |
| Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 4848 | for (const auto *Init : Tmpl->inits()) { |
| Chandler Carruth | f92bd8c | 2010-09-03 21:54:20 +0000 | [diff] [blame] | 4849 | // Only instantiate written initializers, let Sema re-construct implicit |
| 4850 | // ones. |
| 4851 | if (!Init->isWritten()) |
| 4852 | continue; |
| 4853 | |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4854 | SourceLocation EllipsisLoc; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4855 | |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4856 | if (Init->isPackExpansion()) { |
| 4857 | // This is a pack expansion. We should expand it now. |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4858 | TypeLoc BaseTL = Init->getTypeSourceInfo()->getTypeLoc(); |
| Nick Lewycky | 2c30850 | 2013-06-13 00:45:47 +0000 | [diff] [blame] | 4859 | SmallVector<UnexpandedParameterPack, 4> Unexpanded; |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4860 | collectUnexpandedParameterPacks(BaseTL, Unexpanded); |
| Nick Lewycky | 2c30850 | 2013-06-13 00:45:47 +0000 | [diff] [blame] | 4861 | collectUnexpandedParameterPacks(Init->getInit(), Unexpanded); |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4862 | bool ShouldExpand = false; |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4863 | bool RetainExpansion = false; |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 4864 | Optional<unsigned> NumExpansions; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4865 | if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(), |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4866 | BaseTL.getSourceRange(), |
| David Blaikie | b9c168a | 2011-09-22 02:34:54 +0000 | [diff] [blame] | 4867 | Unexpanded, |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4868 | TemplateArgs, ShouldExpand, |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 4869 | RetainExpansion, |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4870 | NumExpansions)) { |
| 4871 | AnyErrors = true; |
| 4872 | New->setInvalidDecl(); |
| 4873 | continue; |
| 4874 | } |
| 4875 | assert(ShouldExpand && "Partial instantiation of base initializer?"); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4876 | |
| 4877 | // Loop over all of the arguments in the argument pack(s), |
| Douglas Gregor | 0dca5fd | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 4878 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4879 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
| 4880 | |
| 4881 | // Instantiate the initializer. |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4882 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 4883 | /*CXXDirectInit=*/true); |
| 4884 | if (TempInit.isInvalid()) { |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4885 | AnyErrors = true; |
| 4886 | break; |
| 4887 | } |
| 4888 | |
| 4889 | // Instantiate the base type. |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4890 | TypeSourceInfo *BaseTInfo = SubstType(Init->getTypeSourceInfo(), |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4891 | TemplateArgs, |
| 4892 | Init->getSourceLocation(), |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4893 | New->getDeclName()); |
| 4894 | if (!BaseTInfo) { |
| 4895 | AnyErrors = true; |
| 4896 | break; |
| 4897 | } |
| 4898 | |
| 4899 | // Build the initializer. |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4900 | MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(), |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4901 | BaseTInfo, TempInit.get(), |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4902 | New->getParent(), |
| 4903 | SourceLocation()); |
| 4904 | if (NewInit.isInvalid()) { |
| 4905 | AnyErrors = true; |
| 4906 | break; |
| 4907 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4908 | |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4909 | NewInits.push_back(NewInit.get()); |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4910 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4911 | |
| Douglas Gregor | 44e7df6 | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 4912 | continue; |
| 4913 | } |
| 4914 | |
| Douglas Gregor | b30f22b | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 4915 | // Instantiate the initializer. |
| Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4916 | ExprResult TempInit = SubstInitializer(Init->getInit(), TemplateArgs, |
| 4917 | /*CXXDirectInit=*/true); |
| 4918 | if (TempInit.isInvalid()) { |
| Douglas Gregor | b30f22b | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 4919 | AnyErrors = true; |
| 4920 | continue; |
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4921 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 4922 | |
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4923 | MemInitResult NewInit; |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4924 | if (Init->isDelegatingInitializer() || Init->isBaseInitializer()) { |
| 4925 | TypeSourceInfo *TInfo = SubstType(Init->getTypeSourceInfo(), |
| 4926 | TemplateArgs, |
| 4927 | Init->getSourceLocation(), |
| 4928 | New->getDeclName()); |
| 4929 | if (!TInfo) { |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4930 | AnyErrors = true; |
| Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 4931 | New->setInvalidDecl(); |
| 4932 | continue; |
| 4933 | } |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4934 | |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4935 | if (Init->isBaseInitializer()) |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4936 | NewInit = BuildBaseInitializer(TInfo->getType(), TInfo, TempInit.get(), |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4937 | New->getParent(), EllipsisLoc); |
| 4938 | else |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4939 | NewInit = BuildDelegatingInitializer(TInfo, TempInit.get(), |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 4940 | cast<CXXRecordDecl>(CurContext->getParent())); |
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4941 | } else if (Init->isMemberInitializer()) { |
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4942 | FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl( |
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4943 | Init->getMemberLocation(), |
| 4944 | Init->getMember(), |
| 4945 | TemplateArgs)); |
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4946 | if (!Member) { |
| 4947 | AnyErrors = true; |
| 4948 | New->setInvalidDecl(); |
| 4949 | continue; |
| 4950 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4951 | |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4952 | NewInit = BuildMemberInitializer(Member, TempInit.get(), |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4953 | Init->getSourceLocation()); |
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4954 | } else if (Init->isIndirectMemberInitializer()) { |
| 4955 | IndirectFieldDecl *IndirectMember = |
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4956 | cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl( |
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 4957 | Init->getMemberLocation(), |
| 4958 | Init->getIndirectMember(), TemplateArgs)); |
| 4959 | |
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4960 | if (!IndirectMember) { |
| 4961 | AnyErrors = true; |
| 4962 | New->setInvalidDecl(); |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4963 | continue; |
| Douglas Gregor | 55e6b31 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 4964 | } |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4965 | |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4966 | NewInit = BuildMemberInitializer(IndirectMember, TempInit.get(), |
| Sebastian Redl | a74948d | 2011-09-24 17:48:25 +0000 | [diff] [blame] | 4967 | Init->getSourceLocation()); |
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4968 | } |
| 4969 | |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4970 | if (NewInit.isInvalid()) { |
| 4971 | AnyErrors = true; |
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4972 | New->setInvalidDecl(); |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4973 | } else { |
| Richard Trieu | 9becef6 | 2011-09-09 03:18:59 +0000 | [diff] [blame] | 4974 | NewInits.push_back(NewInit.get()); |
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4975 | } |
| 4976 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4977 | |
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4978 | // Assign all the initializers to the new constructor. |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4979 | ActOnMemInitializers(New, |
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4980 | /*FIXME: ColonLoc */ |
| 4981 | SourceLocation(), |
| David Blaikie | 3fc2f91 | 2013-01-17 05:26:25 +0000 | [diff] [blame] | 4982 | NewInits, |
| Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 4983 | AnyErrors); |
| Anders Carlsson | 7055394 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 4986 | // TODO: this could be templated if the various decl types used the |
| 4987 | // same method name. |
| 4988 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, |
| 4989 | ClassTemplateDecl *Instance) { |
| 4990 | Pattern = Pattern->getCanonicalDecl(); |
| 4991 | |
| 4992 | do { |
| 4993 | Instance = Instance->getCanonicalDecl(); |
| 4994 | if (Pattern == Instance) return true; |
| 4995 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 4996 | } while (Instance); |
| 4997 | |
| 4998 | return false; |
| 4999 | } |
| 5000 | |
| Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 5001 | static bool isInstantiationOf(FunctionTemplateDecl *Pattern, |
| 5002 | FunctionTemplateDecl *Instance) { |
| 5003 | Pattern = Pattern->getCanonicalDecl(); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5004 | |
| Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 5005 | do { |
| 5006 | Instance = Instance->getCanonicalDecl(); |
| 5007 | if (Pattern == Instance) return true; |
| 5008 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 5009 | } while (Instance); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5010 | |
| Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 5011 | return false; |
| 5012 | } |
| 5013 | |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5014 | static bool |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5015 | isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, |
| 5016 | ClassTemplatePartialSpecializationDecl *Instance) { |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5017 | Pattern |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5018 | = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); |
| 5019 | do { |
| 5020 | Instance = cast<ClassTemplatePartialSpecializationDecl>( |
| 5021 | Instance->getCanonicalDecl()); |
| 5022 | if (Pattern == Instance) |
| 5023 | return true; |
| 5024 | Instance = Instance->getInstantiatedFromMember(); |
| 5025 | } while (Instance); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5026 | |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5027 | return false; |
| 5028 | } |
| 5029 | |
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5030 | static bool isInstantiationOf(CXXRecordDecl *Pattern, |
| 5031 | CXXRecordDecl *Instance) { |
| 5032 | Pattern = Pattern->getCanonicalDecl(); |
| 5033 | |
| 5034 | do { |
| 5035 | Instance = Instance->getCanonicalDecl(); |
| 5036 | if (Pattern == Instance) return true; |
| 5037 | Instance = Instance->getInstantiatedFromMemberClass(); |
| 5038 | } while (Instance); |
| 5039 | |
| 5040 | return false; |
| 5041 | } |
| 5042 | |
| 5043 | static bool isInstantiationOf(FunctionDecl *Pattern, |
| 5044 | FunctionDecl *Instance) { |
| 5045 | Pattern = Pattern->getCanonicalDecl(); |
| 5046 | |
| 5047 | do { |
| 5048 | Instance = Instance->getCanonicalDecl(); |
| 5049 | if (Pattern == Instance) return true; |
| 5050 | Instance = Instance->getInstantiatedFromMemberFunction(); |
| 5051 | } while (Instance); |
| 5052 | |
| 5053 | return false; |
| 5054 | } |
| 5055 | |
| 5056 | static bool isInstantiationOf(EnumDecl *Pattern, |
| 5057 | EnumDecl *Instance) { |
| 5058 | Pattern = Pattern->getCanonicalDecl(); |
| 5059 | |
| 5060 | do { |
| 5061 | Instance = Instance->getCanonicalDecl(); |
| 5062 | if (Pattern == Instance) return true; |
| 5063 | Instance = Instance->getInstantiatedFromMemberEnum(); |
| 5064 | } while (Instance); |
| 5065 | |
| 5066 | return false; |
| 5067 | } |
| 5068 | |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5069 | static bool isInstantiationOf(UsingShadowDecl *Pattern, |
| 5070 | UsingShadowDecl *Instance, |
| 5071 | ASTContext &C) { |
| Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 5072 | return declaresSameEntity(C.getInstantiatedFromUsingShadowDecl(Instance), |
| 5073 | Pattern); |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 5076 | static bool isInstantiationOf(UsingDecl *Pattern, UsingDecl *Instance, |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5077 | ASTContext &C) { |
| Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 5078 | return declaresSameEntity(C.getInstantiatedFromUsingDecl(Instance), Pattern); |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5079 | } |
| 5080 | |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 5081 | template<typename T> |
| 5082 | static bool isInstantiationOfUnresolvedUsingDecl(T *Pattern, Decl *Other, |
| 5083 | ASTContext &Ctx) { |
| 5084 | // An unresolved using declaration can instantiate to an unresolved using |
| 5085 | // declaration, or to a using declaration or a using declaration pack. |
| 5086 | // |
| 5087 | // Multiple declarations can claim to be instantiated from an unresolved |
| 5088 | // using declaration if it's a pack expansion. We want the UsingPackDecl |
| 5089 | // in that case, not the individual UsingDecls within the pack. |
| 5090 | bool OtherIsPackExpansion; |
| 5091 | NamedDecl *OtherFrom; |
| 5092 | if (auto *OtherUUD = dyn_cast<T>(Other)) { |
| 5093 | OtherIsPackExpansion = OtherUUD->isPackExpansion(); |
| 5094 | OtherFrom = Ctx.getInstantiatedFromUsingDecl(OtherUUD); |
| 5095 | } else if (auto *OtherUPD = dyn_cast<UsingPackDecl>(Other)) { |
| 5096 | OtherIsPackExpansion = true; |
| 5097 | OtherFrom = OtherUPD->getInstantiatedFromUsingDecl(); |
| 5098 | } else if (auto *OtherUD = dyn_cast<UsingDecl>(Other)) { |
| 5099 | OtherIsPackExpansion = false; |
| 5100 | OtherFrom = Ctx.getInstantiatedFromUsingDecl(OtherUD); |
| 5101 | } else { |
| 5102 | return false; |
| 5103 | } |
| 5104 | return Pattern->isPackExpansion() == OtherIsPackExpansion && |
| 5105 | declaresSameEntity(OtherFrom, Pattern); |
| Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 5106 | } |
| 5107 | |
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5108 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, |
| 5109 | VarDecl *Instance) { |
| 5110 | assert(Instance->isStaticDataMember()); |
| 5111 | |
| 5112 | Pattern = Pattern->getCanonicalDecl(); |
| 5113 | |
| 5114 | do { |
| 5115 | Instance = Instance->getCanonicalDecl(); |
| 5116 | if (Pattern == Instance) return true; |
| 5117 | Instance = Instance->getInstantiatedFromStaticDataMember(); |
| 5118 | } while (Instance); |
| 5119 | |
| 5120 | return false; |
| 5121 | } |
| 5122 | |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5123 | // Other is the prospective instantiation |
| 5124 | // D is the prospective pattern |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5125 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 5126 | if (auto *UUD = dyn_cast<UnresolvedUsingTypenameDecl>(D)) |
| 5127 | return isInstantiationOfUnresolvedUsingDecl(UUD, Other, Ctx); |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 5128 | |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 5129 | if (auto *UUD = dyn_cast<UnresolvedUsingValueDecl>(D)) |
| 5130 | return isInstantiationOfUnresolvedUsingDecl(UUD, Other, Ctx); |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5131 | |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 5132 | if (D->getKind() != Other->getKind()) |
| Anders Carlsson | 4bb87ce | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 5133 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5134 | |
| Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5135 | if (auto *Record = dyn_cast<CXXRecordDecl>(Other)) |
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5136 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5137 | |
| Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5138 | if (auto *Function = dyn_cast<FunctionDecl>(Other)) |
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5139 | return isInstantiationOf(cast<FunctionDecl>(D), Function); |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5140 | |
| Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5141 | if (auto *Enum = dyn_cast<EnumDecl>(Other)) |
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5142 | return isInstantiationOf(cast<EnumDecl>(D), Enum); |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5143 | |
| Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5144 | if (auto *Var = dyn_cast<VarDecl>(Other)) |
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5145 | if (Var->isStaticDataMember()) |
| 5146 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); |
| 5147 | |
| Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5148 | if (auto *Temp = dyn_cast<ClassTemplateDecl>(Other)) |
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5149 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); |
| Douglas Gregor | f3db003 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 5150 | |
| Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5151 | if (auto *Temp = dyn_cast<FunctionTemplateDecl>(Other)) |
| Douglas Gregor | 14d1bf4 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 5152 | return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); |
| 5153 | |
| Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5154 | if (auto *PartialSpec = |
| 5155 | dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5156 | return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D), |
| 5157 | PartialSpec); |
| 5158 | |
| Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5159 | if (auto *Field = dyn_cast<FieldDecl>(Other)) { |
| Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 5160 | if (!Field->getDeclName()) { |
| 5161 | // This is an unnamed field. |
| Richard Smith | 32952e1 | 2014-10-14 02:00:47 +0000 | [diff] [blame] | 5162 | return declaresSameEntity(Ctx.getInstantiatedFromUnnamedFieldDecl(Field), |
| 5163 | cast<FieldDecl>(D)); |
| Anders Carlsson | 5da8484 | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 5164 | } |
| 5165 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5166 | |
| Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5167 | if (auto *Using = dyn_cast<UsingDecl>(Other)) |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5168 | return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx); |
| 5169 | |
| Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5170 | if (auto *Shadow = dyn_cast<UsingShadowDecl>(Other)) |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5171 | return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx); |
| 5172 | |
| Richard Smith | d8a9e37 | 2016-12-18 21:39:37 +0000 | [diff] [blame] | 5173 | return D->getDeclName() && |
| 5174 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5175 | } |
| 5176 | |
| 5177 | template<typename ForwardIterator> |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5178 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5179 | NamedDecl *D, |
| 5180 | ForwardIterator first, |
| 5181 | ForwardIterator last) { |
| 5182 | for (; first != last; ++first) |
| 5183 | if (isInstantiationOf(Ctx, D, *first)) |
| 5184 | return cast<NamedDecl>(*first); |
| 5185 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5186 | return nullptr; |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5187 | } |
| 5188 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5189 | /// Finds the instantiation of the given declaration context |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5190 | /// within the current instantiation. |
| 5191 | /// |
| 5192 | /// \returns NULL if there was an error |
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5193 | DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC, |
| Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5194 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5195 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { |
| Richard Smith | 4f440e3 | 2017-06-08 01:08:50 +0000 | [diff] [blame] | 5196 | Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs, true); |
| John McCall | aa74a0c | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 5197 | return cast_or_null<DeclContext>(ID); |
| 5198 | } else return DC; |
| 5199 | } |
| 5200 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5201 | /// Find the instantiation of the given declaration within the |
| Douglas Gregor | cd3a097 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 5202 | /// current instantiation. |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5203 | /// |
| 5204 | /// This routine is intended to be used when \p D is a declaration |
| 5205 | /// referenced from within a template, that needs to mapped into the |
| 5206 | /// corresponding declaration within an instantiation. For example, |
| 5207 | /// given: |
| 5208 | /// |
| 5209 | /// \code |
| 5210 | /// template<typename T> |
| 5211 | /// struct X { |
| 5212 | /// enum Kind { |
| 5213 | /// KnownValue = sizeof(T) |
| 5214 | /// }; |
| 5215 | /// |
| 5216 | /// bool getKind() const { return KnownValue; } |
| 5217 | /// }; |
| 5218 | /// |
| 5219 | /// template struct X<int>; |
| 5220 | /// \endcode |
| 5221 | /// |
| Erik Pilkington | 4cae092 | 2019-07-30 23:38:18 +0000 | [diff] [blame] | 5222 | /// In the instantiation of X<int>::getKind(), we need to map the \p |
| 5223 | /// EnumConstantDecl for \p KnownValue (which refers to |
| 5224 | /// X<T>::<Kind>::KnownValue) to its instantiation (X<int>::<Kind>::KnownValue). |
| 5225 | /// \p FindInstantiatedDecl performs this mapping from within the instantiation |
| 5226 | /// of X<int>. |
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5227 | NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, |
| Richard Smith | 4f440e3 | 2017-06-08 01:08:50 +0000 | [diff] [blame] | 5228 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 5229 | bool FindingInstantiatedContext) { |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5230 | DeclContext *ParentDC = D->getDeclContext(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5231 | // FIXME: Parmeters of pointer to functions (y below) that are themselves |
| Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 5232 | // parameters (p below) can have their ParentDC set to the translation-unit |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5233 | // - thus we can not consistently check if the ParentDC of such a parameter |
| Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 5234 | // is Dependent or/and a FunctionOrMethod. |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5235 | // For e.g. this code, during Template argument deduction tries to |
| Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 5236 | // find an instantiated decl for (T y) when the ParentDC for y is |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 5237 | // the translation unit. |
| 5238 | // e.g. template <class T> void Foo(auto (*p)(T y) -> decltype(y())) {} |
| Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5239 | // float baz(float(*)()) { return 0.0; } |
| Faisal Vali | 2cba133 | 2013-10-23 06:44:28 +0000 | [diff] [blame] | 5240 | // Foo(baz); |
| 5241 | // The better fix here is perhaps to ensure that a ParmVarDecl, by the time |
| 5242 | // it gets here, always has a FunctionOrMethod as its ParentDC?? |
| 5243 | // For now: |
| 5244 | // - as long as we have a ParmVarDecl whose parent is non-dependent and |
| 5245 | // whose type is not instantiation dependent, do nothing to the decl |
| 5246 | // - otherwise find its instantiated decl. |
| 5247 | if (isa<ParmVarDecl>(D) && !ParentDC->isDependentContext() && |
| 5248 | !cast<ParmVarDecl>(D)->getType()->isInstantiationDependentType()) |
| 5249 | return D; |
| Rafael Espindola | 09b00e3 | 2013-10-23 04:12:23 +0000 | [diff] [blame] | 5250 | if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) || |
| Douglas Gregor | b9397108 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 5251 | isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) || |
| Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 5252 | ((ParentDC->isFunctionOrMethod() || |
| Michael Kruse | 251e148 | 2019-02-01 20:25:04 +0000 | [diff] [blame] | 5253 | isa<OMPDeclareReductionDecl>(ParentDC) || |
| 5254 | isa<OMPDeclareMapperDecl>(ParentDC)) && |
| Alexey Bataev | e6aa469 | 2018-09-13 16:54:05 +0000 | [diff] [blame] | 5255 | ParentDC->isDependentContext()) || |
| Douglas Gregor | a86bc00 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 5256 | (isa<CXXRecordDecl>(D) && cast<CXXRecordDecl>(D)->isLambda())) { |
| Douglas Gregor | f98d9b6 | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 5257 | // D is a local of some kind. Look into the map of local |
| 5258 | // declarations to their instantiations. |
| Alexey Samsonov | 2c0aac2 | 2014-09-03 18:45:45 +0000 | [diff] [blame] | 5259 | if (CurrentInstantiationScope) { |
| 5260 | if (auto Found = CurrentInstantiationScope->findInstantiationOf(D)) { |
| 5261 | if (Decl *FD = Found->dyn_cast<Decl *>()) |
| 5262 | return cast<NamedDecl>(FD); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5263 | |
| Alexey Samsonov | 2c0aac2 | 2014-09-03 18:45:45 +0000 | [diff] [blame] | 5264 | int PackIdx = ArgumentPackSubstitutionIndex; |
| 5265 | assert(PackIdx != -1 && |
| 5266 | "found declaration pack but not pack expanding"); |
| 5267 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 5268 | return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]); |
| 5269 | } |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 5270 | } |
| 5271 | |
| Serge Pavlov | 7cd8f60 | 2013-07-15 06:14:07 +0000 | [diff] [blame] | 5272 | // If we're performing a partial substitution during template argument |
| 5273 | // deduction, we may not have values for template parameters yet. They |
| 5274 | // just map to themselves. |
| 5275 | if (isa<NonTypeTemplateParmDecl>(D) || isa<TemplateTypeParmDecl>(D) || |
| 5276 | isa<TemplateTemplateParmDecl>(D)) |
| 5277 | return D; |
| 5278 | |
| Serge Pavlov | 074a518 | 2013-08-10 12:00:21 +0000 | [diff] [blame] | 5279 | if (D->isInvalidDecl()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5280 | return nullptr; |
| Serge Pavlov | 074a518 | 2013-08-10 12:00:21 +0000 | [diff] [blame] | 5281 | |
| Serge Pavlov | e7ad831 | 2015-05-15 10:10:28 +0000 | [diff] [blame] | 5282 | // Normally this function only searches for already instantiated declaration |
| 5283 | // however we have to make an exclusion for local types used before |
| 5284 | // definition as in the code: |
| 5285 | // |
| 5286 | // template<typename T> void f1() { |
| 5287 | // void g1(struct x1); |
| 5288 | // struct x1 {}; |
| 5289 | // } |
| 5290 | // |
| 5291 | // In this case instantiation of the type of 'g1' requires definition of |
| 5292 | // 'x1', which is defined later. Error recovery may produce an enum used |
| 5293 | // before definition. In these cases we need to instantiate relevant |
| 5294 | // declarations here. |
| 5295 | bool NeedInstantiate = false; |
| 5296 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) |
| 5297 | NeedInstantiate = RD->isLocalClass(); |
| 5298 | else |
| 5299 | NeedInstantiate = isa<EnumDecl>(D); |
| 5300 | if (NeedInstantiate) { |
| Serge Pavlov | 4c51174 | 2015-05-04 16:44:39 +0000 | [diff] [blame] | 5301 | Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); |
| 5302 | CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 5303 | return cast<TypeDecl>(Inst); |
| 5304 | } |
| 5305 | |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 5306 | // If we didn't find the decl, then we must have a label decl that hasn't |
| 5307 | // been found yet. Lazily instantiate it and return it now. |
| 5308 | assert(isa<LabelDecl>(D)); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5309 | |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 5310 | Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); |
| 5311 | assert(Inst && "Failed to instantiate label??"); |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5312 | |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 5313 | CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 5314 | return cast<LabelDecl>(Inst); |
| Douglas Gregor | f98d9b6 | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 5315 | } |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5316 | |
| Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5317 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 5318 | if (!Record->isDependentContext()) |
| 5319 | return D; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5320 | |
| Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5321 | // Determine whether this record is the "templated" declaration describing |
| 5322 | // a class template or class template partial specialization. |
| Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5323 | ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); |
| Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5324 | if (ClassTemplate) |
| 5325 | ClassTemplate = ClassTemplate->getCanonicalDecl(); |
| 5326 | else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 5327 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) |
| 5328 | ClassTemplate = PartialSpec->getSpecializedTemplate()->getCanonicalDecl(); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5329 | |
| Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5330 | // Walk the current context to find either the record or an instantiation of |
| 5331 | // it. |
| 5332 | DeclContext *DC = CurContext; |
| 5333 | while (!DC->isFileContext()) { |
| 5334 | // If we're performing substitution while we're inside the template |
| 5335 | // definition, we'll find our own context. We're done. |
| 5336 | if (DC->Equals(Record)) |
| 5337 | return Record; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5338 | |
| Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5339 | if (CXXRecordDecl *InstRecord = dyn_cast<CXXRecordDecl>(DC)) { |
| 5340 | // Check whether we're in the process of instantiating a class template |
| 5341 | // specialization of the template we're mapping. |
| 5342 | if (ClassTemplateSpecializationDecl *InstSpec |
| 5343 | = dyn_cast<ClassTemplateSpecializationDecl>(InstRecord)){ |
| 5344 | ClassTemplateDecl *SpecTemplate = InstSpec->getSpecializedTemplate(); |
| 5345 | if (ClassTemplate && isInstantiationOf(ClassTemplate, SpecTemplate)) |
| 5346 | return InstRecord; |
| 5347 | } |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5348 | |
| Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5349 | // Check whether we're in the process of instantiating a member class. |
| 5350 | if (isInstantiationOf(Record, InstRecord)) |
| 5351 | return InstRecord; |
| Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5352 | } |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5353 | |
| Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5354 | // Move to the outer template scope. |
| 5355 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) { |
| 5356 | if (FD->getFriendObjectKind() && FD->getDeclContext()->isFileContext()){ |
| 5357 | DC = FD->getLexicalDeclContext(); |
| 5358 | continue; |
| 5359 | } |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5360 | // An implicit deduction guide acts as if it's within the class template |
| 5361 | // specialization described by its name and first N template params. |
| Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 5362 | auto *Guide = dyn_cast<CXXDeductionGuideDecl>(FD); |
| 5363 | if (Guide && Guide->isImplicit()) { |
| 5364 | TemplateDecl *TD = Guide->getDeducedTemplate(); |
| Richard Smith | 0cd9c04 | 2017-02-21 08:42:39 +0000 | [diff] [blame] | 5365 | // Convert the arguments to an "as-written" list. |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5366 | TemplateArgumentListInfo Args(Loc, Loc); |
| Richard Smith | 0cd9c04 | 2017-02-21 08:42:39 +0000 | [diff] [blame] | 5367 | for (TemplateArgument Arg : TemplateArgs.getInnermost().take_front( |
| 5368 | TD->getTemplateParameters()->size())) { |
| 5369 | ArrayRef<TemplateArgument> Unpacked(Arg); |
| 5370 | if (Arg.getKind() == TemplateArgument::Pack) |
| 5371 | Unpacked = Arg.pack_elements(); |
| 5372 | for (TemplateArgument UnpackedArg : Unpacked) |
| 5373 | Args.addArgument( |
| 5374 | getTrivialTemplateArgumentLoc(UnpackedArg, QualType(), Loc)); |
| 5375 | } |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5376 | QualType T = CheckTemplateIdType(TemplateName(TD), Loc, Args); |
| 5377 | if (T.isNull()) |
| 5378 | return nullptr; |
| Richard Smith | e6d4b77 | 2017-06-07 02:42:27 +0000 | [diff] [blame] | 5379 | auto *SubstRecord = T->getAsCXXRecordDecl(); |
| 5380 | assert(SubstRecord && "class template id not a class type?"); |
| 5381 | // Check that this template-id names the primary template and not a |
| 5382 | // partial or explicit specialization. (In the latter cases, it's |
| 5383 | // meaningless to attempt to find an instantiation of D within the |
| 5384 | // specialization.) |
| 5385 | // FIXME: The standard doesn't say what should happen here. |
| Richard Smith | 4f440e3 | 2017-06-08 01:08:50 +0000 | [diff] [blame] | 5386 | if (FindingInstantiatedContext && |
| 5387 | usesPartialOrExplicitSpecialization( |
| 5388 | Loc, cast<ClassTemplateSpecializationDecl>(SubstRecord))) { |
| Richard Smith | e6d4b77 | 2017-06-07 02:42:27 +0000 | [diff] [blame] | 5389 | Diag(Loc, diag::err_specialization_not_primary_template) |
| 5390 | << T << (SubstRecord->getTemplateSpecializationKind() == |
| 5391 | TSK_ExplicitSpecialization); |
| 5392 | return nullptr; |
| 5393 | } |
| 5394 | DC = SubstRecord; |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 5395 | continue; |
| 5396 | } |
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5397 | } |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5398 | |
| Douglas Gregor | 4109afa | 2011-11-07 17:43:18 +0000 | [diff] [blame] | 5399 | DC = DC->getParent(); |
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5400 | } |
| Douglas Gregor | d225fa0 | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 5401 | |
| Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5402 | // Fall through to deal with other dependent record types (e.g., |
| 5403 | // anonymous unions in class templates). |
| 5404 | } |
| John McCall | 5966088 | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 5405 | |
| Douglas Gregor | 64621e6 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 5406 | if (!ParentDC->isDependentContext()) |
| 5407 | return D; |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5408 | |
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5409 | ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5410 | if (!ParentDC) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5411 | return nullptr; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5412 | |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5413 | if (ParentDC != D->getDeclContext()) { |
| 5414 | // We performed some kind of instantiation in the parent context, |
| 5415 | // so now we need to look into the instantiated parent context to |
| 5416 | // find the instantiation of the declaration D. |
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5417 | |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5418 | // If our context used to be dependent, we may need to instantiate |
| 5419 | // it before performing lookup into that context. |
| Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5420 | bool IsBeingInstantiated = false; |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5421 | if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) { |
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5422 | if (!Spec->isDependentContext()) { |
| 5423 | QualType T = Context.getTypeDeclType(Spec); |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5424 | const RecordType *Tag = T->getAs<RecordType>(); |
| 5425 | assert(Tag && "type of non-dependent record is not a RecordType"); |
| Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5426 | if (Tag->isBeingDefined()) |
| 5427 | IsBeingInstantiated = true; |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 5428 | if (!Tag->isBeingDefined() && |
| 5429 | RequireCompleteType(Loc, T, diag::err_incomplete_type)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5430 | return nullptr; |
| Douglas Gregor | 25edf43 | 2010-11-05 23:22:45 +0000 | [diff] [blame] | 5431 | |
| 5432 | ParentDC = Tag->getDecl(); |
| Douglas Gregor | a04f2ca | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 5433 | } |
| 5434 | } |
| 5435 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5436 | NamedDecl *Result = nullptr; |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 5437 | // FIXME: If the name is a dependent name, this lookup won't necessarily |
| 5438 | // find it. Does that ever matter? |
| Akira Hatanaka | 59e3b43 | 2017-01-31 19:53:32 +0000 | [diff] [blame] | 5439 | if (auto Name = D->getDeclName()) { |
| 5440 | DeclarationNameInfo NameInfo(Name, D->getLocation()); |
| Erik Pilkington | 4cae092 | 2019-07-30 23:38:18 +0000 | [diff] [blame] | 5441 | DeclarationNameInfo NewNameInfo = |
| 5442 | SubstDeclarationNameInfo(NameInfo, TemplateArgs); |
| 5443 | Name = NewNameInfo.getName(); |
| Akira Hatanaka | 59e3b43 | 2017-01-31 19:53:32 +0000 | [diff] [blame] | 5444 | if (!Name) |
| 5445 | return nullptr; |
| 5446 | DeclContext::lookup_result Found = ParentDC->lookup(Name); |
| Erik Pilkington | 4cae092 | 2019-07-30 23:38:18 +0000 | [diff] [blame] | 5447 | |
| 5448 | if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(D)) { |
| 5449 | VarTemplateDecl *Templ = cast_or_null<VarTemplateDecl>( |
| 5450 | findInstantiationOf(Context, VTSD->getSpecializedTemplate(), |
| 5451 | Found.begin(), Found.end())); |
| 5452 | if (!Templ) |
| 5453 | return nullptr; |
| 5454 | Result = getVarTemplateSpecialization( |
| 5455 | Templ, &VTSD->getTemplateArgsInfo(), NewNameInfo, SourceLocation()); |
| 5456 | } else |
| 5457 | Result = findInstantiationOf(Context, D, Found.begin(), Found.end()); |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5458 | } else { |
| 5459 | // Since we don't have a name for the entity we're looking for, |
| 5460 | // our only option is to walk through all of the declarations to |
| 5461 | // find that name. This will occur in a few cases: |
| 5462 | // |
| 5463 | // - anonymous struct/union within a template |
| 5464 | // - unnamed class/struct/union/enum within a template |
| 5465 | // |
| 5466 | // FIXME: Find a better way to find these instantiations! |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5467 | Result = findInstantiationOf(Context, D, |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5468 | ParentDC->decls_begin(), |
| 5469 | ParentDC->decls_end()); |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5470 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5471 | |
| Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5472 | if (!Result) { |
| 5473 | if (isa<UsingShadowDecl>(D)) { |
| 5474 | // UsingShadowDecls can instantiate to nothing because of using hiding. |
| 5475 | } else if (Diags.hasErrorOccurred()) { |
| 5476 | // We've already complained about something, so most likely this |
| 5477 | // declaration failed to instantiate. There's no point in complaining |
| 5478 | // further, since this is normal in invalid code. |
| 5479 | } else if (IsBeingInstantiated) { |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5480 | // The class in which this member exists is currently being |
| Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5481 | // instantiated, and we haven't gotten around to instantiating this |
| 5482 | // member yet. This can happen when the code uses forward declarations |
| 5483 | // of member classes, and introduces ordering dependencies via |
| 5484 | // template instantiation. |
| 5485 | Diag(Loc, diag::err_member_not_yet_instantiated) |
| 5486 | << D->getDeclName() |
| 5487 | << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC)); |
| 5488 | Diag(D->getLocation(), diag::note_non_instantiated_member_here); |
| Richard Smith | 169f219 | 2012-03-26 20:28:16 +0000 | [diff] [blame] | 5489 | } else if (EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) { |
| 5490 | // This enumeration constant was found when the template was defined, |
| 5491 | // but can't be found in the instantiation. This can happen if an |
| 5492 | // unscoped enumeration member is explicitly specialized. |
| 5493 | EnumDecl *Enum = cast<EnumDecl>(ED->getLexicalDeclContext()); |
| 5494 | EnumDecl *Spec = cast<EnumDecl>(FindInstantiatedDecl(Loc, Enum, |
| 5495 | TemplateArgs)); |
| 5496 | assert(Spec->getTemplateSpecializationKind() == |
| 5497 | TSK_ExplicitSpecialization); |
| 5498 | Diag(Loc, diag::err_enumerator_does_not_exist) |
| 5499 | << D->getDeclName() |
| 5500 | << Context.getTypeDeclType(cast<TypeDecl>(Spec->getDeclContext())); |
| 5501 | Diag(Spec->getLocation(), diag::note_enum_specialized_here) |
| 5502 | << Context.getTypeDeclType(Spec); |
| Douglas Gregor | 528ad93 | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 5503 | } else { |
| 5504 | // We should have found something, but didn't. |
| 5505 | llvm_unreachable("Unable to find instantiation of declaration!"); |
| 5506 | } |
| 5507 | } |
| NAKAMURA Takumi | 82a3511 | 2011-10-08 11:31:46 +0000 | [diff] [blame] | 5508 | |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5509 | D = Result; |
| 5510 | } |
| 5511 | |
| Douglas Gregor | 5178331 | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 5512 | return D; |
| 5513 | } |
| Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5514 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5515 | /// Performs template instantiation for all implicit template |
| Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5516 | /// instantiations we have seen until this point. |
| Nick Lewycky | 67c4d0f | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 5517 | void Sema::PerformPendingInstantiations(bool LocalOnly) { |
| Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 5518 | while (!PendingLocalImplicitInstantiations.empty() || |
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 5519 | (!LocalOnly && !PendingInstantiations.empty())) { |
| Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 5520 | PendingImplicitInstantiation Inst; |
| 5521 | |
| 5522 | if (PendingLocalImplicitInstantiations.empty()) { |
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 5523 | Inst = PendingInstantiations.front(); |
| 5524 | PendingInstantiations.pop_front(); |
| Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 5525 | } else { |
| 5526 | Inst = PendingLocalImplicitInstantiations.front(); |
| 5527 | PendingLocalImplicitInstantiations.pop_front(); |
| 5528 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5529 | |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5530 | // Instantiate function definitions |
| 5531 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { |
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5532 | bool DefinitionRequired = Function->getTemplateSpecializationKind() == |
| 5533 | TSK_ExplicitInstantiationDefinition; |
| Erich Keane | 0fb1648 | 2018-08-13 18:33:20 +0000 | [diff] [blame] | 5534 | if (Function->isMultiVersion()) { |
| 5535 | getASTContext().forEachMultiversionedFunctionVersion( |
| 5536 | Function, [this, Inst, DefinitionRequired](FunctionDecl *CurFD) { |
| 5537 | InstantiateFunctionDefinition(/*FIXME:*/ Inst.second, CurFD, true, |
| 5538 | DefinitionRequired, true); |
| 5539 | if (CurFD->isDefined()) |
| 5540 | CurFD->setInstantiationIsPending(false); |
| 5541 | }); |
| 5542 | } else { |
| 5543 | InstantiateFunctionDefinition(/*FIXME:*/ Inst.second, Function, true, |
| 5544 | DefinitionRequired, true); |
| 5545 | if (Function->isDefined()) |
| 5546 | Function->setInstantiationIsPending(false); |
| 5547 | } |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5548 | continue; |
| 5549 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5550 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5551 | // Instantiate variable definitions |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 5552 | VarDecl *Var = cast<VarDecl>(Inst.first); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5553 | |
| 5554 | assert((Var->isStaticDataMember() || |
| 5555 | isa<VarTemplateSpecializationDecl>(Var)) && |
| 5556 | "Not a static data member, nor a variable template" |
| 5557 | " specialization?"); |
| Anders Carlsson | 62215c4 | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 5558 | |
| Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5559 | // Don't try to instantiate declarations if the most recent redeclaration |
| 5560 | // is invalid. |
| Douglas Gregor | ec9fd13 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 5561 | if (Var->getMostRecentDecl()->isInvalidDecl()) |
| Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5562 | continue; |
| 5563 | |
| 5564 | // Check if the most recent declaration has changed the specialization kind |
| 5565 | // and removed the need for implicit instantiation. |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 5566 | switch (Var->getMostRecentDecl() |
| 5567 | ->getTemplateSpecializationKindForInstantiation()) { |
| Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5568 | case TSK_Undeclared: |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 5569 | llvm_unreachable("Cannot instantitiate an undeclared specialization."); |
| Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5570 | case TSK_ExplicitInstantiationDeclaration: |
| Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5571 | case TSK_ExplicitSpecialization: |
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5572 | continue; // No longer need to instantiate this type. |
| 5573 | case TSK_ExplicitInstantiationDefinition: |
| 5574 | // We only need an instantiation if the pending instantiation *is* the |
| 5575 | // explicit instantiation. |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 5576 | if (Var != Var->getMostRecentDecl()) |
| 5577 | continue; |
| 5578 | break; |
| Chandler Carruth | 6b4756a | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 5579 | case TSK_ImplicitInstantiation: |
| 5580 | break; |
| 5581 | } |
| 5582 | |
| Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 5583 | PrettyDeclStackTraceEntry CrashInfo(Context, Var, SourceLocation(), |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5584 | "instantiating variable definition"); |
| Chandler Carruth | cfe41db | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 5585 | bool DefinitionRequired = Var->getTemplateSpecializationKind() == |
| 5586 | TSK_ExplicitInstantiationDefinition; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5587 | |
| 5588 | // Instantiate static data member definitions or variable template |
| 5589 | // specializations. |
| 5590 | InstantiateVariableDefinition(/*FIXME:*/ Inst.second, Var, true, |
| Serge Pavlov | 7dcc97e | 2016-04-19 06:19:52 +0000 | [diff] [blame] | 5591 | DefinitionRequired, true); |
| Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 5592 | } |
| 5593 | } |
| John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 5594 | |
| 5595 | void Sema::PerformDependentDiagnostics(const DeclContext *Pattern, |
| 5596 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| Aaron Ballman | b105e49 | 2014-03-07 14:09:15 +0000 | [diff] [blame] | 5597 | for (auto DD : Pattern->ddiags()) { |
| John McCall | c62bb64 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 5598 | switch (DD->getKind()) { |
| 5599 | case DependentDiagnostic::Access: |
| 5600 | HandleDependentAccessCheck(*DD, TemplateArgs); |
| 5601 | break; |
| 5602 | } |
| 5603 | } |
| 5604 | } |